Implementing OIDC logout and session management: A complete guide
Explore OIDC authentication and session management in depth. Learn how to implement OIDC RP-initiated, IdP-initiated, and back-channel logout for secure session handling.
Explore OIDC authentication and session management in depth. Learn how to implement OIDC RP-initiated, IdP-initiated, and back-channel logout for secure session handling.
OpenID Connect (OIDC) is a simple identity layer built on top of the OAuth 2.0 protocol. It allows clients to verify the identity of the end-user based on the authentication performed by the authorization server, as well as to obtain basic profile information about the end-user in an interoperable and REST-like manner.
OIDC is designed to be easy to use and implement, with a focus on simplicity and flexibility. It is widely used for single sign-on (SSO) and identity verification in web applications, mobile apps, and APIs.
Understanding authentication status and session management in OIDC is crucial. This article explains how OIDC sessions and user authentication status are managed in the context of interactions between the Identity Provider (IdP) and Relying Party (RP) or Service Provider (SP).
This article includes several key terms.
To better understand OIDC session and user authentication status management, let's briefly review the OIDC authentication flow for a web application:
A sign-in session is established when a user logs in to the IdP. This session is used to track the user's authentication status at the IdP. The session typically includes information such as the user's identity, authentication time, and session expiration time. It is created when the user fist logs in and is maintained until the user logs out or the session expires.
A session cookie will be securely set in the user's browser to maintain the session state. The session cookie is used to identify the user's session and authenticate the user for subsequent authentication requests. This cookie is typically set with the HttpOnly and Secure flags to prevent client-side access and ensure secure communication.
For each RP that the user accesses from different devices or browsers, a separate user sign-in session will be established. This means that the user's authentication status is maintained separately for each RP. If the user logs out from one RP, the user will still be authenticated at other RPs until the session expires or the user logs out from all RPs.
This centralized session management also allows the IdP to maintain a consistent authentication state across multiple RPs as long as the user's session is active and the authentication requests come from the same user agent (device/browser). This mechanism enables SSO capabilities, where the user can access multiple RPs without having to log in again.
In OIDC, the client application (RP) relies on the IdP issued tokens to verify the user's identity and authentication or authorization status. The "sign-in session" at the client-side is maintained by the tokens issued by the IdP.
offline_access scope is requested and granted, the client application may receive a refresh token. It provides a means to extend the user's authentication status without requiring the user to re-authenticate. The client application can use the refresh token to obtain a new access token when the current access token expires. As long as the refresh token is valid, the user's authentication status can be maintained without the need for user interaction.The combination of these tokens allows the client application to maintain the user's authentication status and access protected resources on behalf of the user. The client application need to securely store these tokens and manage their lifecycle. (E.g. For SPA applications, the tokens can be stored in the browser's local storage or session storage. For web applications, the tokens can be stored in the server-side session data or cookies.)
The sign-out process in OIDC is a multi-faced concept due to the involvement of both centralized IdP managed sign-in sessions and distributed client-side tokens.
To sign out or revoke a user's authentication status at the client side is relatively straightforward. The client application can remove stored tokens (ID token, access token, and refresh token) from the user's browser or memory. This action effectively invalidates the user’s authentication status on the client side.
For web applications that manage their own user sign-in sessions, additional steps may be necessary. These include clearing the session cookie and any session data (such as tokens issued by the Identity Provider, or IdP) to ensure the user is fully signed out.
The IdP maintains a centralized sign-in session for each user. As long as this session is active, the user may be automatically re-authenticated even if client-side tokens have been cleared, allowing new tokens to be issued to the client application without requiring further interaction with the IdP.
To fully sign out a user from the IdP, the client application (RP) can initiate a sign-out request to the IdP. The application (RP) should redirect the user to the IdP's end-session endpoint to terminate the sign-in session can clear session cookies. This ensures a complete sign-out across all applications (RPs) sharing a same centralized session. Once the sign-in session is terminated, whenever the IdP receives an token request from any linked RPs that share the same session, the IdP will prompt the user to re-authenticate.
In some cases, when a user signs out from one application (RP), they may also want to be automatically signed out from all other applications (RPs) without any additional user interaction. This can be accomplished using the back-channel logout mechanism.
When the IdP receives a sign-out request from an RP, it not only clears the sign-in session but also sends a back-channel logout notification to all RPs use the same session and have a registered back-channel logout endpoint
When the RPs receive the back-channel logout notification, they can perform the necessary actions to clear the user's session and tokens, ensuring that the user is fully signed out from all applications.