RealmDeployDocs
GuidesSingle sign-on for apps

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:

SettingValue
Client typeOpenID Connect
Client IDnextcloud
Client authenticationOn
Authentication flowStandard flow only
PKCE methodS256
Root URLhttps://cloud.example.com
Home URLhttps://cloud.example.com
Valid redirect URIsThe redirect URI from Nextcloud, for example https://cloud.example.com/apps/user_oidc/code
Valid post logout redirect URIshttps://cloud.example.com/
Web originsLeave 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.

  1. On the nextcloud client, open the Client scopes tab and choose nextcloud-dedicated.
  2. Choose Configure a new mapper and pick Group Membership.
  3. Fill it in and choose Save:
FieldValue
Namegroups
Token Claim Namegroups
Full group pathOff
Add to ID tokenOn
Add to access tokenOff
Add to userinfoOn

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_oidc

Then 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 optionField in the formValue
identifier argumentIdentifierkeycloak
--clientidClient IDnextcloud
--clientsecretClient secretFrom the Credentials tab
--discoveryuriDiscovery endpointhttps://auth.example.com/realms/acme/.well-known/openid-configuration
--scopeScopeopenid email profile (the default)
--mapping-uidUser ID mappingLeave empty for sub (the default)
--mapping-display-nameDisplay name mappingname
--mapping-emailEmail mappingemail
--mapping-groupsGroups mappinggroups
--group-provisioning=1Use group provisioningOn
--postlogouturiPost logout URIhttps://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 optionField in the formEffect
--group-whitelist-regex='/^nextcloud-/'Group whitelist regexOnly matching groups are created, updated and deleted from the claim. Others are left alone.
--group-restrict-login-to-whitelist=1Restrict login for users that are not in any whitelisted groupUsers in none of the matching groups cannot sign in.
--group-forbid-login-without-group=1Forbid login to users without at least one groupUsers 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

  1. Open https://cloud.example.com/login in a private window and choose the Keycloak button.
  2. Sign in as a test user. Nextcloud creates the user and their groups.
  3. As an admin, open Accounts and check the user's groups.
  4. To see the claims Keycloak sends, open the nextcloud client 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.
  5. 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

ErrorCauseFix
Invalid parameter: redirect_uri on the Keycloak pageThe 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 loginWrong 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 createdUse 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 hashesUse 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 logoutThe 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 KeycloakThe 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

On this page