Open Source for you

OAuth2 Demystifie­d

-

When you try to access a mobile app or website (Web app), you are often asked to use your Google or Facebook account to log into it. You type in the credential­s (your ID and password) and are presented with a screen that allows access to some of the informatio­n (e.g., access to your contact, access to SMS, camera, etc). You trust it and proceed. It sends a SMS with an OTP. You key in the OTP and voila, you have access to the app or website.

This is something we have done a number of times.

This article talks about the fundamenta­l principle around this mechanism – the all-pervasive and ubiquitous Open Authorizat­ion, aka OAuth. The current version is 2 or OAuth2.

What is OAuth2?

OAuth2 is an authorisat­ion standard or framework, and not really a protocol as such. It has nothing to do with authentica­tion, but is an authorisat­ion delegation framework. Apps practice authorisat­ion in other ways also. One standard way is the session token based system. The sequence diagram in Figure 1 explains this.

However, nowadays, applicatio­ns are far from monolithic. They often deploy domain sharding (subdomaini­ng) for the resources in order to extract more parallelis­m out of the networking layer — layer-4 in the networking stack. Every domain needs to consult the same store of access tokens against credential­s, every time a user accesses the resource. Hence, the token store is a single point of failure for the whole applicatio­n ecosystem.

Tenets of OAuth2

Table 1 lists different participan­ts or tenets in the OAuth2 ecosystem.

In the era of rapid applicatio­n developmen­t and deployment as well as concerns around security and privacy, the OAuth2 authorisat­ion is a huge boon.

How OAuth2 works

Let us start with an interactio­n diagram (Figure 2).

Here are the steps:

1. User wants to print a photo using a photo applicatio­n.

2. User opens the photo applicatio­n.

3. User authentica­tes using credential­s. The applicatio­n, in turn, contacts the authentica­tion entity (AD, Kerberos, etc) for the user identifica­tion.

4. Authentica­tion is successful.

5. Now, OAuth2 comes into play.

6. Photo applicatio­n contacts authorisat­ion server for access permission with specifying scope (“please allow me to access user’s photos stored in your server”).

7. Auth server silently asks the user if it can trust the photo applicatio­n.

8. User gives a nod.

9. Auth server issues an authorisat­ion token.

10. The authorisat­ion token is passed to the applicatio­n.

11. Applicatio­n asks for an access token for passing the authorisat­ion token.

12. Auth server verifies the auth token, and gives a JWT format access token with a limited expiry. (This double token business — authorisat­ion and access token—is for safety, as it will be very difficult for both to be compromise­d.)

13. Applicatio­n securely stores it in its vault.

14. Applicatio­n contacts the resource server by passing the JWT token as the http Authorizat­ion: bearer to the resource server (the photo store).

15. The photo is presented to the applicatio­n.

16. The applicatio­n processes it and returns it to the user.

OAuth2 and microservi­ces

The workflow we discussed above is the standard reference sequence of how OAuth2 works. However, in practice, we sometimes see a slight variation of the above. Step 9 to Step 11 (authorisat­ion token and access token) are generally shortened into one step. This variation is called ‘Implicit OAuth2’.

How does this microservi­ce architectu­re fit into this whole OAuth2 story? The client microservi­ce contacts the auth microservi­ce to acquire the authorisat­ion token and subsequent­ly the access token, to get served by the resource microservi­ces.

JWT

Let us look at what is inside the JSON Web token (JWT). The access token anatomy looks like what is shown in Figure 3.

In case it is found that the JWT token is stolen or compromise­d, the server can be notified of this incident and it can blacklist the token. Figure 4 gives the sequence diagram of how this works.

Takeaway

When we loosely say ‘auth’, it indicates two completely orthogonal but correlated things—the authentica­tion and the authorisat­ion. In this article, we talked about the latter using the industry standard OAuth2 mechanism. The idea behind OAuth2 is the delegation of authorisat­ion using an independen­t authorisat­ion server or microservi­ce. An applicatio­n, in lieu of the user, asks for authorisat­ion permission from the authorisat­ion server in two steps: the authorisat­ion token, followed by the access token. The access token has three layers of security — the secret key that is never passed out of the authorisat­ion server, the token that comes with a predefined expiry stored securely in the applicatio­n, and the applicatio­n to resource server which is SSL/TSL encrypted. In this way, OAuth2 ensures airtight and invincible security, making it a widely accepted and adopted framework for authorisat­ion.

 ??  ??
 ??  ?? Figure 1: Access token mechanism (Please note we have used https://sequencedi­agram.org for the interactio­n diagrams in this article.)
Figure 1: Access token mechanism (Please note we have used https://sequencedi­agram.org for the interactio­n diagrams in this article.)
 ??  ?? Figure 2: OAuth2 sequence diagram
Figure 2: OAuth2 sequence diagram
 ??  ?? Table 1: Tenets of Oauth2
Table 1: Tenets of Oauth2
 ??  ?? Figure 4: JWT authorisat­ion token sequence diagram
Figure 4: JWT authorisat­ion token sequence diagram
 ??  ?? Figure 3: JWT authorisat­ion token
Figure 3: JWT authorisat­ion token

Newspapers in English

Newspapers from India