Files
Ophir LOJKINE 083593d927 Implement secure oidc logout endpoint and sql function (#1141)
* Checkpoint before follow-up message

Co-authored-by: contact <contact@ophir.dev>

* Checkpoint before follow-up message

Co-authored-by: contact <contact@ophir.dev>

* Checkpoint before follow-up message

Co-authored-by: contact <contact@ophir.dev>

* Checkpoint before follow-up message

Co-authored-by: contact <contact@ophir.dev>

* feat: Add OIDC logout functionality

This commit introduces the `oidc_logout_url` function, allowing users to securely log out of OIDC-authenticated applications. It includes CSRF protection and handles redirection to the OIDC provider's logout endpoint.

Co-authored-by: contact <contact@ophir.dev>

* Refactor OIDC logout cookie removal

Co-authored-by: contact <contact@ophir.dev>

* feat: Implement OIDC logout with CSRF protection

This commit implements secure OIDC logout by:

- Using sqlpage.oidc_logout_url() to generate the logout URL.
- Ensuring CSRF protection during the logout process.
- Redirecting to the OIDC provider's logout endpoint.
- Redirecting back to the homepage after logout.
- Adding absolute URI for post logout redirect URI.

* refactor: Enhance build_absolute_uri function to accept scheme parameter

This commit modifies the build_absolute_uri function to include a scheme parameter, allowing for more flexible URL construction. The function now dynamically sets the URL scheme based on the request context, improving compatibility with different environments.

* refactor: Simplify OIDC logout processing and enhance logout token handling

This commit refactors the OIDC logout process by introducing a new function, `parse_logout_params`, to streamline the extraction of logout parameters from the request. It also updates the logout token creation and verification logic, improving security by ensuring the signature is computed correctly. Additionally, the `create_logout_url` function is modified to include a timestamp and signature in the generated URL, enhancing the logout flow's integrity.

* refactor: Improve logout URL generation and parameter parsing

This commit refines the `create_logout_url` function to utilize a query string builder for constructing the logout URL, enhancing readability and maintainability. Additionally, the `parse_logout_params` function is updated to use `Query::into_inner`, streamlining the extraction of logout parameters from the request.

* refactor: Streamline cookie removal in OIDC logout process

This commit simplifies the removal of authentication and nonce cookies during the OIDC logout process by consolidating the cookie removal logic into a single method call for each cookie, enhancing code clarity and maintainability.

* refactor: Enhance cookie removal logic in OIDC logout process

This commit updates the cookie removal process during OIDC logout by utilizing the `Cookie::build` method to specify cookie attributes, improving clarity and ensuring proper cookie handling.

* chore: Update CHANGELOG for version 0.40.1

- Added new function `sqlpage.oidc_logout_url(redirect_uri)` to generate secure logout URLs for OIDC users, supporting RP-Initiated Logout.
- Fixed compatibility issues with Auth0 for OpenID-Connect authentication.
2025-12-07 01:06:52 +01:00
..
2024-04-28 14:07:29 +02:00
2024-04-28 14:07:29 +02:00

SQLPage Single Sign-On demo

This project demonstrates how to implement external authentication (Single Sign-On) in a SQLPage application using SQLPage's built-in OIDC support.

It demonstrates the implementation of two external authentication protocols:

Depending on your use case, you can choose the appropriate protocol for your application.

Screenshots

Home Page Login Page User Info
Home Page Login Page User Info

Running the Demo

To run the demo, you just need docker and docker-compose installed on your machine. Then, run the following commands:

docker compose up --watch

This will start a Keycloak server and a SQLPage server. You can access the SQLPage application at http://localhost:8080.

The credentials for the demo are:

  • Username: demo
  • Password: demo

The credentials to the keycloak admin console accessible at http://localhost:8180 are admin/admin.

CAS Client

This example also contains a CAS (Central Authentication Service) client in the cas directory that demonstrates how to authenticate users using the CAS protocol (version 3.0), which is mostly used in academic institutions.

OIDC Client

OIDC is an authentication protocol that allows users to authenticate with a third-party identity provider and then access applications without having to log in again. This is useful for single sign-on (SSO) scenarios where users need to access multiple applications with a single set of credentials. OIDC can be used to implement a "Login with Google" or "Login with Facebook" button in your application, since these providers support the OIDC protocol.

SQLPage has built-in support for OIDC authentication since v0.35. This project demonstrates how to use it with the free and open source Keycloak OIDC provider. You can easily replace Keycloak with another OIDC provider, such as Google, or your enterprise OIDC provider, by following the steps in the Configuration section.

Public and Protected Pages

By default, SQLPage's built-in OIDC support protects the entire website. However, you can configure it to have a mix of public and protected pages using the oidc_protected_paths option in your sqlpage.json file.

This allows you to create a public-facing area (like a homepage with a login button) and a separate protected area for authenticated users.

Configuration

To use OIDC authentication in your own SQLPage application, you need to configure it in your sqlpage.json file:

{
  "oidc_issuer_url": "https://your-keycloak-server/auth/realms/your-realm",
  "oidc_client_id": "your-client-id",
  "oidc_client_secret": "your-client-secret",
  "host": "localhost:8080",
  "oidc_protected_paths": ["/protected"]
}

The configuration parameters are:

  • oidc_issuer_url: The base URL of your OIDC provider
  • oidc_client_id: The ID that identifies your SQLPage application to the OIDC provider
  • oidc_client_secret: The secret key for your SQLPage application
  • host: The web address where your application is accessible

Accessing User Information

Once OIDC is configured, you can access information about the authenticated user in your SQL files using these functions:

  • sqlpage.user_info(claim_name): Get a specific claim about the user (like name or email)
  • sqlpage.user_info_token(): Get the entire identity token as JSON

Example:

select 'text' as component, 'Welcome, ' || sqlpage.user_info('name') || '!' as contents_md;

Implementation Details

The demo includes several SQL files that demonstrate different aspects of OIDC integration:

  1. index.sql: A public page that shows a welcome message and a login button. If the user is logged in, it displays their email and a link to the protected page.

  2. protected.sql: A page that is only accessible to authenticated users. It displays the user's information.

  3. logout.sql: Logs the user out by removing the authentication cookie and redirecting to the OIDC provider's logout page.

Docker Setup

The demo uses Docker Compose to set up both SQLPage and Keycloak. The configuration includes:

  • SQLPage service with:

    • Volume mounts for the web root and configuration
    • CAS configuration for optional CAS support
    • Debug logging enabled
  • Keycloak service with:

    • Pre-configured realm and users
    • Health checks to ensure it's ready before SQLPage starts
    • Admin credentials for management

References