OpenID Connect (OIDC) is an identity layer built on top of OAuth 2.0 authorization protocol. It facilitates third-party apps(clients) in verifying user's identity as well as accessing their basic profile information.
Now, let's familiarize ourselves with few terminologies before understanding how OIDC works:
An OAuth 2.0 authorization component that helps in authenticating the user and providing user information to the client requesting these information.
The client application that requests user authentication and user information from the OpenID provider.
Prerequisites for clients:
Clients(Relying Party) should have registered themselves with the Resource Provider (OpenID Provider) and gotten their Client ID and Client Secret from OpenID Provider.
Basic OIDC flow:
Relying Party requests the Authorization Endpoint of OpenID Provider to authenticate the user and get user's authorization to access certain user information. After authenticating the user and obtaining authorization, the authorization endpoint sends an ID token and access token to the Relying Party.
The method used for this token exchange varies based on the Relying Party (RP) type and the authentication flow chosen. We will explore about the RP types and the recommended authentication flows for each in the later sections of this article.
RP requests user information (claims) to the UserInfo Endpoint of the OP with the access token. OP sends the consented claims to the RP.
Relying Party types and recommended authentication flows:
Regular Web Application (MPA) and Authorization Code flow:
These applications run on a server and send new page requests to the server for each action. These applications can store client secrets securely; hence, they are also referred to as Confidential Clients. The optimal authentication flow recommended for MPAs is Authorization Code Flow.
In this flow, RP(client) requests the Authorization Endpoint of OP to authenticate the user and get authorization to access certain user information. After authenticating the user and obtaining authorization, the authorization endpoint sends Authorization Code to the client.The client then exchanges this authorization code for ID token and access token (if requested, refresh token as well) at the token endpoint of OP. The client retrieves required user information(claims) from the ID token.
Single-Page Application (SPA) and Implicit flow
SPAs are modern web applications that loads the required section based on your action. These applications typically run on the client side after initially retrieving all the necessary resources from the server. They are also referred to as public clients, and they can't store client secrets securely as their entire source is on a browser. The suggested authentication flow for SPAs is the Implicit Code Flow.
In this flow, client(RP) requests Authorization Endpoint of OP to authenticate the user and get authorization to access certain user information. After authenticating the user and obtaining authorization, the authorization endpoint sends the ID token directly to the client. If requested, they also send access and refresh tokens. The client retrieves necessary user information from the ID token. Token endpoint is not used in this flow.
Native Applications and PKCE flow
Native applications are the ones installed directly on the specific device. They are also known as public clients. They can't store their secrets securely, as they are directly installed onto a device, and the applications can be decompiled by anyone to access the client secrets. The flow recommended for native apps is Authorization Code Flow with Proof Key for Code Exchange (PKCE).
In this flow, the client(RP) generates a Code Verifier(a random string) and a Code Challenge (hashed version of the code verifier using any hashing method). Along with the authorization request sent to the Authorization endpoint of OP, the client also sends the code verifier. After authenticating the user and obtaining authorization, the authorization endpoint sends the Authorization Code to the client. At the token endpoint of OP, the client provides this authorization code along with the Code Challenge and the hashing method used to hash the code verifier. The token endpoint then dehashes the code challenge using the mentioned hashing method and checks whether the answer and the code verifier matches, to verify that it received the authorization code from the same client who sent authorization request.The token endpoint then provides the ID token and access token (if requested, refresh token as well). The client gets required user information from the ID token.