|
| 1 | +# Generic OpenID Connect (OIDC) login |
| 2 | + |
| 3 | +WiseMapping can delegate sign-in to any OpenID Connect provider — Keycloak, Authentik, |
| 4 | +Authelia, Okta, Microsoft Entra ID, Zitadel, … — next to (or instead of) Google and Facebook. |
| 5 | +The provider is configured with standard Spring Security properties under the registration id |
| 6 | +**`oidc`**; nothing is enabled unless that registration exists. |
| 7 | + |
| 8 | +## How it works |
| 9 | + |
| 10 | +1. The login page shows a **"Sign in with {label}"** button when `/api/restful/app/config` |
| 11 | + returns `oidcOauth2Enabled: true`. The label comes from `app.oauth.oidc.label`. |
| 12 | +2. The button opens `{api-base-url}/oauth2/authorization/oidc`; Spring Security runs the |
| 13 | + authorization-code flow against the provider. |
| 14 | +3. On the callback (`{api-base-url}/login/oauth2/code/oidc`) the user is matched **by e-mail** |
| 15 | + (ignoring case; a login whose e-mail matches several accounts that differ only in case is refused): |
| 16 | + - **no account** → an account of type `OIDC` is created (no local password); |
| 17 | + - **an `OIDC` account** → signed in; |
| 18 | + - **a local (`DATABASE`) account** → the user is asked to confirm the link once; the account |
| 19 | + then becomes `OIDC` and its local password is cleared; |
| 20 | + - **an account bound to another provider** (Google, Facebook, LDAP) → refused. |
| 21 | + |
| 22 | +> **Security** — because accounts are matched by e-mail, the provider must only issue |
| 23 | +> **verified** e-mail addresses. If its users can sign up or change their e-mail without |
| 24 | +> verifying it, they can sign in as any WiseMapping user with that address, the admin included. |
| 25 | +
|
| 26 | +Provisioning on first login does **not** depend on `app.registration.enabled`: an instance that |
| 27 | +lets only its identity provider in can close the public sign-up form |
| 28 | +(`app.registration.enabled: false`) and still create accounts on first OIDC login. |
| 29 | + |
| 30 | +## Configuration |
| 31 | + |
| 32 | +Endpoints are discovered from the issuer (`<issuer-uri>/.well-known/openid-configuration`). |
| 33 | + |
| 34 | +```yaml |
| 35 | +spring: |
| 36 | + security: |
| 37 | + oauth2: |
| 38 | + client: |
| 39 | + registration: |
| 40 | + oidc: |
| 41 | + client-id: wisemapping |
| 42 | + client-secret: <client secret> |
| 43 | + authorization-grant-type: authorization_code # required for a custom registration id |
| 44 | + scope: openid,profile,email |
| 45 | + redirect-uri: "${app.site.api-base-url}/login/oauth2/code/{registrationId}" |
| 46 | + provider: |
| 47 | + oidc: |
| 48 | + issuer-uri: https://sso.example.com/realms/example |
| 49 | + user-name-attribute: sub |
| 50 | + |
| 51 | +app: |
| 52 | + oauth: |
| 53 | + oidc: |
| 54 | + label: Example SSO # button text: "Sign in with Example SSO" |
| 55 | +``` |
| 56 | +
|
| 57 | +The same settings as environment variables (Docker): |
| 58 | +
|
| 59 | +```sh |
| 60 | +SPRING_SECURITY_OAUTH2_CLIENT_REGISTRATION_OIDC_CLIENT_ID=wisemapping |
| 61 | +SPRING_SECURITY_OAUTH2_CLIENT_REGISTRATION_OIDC_CLIENT_SECRET=<client secret> |
| 62 | +SPRING_SECURITY_OAUTH2_CLIENT_REGISTRATION_OIDC_AUTHORIZATION_GRANT_TYPE=authorization_code |
| 63 | +SPRING_SECURITY_OAUTH2_CLIENT_REGISTRATION_OIDC_SCOPE=openid,profile,email |
| 64 | +SPRING_SECURITY_OAUTH2_CLIENT_REGISTRATION_OIDC_REDIRECT_URI={baseUrl}/login/oauth2/code/{registrationId} |
| 65 | +SPRING_SECURITY_OAUTH2_CLIENT_PROVIDER_OIDC_ISSUER_URI=https://sso.example.com/realms/example |
| 66 | +SPRING_SECURITY_OAUTH2_CLIENT_PROVIDER_OIDC_USER_NAME_ATTRIBUTE=sub |
| 67 | +APP_OAUTH_OIDC_LABEL="Example SSO" |
| 68 | +``` |
| 69 | + |
| 70 | +The provider must return the `email` claim (scope `email`). `given_name` and `family_name` |
| 71 | +fill the account's first and last name. |
| 72 | + |
| 73 | +### Keycloak |
| 74 | + |
| 75 | +- Client: *OpenID Connect*, *Client authentication* on, *Standard flow* only. |
| 76 | +- Valid redirect URI: `https://wm.example.com/login/oauth2/code/oidc`. |
| 77 | +- Issuer: `https://keycloak.example.com/realms/<realm>`. |
| 78 | + |
| 79 | +### Authentik |
| 80 | + |
| 81 | +- Provider: *OAuth2/OpenID Provider*, client type *Confidential*, scopes `openid`, `email`, |
| 82 | + `profile`. |
| 83 | +- Redirect URI (strict): `https://wm.example.com/login/oauth2/code/oidc`. |
| 84 | +- Issuer: `https://authentik.example.com/application/o/<application-slug>/` |
| 85 | + (keep the trailing slash: it is part of the issuer Authentik announces). |
| 86 | + |
| 87 | +## Checking the setup |
| 88 | + |
| 89 | +1. `curl -s {api-base-url}/api/restful/app/config` must return `"oidcOauth2Enabled":true` and |
| 90 | + the label in `oidcOauth2Label`. `false` means the `client-id` setting did not reach the API. |
| 91 | +2. The login page shows the **"Sign in with {label}"** button. |
| 92 | +3. Signing in with a user of the provider brings you back to WiseMapping signed in; the first |
| 93 | + login creates the account. |
| 94 | +4. If the login fails, the API log says why (see below). |
| 95 | + |
| 96 | +## Troubleshooting |
| 97 | + |
| 98 | +- **`authorizationGrantType cannot be null` at startup** — add |
| 99 | + `authorization-grant-type: authorization_code`; Spring only presets it for the providers it |
| 100 | + knows (Google, Facebook, GitHub, Okta). |
| 101 | +- **`redirect_uri` mismatch** — the URI registered in the provider must be exactly |
| 102 | + `{api-base-url}/login/oauth2/code/oidc`. Behind an HTTPS reverse proxy, a mismatch on the |
| 103 | + scheme (`http://` sent instead of `https://`) means the proxy does not forward |
| 104 | + `X-Forwarded-Proto`; see |
| 105 | + [SSL/HTTPS with Reverse Proxy](../../distribution/app/README.md#sslhttps-with-reverse-proxy). |
| 106 | +- **Login fails and the log says `the provider returned no email claim`** — request the `email` |
| 107 | + scope and make sure the provider releases the claim (Microsoft Entra ID, for instance, only |
| 108 | + sends it when the optional `email` claim is configured on the app registration). |
| 109 | +- **Issuer mismatch** — `issuer-uri` must equal the `issuer` field of the discovery document, |
| 110 | + character for character. |
0 commit comments