Skip to content

Configure OAuth2 SSO Login as an Administrator (Keycloak Example)

This document describes how to configure an OAuth2 SSO Provider in SwanLab, as well as the callback address and field mapping required on the IdP side.

1. Feature Introduction

OAuth2 SSO applies to identity services that only provide the OAuth2 Authorization Code flow. SwanLab redirects to the IdP authorization page, exchanges the authorization code for an Access Token, and then calls the Userinfo endpoint to read third-party user information.

Account policy:

  • First-time OAuth2 login must be bound to an existing SwanLab account.
  • Bound users log in directly after authentication succeeds.

2. Procedure

2.1 SwanLab Configuration

  1. In the SwanLab administrator control panel, click to create OAuth2.

  1. Fill in the basic configuration.
FieldRequiredDescription
NameYesUnique Provider identifier. Up to 25 characters. Only letters, digits, underscores, and hyphens are allowed
Display nameYesName shown on the login button. Up to 100 characters

  1. Fill in the OAuth2 configuration.
FieldRequiredDescription
Client IDYesClient ID configured on the IdP. It is usually manually entered when configuring the IdP
Client SecretYesOAuth2 Client Secret assigned to SwanLab by the IdP. It is usually generated by the IdP and must be obtained from the IdP
Authorization URLYesOAuth2 Authorization Endpoint. Different authentication services may use different values. Check the IdP documentation
Access Token URLYesToken Endpoint used to exchange the authorization code for an Access Token. Different authentication services may use different values. Check the IdP documentation
Userinfo URLYesUserinfo Endpoint used to retrieve user profile data with the Access Token. Different authentication services may use different values. Check the IdP documentation
ScopesNoSpace-separated authorization scopes. Defaults to openid profile when left empty, but manual configuration is recommended

"Authorization URL", "Access Token URL", and "Userinfo URL" are OAuth2 protocol fields, but different services do not have unified path requirements. For example, for "Authorization URL":

  • Keycloak: https://<IDP URL>/realms/<realm name>/protocol/openid-connect/auth
  • Authentik: https://<IDP URL>/application/o/authorize/

The following uses keycloak as an example:

  1. Fill in user mapping.
FieldRequiredDescription
User ID fieldYesField name corresponding to the third-party unique user ID
Username fieldYesField name corresponding to the third-party username
Recommended unique user ID fieldRecommended username field
id, uid, user_id, subusername, login, preferred_username

  1. Fill in the display configuration.

We recommend using an online link for the Icon URL.

  1. Enable the IdP.

After creation, you can view existing IdPs in the list. After enabling one, you can test and use it:

  1. View it in the login list.

3. IdP-side Configuration

The key item to configure in the IdP OAuth2 application is the Redirect URI:

text
https://<SwanLab external access address>/api/auth/sso/oauth2/callback/<Provider name>

Ensure that this address exactly matches the address generated by SwanLab, including protocol, domain, port, subpath, and Provider name.

Example:

text
https://swanlab.example.com/api/auth/sso/oauth2/callback/arui-oauth2

The following uses keycloak as an example:

The Client Secret is usually generated by the IdP. Copy it and fill it in the SwanLab configuration: