OAuth 2.0 Authorization Code Grant

The Authorization Code Grant is used to request an authorization code that can be exchanged for an ID token and, typically an access token. This grant is advantageous in that tokens cannot be intercepted as they are never exposed to the user agent (typically, a web browser). The flow is redirection-based, so the client needs to be able to interact with the resource owner’s user agent. 

The sequence for this flow is as follows:

 

 

  1. The client application initiates the flow by directing the resource owner’s user agent to the EmpowerID Authorization endpoint with the required parameters. These parameters include a response type, client identifier, scope, and a redirect URL that specifies where to send the user agent after the resource owner authenticates.

  2. The resource owner submits credentials.

  3. Upon successful authentication, the Authorization sever sends an authorization code and identity token to the specified redirect URL.

  4. The client application calls the EmpowerID Token endpoint to exchange the authorization code for an access token and a refresh token.

  5. The Authorization server authenticates the client application by verifying the Client ID and Client Secret, validates the authorization code, checks the redirect URL and issues the access and refresh tokens.  

You can download sample .NET framework code at https://dl1.empowerid.com/files/OAuthTestSamplecode.zip

Authorization Code Grant

1. Initiate a login request to the EmpowerID Authorization endpoint, https://<EID Server>/oauth/v2/ui/authorize

https://<EID Server>/oauth/v2/ui/authorize ?client_id=xxxxxxxxxxxxxxxxxx &redirect_uri=https%3A%2F%2Ftestoauthapp.com%2FcallbackUrl &response_type=code &scope=openid &state=xxxxxxxxxxxxxxxxxx &nonce=xxxxxxxxxxxxxxxxxx

Request Parameter

Required/Optional

Description

Request Parameter

Required/Optional

Description

response_type

required

Must be code to initiate authorization code flow. For OpenID Connect flow use code id_token as response type.

client_id 

required

Must be the EmpowerID OAuth application client identifier.

redirect_uri 

required

The client app URL to which the authorization server will redirect after request approval. This URL should be registered in the Callback URLs on the EmpowerID OAuth application.

scope

required

A space-separated list of strings that the user consents to. Values include openid for OpenID Connect flow.

state 

optional

A random string value sent by the client to maintain session and prevent CSR attacks

nonce

optional

A random string value sent by client to uniquely identify each request

2. Authenticate using username and password or any of the allowed external identity providers.

3. Authorization server redirects to the redirect_uri with the response parameters in the query string.

https://testoauthapp.com/callbackUrl ?state=xxxxxxxxxxxxxxxxxx &code= xxxxxxxxxxxxxxxxxx &id_token= xxxxxxxxxxxxxxxxxx

Response Parameter

Description

Response Parameter

Description

state

The value sent by the client to maintain the session

code

The authorization code generated by the authorization server

id_token

The identity token issued by the authorization server for OpenID Connect flow

4. Exchange the code for an access token by calling the EmpowerID Token endpoint, https://<EID Server>/oauth/v2/token

https://<EID Server>/oauth/v2/token ?client_id={The Client ID of the OAuth app you registered in EmpowerID} &client_secret={The Client Secret of the OAuth app you registered in EmpowerID} &grant_type=authorization_code &code=xxxxxxxxxxxxxxxxxx

Request Parameter

Required/Optional

Description

Request Parameter

Required/Optional

Description

grant_type

required

Must be authorization_code to initiate authorization code flow.

client_id 

required

Must be the EmpowerID OAuth application client identifier.

client_secret  

required

Must be the EmpowerID OAuth application client secret.

code 

required

The authorization code received from the authorization server

5. Returns access token and refresh token in the response.

Authorization Code Grant using .NET Client Library

1. Initialize ClientSettings by passing the client_id, client_secret, redirect_uri, token_endpoint, authorization_endpoint, tokeninfo_endpoint and userinfo_endpoint. Also initialize a new AuthorizationCodeGrant by passing the clientSettings model.

2. Call the BuildAuthorizationRequestPacket() method to build authorization code flow parameters.

3. In the application’s callback method AuthorizationCodeGrantResponse() for example, extract the code and state, build an AuthorizationResponseModel model and send it to the GetAccessToken() method.

Authorization Code Grant with PKCE (Proof Key for Code Exchange) Extension

1. Initiate a login request to the EmpowerID Authorization endpoint, https://<EID Server>/oauth/v2/ui/authorize

Request Parameter

Required/Optional

Description

Request Parameter

Required/Optional

Description

response_type

required

Must be code to initiate authorization code flow. For OpenID Connect flow use code id_token as a response type.

client_id 

required

Must be the EmpowerID OAuth application client identifier.

redirect_uri 

required

The client app URL to which the authorization server will redirect after request approval. This URL should be registered in the Callback URLs on the EmpowerID OAuth application.

scope

required

A space-separated list of strings that the user consents to. Values include openid for OpenID Connect flow.

code_challenge_method

recommended

Specifies the transformation method used for the code_challenge. Permitted values are

  • plain

  • S256

Defaults to plain if not present in the request

code_challenge

required

The string derived from the code_verifier.

  • plain - code_challenge = code_verifier

  • S256 - code_challenge = BASE64URL-ENCODE(SHA256(ASCII(code_verifier)))

Please refer to the PKCE RFC for generating the Code Verifier.

state 

optional

A random string value sent by the client to maintain session and prevent CSR attacks

nonce

optional

A random string value sent by client to uniquely identify each request

2. Authenticate using username and password or any of the allowed external identity providers.

3. Authorization server redirects to the redirect_uri with the response parameters in the query string.

Response Parameter

Description

Response Parameter

Description

state

The value sent by the client to maintain the session

code

The authorization code generated by the authorization server

id_token

The identity token issued by the authorization server for OpenID Connect flow

4. Exchange the code for an access token by calling the EmpowerID Token endpoint, https://<EID Server>/oauth/v2/token

Request Parameter

Required/Optional

Description

Request Parameter

Required/Optional

Description

grant_type

required

Must be authorization_code to initiate authorization code flow.

client_id 

required

Must be the EmpowerID OAuth application client identifier.

client_secret  

required

Must be the EmpowerID OAuth application client secret.

code 

required

The authorization code received from the authorization server

code_verifier

required

The Code Verifier value generated by the client during the initial authorization request

5. Returns access token and refresh token in the response.

Authorization Code Grant with PKCE Extension using .NET Client Library

1. Initialize ClientSettings by passing the client_id, client_secret, redirect_uri, token_endpoint, authorization_endpoint, tokeninfo_endpoint and userinfo_endpoint. Also initialize a new AuthorizationCodeGrant by passing the clientSettings model.

2. Call the BuildAuthorizationRequestPacketWithPKCE() method to build the fully qualified URL to redirect to the authentication endpoint.

3. In the application’s callback method AuthorizationCodeGrantWithPKCE() for example, extract the code ,state and the generated code_verifier , build an AuthorizationResponseModel model and send it to the GetAccessToken() method.

IN THIS ARTICLE