OAuth 2.0 Authorization Framework (2024)

The OAuth 2.0 authorization framework is a protocol that allows a user to grant a third-party web site or application access to the user's protected resources, without necessarily revealing their long-term credentials or even their identity.

OAuth introduces an authorization layer and separates the role of the client from that of the resource owner. In OAuth, the client requests access to resources controlled by the resource owner and hosted by the resource server and is issued a different set of credentials than those of the resource owner. Instead of using the resource owner's credentials to access protected resources, the client obtains an Access Token--a string denoting a specific scope, lifetime, and other access attributes. Access tokens are issued to third-party clients by an authorization server with the approval of the resource owner. Then the client uses the access token to access the protected resources hosted by the resource server.

Auth0 generates access tokens for API authorization scenarios, in JSON web token (JWT) format. The permissions represented by the access token, in OAuth terms, are known as scopes. When an application authenticates with Auth0, it specifies the scopes it wants. If those scopes are authorized by the user, then the access token will represent these authorized scopes.

Roles

An OAuth 2.0 flow has the following roles:

  • Resource Owner: Entity that can grant access to a protected resource. Typically, this is the end-user.

  • Resource Server: Server hosting the protected resources. This is the API you want to access.

  • Client: Application requesting access to a protected resource on behalf of the Resource Owner.

  • Authorization Server: Server that authenticates the Resource Owner and issues access tokens after getting proper authorization. In this case, Auth0.

OAuth 2.0 defines four flows to get an access token. These flows are called grant types. Deciding which one is suited for your case depends mostly on your application type.

  • Authorization Code Flow: used by Web Apps executing on a server. This is also used by mobile apps, using the Proof Key for Code Exchange (PKCE) technique.

  • Implicit Flow with Form Post: used by JavaScript-centric apps (Single-Page Applications) executing on the user's browser.

  • Resource Owner Password Flow: used by highly-trusted apps.

  • Client Credentials Flow: used for machine-to-machine communication.

The specification also provides an extensibility mechanism for defining additional grant types. To learn more about how each grant type works and when it should be used, see Authentication and Authorization Flows.

Endpoints

OAuth 2.0 uses two endpoints: the /authorize endpoint and the /oauth/token endpoint.

Authorization endpoint

The /authorize endpoint is used to interact with the resource owner and get the authorization to access the protected resource. To better understand this, imagine that you want to log in to a service using your Google account. First, the service redirects you to Google in order to authenticate (if you are not already logged in) and then you will get a consent screen, where you will be asked to authorize the service to access some of your data (protected resources); for example, your email address and your list of contacts.

The request parameters of the /authorize endpoint are:

ParameterDescription
response_typeTells the authorization server which grant to execute.
response_mode(Optional) How the result of the authorization request is formatted. Values:
- query: for Authorization Code grant. 302 Found triggers redirect.
- fragment: for Implicit grant. 302 Found triggers redirect.
- form_post: 200 OK with response parameters embedded in an HTML form as hidden parameters.
- web_message: For Silent Authentication. Uses HTML5 web messaging.
client_idThe ID of the application that asks for authorization.
redirect_uriHolds a URL. A successful response from this endpoint results in a redirect to this URL.
scopeA space-delimited list of permissions that the application requires.
stateAn opaque value, used for security purposes. If this request parameter is set in the request, then it is returned to the application as part of the redirect_uri.
connectionSpecifies the connection type for Passwordless connections

You can configure custom query parameters when your application makes the initial call to the /authorize endpoint to authenticate a user. You can use custom query parameters to provide additional context to the page template for the New Universal Login experience.

You must enable ID First to use the connection parameter. For more information on the connection parameter and the New Universal Login experience, review Passwordless for New Universal Login.

Query parameters prefixed with ext- automatically appear in the page template context.

This endpoint is used by the Authorization Code and the Implicit grant types. The authorization server needs to know which grant type the application wants to use since it affects the kind of credential it will issue:

  • For the Authorization Code grant, it will issue an authorization code (which can later be exchanged for an access token at the /oauth/token endpoint).

  • For the Implicit grant, it will issue an access token, which is an opaque string (or a JWT in an Auth0 implementation) that denotes who has authorized which permissions (scopes) to which application.

To inform the authorization server which grant type to use, the response_type request parameter is used as follows:

  • For the Authorization Code grant, use response_type=code to include the authorization code.

  • For the Implicit grant, use response_type=token to include an access token. An alternative is to use response_type=id_token token to include both an access token and an ID token.

An ID token is a JWT that contains information about the logged in user. It was introduced by OpenID Connect (OIDC).

The OAuth 2.0 Multiple Response Type Encoding Practices specification added a parameter that specifies how the result of the authorization request is formatted. This parameter is called response_mode. It is optional and can take the following values:

ValueDescription
queryThis is the default for Authorization Code grant. A successful response is 302 Found which triggers a redirect to the redirect_uri. The response parameters are embedded in the query component (the part after ?) of the redirect_uri in the Location header.
For example:
HTTP/1.1 302 Found
Location: https://my-redirect-uri.callback?code=js89p2x1 where the authorization code is js89p21.
fragmentThis is the default for Implicit grant. A successful response is 302 Found, which triggers a redirect to the redirect_uri (which is a request parameter). The response parameters are embedded in the fragment component (the part after #) of the redirect_uri in the Location header.
For example:
HTTP/1.1 302 Found
Location: https://my-redirect-uri/callback#access_token=eyB...78f&token_type=Bearer&expires_in=3600.
form_postThe response mode is defined by the OAuth 2.0 Form Post Response Mode specification. A successful response is 200 OK and the parameters are embedded in an HTML form as hidden params. The action of the form is the redirect_uri and the onload attribute is configured to submit the form. After the HTML is loaded by the browser, a redirect to the redirect_uri is done.
web_messageThis response mode is defined in OAuth 2.0 Web Message Response Mode specification. It uses HTML5 Web Messaging instead of the redirect for the authorization response from the /authorization endpoint. This is particularly useful when using Silent Authentication. To do this response mode, you must register your app's URL at the Allowed Web Origins field in your Auth0 application settings.

Token endpoint

The /oauth/token endpoint is used by the application in order to get an access token or a refresh token. It is used by all flows except for the Implicit Flow because in that case an access token is issued directly.

  • In the Authorization Code Flow, the application exchanges the authorization code it got from the authorization endpoint for an access token.

  • In the Client Credentials Flow and Resource Owner Password Credentials Grant Exchange, the application authenticates using a set of credentials and then gets an access token.

State parameters

Authorization protocols provide a state parameter that allows you to restore the previous state of your application. The state parameter preserves some state object set by the client in the Authorization request and makes it available to the client in the response. The primary reason for using the state parameter is to mitigate CSRF attacks. See Use OAuth 2.0 State Parameters for details.

Learn more

  • Prevent Attacks and Redirect Users with OAuth 2.0 State Parameters
  • Which OAuth 2.0 Flow Should I Use?
OAuth 2.0 Authorization Framework (2024)

FAQs

What is the OAuth 2.0 framework? ›

The OAuth 2.0 authorization framework is a protocol that allows a user to grant a third-party web site or application access to the user's protected resources, without necessarily revealing their long-term credentials or even their identity.

What is an OAuth 2.0 authorization server? ›

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. This OAuth 2.0 flow is specifically for user authorization.

What is the OAuth2 authentication method? ›

OAuth 2.0 uses Access Tokens. An Access Token is a piece of data that represents the authorization to access resources on behalf of the end-user. OAuth 2.0 doesn't define a specific format for Access Tokens. However, in some contexts, the JSON Web Token (JWT) format is often used.

Is OAuth 2.0 an authentication protocol? ›

OAuth 2.0 is not an authentication protocol.

This article is intended to help potential identity providers with the question of how to build an authentication and identity API using OAuth 2.0 as the base. Essentially, if you're saying "I have OAuth 2.0, and I need authentication and identity", then read on.

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.

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.

Is Google OAuth2 free? ›

The API we'll use is Google OAuth 2.0 Playground. It's public, but it has rate limits on the requests you can make. This “free” API will work wonderfully for testing your application and understanding how OAuth works, but it isn't meant for production-level code.

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.

How to build your own OAuth server? ›

OAuth2 Client Credentials Grant & Machine-to-Machine
  1. create an OAuth 2.0 Client;
  2. perform the OAuth 2.0 Client Credentials Flow;
  3. Receive an OAuth 2.0 Access Token.
  4. Validate the OAuth 2.0 Access Token.
Mar 12, 2024

Which API uses OAuth 2.0 for authorization? ›

Google APIs use the OAuth 2.0 protocol for authentication and authorization. Google supports common OAuth 2.0 scenarios such as those for web server, client-side, installed, and limited-input device applications. To begin, obtain OAuth 2.0 client credentials from the Google API Console.

Is OAuth 2.0 a modern authentication? ›

Modern Authentication or OAuth 2.0 is the new, secure way to connect your Microsoft Outlook email account to third party applications such as EXACT. Gmail are introducing changes in February 2024, the changes affect anyone sending email communications to Gmail accounts.

What is the difference between SSO and OAuth? ›

In summary, SSO is used for authenticating users, while OAuth is used for granting access to resources. OAuth can be used as part of an SSO solution, but it is not a replacement for SSO.

How to setup OAuth2? ›

Setting up OAuth 2.0
  1. Go to the API Console.
  2. From the projects list, select a project or create a new one.
  3. If the APIs & services page isn't already open, open the console left side menu and select APIs & services.
  4. On the left, click Credentials.
  5. Click New Credentials, then select OAuth client ID.

How to get authorization code in OAuth2? ›

OAuth2 Authorization Code Flow
  1. Step 1: Create an Authorization Request.
  2. Example Authorization Request.
  3. Step 2: Get Authorization.
  4. Step 3: Get the Authorization Code.
  5. Step 4: Get the Access Token and Refresh Token.
  6. Step 5: Validate the Access Token. ...
  7. Step 6: Add the Access Token to the Authorization Request.

What is the authorization server in OAuth2? ›

At its core, an authorization server is simply an engine for minting OpenID Connect (OIDC) or OAuth 2.0 tokens. An authorization server is also used to apply access policies. Each authorization server has a unique issuer URI and its own signing key for tokens to keep a proper boundary between security domains.

What is OAuth and how does it work? ›

OAuth is an open-standard authorization protocol or framework that provides applications the ability for “secure designated access.” For example, you can tell Facebook that it's OK for ESPN.com to access your profile or post updates to your timeline without having to give ESPN your Facebook password.

What is OAuth 2.0 and its use cases? ›

OAuth2 provides access to resources hosted by other web apps on behalf of a user, without sharing the user's credentials. The specification also covers delegated access to client types such as browser-based, server-side web, native/mobile apps, and connected devices.

What is the difference between API and OAuth2? ›

API keys are a simple way to authenticate users, but they don't provide granular authorization control. OAuth tokens are more complex to implement, but they offer more granular authorization control and can be set to expire.

What is the difference between JWT and OAuth2? ›

JWT is suitable for stateless applications, as it allows the application to authenticate users and authorize access to resources without maintaining a session state on the server. OAuth, on the other hand, maintains a session state on the server and uses a unique token to grant access to the user's resources.

Top Articles
Latest Posts
Article information

Author: Gregorio Kreiger

Last Updated:

Views: 6122

Rating: 4.7 / 5 (77 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Gregorio Kreiger

Birthday: 1994-12-18

Address: 89212 Tracey Ramp, Sunside, MT 08453-0951

Phone: +9014805370218

Job: Customer Designer

Hobby: Mountain biking, Orienteering, Hiking, Sewing, Backpacking, Mushroom hunting, Backpacking

Introduction: My name is Gregorio Kreiger, I am a tender, brainy, enthusiastic, combative, agreeable, gentle, gentle person who loves writing and wants to share my knowledge and understanding with you.