OIDC
Overview
OpenID Connect (OIDC) Core 1.0 is an authentication protocol that is built on top of the OAuth 2.0 authorisation protocol. It allows relying parties to verify the identity of the end-user and to obtain basic user profile information. OIDC uses the standardised message flows from OAuth 2.0 to provide identity services.
Supported Grant Types
Edupass supports the following grant types:
- Authorisation Code Flow with Proof Key for Code Exchange (PKCE).
Authorisation Code Flow with Proof Key for Code Exchange (PKCE)
Since Edupass makes use of Entra ID as its OIDC provider, this section is based off https://learn.microsoft.com/en-us/entra/identity-platform/v2-oauth2-auth-code-flow but adapted for Edupass’s context.
The following sequence diagram provides a step-by-step breakdown of the entire authorisation code flow with PKCE. Noteworthy steps in the sequence diagram are elaborated upon below.
Note that the RP in the sequence diagram is split into “Frontend (Public Client)” and “Backend (Confidential Client)”. This distinction is necessary in an OAuth 2.0 context due to the following security consideration:
- Public client - Cannot securely store client secrets.
- Confidential client - Can securely store client secrets.
Examples of RPs which contain both a frontend and a backend include:
- Web applications with a single page application (SPA) frontend written in JavaScript.
- Desktop and mobile applications.
For the former, the source code of SPAs, along with any client secrets within, is available in its entirety to the browser. For the latter, the desktop/mobile frontend can be decompiled to retrieve its source code, along with any client secrets within.
RPs which do not have a frontend should treat the “Frontend (Public Client)” and “Backend (Confidential Client)” lifelines as a single lifeline.
[1] Code Verifier & Code Challenge Generation
Proof Key for Code Exchange (PKCE)
Public clients are susceptible to authorisation code interception attacks as described in https://datatracker.ietf.org/doc/html/rfc7636#section-1.
To guard against such attacks, the PKCE-enhanced authorisation code flow introduces a secret created by the RP in the form of the code verifier. When retrieving the authorisation code, the RP sends over a transformed value of the code verifier called the code challenge which is bound to the authorisation code that the authorisation endpoint returns. Subsequently, when exchanging the authorisation code for a token, a code verifier that transforms into the bound code challenge needs to be sent, else the token endpoint will reject the request. This prevents attackers from exchanging intercepted authorisation codes with tokens as they will not have the corresponding code verifier.
[Defense in depth] For confidential clients, the use of PKCE is recommended as it provides strong protection against the misuse and injection of authorisation codes. This recommendation is outlined in https://datatracker.ietf.org/doc/html/rfc9700#name-authorization-code-grant.
To secure the authorisation code grants using Proof Key for Code Exchange (PKCE), the client needs to generate both a code verifier and a code challenge before initiating a request to the authorisation endpoint.
The code verifier is a high-entropy cryptographic string with a minimum length of 43 characters and a maximum length of 128 characters. It can consist of alphanumeric characters, along with the hyphen (-), period (.), underscore (_) and tilde (~) characters.
The client should then derive the code challenge from the code verifier by:
- Calculating the SHA256 hash of the code verifier.
- Encoding the SHA256 hash with the base64url encoding.
[2] Authorisation Endpoint Request
The authorisation code flow begins with the client redirecting the user to the /authorize endpoint to request for an authorisation code.
[3] Authorisation Endpoint Response (Error)
When an error occurs during the authentication and authorisation process, Edupass will redirect the user to the URI specified in redirect_uri with an error message.
An error response for the authorisation endpoint is returned only under exceptional circumstances. For expected scenarios such as users not being able to successfully authenticate or authorise, Edupass will directly display an error message to the user without redirecting them back to the RP.
List of error codes
[4] Authorisation Endpoint Response (Success)
After the user successfully authenticates and authorises with Edupass, they will be redirected to the URI specified in redirect_uri with the authorisation code.
[5] Cross-Site Request Forgery (CSRF) Prevention
Cross-Site Request Forgery (CSRF)
Cross-Site Request Forgery (CSRF) is a class of exploits where an attacker causes a user to follow a malicious URI (e.g., via a misleading link, an embedded image, or redirection) to a trusting server (usually established through the presence of a session cookie).
A CSRF attack against an RP's redirect URI allows an attacker to inject their own authorisation code, which can result in the RP using an access token associated with the attacker's protected resources rather than the user's. This could be used to save confidential information belonging to the user to a protected resource controlled by the attacker.
To guard against CSRF attacks, RPs should generate an unpredictable value called the CSRF token before each request to the authorisation endpoint. This CSRF token should be encoded within the state parameter of the authorisation request, which the OIDC provider will reflect back as part of the authorisation response. RPs are then able to distinguish between genuine authorisation responses from malicious attacker-forged authorisation responses by the presence of a valid CSRF token in the state parameter of the authorisation response.
RPs should implement CSRF tokens even if they also implement PKCE.
The use of PKCE protects against certain CSRF attacks as an attacker-injected authorisation code will not match the PKCE code verifier, and thus will not be able to be exchanged for any tokens. However, it does not protect against CSRF attacks which do not require token exchange. The use of CSRF tokens is necessary to protect against the full range of possible CSRF attacks.
Before initiating requests to the authorisation endpoint, RPs should generate a secret, unpredictable value to act as the CSRF token. This CSRF token should be generated on a per-request basis, and should not be reused across requests.
When making requests to the authorisation endpoint, RPs should encode the CSRF token as part of the state parameter. The CSRF token should also be stored as part of the user’s session, be it as a server-side session, or as a secure HTTP-only cookie.
Upon receiving an authorisation endpoint response, RPs should compare the value of the state parameter in the response with that of the request as stored in the user’s session. If the values do not match, RPs should reject the response.
The CSRF token should be validated even for error authorisation endpoint responses to guard against malicious error responses forged by an attacker.
[6] Token Endpoint Request
Together with the code verifier generated in [1] Code Verifier & Code Challenge Generation, the authorisation code acquired should then be exchanged for ID, access, and refresh tokens by making a call to the /token endpoint.
Client Assertion Generation
This section is adapted from https://learn.microsoft.com/en-us/entra/identity-platform/certificate-credentials#assertion-format.
For added security, confidential clients are required to authenticate themselves with Entra ID using a JSON Web Token (JWT) assertion when making requests to the token endpoint. Public clients are exempt from this requirement because they are unable to securely store secrets.
The X.509 certificate mentioned in this section refers to the one provided by the RP to the Edupass team during onboarding.
The JWT assertion is made up of 3 parts, a header, claims, and signature.
Header
Contains metadata about the type of the token and the cryptographic algorithms used to secure its contents.
Claims
Contains assertions about the RP.
Signature
Used to validate that the token is trustworthy and has not been tampered with.
To create the signature,
- Base64url-encode the header and the claims.
- Join the encoded header and claims with a dot.
- Hash the combined string using SHA-256.
- Sign the hash using the RSA private key of the X.509 certificate with the RSASSA-PSS (Probabilistic Signature Scheme) padding scheme.
Encoded JWT
To form the final encoded JWT that will be sent as an assertion, base64url-encode the header, claims and signature separately, then join them with dots.
[7] Token Endpoint Response (Error)
The following response is returned when an error occurs during token exchange.
List of error codes
[8] Token Endpoint Response (Success)
The following response is returned when the token exchange is successful:
ID Token
The ID token is a JWT containing information about the user that has authenticated.
Example ID token returned when only the openid scope is set:
Claims with null or empty values are omitted in the ID token object in accordance with the OIDC specifications: https://openid.net/specs/openid-connect-core-1_0.html#UserInfoResponse. In the example above, the user does not have a WOG email address, and thus the wogEmail claim is omitted entirely.
Edupass will provide all claims about the user as part of the ID token, rather than via the OIDC UserInfo endpoint.
Upon receiving an ID token, RPs should:
- Verify the signature of the token using Entra ID’s signing keys, which can be found at https://login.microsoftonline.com/{tenant_id}/discovery/v2.0/keys.
- Validate that the
issclaim matches the expected Entra ID tenant, i.e.,https://login.microsoftonline.com/{tenant-id}/v2.0. - Validate that the
audclaim matches the client ID for the RP. - Validate that the
iat,nbf, andexptimestamps all fall before or after the current time, as appropriate.
Access Token
The access token is not currently used.
Refresh Token
The refresh token is used to obtain new access and refresh token pairs when the current access token expires. Along with the access token, it is not currently used.
Signing Out Users
To sign a user out, the following 2 operations must be done:
- Redirect the user to Entra ID’s logout endpoint.
- End the user’s session in the RP.
Failure to perform either of these operations may result in the user remaining authenticated and not being prompted to sign-in the next time they access the RP.
The logout endpoint supports both GET and POST requests.