OAuth 2.0 Authorization Code Flow
The Authorization Code Flow is a secure OAuth 2.0 method where an authorization server issues a temporary authorization code to a client application. The client exchanges this code for tokens, such as:
This flow is designed for confidential clients, like web applications with secure backends for storing credentials. The user must consent to resource access before the authorization server issues the code. Single-page apps can only use this flow with PKCE.
How Authorization Code Flow works
The following diagram illustrates the key interactions between the user, client application, authorization server, and resource server during the OAuth 2.0 Authorization Code Flow.
This sequence of steps provides a detailed breakdown of how the Authorization Code Flow operates based on the interactions shown in the diagram.
User Access. User accesses the client application.
Authorization Request. Client application sends an authorization request to the authorization server.
Sample call to the authorize endpoint:
curl --location \ --get \ --url "https://$TENANT_ID.$REGION_ID.authz.cloudentity.io/$TENANT_ID/$WORKSPACE_ID/oauth2/authorize" \ --data-urlencode "response_type=code" \ --data-urlencode "client_id=$CLIENT_ID"
Consent Display. Authorization server displays the consent form to the user.
User Consent. User reviews and provides consent to the authorization server.
Authorization Code Issued. Authorization server issues an authorization code to the client application.
Token Request. Client application sends a request to the authorization server for a token, including the authorization code.
Sample call to the token endpoint:
curl --request POST \ --url "https://$TENANT_ID.$REGION_ID.authz.cloudentity.io/$TENANT_ID/$WORKSPACE_ID/oauth/token" \ --data-raw "grant_type=authorization_code&code=$CODE&client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET"
Token Validation. Authorization server validates the token request, including the authorization code, client ID, and and client secret.
Token Issued. Authorization server returns the token to the client application.
API Call. Client application calls the resource server’s API with the token.
Data Returned. Resource server validates the token and responds with the requested data.
Best practices
Redirection Endpoints. Configure the redirection endpoint for the client application before making calls.
Scope Management. Limit the scope the client application can access when calling the
/authorize
and/token
endpoints.