Single Sign-On (SSO)
OIDC
OAuth2
SSO
Single Sign-On (SSO) allows users to authenticate with multiple applications using a single set of credentials. This plugin supports OpenID Connect (OIDC) and OAuth2 providers.
SAML support is coming soon. Upvote the feature request on our GitHub
Installation
Add Plugin to the server
Migrate the database
Run the migration or generate the schema to add the necessary fields and tables to the database.
See the Schema section to add the fields manually.
Add the client plugin
Usage
Register an OIDC Provider
To register an OIDC provider, use the createOIDCProvider
endpoint and provide the necessary configuration details for the provider.
A redirect URL will be automatically generated using the provider ID. For instance, if the provider ID is hydra
, the redirect URL would be {baseURL}/api/auth/sso/callback/hydra
. Note that /api/auth
may vary depending on your base path configuration.
Example
Full method
Prop | Description | Type |
---|---|---|
providerId | The ID of the provider. This is used to identify the provider during login and callback. | string |
issuer | The issuer url of the provider. | string |
domain | The domain of the provider. This is used for email matching. | string |
clientId | The client ID. | string |
clientSecret | The client secret. | string |
authorizationEndpoint? | The authorization endpoint. | string |
tokenEndpoint? | The token endpoint. | string |
userInfoEndpoint? | The user info endpoint. | string |
tokenEndpointAuthentication? | The authentication method for the token endpoint. Defaults to 'client_secret_post'. | "client_secret_post" | "client_secret_basic" |
jwksEndpoint? | The JWKS endpoint. | string |
discoveryEndpoint? | string | |
scopes? | The scopes to request. Defaults to ['openid', 'email', 'profile', 'offline_access']. | string[] |
pkce? | Whether to use PKCE for the authorization flow. | boolean |
mapping? | Object | |
mapping.id? | The field in the user info response that contains the id. Defaults to 'sub'. | string |
mapping.email? | The field in the user info response that contains the email. Defaults to 'email'. | string |
mapping.emailVerified? | The field in the user info response that contains whether the email is verified. defaults to 'email_verified'. | string |
mapping.name? | The field in the user info response that contains the name. Defaults to 'name'. | string |
mapping.image? | The field in the user info response that contains the image. Defaults to 'picture'. | string |
mapping.extraFields? | Record<string, string> | |
organizationId? | If organization plugin is enabled, the organization id to link the provider to. | string |
overrideUserInfo? | Override user info with the provider info. Defaults to false | boolean |
Sign In with SSO
To sign in with an SSO provider, you can call signIn.sso
You can sign in using the email with domain matching:
or you can specify the domain:
You can also sign in using the organization slug if a provider is associated with an organization:
Alternatively, you can sign in using the provider's ID:
To use the server API you can use signInSSO
Full method
Prop | Description | Type |
---|---|---|
email? | The email address to sign in with. This is used to identify the issuer to sign in with. It's optional if the issuer is provided. | string |
organizationSlug? | The slug of the organization to sign in with. | string |
providerId? | The ID of the provider to sign in with. This can be provided instead of email or issuer. | string |
domain? | The domain of the provider. | string |
callbackURL | The URL to redirect to after login. | string |
errorCallbackURL? | The URL to redirect to after login. | string |
newUserCallbackURL? | The URL to redirect to after login if the user is new. | string |
scopes? | Scopes to request from the provider. | string[] |
requestSignUp? | Explicitly request sign-up. Useful when disableImplicitSignUp is true for this provider. | boolean |
When a user is authenticated, if the user does not exist, the user will be provisioned using the provisionUser
function. If the organization provisioning is enabled and a provider is associated with an organization, the user will be added to the organization.
Schema
The plugin requires additional fields in the ssoProvider
table to store the provider's configuration.
Field Name | Type | Key | Description |
---|---|---|---|
id | string | A database identifier | |
issuer | string | - | The issuer identifier |
domain | string | - | The domain of the provider |
oidcConfig | string | - | The OIDC configuration |
userId | string | - | The user ID |
providerId | string | - | The provider ID. Used to identify a provider and to generate a redirect URL. |
organizationId | string | - | The organization Id. If provider is linked to an organization. |
Options
Server
provisionUser: A custom function to provision a user when they sign in with an SSO provider.
organizationProvisioning: Options for provisioning users to an organization.
Prop | Type | Default |
---|---|---|
provisionUser? | function | - |
organizationProvisioning? | object | - |