The Authorization Code Flow is used to securely obtain an access token from an authorization server on behalf of a user. This flow is typically used in web applications where user authentication and consent are required.
- Web applications with server-side backends
- Applications that need to access user data
- Scenarios requiring user authentication and consent
You can create OAuth clients for the Authorization Code Flow in the Settings page of the Jiko authentication portal.
- The user initiates login by clicking a "Login" or "Authorize" button.
- The application redirects the user to the authorization server.
- The user authenticates and grants permission.
- The authorization server redirects back with an authorization code.
- The application exchanges the code for an access token.
- The application uses the access token to access protected resources.
The application redirects the user to the authorization endpoint with:
response_type: Must becodeclient_id: The client identifierredirect_uri: Where to redirect after authorizationscope: The requested scopesstate: A random string to prevent CSRF attackscode_challenge: PKCE challenge (see PKCE)code_challenge_method: Must beS256
GET /api/oauth2/authorize?response_type=code&client_id=your-client-id&redirect_uri=https://your-app.com/callback&scope=pockets.read&state=some-random-state&code_challenge=CODE_CHALLENGE&code_challenge_method=S256After the user authenticates and approves, the authorization server redirects back:
HTTP/1.1 303 See Other
Location: https://your-app.com/callback?code=authorization-code&state=some-random-stateThe application exchanges the authorization code for tokens by sending a request to the token endpoint with:
grant_type: Must beauthorization_codecode: The authorization code receivedclient_id: The client identifierclient_assertion_type: Specifies the JWT formatclient_assertion: A signed JWT for client authentication (see Private Key JWT)code_verifier: The PKCE code verifier (see PKCE)
POST /api/oauth2/token HTTP/1.1
Host: auth.jiko.io
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code&
code=authorization-code&
client_id=your-client-id&
client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer&
client_assertion=eyJhbGciOiJQUzI1NiIsInR5cCI6IkpXVCJ9...&
code_verifier=CODE_VERIFIER{
"access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...",
"refresh_token": "dGhpcy1yZWZyZXNoLXRva2VuLi4u...",
"token_type": "Bearer",
"expires_in": 900
}- Access tokens have a lifespan of
15 minutes. - Refresh tokens have a lifespan of
90 days. - Use the refresh token to obtain new access tokens without user re-authentication (see Refresh Tokens).
- Always use PKCE to prevent authorization code interception attacks.
- Validate the
stateparameter to prevent CSRF attacks. - Store tokens securely on the server side.
- Use short-lived JWTs for the
client_assertion(recommended max 5 minutes).
RFC 6749 - Authorization Code Grant - OAuth 2.0 Authorization Code Grant specification.
oauth.com - Detailed information and technical breakdowns.