Sobes.tech
Senior

Describe the interaction scheme for the OAuth 2.0 protocol (Authorization Code Flow).

sobes.tech AI

Answer from AI

The Authorization Code Flow in OAuth 2.0 is a scheme that allows a client application to access user resources through authorization and exchange a code for a token.

Main steps:

  1. The client redirects the user to the authorization server's authorization page with parameters (client_id, redirect_uri, scope, etc.).
  2. The user authenticates and consents.
  3. The authorization server redirects the user back to redirect_uri with an authorization code.
  4. The client application sends this code to the authorization server along with client_secret to exchange for an access token.
  5. The authorization server verifies the code and returns an access token (and possibly a refresh token).
  6. The client uses the access token to access protected resources on the Resource Server.

This scheme is secure because the access token is not transmitted through the browser, and the authorization code is single-use and short-lived.

Example URL for authorization request:

GET /authorize?response_type=code&client_id=CLIENT_ID&redirect_uri=REDIRECT_URI&scope=read

Exchanging code for token:

POST /token
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code&code=AUTH_CODE&redirect_uri=REDIRECT_URI&client_id=CLIENT_ID&client_secret=CLIENT_SECRET

The client receives an access token for API access.