Authorization code grant
- Endpoints
The authorization code grant is a two-step interactive process used when the client, for example, a Java application running on a server, requires access to protected resources.
The authorization code grant is the most secure of all the OAuth 2.0 grants for the following reasons:
-
It is a two-step process. The user must authenticate and authorize the client to see the resources and the authorization server must validate the code again before issuing the access token.
-
The authorization server delivers the access token directly to the client, usually over HTTPS. The client secret is never exposed publicly, which protects confidential clients.
Authorization code grant flow explained
-
The client, usually a web-based service, receives a request to access a protected resource. To access the resources, the client requires authorization from the resource owner.
-
The client redirects the resource owner’s user-agent to the authorization server.
-
The authorization server authenticates the resource owner, confirms resource access, and gathers consent if not previously saved.
-
The authorization server redirects the resource owner’s user agent to the client.
-
During the redirection process, the authorization server appends an authorization code.
-
The client receives the authorization code and authenticates to the authorization server to exchange the code for an access token.
Note that this example assumes a confidential client. Public clients are not required to authenticate.
-
If the authorization code is valid, the authorization server returns an access token (and a refresh token, if configured) to the client.
-
The client requests access to the protected resources from the resource server.
-
The resource server contacts the authorization server to validate the access token.
-
The authorization server validates the token and responds to the resource server.
-
If the token is valid, the resource server allows the client to access the protected resources.
Perform the steps in the following procedures to obtain an authorization code and exchange it for an access token:
Obtain an authorization code using a browser
This procedure assumes the following configuration:
-
AM is configured as an OAuth 2.0 authorization server. Ensure that:
-
The
codeplugin is configured in the Response Type Plugins field. -
The
Authorization Codegrant type is configured in the Grant Types field.
For more information, see Authorization server configuration.
-
-
A confidential client called
myClientis registered in AM with the following configuration:-
Client secret:
forgerock -
Scopes:
write -
Response Types:
code -
Grant Types:
Authorization Code
-
For more information, see Client registration.
Perform the steps in this procedure to obtain an authorization code using a browser:
-
The client redirects the resource owner’s user-agent to the authorization server’s authorization endpoint specifying, at least, the following form parameters:
-
client_id=your-client-id
-
response_type=code
-
redirect_uri=your-redirect-id
For information about the parameters supported by the
/oauth2/authorizeendpoint, see /oauth2/authorize.If the OAuth 2.0 provider is configured for a subrealm rather than the Top Level Realm, you must specify it in the endpoint. For example, if the OAuth 2.0 provider is configured for the
/alpharealm, then use/oauth2/realms/root/realms/alpha/authorize.For example:
https://openam.example.com:8443/openam/oauth2/realms/root/realms/alpha/authorize \ ?client_id=myClient \ &response_type=code \ &scope=write \ &state=abc123 \ &redirect_uri=https://www.example.com:443/callbackNote that the URL is split and spaces have been added for readability purposes and that the
scopeandstateparameters have been included. Scopes are not required, since they can be configured by default in the authorization server and the client, and have been added only as an example. Thestateparameter is added to protect against CSRF attacks. -
-
The resource owner authenticates to the authorization server, for example, using the credentials of the
demouser. In this case, they log in using the default chain or tree configured for the realm.After logging in, the authorization server presents the AM consent screen:
Figure 2. OAuth 2.0 Consent Screen -
The resource owner selects the
Allowbutton to grant consent for thewritescope.The authorization server redirects the resource owner to the URL specified in the
redirect_uriparameter. -
Inspect the URL in the browser.
It contains a
codeparameter with the authorization code the authorization server has issued.For example:
http://www.example.com/?code=g5B3qZ8rWzKIU2xodV_kkSIk0F4&scope=write&iss=https%3A%2F%2Fopenam.example.com%3A8443%2Fopenam%2Foauth2&state=abc123&client_id=myClient
-
The client performs the steps in Exchange an authorization code for an access token to exchange the authorization code for an access token.
Obtain an authorization code without using a browser
This procedure assumes the following configuration:
-
AM is configured as an OAuth 2.0 authorization server. Ensure that:
-
The
codeplugin is configured in the Response Type Plugins field. -
The
Authorization Codegrant type is configured in the Grant Types field.
For more information, see Authorization server configuration.
-
-
A confidential client called
myClientis registered in AM with the following configuration:-
Client secret:
forgerock -
Scopes:
write -
Response Types:
code -
Grant Types:
Authorization Code
-
For more information, see Client registration.
Perform the steps in this procedure to obtain an authorization code without using a browser:
-
The resource owner logs in to the authorization server, for example, using the credentials of the
demouser.For example:
$ [${resources.dir}/endpoints/authenticate.bash:#POST-authenticate-demo] [${resources.dir}/endpoints/authenticate.bash:#POST-authenticate-demo-expected] -
The client makes a POST call to the authorization server’s authorization endpoint, specifying the SSO token of the
demoin a cookie and, at least, the following parameters:-
client_id=your-client-id
-
response_type=code
-
redirect_uri=your-redirect-uri
-
decision=allow
-
csrf=demo-user-SSO-token
For information about the parameters supported by the
/oauth2/authorizeendpoint, see /oauth2/authorize.If the OAuth 2.0 provider is configured for a subrealm rather than the Top Level Realm, you must specify it in the endpoint. For example, if the OAuth 2.0 provider is configured for the
/alpharealm, then use/oauth2/realms/root/realms/alpha/authorize.For example:
$ [${resources.dir}/endpoints/oauth2.bash:#oauth2_authorize-auth-code-Example]Note that the
scopeand thestateparameters have been included. Scopes are not required, since they can be configured by default in the authorization server and the client, and have been added only as an example. Thestateparameter is added to protect against CSRF attacks.If the authorization server is able to authenticate the user and the client, it returns an HTTP 302 response with the authorization code appended to the redirection URL:
[${resources.dir}/endpoints/oauth2.bash:#oauth2_authorize-auth-code-Example-OUT] -
-
Perform the steps in Exchange an authorization code for an access token to exchange the authorization code for an access token.
Exchange an authorization code for an access token
Perform the steps in the following procedure to exchange an authorization code for an access token:
-
Ensure the client has obtained an authorization code by performing the steps in either Obtain an authorization code using a browser or Obtain an authorization code without using a browser.
-
The client creates a POST request to the token endpoint in the authorization server specifying, at least, the following parameters:
-
grant_type=authorization_code
-
code=your-authorization-code
-
redirect_uri=your-redirect-uri
For information about the parameters supported by the
/oauth2/access_tokenendpoint, see /oauth2/access_token.Confidential clients can authenticate to the OAuth 2.0 endpoints in several ways. This example uses the following form parameters:
-
client_id=your-client-id
-
client_secret=your-client-secret
For more information, see OAuth 2.0 client authentication.
If the OAuth 2.0 provider is configured for a subrealm rather than the Top Level Realm, you must specify it in the endpoint. For example, if the OAuth 2.0 provider is configured for the
/alpharealm, then use/oauth2/realms/root/realms/alpha/access_token.For example:
$ [${resources.dir}/endpoints/oauth2.bash:#oauth2_authorization_grant-Example]The
client_idand theredirection_uriparameters specified in this call must match those used as part of the authorization code request, or the authorization server will not validate the code.The authorization server returns an access token in the
access_tokenproperty. For example:[${resources.dir}/endpoints/oauth2.bash:#oauth2_access_token-OUT]The authorization server can also issue refresh tokens at the same time the access tokens are issued. For more information, see Refresh tokens.
-