Authorization Code Grant
An OAuth 2.0 flow that involves a user granting authorization to a client application
Last updated
An OAuth 2.0 flow that involves a user granting authorization to a client application
Last updated
The Authorization Code Grant is an OAuth 2.0 flow that regular web apps use in order to access an API, typically as web applications with backend and frontend (browser-based SPA, for example).
In this flow the application receives authorization from the user. Once the user has authorized the application, they get redirected back to it with a temporary access code in the URL. The application exchanges that code for an access token. For more detailed information read OAuth 2.0 specification.
For applications that are able to securely store a secret it is recommended to supply the secret in the token request due to security considerations. Otherwise, if the application is unable to securely store a secret (i.e. a frontend-only app), we suggest using PKCE. Both methods are supported by Aidbox.
The easiest way to test the Authorization Code Grant flow is through the Aidbox Sandbox UI (IAM -> Sandbox -> Authorization Code).
The first step is to configure Client for Authorization Grant with secret
and redirect_uri
, as well as code
grant type:
Client will act on behalf of the user, which means Access Policies should be configured for User, not for Client.
You can configure Client for JWT tokens, set token expiration and enable a refresh token:
auth_._authorization_code. | options | desc |
---|---|---|
token_format | jwt | use access token in jwt format |
access_token_expiration | int (seconds) | token expiration time from issued at |
refresh_token | true/false | enable refresh_token |
refresh_token_expiration | int (seconds) | refresh token expiration time from issued at or last usage. If not present, token will be expireless |
secret_required | true/false | require secret for token |
pkce | true/false | enable PKCE flow |
If you want to use Authorization Code Grant for Single Page Application you do not need to set the secret
attribute, use PKCE instead!
If your application is a major consumer of Aidbox API, you can set first_party attribute as true. This means that the same User Session will be shared between Aidbox and client, so if you close the client session, Aidbox User Session will be closed too.
The next step is to query an authorize endpoint with client_id
and response_type
with value code.
For PKCE you will need to additionally supply code_challenge
and code_challenge_method
. First create a high-entropy string value with a minimum length of 43 characters and a maximum length of 128 characters, then produce a code_challenge
using the S256 hashing method.
To keep your client stateless, you can send a state
parameter with arbitrary content, which will be sent back in the redirect response.
If users are not logged in, they will see the default login screen.
If a client is not first_party or the user has not yet granted permissions to the client, the user will see the grant page:
If the client was granted permission, the user agent will be redirected to the url configured in Client.auth.authorization_code.redirect_uri with the authorization code parameter.
With this code and client secret, you can request an Access Token withgrant_type: authorization_code
. If you're using PKCE, you will need to supply code_verifier
used to produce the code_challenge
.
If the provided code is accurate, you will get access token, user information and refresh token (if enabled):
You can use access token in the Authorization header for Aidbox API calls:
To get new access token using refresh token
Aidbox creates a Session resource for each Access Token which can be closed with a special endpoint DELETE /Session
with the token in Authorization header:
Session is just a resource and you can inspect and manipulate sessions with standard Search & CRUD API. For example, use GET /Session
to get all sessions.