Decoding Authorization Grant Types

Safeguarding Access in the Digital Realm

Seefa Banu
10 min readMar 6, 2024

--

Embarking on a journey through the intricacies of Authorization Grant Types is essential for any developer or security enthusiast. In this blog, we unravel the cryptographic threads that secure our digital interactions. From the widely adopted Authorization Code Grant to the streamlined Implicit Grant and the nuanced Resource Owner Password Credentials Grant, we explore each type’s strengths, weaknesses, and ideal use cases. Let’s get started.

Grant types are different ways applications can obtain access tokens to protected resources in the OAuth 2.0 protocol. So what OAuth 2.0 is, It’s simply an industry-standard protocol that allows users to grant third-party applications access to their data on other websites or services, without sharing their passwords. Think of it as a secure way for two apps to talk to each other on your behalf, without you having to give either of them your login credentials.

In the OAuth 2.0 Flow outlined above, the steps cover obtaining an authorization grant and access token. The authorization grant type depends on the method used by the application to request authorization, and the grant types supported by the API. OAuth 2 defines several grant types, each of which is useful in different cases:

  • Authorization Code
  • Implicit
  • Password
  • Client Credentials
  • Refresh Token
  • Device Code

Note : The Implicit Flow type and the Password Grant types are both considered insecure, and are no longer recommended for use.

Now we will describe grant types in more detail, their use cases and flows, in the following sections.

A. Authorization Code

The Authorization Code Grant Type stands out as the most widely adopted mechanism in OAuth 2.0. It is meticulously designed to cater to scenarios where the source code remains confidential, ensuring optimal security for sensitive information. This grant type employs a redirection-based flow, necessitating the application’s capability to interact with the user-agent, typically the user’s web browser.

The client application, in this context, does not have access to the user’s credentials directly. Instead, the user authenticates solely with the authorization server, which subsequently issues an authorization code. This code, in turn, serves as a secure token that the client application can exchange for an access token. The Figure below shows the flow of Authorization Code Grant.

Note : I am using Asgardeo as a Authorization server to demonstrate the flow . Asgardeo is a powerful Identity and Access Management (IAM) solution developed by WSO2. It helps organizations manage user identities, access control, and security for both B2C (Business-to-Consumer) and B2B (Business-to-Business) applications. Click to get started with Asgardeo.

Let’s get deep dive in to the Authorization Code Flow:

Step 1 — Authorization Code Link

Authorization endpoint

https://api.asgardeo.io/t/<organization_name>/oauth2/authorize

First, the user is given an authorization code link (with PKCE ) that looks like the following:

https://api.asgardeo.io/t/bifrost/oauth2/authorize?scope=openid&response_type=code&redirect_uri=https://localhost:5000&client_id=fv_LScHaB83PN4VPX1cHufphtHQa&code_challenge_method=S256&code_challenge=IMbNq8j9HZBlbLuZ4nHcYOv1ZkRF5TVNAfVIGyeUsi0

This authorization request takes the following parameters.

  • client_id :the application’s client ID (how the API identifies the application)
  • redirect_uri : where the service redirects the user-agent after an authorization code is granted
  • response_type = code: specifies that your application is requesting an authorization code grant
  • scope=read: specifies the level of access that the application is requesting
  • Code_challenge : The client creates and records a secret cryptographical random string (code_verifier), which is then encoded using URL safe base64 encoding to transform it into the code_challenge. The code_challenge is required for the authorization code flow with PKCE. You can use some tools to generate the code_challenge and code_verifier.
  • Code_challenge_method : This is the method used for transforming the code_verifier into the code_challenge. Asgardeo supports S256 and plain. This is required for the authorization code flow with PKCE.

Step 2 — User Authorizes Application

When the user clicks the link, they must first log in to the service to authenticate their identity (unless they are already logged in). Then they will be prompted by the service to authorize or deny the application access to their account.

Step 3 — Application Receives Authorization Code

When the user is authenticated, Asgardeo redirects to the redirect_uri with the authorization code.

https://localhost:5000/?code=60cb4ba7-b7b2-3f2f-8319-58122f1b2f5d&session_state=a0c3bc89849ba0f236791f7fe76a837b7b4422fdc9aca16db394d19a28724a29.wQc7eSHSRrGNfECJRMhSAw

Step 4 — Application Requests Access Token

The application requests an access token from the API by passing the authorization code along with authentication details, including the client secret, to the API token endpoint. Here is an example POST request to Asgardeo’s token endpoint:

https://api.asgardeo.io/t/bifrost/oauth2/token?code=60cb4ba7-b7b2-3f2f-8319-58122f1b2f5d&grant_type=authorization_code&redirect_uri=https%3A%2F%2Flocalhost%3A5000&code_verifier=zYYoWc9LNIahfonUKyKZcpDc0oWV0zGbn-gTkrr4lkw&client_id=fv_LScHaB83PN4VPX1cHufphtHQa

Step 5 — Application Receives Access Token

Sample response:

{
"access_token": "54bd024f-5080-3db5-9422-785f5d610605",
"refresh_token": "7024af0f-9216-3b8f-a249-edf6db7f72ab",
"scope": "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NyNn5SKICCUeO9p1sqjLXsbGRwN6TN0H7oVBoK7Q6o9R6gYPWR_hBk9uxpt3CyKej1uCVsmxcoGGqPXMQugIz5sU2wwo685XNAv6Q9TlTEqFBWpbSpYZ0g73YjnhTvaaaHdasgE1VCw",
"token_type": "Bearer",
"expires_in": 3600
}

The Authorization Code grant has the extra step of exchanging the authorization code for the access token, it provides an additional layer of security not present in the Implicit grant type . Therefore it could be used in web and mobile apps as well.

Note : If you’re using the Authorization Code flow in a mobile app, or any other type of application that can’t store a client secret, then you should also use the PKCE (Proof Key for Code Exchange (or PKCE, pronounced like “pixie”))extension, which provides protections against other attacks where the authorization code may be intercepted.

The code exchange step ensures that an attacker isn’t able to intercept the access token, since the access token is always sent via a secure backchannel between the application and the OAuth server.

The Authorization Code Grant Type establishes a robust layer of protection around the user’s credentials, shielding them from potential compromise by malicious client applications. In essence, this method facilitates a secure and seamless interaction, ensuring that sensitive user information remains confidential throughout the authorization process.

Benefits:

  • Highly secure: Uses redirect flow and server-side exchange, minimizing exposure of tokens.
  • Widely supported: Most popular among apps and services.
  • Supports refresh tokens: Provides long-term access without user re-authorization.
  • Good for user-facing apps: Offers better user experience with login confirmation.

Drawbacks:

  • More complex: Requires server-side implementation and redirect handling.
  • Not suitable for stateless clients: Client needs to handle redirect and state.

B. Implicit Grant

The implicit grant flow is an OAuth 2.0 grant type that enables a client application to obtain an access token directly from the authorization server without an intermediate authorization code exchange. This flow is commonly used in browser-based applications where the client application runs in a web browser.

However, it is important to note that the access token is exposed in the browser’s URL fragment, which can make it vulnerable to certain types of attacks, such as cross-site scripting (XSS). As a result, this flow is typically not recommended for applications that require high security.

Note : This Grant Type is not recommended due to the inherent risks of returning access tokens in an HTTP redirect without any confirmation that it has been received by the client.

Benefits:

  • Simple and fast: No server-side code needed, ideal for single-page apps.
  • Good for user experience: No redirects, seamless authorization flow.

Drawbacks:

  • Less secure: Token exposed in browser URL, vulnerable to XSS attacks.
  • No refresh tokens: Limited lifespan, requires frequent user re-authorization.
  • Not for confidential clients: Client secrets exposed in URL, not suitable for server-to-server use.

C. Password Grant

The password grant flow, within OAuth 2.0, facilitates a client application in acquiring an access token by directly submitting the user’s username and password to the authorization server. However, it’s crucial to note that this approach is often regarded as less secure compared to alternative grant types. The inherent security concern arises from the necessity for the client application to manage and transmit the user’s credentials, introducing potential vulnerabilities.

Typically employed in situations where the client application is deemed highly trustworthy, the password grant prioritizes user experience over stringent security measures. Nevertheless, caution should be exercised, and it is generally discouraged for use in public-facing applications or scenarios where sensitive data is at stake. This distinction underscores the importance of selecting grant types based on the specific security requirements and sensitivity of the data being accessed.

Benefits:

  • Simple to implement: Basic user login approach, familiar to users.

Drawbacks:

  • Highly insecure: Never share user passwords with apps, risks credential theft.
  • Not recommended: Deprecated by OAuth due to security concerns.

D. Client Credentials

The client credentials grant type offers a secure avenue for client applications to acquire an access token without necessitating user authentication. This proves beneficial in situations where the client application solely requires access to its own resources, such as internal data storage or APIs, without the need for user-specific information. Nevertheless, it is crucial to prioritize the security of client credentials, as unauthorized possession of these credentials could potentially lead to the acquisition of access tokens, providing access to the client’s resources. Caution and stringent security measures are imperative to prevent unauthorized parties from exploiting these credentials and compromising the integrity of the client’s resources.

Benefits:

  • Simple and efficient: No user involvement, ideal for server-to-server communication.
  • Scalable: Suitable for large-scale automated API interactions.

Drawbacks:

  • Less secure: Grants broad access without user consent, requires trust in client.
  • Not for user-facing apps: No user verification or authorization.

E. Refresh Token Grant

The refresh token grant type stands as a secure mechanism for client applications to obtain a fresh access token without necessitating the user to undergo re-authentication. This functionality significantly enhances the user experience by eliminating unnecessary login prompts and concurrently reduces the burden on the authorization server by minimizing the frequency of authentication requests. Refresh tokens, which possess a longer lifespan compared to access tokens, offer a valuable tool for maintaining persistent access to APIs.

In the realm of identity management, it’s a delicate balancing act between the desire for short-lived access tokens to enhance security and the need to provide a seamless user experience. Refresh tokens play a pivotal role in achieving this equilibrium. They empower clients to acquire new access tokens without prompting the user for interaction repeatedly. However, it’s essential to note that each time a client refreshes a token, it must engage in an authenticated back-channel communication with the Identity Server. This process ensures the ongoing validity of the refresh token, guarding against potential revocation.

Benefits:

  • Maintains long-term access: Obtains new access tokens without user re-authorization.
  • Reduces user interaction: Improves app usability by minimizing login prompts.

Drawbacks:

  • Requires secure storage: Refresh tokens require careful handling to prevent misuse.
  • Potential security risk: Compromised refresh token can grant unauthorized access.

F. Device Code

The Device Authorization Flow offers a practical solution for devices with limited input capabilities that seek to connect to the internet. Rather than requiring direct user authentication, these devices prompt users to visit a specific link on their computer or smartphone to authorize the device. This approach addresses potential challenges in user experience for devices lacking easy text input methods. The Device Authorization Flow, formally ratified in OAuth 2.0, enables device applications to kickstart the authorization process by transmitting their Client ID, ultimately obtaining the necessary token.

In scenarios where it becomes necessary to compel the user to reauthorize a device, a mechanism involves revoking the Refresh Token associated with that device. This action can be achieved by unlinking devices from users, as detailed in the guidelines for device reauthorization. It is important to note that the device won’t be prompted for reauthorization until the current Access Token expires and the application attempts to utilize the revoked Refresh Token. For further insights into the functioning of Refresh Tokens and their role in managing device reauthorization, additional information is available in the documentation on Refresh Tokens. This multifaceted approach ensures a streamlined and secure device authorization experience, catering to the unique challenges posed by input-constrained devices.

Benefits:

  • Secure for offline devices: No user interaction on device, suitable for limited input devices.
  • Multi-device verification: Allows user confirmation on another device.

Drawbacks:

  • Complex flow: Requires user input on multiple devices, can be confusing.
  • Less popular: Not as widely supported as other grant types.

Choosing the right grant type depends on various factors:

  • Application type: Is it user-facing or server-to-server?
  • Security requirements: How sensitive is the data accessed?
  • User experience: Do you prioritize convenience or strict control?
  • Resource server capabilities: Does it support your preferred grant type?

By understanding the benefits and drawbacks of each grant type, you can make informed decisions to secure and optimize your OAuth integrations.

I hope this comprehensive information helps! Feel free to ask if you have any further questions.

--

--