Skip to main content

Nextcloud Google Login (Keycloak SSO)

This guide connects Nextcloud to Keycloak with the user_oidc app, so users sign in with their Google account. Nextcloud never talks to Google directly: it talks to Keycloak (OIDC), and Keycloak handles Google as an identity provider.

Nextcloud -> Keycloak (realm: homelab) -> Google

Prerequisites​

Complete these guides first. Each one is done once for the whole realm, not per application.

GuideWhat it gives you
Install KeycloakA running Keycloak at https://auth.rizwan.my.id.
Realm, Groups and UsersThe homelab realm, groups, and local users with a local password and the same email as their Google account.
Google Identity ProviderThe google identity provider in the realm.
Account LinkingThe first-broker-login-homelab flow, so a Google login links to the local user.
Groups Client ScopeThe groups client scope and mapper.

You also need Nextcloud installed and reachable at https://cloud.rizwan.my.id. This command must print installed: true:

docker exec -u www-data nextcloud-app php occ status | grep installed

Create the Keycloak client​

Create the client as described in Create an OIDC Client, using these values:

FieldValue
Client IDnextcloud
Client authenticationOn
Standard flowOn
Direct access grantsOff
Root URLhttps://cloud.rizwan.my.id
Home URLhttps://cloud.rizwan.my.id
Valid redirect URIshttps://cloud.rizwan.my.id/apps/user_oidc/code
Valid post logout redirect URIshttps://cloud.rizwan.my.id/*
Web originshttps://cloud.rizwan.my.id

Then copy the Client secret from the Credentials tab, and assign the groups scope to the client as Default.

warning

The redirect URI for user_oidc is /apps/user_oidc/code. The path /apps/oidc_login/oidc/* belongs to a different plugin (oidc_login) and causes Invalid parameter: redirect_uri.

Install the user_oidc app​

Download the latest release from the GitHub releases page:

https://github.com/nextcloud-releases/user_oidc/releases

Pick the newest user_oidc-*.tar.gz, copy it to the Nextcloud server, then extract it into custom_apps and enable it:

sudo mkdir -p /mnt/nextcloud/html/custom_apps
sudo tar -xzf user_oidc-*.tar.gz -C /mnt/nextcloud/html/custom_apps/
sudo chown -R 33:33 /mnt/nextcloud/html/custom_apps/user_oidc
docker exec -u www-data nextcloud-app php occ app:enable user_oidc

Expected output:

user_oidc 8.11.0 enabled

Check that the app is active:

docker exec -u www-data nextcloud-app php occ app:list | grep user_oidc
tip

docker exec -u www-data nextcloud-app php occ app:install user_oidc also works when the app store is reachable. If it fails with not found on the appstore, the app store may be rate limiting your IP (HTTP 429). Use the manual install above instead of retrying.

Allow connections to local servers​

Check that the Nextcloud container can reach Keycloak:

docker exec nextcloud-app curl -sS -o /dev/null -w "code=%{http_code}\n" \
https://auth.rizwan.my.id/realms/homelab/.well-known/openid-configuration

You should get code=200. If the host does not resolve, set the DNS server in /etc/docker/daemon.json (or dns: in the compose file) and restart Docker.

If Keycloak resolves to a private IP inside the LAN (split DNS), Nextcloud blocks the request by default and shows Could not reach the OpenID Connect provider, even though curl works. Allow local addresses:

docker exec -u www-data nextcloud-app php occ config:system:set allow_local_remote_servers --value=true --type=boolean

Register the provider​

The provider identifier is the text shown on the login button, and it cannot be renamed later. Use Google so the button reads "Login with Google".

docker exec -u www-data nextcloud-app php occ user_oidc:provider Google \
--clientid="nextcloud" \
--clientsecret="SECRET" \
--discoveryuri="https://auth.rizwan.my.id/realms/homelab/.well-known/openid-configuration" \
--scope="openid email profile groups" \
--unique-uid=0 \
--mapping-uid=preferred_username \
--mapping-display-name=name \
--mapping-email=email \
--group-provisioning=1 \
--mapping-groups=groups

What the options do:

OptionPurpose
--unique-uid=0Use the readable username as the Nextcloud user ID instead of a long hash.
--mapping-uid=preferred_usernameTake the user ID from the Keycloak username.
--mapping-display-name=nameDisplay name comes from the name claim.
--mapping-email=emailEmail comes from the email claim.
--group-provisioning=1Create and sync groups from the token.
--mapping-groups=groupsRead groups from the groups claim.

The command prints nothing on success. Verify it:

docker exec -u www-data nextcloud-app php occ user_oidc:provider

SSO Provider

warning

The client secret ends up in your shell history. Remove the entry with history -d <number>, or regenerate the secret in Keycloak and update it with:

docker exec -u www-data nextcloud-app php occ user_oidc:provider Google --clientsecret="NEW_SECRET"

To remove a provider you no longer need:

docker exec -u www-data nextcloud-app php occ user_oidc:provider:delete Google

Test the login​

Demo: signing in to Nextcloud with Google through Keycloak.

  1. Open https://cloud.rizwan.my.id in a private window.
  2. Click Login with Google.
  3. On the Keycloak page, choose Google and sign in.
  4. On the first login, Keycloak asks for the local password of the user to link the Google account (see Account Linking). Later logins skip this.
  5. You are redirected back to Nextcloud and the user is created automatically.

Then check the user and the groups that came from the token:

docker exec -u www-data nextcloud-app php occ user:list
docker exec -u www-data nextcloud-app php occ group:list

You should see the user ID (for example rizwan.fairuz) and groups such as admins.

Make a user admin​

Groups from Keycloak (for example admins) do not match the built-in Nextcloud group admin. After the user's first login, add them manually:

docker exec -u www-data nextcloud-app php occ group:adduser admin rizwan.fairuz
tip

Keep the local admin account as a break-glass login, and do not disable password login until SSO is stable. If you get locked out of the SSO redirect, use https://cloud.rizwan.my.id/login?direct=1.

Skip the Keycloak page​

To jump straight to Google, open Keycloak Authentication → Flows → browser, click the gear on Identity Provider Redirector, and set Default Identity Provider to google.

warning

This applies to every login in the realm, for every application. The Keycloak username and password form is no longer shown, which also hides the local backup login. Only enable it when Google login is stable.

Troubleshooting​

SymptomLikely cause and fix
Nextcloud is not installed - only a limited number of commands are availableThe first-start install has not finished. Wait, then check occ status and docker logs nextcloud-app.
Could not download app user_oidc, it was not found on the appstoreApp store rate limit (429) or no route. Install manually from the GitHub release.
There are no commands defined in the "user_oidc" namespaceThe app is not enabled yet. Run occ app:enable user_oidc.
Could not reach the OpenID Connect providerThe container cannot resolve or reach Keycloak, or local servers are blocked. Fix DNS and set allow_local_remote_servers to true.
Invalid parameter: redirect_uriThe redirect URI in Keycloak must be exactly https://cloud.rizwan.my.id/apps/user_oidc/code.
invalid_client or unauthorizedWrong client secret. Update it with --clientsecret.
Invalid username or password on first Google loginThe user has no local password in Keycloak, or the wrong one was entered. Set it under Users → Credentials. See Account Linking.
No groups in NextcloudThe groups scope is not assigned to the client, or --scope does not include groups. Check the evaluated token as in Groups Client Scope.
Untrusted domainCheck NEXTCLOUD_TRUSTED_DOMAINS and the headers sent by the reverse proxy.
A second user such as rizwan.fairuz_1 appearsThe user ID mapping changed after the first login. Keep --unique-uid=0 and --mapping-uid=preferred_username consistent.

For details, read the Nextcloud log:

docker exec nextcloud-app tail -n 30 /var/www/html/data/nextcloud.log