Picture of the author
Published on

OAuth2

Authors

Enterprise authentication solution

SAML protocol: Single sign on(SSO) across sites.

Legacy authentication solution

Simple login: Forms and cookes.

What problem does Oauth2 address

  • Delegated authorization problem.

Oauth2 flow

  • Click on login with Gmail.
  • Redirected to accounts.google.com and prompted to login.
  • After logging in, prompt the user if they want to share public profile and contacts with Yelp?
  • If user clicks Yes, then he is redirected back to application, at a specific place called 'redirect uri'.

Oauth2 terminology

  • Resource owner: The user clicking yes at prompt.
  • Client: The application for eg. Yelp.
  • Authorization server: System which user uses to say Yes, for eg. accounts.google.com
  • Resource server: The API or resources which the client want access to. For eg Google contacts API.
  • Authorization grant: Proves that resource owner/user has clicked yes.
  • Redirect Uri/callback: Auth flow goes here when "user" clicks yes.
  • Access token: Used by client to get access to resource server.
  • Scope: Authorization server has a list of scopes for eg contact-read, contact-write, profile-read etc.
  • Consent:

Network security terminology:

  • Back channel: Highly secure channel, for eg. from a backend server to resource server. The secret key/application id/ access token is never known to and sent from the front channel.
  • Front channel: Less secure channel, for eg. browser. We cannot put a key in html or javascript of the web app as anyone can view the source. It is used to interact with the user or resource owner.

Oauth2 Flows

Oauth2 Flows

Oauth2 authorization code Flow

Oauth2 code Flow

Note

  • state is used to prevent CSRF.
  • The client registers with authorization server and gets client ID and client secret which are later used in front and back channel communication respectively. It also has to specify redirect URI/Callback.

Starting the Oauth2 authorization code flow

Starting the flow

Calling back

Starting the flow

Exchange the code for access token

Exchange code for token

Access token from server

Exchange code for token

Use the access token

Exchange code for token

Identity use cases Pre 2014

Identity use cases Pre 2014

Problems with Oauth2 Authentication

  • Oauth2 was primarily designed for authorization. As there is no standard way of getting user's information.
  • No standard way to get user's information.
  • Every implementation is little different.
  • No common set of scopes.

What OpenID connect adds

  • ID token.
  • UsertInfo endpoint for getting more user information.
  • Standard set of scopes.
  • Standardized implementation.

Identity use cases today (with openID)

Identity use cases today

OpenID stack

OpenID stack

Oauth2 vs OpenID

Oauth2 with OpenID

OpenID connect authorization code flow

openID flow

Starting OIDC flow

Starting OIDC

Exchange code for access token and ID token

Starting OIDC

Auth server returns access and ID tokens

Return token and ID

ID token (aka JWT) in OpenID

The ID token

ID token in code/json

ID token in code/json

ID Token(JWT) in debugger

ID TOken(JWT) in debugger

Calling user info endpoint

Calling user info endpoint

Oauth2 Implicit flow

Oauth2 Implicit flow

Which authorization grant type (flow) to use

Authorization grant types

Server flow example

Server flow example

Mobile flow example

Mobile flow example

Authorization code with PKCE part1

Authorization code with PKCE part1

Authorization code with PKCE part2

Authorization code with PKCE part2

Implicit Flow example

Implicit Flow example

Oauth discovery

Oauth discovery

Third party(SAML) integration example

Third party integration example

Token validation

  • Local validation: The fast way.
    • Check expiration timestamp.
    • Validate cryptographic signature.
  • Introspection: The strong way.

Keeping user signed in

For both local validation and introspection, the token is invalid once it expires

  • If there is user at keyboard, just redirect him to authorization server.
  • If there is no user(automated tasks), request a refresh token(offline scope).

Note: Using okta you can spin your own authorization server in the cloud.