Skip to content

Instantly share code, notes, and snippets.

@makomweb
Created January 3, 2020 13:55
Show Gist options
  • Save makomweb/e7c8e2311253ea1f97411239174fe306 to your computer and use it in GitHub Desktop.
Save makomweb/e7c8e2311253ea1f97411239174fe306 to your computer and use it in GitHub Desktop.
My learnings about OAuth2.0 and Open ID Connect

OAuth 2.0

Endpoints

Endpoint Example Purpose
Authorization https://<auth-server>/oauth/authorize used by the client application to obtain authorization grant from the resource owner via user-agent redirection
Token https://<auth-server>/oauth/token used by the client application to exchange an authorization grant for an access token, typically with client authentication
Redirection https://<client>/callback used by the authorization server to return responses containing authorization grants to the client via the resource owner user-agent

Channels: Back channel vs. Front channel

Flow Front channel Back channel
Authorization code flow
Implicit flow
Resource owner password credentials flow
Client credentials flow
  • server to server / machine to machine (BC)

    • exchange of auth-code into an access token
    • for security reasons, so that browsers cannot leak the access token
    • trust relationship between the servers must exist
    • secret "keys" cannot leak from the server(s)
    • client id + client secret enables server to perform token exchange
  • browser to server (FC)

    • full page redirects
    • client id enables client to init OAuth2 flow

OAuth2.0 - Authorization code flow

  • aka auth-code-flow
  • uses front channel (solid lines) and back channel (dashed line)
  • response type code
  • client-secret is usually not needed in this scenario

Flow

OAuth2.0 - Implicit flow

  • when there is no back-channel involved
  • uses the front channel (solid lines) exclusively
  • browser to server: e.g. SPA using JS which talks to the server
  • response type resp. grant_type: token
  • ensure the access_token cannot be leaked from the apps local storage
  • refresh tokens are not given to a client with this flow

Flow

OAuth2.0 - Hybrid Flow

  • is a mix of implicit flow and authorization code flow
  • after a client got an access_token through the implicit flow it requests an authorization_code
  • with this authorization_code it can do the token-exchange to obtain a refresh_token

OAuth2.0 - Client Credentials Flow

  • back channel only

  • machine to machine communication

  • service to service communication

  • Request is made to the Auth-Service with the following information:

    • grant_type is set to client_credentials
    • client-id identifies the requesting party
    • client-secret authorizes the requesting party
    • scope contains the requested permissions
  • access_token is provided by the Auth-Service with the response

  • (id_token can also be part of the response when the server is able to talk OIDC)

  • SEE VIDEO https://www.javainuse.com/spring/springboot-oauth2-client-grant !!!

OAuth2.0 - Resource owner password credentials flow

  • back channel only
  • (need to find out more information)

Open ID Connect

  • extension to OAuth2.0
  • ID token
  • /userinfo endpoint to get user information in a standardized way

Open ID Connect authorization code flow

Flow

  • scope contains openid
  • auth_code is used to exchange with an access_token and id_token
  • id-token is a Json Web Token and can be used to provide a small subset of user information
  • /userinfo endpoint is used to get more user information

Use cases

Identity use cases
Simple login (Open ID Connect) Authentication
Single sign-on across sites (OpenID Connect) Authentication
Mobile app login (Open ID Connect) Authentication
Delegated authorization (OAuth2.0) Authorization
Granting access to APIs (OAuth2.0) Authorization
Getting access to user data in other systems (OAuth2.0) Authorization
Logging the user in (Open ID Connect) Authentication
Making your accounts available in other systems (Open ID Connect) Authentication

Which grant type flow do I use?

Grant type Scenario
Authorization web application with server backend, native mobile app
Implicit SPA (Javascript app) with API backend
Client Credentials Microservices and APIs - access happens not on behalf of a user
Refresh Token All scenarios where an access token can expire
Password when a clients exchanges username and password for an access_token
Resource Owner Password aka Password flow
The password grant type is highly discouraged!

Introspection endpoint

Destructures an access_token. Only trusted clients are allowed to issue requests against this endpoint. Therefor they need to authenticate.

Read documentation here to get an idea about security considerations and token phishing.

Refresh Token

Exchange refresh token for access token by providing:

  • refresh token
  • client-id
  • client-secret
  • grant_type: refresh_token

Response is a new access token plus (optionally) an new refresh token.

If you do not get back a new refresh token, then it means your existing refresh token will continue to work when the new access token expires.
When refreshing fails the user must be prompted to relogin!

Note:

  • access_token s have a short lifetime
  • refresh_token s have long lifetime
  • the combination maximized security and flexibility
  • some services issue refresh_tokens which never expire

Rotate out refresh tokens

When your service issues a new access token in response to a refresh token grant, it is possible for your service to issue a new refresh token simultaneously, and expire the previous one. This means refresh tokens rotate out frequently, which may be desirable for your application. If this is the case, ensure developers know this will happen so they don’t mistakenly assume the first refresh token they obtain will continue to work indefinitely.

Not covered

  • scopes
  • proof code for code exchange (PKCE)

Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment