I've searched for a tutorial on how to use Keycloak as a user Backend for Nextcloud for weeks, but wasn't able to find one fitting my needs.
I wanted a solution that fulfills the following criteria:
This setup reduces the amount of passwords and 2FA needed for different services. It also reduces the amount of logins, as you only need to login to one account.
I used the following software:
I assume you already have set up Nextcloud and an instance of Keycloak with a realm and users with the following URLs:
example.comConfigure > Clients > Create
Client ID: nextcloudAccess Typ: confidentialValid Redirect URIs: https://cloud.example.com/apps/oidc_login/oidc or https://cloud.example.com/*CredentialsCreate groups as you like, eg. admin, and add your users to them. The groups will be mapped 1:1 to those in Nextcloud.
Note that groups won't be created automatically in Nextcloud (Open PR).
Add a new attribute to the groups: Manage > Groups > GROUP_NAME > Attributes > Add
Key: nextcloudquotaValue: 549755813888 This is the size in bytes, here 512GiBCreate a new Mapper: Configure > Clients > nextcloud > Mappers > Create
Name: Nextcloud QuotaMapper Type: User AttributeUser Attribute: nextcloudquotaToken Claim Name: nextcloudquotaClaim JSON Type: StringCreate another Mapper: Configure > Clients > nextcloud > Mappers > Create
Name: Groups MapperMapper Type: Group MembershipToken Claim Name: groupsFull group path: OFFEdit the config.php file (there are no UI settings) with the according settings:
<?php
$CONFIG = array (
// STANDARD NEXTCLOUD CONFIG
// Some Nextcloud options that might make sense here
'allow_user_to_change_display_name' => false,
'lost_password_link' => 'disabled',
// OIDC SPECIFIC CONFIG
// URL of provider. All other URLs are auto-discovered from .well-known
'oidc_login_provider_url' => 'https://auth.example.com/auth/realms/example.com',
// Client ID and secret registered with the provider
'oidc_login_client_id' => 'nextcloud',
// the secret you copied from Keycloak
'oidc_login_client_secret' => '$SECRET',
// Automatically redirect the login page to the provider
'oidc_login_auto_redirect' => true,
// Redirect to this page after logging out the user
'oidc_login_logout_url' => 'https://auth.example.com/auth/realms/example.com/protocol/openid-connect/logout?redirect_uri=https%3A%2F%2Fcloud.example.com%2F',
// Quota to assign if no quota is specified in the OIDC response (bytes)
// 15GB
'oidc_login_default_quota' => '549755813888',
// Hide the NextCloud password change form.
'oidc_login_hide_password_form' => true,
// Attribute map for OIDC response
'oidc_login_attributes' => array (
'id' => 'preferred_username',
'name' => 'name',
'mail' => 'email',
'quota' => 'nextcloudquota',
'groups' => 'groups',
),
// Set OpenID Connect scope
'oidc_login_scope' => 'openid profile',
// Disable creation of new users from OIDC login
// needed since Keycloak is our only backend
'oidc_login_disable_registration' => false,
// Fallback to direct login if login from OIDC fails
// Note that no error message will be displayed if enabled
'oidc_login_redir_fallback' => false,
);
By default only OTP (as in the 6 digit codes) is enabled in Keycloak. We'll also enable WebAuthn, ie. hardware security tokens. We'll configure Keycloak to automatically choose the correct 2FA method.
Authentication > Required ActionI have two options for a WebAuthn Browser Flow:
Configure > Authentication > Flows > Select Browser and make a copy, call it WebAuthn for example
Edit the new copy as indented:
FormsWebauthn Forms
Username Password Form (REQUIRED)Conditional 2FA (CONDITIONAL)
Condition - User Configured (REQUIRED)WebAuthn Authenticator (ALTERNATIVE)Condition - User Configured (REQUIRED)OTP Form (ALTERNATIVE)Configure > Authentication > Flows > Select Browser and make a copy, call it WebAuthn Passwordless for example
Edit the new copy as indented (the names of the Flows are arbitrary):
FormsWebauthn Forms
Username Form (REQUIRED)Passwordless_or_2FA (REQUIRED)
WebAuthn Passwordless Authenticator (ALTERNATIVE)Password_and_2FA (ALTERNATIVE)
Password Form (REQUIRED)2FA (CONDITIONAL)
Condition - User Configured (REQUIRED)WebAuthn Authenticator (ALTERNATIVE)Condition - User Configured (REQUIRED)OTP Form (ALTERNATIVE)I want to explore the possibilities of Roles and integration with synapse.