OAuth 2.0 là framework ủy quyền (delegated authorization) — không phải authentication; OpenID Connect (OIDC) là layer trên OAuth bổ sung authentication, trả về id_token.
- Bốn vai trò — Resource Owner (user), Client (app), Authorization Server (cấp token), Resource Server (API).
- Authorization Code flow — (1) app redirect user đến Authorization Server kèm
client_id,redirect_uri,scope,state(chống CSRF); (2) user đăng nhập và consent; (3) server redirect về app kèm authorization code; (4) app đổi code lấy access token qua back-channel POST —client_secretkhông lộ trong URL; (5) app gọi API bằng access token; (6) dùng refresh token lấy access token mới khi hết hạn. - id_token vs access_token —
id_token(JWT của OIDC) chứa danh tính user, dành cho client đọc;access_tokendành cho Resource Server — client không nên tự parse. - PKCE — bắt buộc cho public client (SPA, mobile — không giữ được secret): chứng minh app đổi code chính là app đã khởi tạo flow — cơ chế verifier/challenge chi tiết xem câu OAuth security best practices.
- Flow khác — Implicit đã deprecated (token lộ trong URL); Client Credentials cho machine-to-machine. OAuth 2.1 gộp best practices: PKCE bắt buộc cho mọi client, xóa Implicit và Password flow.
- Lưu token phía SPA — access token trong memory, refresh token trong httpOnly cookie (chi tiết ở câu JWT vs Session).