OAuth2/OpenID Connect (OIDC)#

Sources#

The colored mermaids diagram are Joerg’s creations, drawing from https://medium.com/identity-beyond-borders/oauth-2-0-basics-3d06bce33870

The fine ASCII art is from the RFC itself, https://datatracker.ietf.org/doc/html/rfc6749

Roles/Participants#

  • Resource server: the server holding valuable resources, e.g. user info or cat pictures

  • Client application: a service that wants to access something, e.g. a printing service

  • Resource owner / User / Browser: typically a human that has control over the valuable resources on the server. Clicks the web in the browser.

  • Authorization server: a service that grants access tokens. Trusted by the resource server.

OAuth flows#

Preparation#

Done as a preparation in all following scenarios

../_images/oauth_flows_preparation.svg

Authorization Code#

Most secure, with separate channels for the browser and the client application

../_images/oauth_flows_channels.svg

The workflow:

../_images/oauth_flows_authorization_code.svg
+----------+
| Resource |
|   Owner  |
|          |
+----------+
  ^
  |
 (B)
+----|-----+          Client Identifier      +---------------+
|         -+----(A)-- & Redirection URI ---->|               |
|  User-   |                                 | Authorization |
|  Agent  -+----(B)-- User authenticates --->|     Server    |
|          |                                 |               |
|         -+----(C)-- Authorization Code ---<|               |
+-|----|---+                                 +---------------+
|    |                                         ^      v
(A)  (C)                                        |      |
|    |                                         |      |
^    v                                         |      |
+---------+                                      |      |
|         |>---(D)-- Authorization Code ---------'      |
|  Client |          & Redirection URI                  |
|         |                                             |
|         |<---(E)----- Access Token -------------------'
+---------+       (w/ Optional Refresh Token)

 Note: The lines illustrating steps (A) and (B) are broken into two
 parts as they pass through the user-agent.

(A)  The client initiates the flow by directing the resource owner's
    user-agent to the authorization endpoint.  The client includes
    its client identifier, requested scope, local state, and a
    redirection URI to which the authorization server will send the
    user-agent back once access is granted (or denied).

(B)  The authorization server authenticates the resource owner (via
    the user-agent) and establishes whether the resource owner
    grants or denies the client's access request.

(C)  Assuming the resource owner grants access, the authorization
    server redirects the user-agent back to the client using the
    redirection URI provided earlier (in the request or during
    client registration).  The redirection URI includes an
    authorization code and any local state provided by the client
    earlier.

(D)  The client requests an access token from the authorization
    server's token endpoint by including the authorization code
    received in the previous step.  When making the request, the
    client authenticates with the authorization server.  The client
    includes the redirection URI used to obtain the authorization
    code for verification.

(E)  The authorization server authenticates the client, validates the
    authorization code, and ensures that the redirection URI
    received matches the URI used to redirect the client in
    step (C).  If valid, the authorization server responds back with
    an access token and, optionally, a refresh token.

from https://datatracker.ietf.org/doc/html/rfc6749#section-4.1

Implicit#

Insecure, better use authorization code

../_images/oauth_flows_implicit.svg
+----------+
| Resource |
|  Owner   |
|          |
+----------+
  ^
  |
 (B)
+----|-----+          Client Identifier     +---------------+
|         -+----(A)-- & Redirection URI --->|               |
|  User-   |                                | Authorization |
|  Agent  -|----(B)-- User authenticates -->|     Server    |
|          |                                |               |
|          |<---(C)--- Redirection URI ----<|               |
|          |          with Access Token     +---------------+
|          |            in Fragment
|          |                                +---------------+
|          |----(D)--- Redirection URI ---->|   Web-Hosted  |
|          |          without Fragment      |     Client    |
|          |                                |    Resource   |
|     (F)  |<---(E)------- Script ---------<|               |
|          |                                +---------------+
+-|--------+
|    |
(A)  (G) Access Token
|    |
^    v
+---------+
|         |
|  Client |
|         |
+---------+

Note: The lines illustrating steps (A) and (B) are broken into two
parts as they pass through the user-agent.


(A)  The client initiates the flow by directing the resource owner's
    user-agent to the authorization endpoint.  The client includes
    its client identifier, requested scope, local state, and a
    redirection URI to which the authorization server will send the
    user-agent back once access is granted (or denied).

(B)  The authorization server authenticates the resource owner (via
    the user-agent) and establishes whether the resource owner
    grants or denies the client's access request.

(C)  Assuming the resource owner grants access, the authorization
    server redirects the user-agent back to the client using the
    redirection URI provided earlier.  The redirection URI includes
    the access token in the URI fragment.

(D)  The user-agent follows the redirection instructions by making a
    request to the web-hosted client resource (which does not
    include the fragment per [RFC2616]).  The user-agent retains the
    fragment information locally.

(E)  The web-hosted client resource returns a web page (typically an
    HTML document with an embedded script) capable of accessing the
    full redirection URI including the fragment retained by the
    user-agent, and extracting the access token (and other
    parameters) contained in the fragment.

(F)  The user-agent executes the script provided by the web-hosted
    client resource locally, which extracts the access token.

(G)  The user-agent passes the access token to the client.

from https://datatracker.ietf.org/doc/html/rfc6749#section-4.2

Password#

Getting even worse then implicit.

../_images/oauth_flows_password.svg
+----------+
| Resource |
|  Owner   |
|          |
+----------+
  v
  |    Resource Owner
 (A) Password Credentials
  |
  v
+---------+                                  +---------------+
|         |>--(B)---- Resource Owner ------->|               |
|         |         Password Credentials     | Authorization |
| Client  |                                  |     Server    |
|         |<--(C)---- Access Token ---------<|               |
|         |    (w/ Optional Refresh Token)   |               |
+---------+                                  +---------------+

        Figure 5: Resource Owner Password Credentials Flow

The flow illustrated in Figure 5 includes the following steps:

(A)  The resource owner provides the client with its username and
    password.

(B)  The client requests an access token from the authorization
    server's token endpoint by including the credentials received
    from the resource owner.  When making the request, the client
    authenticates with the authorization server.

(C)  The authorization server authenticates the client and validates
    the resource owner credentials, and if valid, issues an access
    token.

from https://datatracker.ietf.org/doc/html/rfc6749#section-4.3

Client credentials#

The client application is the resource owner. No human involved

../_images/oauth_flows_client_credentials.svg
+---------+                                  +---------------+
|         |                                  |               |
|         |>--(A)- Client Authentication --->| Authorization |
| Client  |                                  |     Server    |
|         |<--(B)---- Access Token ---------<|               |
|         |                                  |               |
+---------+                                  +---------------+

                 Figure 6: Client Credentials Flow

The flow illustrated in Figure 6 includes the following steps:

(A)  The client authenticates with the authorization server and
    requests an access token from the token endpoint.

(B)  The authorization server authenticates the client, and if valid,
    issues an access token.

from https://datatracker.ietf.org/doc/html/rfc6749#section-4.4

OpenID Connect (OIDC)#

On top of OAuth2. Looks quite similar.

../_images/oauth_flows_oidc.svg

The id token in (6) is the crucial bit. The client can access resources after this as well, because the client also received an access token

Reading materials#

  1. https://developer.okta.com/blog/2019/10/21/illustrated-guide-to-oauth-and-oidc

  2. https://www.onelogin.com/blog/openid-connect-explained-in-plain-english-2

  3. https://openidconnect.net/

  4. https://connect2id.com/learn/openid-connect, especially https://connect2id.com/learn/openid-connect#example-auth-code-flow

  5. https://hutten.knut.univention.de/mediawiki/index.php/Uttuusch (search for “Erik”)