Authorization Code Grant - OAuth 2.0 Simplified (2024)

The authorization code is a temporary code that the client will exchange for an access token. The code itself is obtained from the authorization server where the user gets a chance to see what the information the client is requesting, and approve or deny the request.

The authorization code flow offers a few benefits over the other grant types. When the user authorizes the application, they are redirected back to the application with a temporary code in the URL. The application exchanges that code for the access token. When the application makes the request for the access token, that request can be authenticated with the client secret, which reduces the risk of an attacker intercepting the authorization code and using it themselves. This also means the access token is never visible to the user or their browser, so it is the most secure way to pass the token back to the application, reducing the risk of the token leaking to someone else.

The first step of the web flow is to request authorization from the user. This is accomplished by creating an authorization request link for the user to click on.

The authorization URL is usually in a format such as:

https://authorization-server.com/oauth/authorize?client_id=a17c21ed&response_type=code&state=5ca75bd30&redirect_uri=https%3A%2F%2Fexample-app.com%2Fauth&scope=photos

The exact URL endpoint will be specified by the service to which you are connecting, but the parameter names will always be the same.

Note that you will most likely first need to register your redirect URL at the service before it will be accepted. This also means you can’t change your redirect URL per request. Instead, you can use the state parameter to customize the request. See below for more information.

After the user visits the authorization page, the service shows the user an explanation of the request, including application name, scope, etc. (See “approves the request” for an example screenshot.) If the user clicks “approve”, the server will redirect back to the app, with a “code” and the same “state” parameter you provided in the query string parameter. It is important to note that this is not an access token. The only thing you can do with the authorization code is to make a request to get an access token.

OAuth Security

Up until 2019, the OAuth 2.0 spec only recommended using the PKCE extension for mobile and JavaScript apps. The latest OAuth Security BCP now recommends using PKCE also for server-side apps, as it provides some additional benefits there as well. It is likely to take some time before common OAuth services adapt to this new recommendation, but if you’re building a server from scratch you should definitely support PKCE for all types of clients.

Authorization Request Parameters

The following parameters are used to make the authorization request. You should build a query string with the below parameters, appending that to the application’s authorization endpoint obtained from its documentation.

response_type=code

response_type is set to code indicating that you want an authorization code as the response.

client_id

The client_id is the identifier for your app. You will have received a client_id when first registering your app with the service.

redirect_uri (optional)

The redirect_uri may be optional depending on the API, but is highly recommended. This is the URL to which you want the user to be redirected after the authorization is complete. This must match the redirect URL that you have previously registered with the service.

scope (optional)

Include one or more scope values (space-separated) to request additional levels of access. The values will depend on the particular service.

state

The state parameter serves two functions. When the user is redirected back to your app, whatever value you include as the state will also be included in the redirect. This gives your app a chance to persist data between the user being directed to the authorization server and back again, such as using the state parameter as a session key. This may be used to indicate what action in the app to perform after authorization is complete, for example, indicating which of your app’s pages to redirect to after authorization.

The state parameter also serves as a CSRF protection mechanism if it contains a random value per request. When the user is redirected back to your app, double check that the state value matches what you set it to originally.

PKCE

If the service supports PKCE for web server apps, include the PKCE challenge and challenge method here as well. This is described in a complete example in Single-Page Apps and Mobile Apps.

Combine all of these query string parameters into the authorization URL, and direct the user’s browser there. Typically apps will put these parameters into a login button, or will send this URL as an HTTP redirect from the app’s own login URL.

The user approves the request

After the user is taken to the service and sees the request, they will either allow or deny the request. If they allow the request, they will be redirected back to the redirect URL specified along with an authorization code in the query string. The app then needs to exchange this authorization code for an access token.

Exchange the authorization code for an access token

To exchange the authorization code for an access token, the app makes a POST request to the service’s token endpoint. The request will have the following parameters.

grant_type (required)

The grant_type parameter must be set to “authorization_code”.

code (required)

This parameter is for the authorization code received from the authorization server which will be in the query string parameter “code” in this request.

redirect_uri (possibly required)

If the redirect URL was included in the initial authorization request, it must be included in the token request as well, and must be identical. Some services support registering multiple redirect URLs, and some require the redirect URL to be specified on each request. Check the service’s documentation for the specifics.

Client Authentication (required)

The service will require the client authenticate itself when making the request for an access token. Typically services support client authentication via HTTP Basic Auth with the client’s client_id and client_secret. However, some services support authentication by accepting the client_id and client_secret as POST body parameters. Check the service’s documentation to find out what the service expects, since the OAuth 2.0 spec leaves this decision up to the service.

More advanced OAuth servers may also require other forms of client authentication such as mTLS or private_key_jwt. Refer to the service’s own documentation for those examples.

PKCE Verifier

If the service supports PKCE for web server apps, then the client will need to include the followup PKCE parameter when exchanging the authorization code as well. Again, see Single-Page Apps and Mobile Apps for a complete example of using the PKCE extension.

Authorization Code Grant - OAuth 2.0 Simplified (2024)

FAQs

Authorization Code Grant - OAuth 2.0 Simplified? ›

The Authorization Code grant type is used by confidential and public clients to exchange an authorization code for an access token. After the user returns to the client via the redirect URL, the application will get the authorization code from the URL and use it to request an access token.

What is OAuth 2.0 in simple terms? ›

OAuth 2.0, which stands for “Open Authorization”, is a standard designed to allow a website or application to access resources hosted by other web apps on behalf of a user.

How to authorize with OAuth? ›

Using OAuth 2.0 for Web Server Applications
  1. Step 1: Set authorization parameters.
  2. Step 2: Redirect to Google's OAuth 2.0 server.
  3. Step 3: Google prompts user for consent.
  4. Step 4: Handle the OAuth 2.0 server response.
  5. Step 5: Exchange authorization code for refresh and access tokens.

How do I get an access token with an authorization code grant? ›

The following section describes the steps for obtaining the access token and refresh token using the authorization code grant mechanism:
  1. Step 1: Authenticate a User and Create a User Session.
  2. Step 2: [Optional] Generating Client Credentials.
  3. Step 3: Generate Authorization Code.
  4. Step 4: Exchange Auth Code for a Token.

What is the authorization code in OAuth2? ›

The OAuth 2.0 authorization code grant type, or auth code flow, enables a client application to obtain authorized access to protected resources like web APIs. The auth code flow requires a user-agent that supports redirection from the authorization server (the Microsoft identity platform) back to your application.

Why is a bad idea to use OAuth 2.0 for authentication? ›

Perhaps the most infamous OAuth-based vulnerability is when the configuration of the OAuth service itself enables attackers to steal authorization codes or access tokens associated with other users' accounts. By stealing a valid code or token, the attacker may be able to access the victim's data.

Is OAuth 2.0 authentication or authorization? ›

OAuth 2.0 is the industry-standard protocol for authorization. OAuth 2.0 focuses on client developer simplicity while providing specific authorization flows for web applications, desktop applications, mobile phones, and living room devices.

What is an example of OAuth2 authentication? ›

OAuth 2.0 allows users to share specific data with an application while keeping their usernames, passwords, and other information private. For example, an application can use OAuth 2.0 to obtain permission from users to store files in their Google Drives.

How to get auth code? ›

Get the authorization code

On the Registrations list, click the domain name that you want to transfer. On the Domain details page, click Authorization code. The code is available in the Authorization code dialog.

What is the difference between authorization code and access token? ›

The authorization code is a temporary code that the client will exchange for an access token. The code itself is obtained from the authorization server where the user gets a chance to see what the information the client is requesting, and approve or deny the request.

How does an authorization code work? ›

An authorization code is typically a sequence of letters, numbers, or a combination of both, that validates a person's identity, approves a transaction or provides access to a secured area.

How to generate an authorization token? ›

If you will use an API key for authentication:
  1. Open secret. ...
  2. Paste it in the field provided.
  3. Provide the required sample Parameters requested.
  4. Click Generate to produce a corresponding Token.io web app URL.
  5. Click Test to link to the Token.io web app and see the UI that will be presented to a customer.

Which OAuth 2.0 authorization grant type is used the most? ›

The most common OAuth grant types are listed below.
  • Authorization Code.
  • PKCE.
  • Client Credentials.
  • Device Code.
  • Refresh Token.

How to use OAuth for authentication? ›

  1. Obtain OAuth 2.0 credentials from the Google API Console.
  2. Obtain an access token from the Google Authorization Server.
  3. Examine scopes of access granted by the user.
  4. Send the access token to an API.
  5. Refresh the access token, if necessary.

What is the difference between implicit grant and authorization code in OAuth2? ›

As mentioned above, implicit flow is another grant type. It is a 'simplified' version of authorization code flow, which skips the authorization code part and directly returns an access token. If the hacker steals the access token, he can call Google API, such as Google Drive API, to get the user's Google Drive data.

What is the difference between basic authentication and OAuth2? ›

Opposed to OAuth, Basic Authentication is a more straightforward, yet less secure method embedded within the HTTP framework. It involves transmitting a username and password with every request, often encoded in Base64. This method, while simple, risks exposing user credentials more openly.

How does OAuth 2.0 work in Rest API? ›

OAuth 2.0 is a standard for implementing delegated authorization, and authorization is based on the access token required to access a resource. The access token can be issued for a given scope, which defines what the access token can do and what resources it can access.

What is the difference between API and OAuth2? ›

OAuth2 vs API keys

Here are some of the benefits of OAuth2 over the API key: Access token is tied to a specific user, not an app. User credentials are never exposed to an app, authentication is done in a single place – Authorization Server.

What is an example of OAuth? ›

Examples of OAuth

For example, a user's Strava account can access their Garmin Connect account without needing to share their Garmin username and password with Strava.

References

Top Articles
Latest Posts
Article information

Author: Ray Christiansen

Last Updated:

Views: 5991

Rating: 4.9 / 5 (49 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Ray Christiansen

Birthday: 1998-05-04

Address: Apt. 814 34339 Sauer Islands, Hirtheville, GA 02446-8771

Phone: +337636892828

Job: Lead Hospitality Designer

Hobby: Urban exploration, Tai chi, Lockpicking, Fashion, Gunsmithing, Pottery, Geocaching

Introduction: My name is Ray Christiansen, I am a fair, good, cute, gentle, vast, glamorous, excited person who loves writing and wants to share my knowledge and understanding with you.