Keycloak SSO for Nextcloud
Sign in to Nextcloud with Keycloak through the official OpenID Connect user backend app (user_oidc), with group provisioning and logout from both.
Nextcloud signs users in with Keycloak through its official OpenID Connect user backend app, user_oidc. The Nextcloud server exchanges the authorization code with Keycloak, so the client is confidential and has a secret. The app creates users on first sign-in and can create and update their groups from a claim.
Checked against user_oidc 8.13.0 (for Nextcloud 29 to 36), the Nextcloud 35 admin manual and Keycloak 26.8 on 25 November 2026.
Replace auth.example.com with your RealmDeploy hostname and acme with your realm. The examples use https://cloud.example.com for Nextcloud.
In Keycloak
Create the client
First find Nextcloud's redirect URI: install the app (below), then open Administration settings → OpenID Connect. The page shows Redirect URI (to be authorized in the provider client configuration). It is https://cloud.example.com/apps/user_oidc/code when Nextcloud has pretty URLs, and https://cloud.example.com/index.php/apps/user_oidc/code when it does not. Use the one the page shows.
Open Clients, choose Create client, and use these values:
| Setting | Value |
|---|---|
| Client type | OpenID Connect |
| Client ID | nextcloud |
| Client authentication | On |
| Authentication flow | Standard flow only |
| PKCE method | S256 |
| Root URL | https://cloud.example.com |
| Home URL | https://cloud.example.com |
| Valid redirect URIs | The redirect URI from Nextcloud, for example https://cloud.example.com/apps/user_oidc/code |
| Valid post logout redirect URIs | https://cloud.example.com/ |
| Web origins | Leave empty. Nextcloud talks to Keycloak from its server, not from the browser. |
Choose Save, then copy the secret from the Credentials tab.
user_oidc uses PKCE by default when the discovery document lists S256, which Keycloak's does.
Add a groups claim
user_oidc reads the user's details from the ID token, so the groups have to be there.
- On the
nextcloudclient, open the Client scopes tab and choosenextcloud-dedicated. - Choose Configure a new mapper and pick Group Membership.
- Fill it in and choose Save:
| Field | Value |
|---|---|
| Name | groups |
| Token Claim Name | groups |
| Full group path | Off |
| Add to ID token | On |
| Add to access token | Off |
| Add to userinfo | On |
In Nextcloud
Install OpenID Connect user backend from Apps, or from the command line:
sudo -u www-data php /var/www/nextcloud/occ app:install user_oidcThen register Keycloak as a provider. With occ:
sudo -u www-data php /var/www/nextcloud/occ user_oidc:provider keycloak \
--clientid="nextcloud" \
--clientsecret="CLIENT_SECRET_FROM_KEYCLOAK" \
--discoveryuri="https://auth.example.com/realms/acme/.well-known/openid-configuration" \
--scope="openid email profile" \
--mapping-display-name="name" \
--mapping-email="email" \
--mapping-groups="groups" \
--group-provisioning=1 \
--postlogouturi="https://cloud.example.com/"keycloak is the provider's identifier inside Nextcloud. Run the same command again with other options to change the provider, and occ user_oidc:provider keycloak alone to show its settings. To keep the secret out of your shell history, use --clientsecret-file or --clientsecret-env instead of --clientsecret.
The same settings are in the web form under Administration settings → OpenID Connect → Register new provider:
occ option | Field in the form | Value |
|---|---|---|
| identifier argument | Identifier | keycloak |
--clientid | Client ID | nextcloud |
--clientsecret | Client secret | From the Credentials tab |
--discoveryuri | Discovery endpoint | https://auth.example.com/realms/acme/.well-known/openid-configuration |
--scope | Scope | openid email profile (the default) |
--mapping-uid | User ID mapping | Leave empty for sub (the default) |
--mapping-display-name | Display name mapping | name |
--mapping-email | Email mapping | email |
--mapping-groups | Groups mapping | groups |
--group-provisioning=1 | Use group provisioning | On |
--postlogouturi | Post logout URI | https://cloud.example.com/ |
name, email and groups are also the app's defaults, so those three mappings only make the choice visible.
Logout. Single logout is on by default. When a user logs out of Nextcloud, the app sends the browser to Keycloak's end-session endpoint with client_id and post_logout_redirect_uri, and Keycloak sends the user back to the Post logout URI (Nextcloud's base URL when it is empty). That URL must be listed in Valid post logout redirect URIs. Without id_token_hint, Keycloak asks the user to confirm the logout. To skip the question, add --send-id-token-hint=1 (Send ID token hint on logout); this puts the ID token in the logout URL in the browser.
Prefer this browser logout. user_oidc also supports back-channel logout, where Keycloak calls Nextcloud's /apps/user_oidc/backchannel-logout/keycloak endpoint from your tenant. Outgoing connections from your tenant are closed by default, so tell us Nextcloud's host and port through How to reach us before you set a Backchannel logout URL on the client.
Groups and roles
With group provisioning on, user_oidc creates the groups named in the groups claim if they do not exist yet, and adds and removes the user's memberships at every sign-in. The claim can be an array of names, which is what the Group Membership mapper sends:
{
"preferred_username": "jane",
"groups": ["nextcloud-users", "finance"]
}Narrow it down with these provider options:
occ option | Field in the form | Effect |
|---|---|---|
--group-whitelist-regex='/^nextcloud-/' | Group whitelist regex | Only matching groups are created, updated and deleted from the claim. Others are left alone. |
--group-restrict-login-to-whitelist=1 | Restrict login for users that are not in any whitelisted group | Users in none of the matching groups cannot sign in. |
--group-forbid-login-without-group=1 | Forbid login to users without at least one group | Users with an empty groups claim cannot sign in. |
Nextcloud's own admin group gives full admin rights. A Keycloak group called admin would put its members in it, so use a whitelist regex that leaves admin out.
Test it
- Open
https://cloud.example.com/loginin a private window and choose the Keycloak button. - Sign in as a test user. Nextcloud creates the user and their groups.
- As an admin, open Accounts and check the user's groups.
- To see the claims Keycloak sends, open the
nextcloudclient in Keycloak, go to Client scopes → Evaluate, pick the user and look at Generated ID token. Paste a token into the JWT decoder to read it. - Log out of Nextcloud. You pass through Keycloak's logout and land back on Nextcloud.
To send everyone to Keycloak without the Nextcloud login form, run occ config:app:set --type=string --value=0 user_oidc allow_multiple_user_backends. This works when there is one provider. Admins can still use the local form at /login?direct=1.
Common errors
| Error | Cause | Fix |
|---|---|---|
Invalid parameter: redirect_uri on the Keycloak page | The redirect URI differs from Valid redirect URIs, often with or without /index.php, or http:// behind a proxy. | Copy the exact URI from Administration settings → OpenID Connect. Compare both with the redirect URI matcher. |
invalid_client after login | Wrong client secret, or Client authentication is off. | Copy the secret again and update the provider. Look up other codes in the OAuth error lookup. |
| Groups are not created | Use group provisioning is off, the claim is not in the ID token, or Full group path is on. | Turn group provisioning on, turn Add to ID token on and Full group path off. |
| User IDs look like long hashes | Use unique user ID is on (the default): the user ID is a hash of the provider and the sub claim. | See the FAQ below before you change it. |
Invalid redirect uri on logout | The Post logout URI, or Nextcloud's base URL, is not in Valid post logout redirect URIs. | Add https://cloud.example.com/ to the client. Build and check the URL with the logout URL builder. |
| Login fails after a key rotation in Keycloak | The app caches the realm's JWKS for an hour. | Run occ user_oidc:provider keycloak --clientid nextcloud to clear the cache. |
FAQ
Can user IDs be readable names instead of hashes?
Yes. Set --mapping-uid=preferred_username and turn Use unique user ID off with --unique-uid=0. The app then uses the claim as the user ID. Its warning applies: another provider or user backend could produce the same ID and sign in as that user, so do this only when Keycloak is your only source of users. Decide before users sign in for the first time, because changing it later creates new accounts.
Do I need allow_local_remote_servers?
No. That setting, and the app's httpclient.allowlocal, are for an identity provider on a private or local address. Your RealmDeploy hostname is public.
Can Nextcloud apps and clients use Keycloak tokens?
user_oidc can accept a Keycloak token as a bearer token for Nextcloud's API and WebDAV with --check-bearer=1. The token's audience must include the nextcloud client, so add an Audience mapper for it on the calling client.
Sources
user_oidcREADME (configuration, occ commands, logout, provisioning, group provisioning): https://github.com/nextcloud/user_oidcuser_oidc8.13.0 release: https://github.com/nextcloud/user_oidc/releases/tag/v8.13.0user_oidcocc options: https://github.com/nextcloud/user_oidc/blob/main/lib/Command/UpsertProvider.phpuser_oidcadmin settings (redirect URI and form labels): https://github.com/nextcloud/user_oidc/blob/main/src/components/AdminSettings.vue- Nextcloud admin manual, OpenID Connect integration: https://docs.nextcloud.com/server/latest/admin_manual/configuration_user/user_auth_oidc.html
- Keycloak Server Administration Guide, client settings, protocol mappers and RP-initiated logout: https://www.keycloak.org/docs/latest/server_admin/
Keycloak SSO for SonarQube
Sign in to SonarQube Server or Community Build with Keycloak over SAML, with login, name, email and group attributes for just-in-time group sync.
Keycloak SSO for Portainer
Sign in to Portainer with Keycloak through its Custom OAuth provider, with user provisioning and, in Business Edition, teams from a groups claim.