Authentication and authorization

Providing authentication and authorization is de facto for web applications. We'll discuss authentication and authorization in this section. The new paradigm that has evolved over the past few years is OAuth. We'll learn and use OAuth 2.0 for implementation. OAuth is an open authorization mechanism, implemented in every major web application. Web applications can access each other's data by implementing the OAuth standard. It has become the most popular way to authenticate oneself for various web applications. Like on www.quora.com, you can register, and login using your Google or Twitter login IDs. It is also more user friendly, as client applications (for example, www.quora.com) don't need to store the user's passwords. The end user does not need to remember one more user ID and password.

Authentication and authorization

OAuth 2.0 example usage

The Internet Engineering Task Force (IETF) governs the standards and specifications of OAuth. OAuth 1.0a was the most recent version before OAuth 2.0 that was having a fix for session-fixation security flaw in the OAuth 1.0. OAuth 1.0 and 1.0a were very different from OAuth 2.0. OAuth 1.0 relies on security certificates and channel binding. OAuth 2.0 does not support security certification and channel binding. It works completely on Transport Security Layer (TSL). Therefore, OAuth 2.0 does not provide backward compatibility.

We'll try to discuss and understand the OAuth 2.0 specifications in a concise manner. Let's first see how signing in using Twitter works.

Please note that the process mentioned here was used at the time of writing. It may change in future. However, this process describes one of the OAuth 2.0 processes properly:

  1. The user visits the Quora home page. It shows various login options. We'll explore the process of the Continue with Twitter link.
  2. When the user clicks on the Continue with Twitter link, Quora opens a new window (in Chrome) that redirects the user to the www.twitter.com application. During this process few web applications redirect the user to the same opened tab/window.
  3. In this new window/tab, the user signs in to www.twitter.com with their credentials.
  4. If the user has not authorized the Quora application to use their data earlier, Twitter asks for the user's permission to authorize Quora to access the user's information. If the user has already authorized Quora, then this step is skipped.
  5. After proper authentication, Twitter redirects the user to Quora's redirect URI with an authentication code.
  6. Quora sends the client ID, client secret token, and authentication code (sent by Twitter in step 5) to Twitter when Quora redirect URI entered in the browser.
  7. After validating these parameters, Twitter sends the access token to Quora.
  8. The user is logged in to Quora on successful retrieval of the access token.
  9. Quora may use this access token to retrieve user information from Quora.

You must be wondering how Twitter got Quora's redirect URI, client ID, and secret token. Quora works as a client application and Twitter as an authorization server. Quora, as a client, registered on Twitter by using Twitter's OAuth implementation to use resource owner (end user) information. Quora provides a redirect URI at the time of registration. Twitter provides the client ID and secret token to Quora. It works this way. In OAuth 2.0, user information is known as user resources. Twitter provides a resource server and an authorization server. We'll discuss more of these OAuth terms in the next sections.

There are four roles defined in the OAuth 2.0 specifications:

The client that communicates with the authorization server to obtain the access key for a resource should first be registered with the authorization server. The OAuth 2.0 specification does not specify the way a client registers with the authorization server. Registration does not require direct communication between the client and the authorization server. Registration can be done using self-issued or third-party-issued assertions. The authorization server obtains the required client properties using one of these assertions. Let's see what the client properties are:

There are two types of client described by the specification, based on their ability to maintain the confidentiality of client credentials: confidential and public. Client credentials are secret tokens issued by the authorization server to clients in order to communicate with them.

An endpoint is nothing but a URI we use for REST or web components such as Servlet or JSP. OAuth 2.0 defines three types of endpoint. Two are authorization server endpoints and one is a client endpoint:

The client requests an access token from the authorization server, based on the obtained authorization from the resource owner. The resource owner gives authorization in the form of an authorization grant. OAuth 2.0 defines four types of authorization grant:

OAuth 2.0 also provides an extension mechanism to define additional grant types. You can explore this in the official OAuth 2.0 specifications.

The first sample flow that we discussed in the OAuth 2.0 example flow for signing in with Twitter depicts an authorization code grant. We'll add a few more steps for the complete flow. As you know, after the eighth step, the end user logs in to the Quora application. Let's assume the user is logging in to Quora for the first time and requests their Quora profile page:

If you looked at all the steps (a total of 13) of the authorization code flow, you can see that there are a total of two requests made by the client to the authorization server, and the authorization server in reply provides two responses: one request-response for the authentication token and one request-response for the access token.

Let's discuss the parameters used for each of these requests and responses.

The authorization request (step 4) to the authorization endpoint URI:

Parameter

Required / Optional

Description

response_type

Required

code (this value must be used).

client_id

Required

It represents the ID issued by the authorization server to the client at the time of registration.

redirect_uri

Optional

It represents the redirect URI given by the client at the time of registration.

scope

Optional

The scope of the request. If not provided, then the authorization server provides the scope based on the defined policy.

state

Recommended

The client uses this parameter to maintain the client state between the requests and callback (from the authorization server). The specification recommends it to protect against cross site request forgery attacks.

Authorization response (step 5):

Parameter

Required / Optional

Description

code

Required

Code (authorization code) generated by the authorization server.

Code should be expired after it is generated; the maximum recommended lifetime is 10 minutes.

The client must not use the code more than once.

If the client uses it more than once, then the request must be denied and all previous tokens issued based on the code should be revoked.

Code is bound to the client ID and redirect URI.

state

Required

It represents the ID issued by the authorization server to the client at the time of registration.

Token request (step 7) to token endpoint URI:

Parameter

Required / Optional

Description

grant_type

Required

authorization_code (this value must be used).

code

Required

Code (authorization code) received from the authorization server.

redirect_uri

Required

Required if it was included in the authorization code request and the values should match.

client_id

Required

It represents the ID issued by the authorization server to the client at the time of registration.

Token response (step 8):

Parameter

Required / Optional

Description

access_token

Required

The access token issued by the authorization server.

token_type

Required

The token type defined by the authorization server. Based on this, the client can utilize the access token. For example, bearer or mac.

refresh_token

Optional

This token can be used by the client to get a new access token using the same authorization grant.

expires_in

Recommended

Denotes the lifetime of the access token in seconds. A value of 600 denotes 10 minutes of lifetime for the access token. If this parameter is not provided in the response, then the document should highlight the lifetime of the access token.

scope

Optional/Required

Optional if identical to the scope requested by the client.

Required if the access token scope is different from the one the client provided in their request to inform the client about the actual scope of the access token granted.

If the client does not provide the scope while requesting the access token, then the authorization server should provide the default scope, or deny the request, indicating the invalid scope.

Error response:

Parameter

Required / Optional

Description

error

Required

One of the error codes defined in the specification, for example, unauthorized_client, invalid_scope.

error_description

Optional

Short description of the error.

error_uri

Optional

The URI of the error page describing the error.

An additional error parameter state is also sent in the error response if the state was passed in the client authorization request.

The first sample flow that we discussed in the OAuth 2.0 example flow for signing in with Twitter depicts the authorization code grant. We'll add a few more steps for its complete flow. As you know after eighth steps, end user logs in to the Quora application. Let's assume user is logging in first time on Quora and requests for its Quora profile page:

If you looked at all the steps (a total of 13) of the authorization code flow, you can see that there are total of two request made by the client to the authorization server, and the authorization server in reply provides two responses: one request-response for the authentication token and one request-response for the access token.

Let's discuss the parameters used for each of these requests and responses.

Authorization request to the authorization endpoint URI:

Parameter

Required / Optional

Description

response_type

Required

Token (this value must be used).

client_id

Required

It represents the ID issued by the authorization server to the client at the time of registration.

redirect_uri

Optional

It represents the redirect URI given by the client at the time of registration.

scope

Optional

The scope of the request. If not provided, then the authorization server provides the scope based on the defined policy.

state

Recommended

The client uses this parameter to maintain the client state between the requests and the callback (from the authorization server). The specification recommends it to protect against cross site request forgery attacks.

Access token response:

Parameter

Required / Optional

Description

access_token

Required

The access token issued by the authorization server.

token_type

Required

The token type defined by the authorization server. Based on this, the client can utilize the access token. For example, bearer or mac.

refresh_token

Optional

This token can be used by the client to get a new access token using the same authorization grant.

expires_in

Recommended

Denotes the lifetime of the access token in seconds. A value of 600 denotes 10 minutes of lifetime for the access token. If this parameter is not provided in the response, then the document should highlight the lifetime of the access token.

scope

Optional/Required

Optional if identical to the scope requested by the client.

Required if the access token scope is different from the one the client provided in the request to inform the client about the actual scope of the access token granted.

If the client does not provide the scope while requesting the access token, then the authorization server should provide the default scope, or deny the request, indicating the invalid scope.

State

Optional/Requried

Required if the state was passed in the client authorization request.

Error response:

Parameter

Required / Optional

Description

error

Required

One of the error codes defined in the specification, for example, unauthorized_client, invalid_scope.

error_description

Optional

Short description of the error.

error_uri

Optional

The URI of the error page describing the error.

An additional error parameter state is also sent in the error response if the state was passed in the client authorization request.

The first sample flow that we discussed in the OAuth 2.0 example flow for signing in with Twitter depicts the authorization code grant. We'll add a few more steps for its complete flow. As you know, after the eighth step, the end user logs in to the Quora application. Let's assume the user is logging in to Quora for the first time and requests their Quora profile page:

Resource owner password credentials grant requests and responses.

As seen in the previous section, in all the steps (a total of 13) of the authorization code flow, you can see that there are total of two requests made by the client to the authorization server, and the authorization server in reply provides two responses: one request-response for the authentication token and one request-response for the access token.

Let's discuss the parameters used for each of these requests and responses.

Access token request to the token endpoint URI:

Parameter

Required / Optional

Description

grant_type

Required

Password (this value must be used).

username

Required

Username of the resource owner.

password

Required

Password of the resource owner.

scope

Optional

The scope of the request. If not provided, then the authorization server provides the scope based on the defined policy.

Access token response (step 8):

Parameter

Required / Optional

Description

access_token

Required

The access token issued by the authorization server.

token_type

Required

The token type defined by the authorization server. Based on this, the client can utilize the access token. For example, bearer or mac.

refresh_token

Optional

This token can be used by the client to get a new access token using the same authorization grant.

expires_in

Recommended

Denotes the lifetime of the access token in seconds. A value of 600 denotes 10 minutes of lifetime for the access token. If this parameter is not provided in the response, then the document should highlight the lifetime of the access token.

Optional parameter

Optional

Additional parameter.

The first sample flow that we discussed in the OAuth 2.0 example flow for signing in with Twitter depicts the authorization code grant. We'll add a few more steps for its complete flow. As you know, after the eighth step, the end user logs in to the Quora application. Let's assume the user is logging in to Quora for the first time and requests their Quora profile page:

Client credentials grant requests and responses.

If you looked at all the steps (a total of 13) of the authorization code flow, you can see that there are total of two requests made by the client to the authorization server, and the authorization server in reply provides two responses: one request-response for the authentication token and one request-response for the access token.

Let's discuss the parameters used for each of these requests and responses.

Access token request to the token endpoint URI:

Parameter

Required / Optional

Description

grant_type

Required

client_credentials (this value must be used).

scope

Optional

The scope of the request. If not provided, then the authorization server provides the scope based on the defined policy.

Access token response:

Parameter

Required / Optional

Description

access_token

Required

The access token issued by the authorization server.

token_type

Required

The token type defined by the authorization server. Based on this, the client can utilize the access token. For example, bearer or mac.

expires_in

Recommended

Denotes the lifetime of the access token in seconds. A value of 600 denotes 10 minutes of lifetime for the access token. If this parameter is not provided in the response, then the document should highlight the lifetime of the access token.