OAuth 2.0 and OpenID Connect API

OAuth 2.0

According to RFC6749, OAuth 2.0 is an authorization framework that enables a third-party application to obtain limited access to an HTTP service, either on behalf of a resource owner by orchestrating an approval interaction between the resource owner and the HTTP service, or by allowing the third-party application to obtain access on its own behalf.

OAuth 2.0 RFC defines multiple ways an application can get an access token, and it refers to it as grant_types. The protocol defines four grant types, which OneWelcome implements, such as:

  • Authorization Code
  • Implicit
  • Resource Owner Password Credentials
  • Client Credentials

You may ask yourself, why do we have all these ways for issuing an access token? Well, these grants serve different purposes, meaning different grants are suited for different use cases, such as web applications, server-to-server applications, or devices which are not able to launch a web browser.

Endpoints

OneWelcome implements the endpoints defined by OAuth 2.0 and OpenID Connect RFC, the full API definition can be found in OAuth 2.0 & OpenID Connect API specifications.

How does authorization code (AC) grant work?

Authorization code (AC) grant is used by web and mobile application, and as the name states, there is an authorization code issued by the authorization server which the client application exchanges it for an access token.

A step by step flow for issuing an access token using the AC grant can be seen below:

User has an active session:

  1. User opens a browser and is accessing an application
  2. Application is making an authorization request to the authorization server (AS)
  3. User is already logged in and AS issues an authorization code
  4. AS redirects the user to the application with the authorization code
  5. Application exchanges the authorization code for an access token

User does not have an active session:

  1. User opens a browser and is accessing an application
  2. Application is making an authorization request to the authorization server (AS)
  3. User is not logged in, and AS redirects the user to the login page
  4. User authenticates with one of the configured login options
  5. Once is successfully authenticated, AS will issue an authorization code
  6. AS redirects the user back to the application with the authorization code
  7. Application exchanges the authorization code for an access token

What API do you need?

For sending the authorization request, the authorize API is needed, and for exchanging the authorization code for an access token, token API is required.

Example of authorization request:

Authorization request
Copy

Example of response on authorization request in case the user is not authenticated:

Authorization response
Copy

Example of response on authorization request in case the user is authenticated:

Authorization response
Copy

Example of token request:

Token Request
Copy

Please note the grant_type=authorization_codefrom the request above. Sending that parameter in the request, indicates to the authorization server that an authorization_code is exchanged for an access token. Keep in mind that when the authorization_code has been used once, it becomes invalid.

How does OAuth 2.0 implicit grant works?

According to RFC6749 ,the implicit grant is a simplified authorization code flow optimized for clients implemented in a browser using a scripting language such as JavaScript. In the implicit flow, instead of issuing the client an authorization code, the client is issued an access token directly.

A step by step flow can be seen below:

  1. User opens a browser and is accessing an application
  2. Application is making an authorization request to the authorization server (AS)
  3. If user is already logged in and AS issues an access token, otherwise the user will be redirected to the login page asking for login, and once logged in it will issue an access token
  4. AS redirects the user to the application with the access token

What API do you need?

For the implicit grant, only the authorization endpoint is needed, since the response_type=token indicates that the implicit grant is used.

Example of authorize request:

Bash
Copy

Example of response when the user is not authenticated

Bash
Copy

Example of response when the user is authenticated:

Bash
Copy

How does OAuth 2.0 Resource Owner Password Credentials Grant (ROPC) works?

Resource Owner Password Credentials Grant (ROPC) requires users to provide their credentials (username and password) and because of this we recommend using this flow only when there is a high degree of trust between the resource owner and the client.

A step by step flow, can be seen below:

  1. User is accessing your application and clicks login
  2. User is presented with form for filling his/her credentials (username and password)
  3. Authorization server validates the credentials, and issues an access token

Please note, that for the ROPC grant there is no redirect from the client application to the authorization server, which means that the application is calling the endpoints with user's credentials directly.

What API do you need?

For ROPC grant you need the token endpoint, and specify in the request body that the request is a ROPC one by sending grant_type=password.

Example of request:

Bash
Copy

Example of response:

JSON
Copy

How does Client Credentials Grant Works?

Client credentials is intended for server-side client applications, in which the client authenticates on behalf of itself.

As RFC6749 states, client credentials grant is used as an authorization grant typically when the client is acting on its own behalf (the client is also the resource owner) or is requesting access to protected resources based on an authorization previously arranged with the authorization server.

Please note that according to RFC6749, Client Credentials Flow is allowed only for confidential clients.

A step by step flow can be seen below:

  1. Application sends a request to the authorization server, using it's own client_id and client_secret
  2. Authorization server issues an access token which can be used to call different APIs

What APIs do you need?

For the Client Credentials Grant, as for the ROPC Grant, only the token endpoint is needed, with the difference that the value of grant_type has to be set to client_credentials.

An example of request can be seen below:

Bash
Copy

The response of this api call will look like below:

Bash
Copy

OIDC

OIDC is an extension of oauth2, and according to RFCit enables Clients to verify the identity of the End-User based on the authentication performed by an Authorization Server, as well as to obtain basic profile information about the End-User in an interoperable and REST-like manner.

The main difference between OAuth and OIDC is that OAuth is about authorization, and OIDC is about authentication. The definition of the authorization api that RFC 6749 specifies, states that for the attribute response_typethe possible values can be either token or code. OpenID adds a new value to the response type, which is id_tokenand allows to request in the response_type request parameter any combination of code, token and id_token.

Please note, that when sending the request with the response_type=id_token the scope request parameter has to include the value openid.

Apart from the id_token response type, the specification also introduced a special value, none which is not allowed to be sent in any combination with the code, token and id_token.

As mentioned, the id_token contains informations about the authenticated user, and the properties added is referred to as claims. Not everything about the user is added in the id_token, but only informations that were requested using scopes.

If we take for example an authorize request with...&response_type=id_token&scope=openid email phone, this means that there should be an id token issued, and it should have the following information/claims about the user: email, email_verified,phone_number_verified, phone_number. This means that there is a one to many relationship between the requested scope and the number of claims/user attributes.

Why is an id_token useful?

Let's assume you have a regular web application that you register it with OneWelcome, and you want to configure it to allow user to login with Microsoft. Once the user logs in with Microsoft, an id_token will be generated and handed over to your application. Having the id_token you can show informations about the authenticated user, such as first name, last name and phone number, without any extra request, as it's the case with oauth2.

Example of request:

Bash
Copy

Example of response:

Bash
Copy

By decoding the JWT you can see details about the authenticated user, along with the standard claims defined by OpenID Connect Specification, the decoded JWT from the example above can be seen below:

Decoded JWT Token
Copy

What is response type none?

When the response_typeis none nothing is issued by the authorization server. According to OAuth2 RFC, the purpose for this attribute is to enable use cases where a party requests the Authorization Server to register a grant of access to a Protected Resource on behalf of a Client but requires no access credentials to be returned to the Client at that time.

A scenario described by OAuth2 RFC describing the usage of this response type is when a user wishes to purchase an application from a market, and desires to authorize application installation and grant the application access to Protected Resources in a single step. However, since the user is not presently interacting with the (not yet active) application, it is not appropriate to return access credentials simultaneously in the authorization step.

This flow requires to use only authorization API, and an example of a request, can be seen below:

Bash
Copy

A successful response would look like below:

Bash
Copy
Type to search, ESC to discard
Type to search, ESC to discard
Type to search, ESC to discard
  Last updated by Stein Welberg