Integrate with Nextcloud
Support level: Community
What is Nextcloud
Nextcloud is a suite of client-server software for creating and using file hosting services. Nextcloud is free and open-source, which means that anyone is allowed to install and operate it on their own private server devices.
If you require Server Side Encryption, you must use LDAP. OpenID and SAML will cause irrevocable data loss. Nextcloud Server-Side Encryption requires access to the user's cleartext password, which Nextcloud only has access to when using LDAP as the user enters their password directly into Nextcloud.
This setup only works when Nextcloud is running with HTTPS enabled. See here on how to configure this.
In case something goes wrong with the configuration, you can use the URL http://nextcloud.company/login?direct=1
to log in using the built-in authentication.
This documentation lists only the settings that you need to change from their default values. Be aware that any changes other than those explicitly mentioned in this guide could cause issues accessing your application.
Configuration methods
It is possible to configure Nextcloud to use either OpenID Connect or SAML for authentication. Below are the steps to configure both methods.
- OpenID Connect
- SAML
Preparation
The following placeholders are used in this guide:
nextcloud.company
is the FQDN of the Nextcloud installation.authentik.company
is the FQDN of the authentik installation.
Let's start by considering which user attributes need to be available in Nextcloud:
- name
- unique user ID
- storage quota (optional)
- groups (optional)
authentik already provides some default scopes with claims, such as:
email
scope: includesemail
andemail_verified
profile
scope: includesname
,given_name
,preferred_username
,nickname
,groups
openid
scope: a default required by the OpenID spec (contains no claims)
Custom Profile Scope
If you do not need storage quota, group information, or to manage already existing users in Nextcloud, skip to the next step.
If you want to control user storage and designate Nextcloud administrators, create a custom profile
scope. Go to Customization > Property mappings and create a Scope mapping with:
-
Name: Nextcloud Profile
-
Scope name: profile
-
Expression:
# Extract all groups the user is a member of
groups = [group.name for group in user.ak_groups.all()]
# Nextcloud admins must be members of a group called "admin".
# This is static and cannot be changed.
# Append "admin" to the user's groups if they are an admin in authentik.
if user.is_superuser and "admin" not in groups:
groups.append("admin")
return {
"name": request.user.name,
"groups": groups,
# Set a quota by using the "nextcloud_quota" property in the user's attributes
"quota": user.group_attributes().get("nextcloud_quota", None),
# To connect an existing Nextcloud user, set "nextcloud_user_id" to the Nextcloud username.
"user_id": user.attributes.get("nextcloud_user_id", str(user.uuid)),
}
To set a quota, define the nextcloud_quota
attribute for individual users or groups. For example, setting it to 1 GB
will restrict the user to 1GB of storage. If not set, storage is unlimited.
To connect to an existing Nextcloud user, set the nextcloud_user_id
attribute to match the Nextcloud username (found under the user's Display name in Nextcloud).
Provider and Application
-
Create a provider:
In the authentik Admin Interface, navigate to Applications > Providers. Create an OAuth2/OpenID Provider with the following settings:- Name: Nextcloud
- Client type: Confidential
- Redirect URIs/Origins (RegEx):
https://nextcloud.company/apps/user_oidc/code
- Signing key: Any valid certificate
-
Configure advanced settings:
Under advanced settings, set:-
Scopes:
authentik default Oauth Mapping email
Nextcloud Profile
(orauthentik default Oauth Mapping profile
if you skipped the custom profile scope)
-
Subject mode: Based on the User's UUID
dangerMapping the subject mode to authentik usernames is not recommended due to their mutable nature. If you choose to map to usernames, disable username changing in authentik and set it to
Based on the User's username
. -
Include claims in ID token: Enabled
Note: Save your
client ID
andsecret ID
for later. -
An issue with the Nextcloud OIDC app limited the secret ID size to 64 characters. This has been fixed as of December 2023—ensure you update the OpenID Connect user backend to the latest version.
Depending on your Nextcloud configuration, you might need to use https://nextcloud.company/index.php/
instead of https://nextcloud.company/
.
- Link the provider to an application:
In Applications > Applications, create an application and select the provider you just created. Note the application slug for later use.
Nextcloud configuration
-
Install the app:
In Nextcloud, ensure the OpenID Connect user backend app is installed. Then navigate to Settings > OpenID Connect. -
Add a provider:
Click the + button and enter the following:-
Identifier: Authentik
-
Client ID: (from the provider)
-
Client secret: (from the provider)
-
Discovery endpoint:
https://authentik.company/application/o/<nextcloud-app-slug>/.well-known/openid-configuration
-
Scope:
email profile
(omitopenid
if preferred) -
Attribute mappings:
-
User ID mapping:
sub
(oruser_id
for existing users) -
Display name mapping:
name
-
Email mapping:
email
-
Quota mapping:
quota
(leave blank if the custom profile scope was skipped) -
Groups mapping:
groups
(leave blank if the custom profile scope was skipped)tipEnable Use group provisioning to allow writing to this field.
-
-
Use unique user ID:
If deselected, Nextcloud uses the mapped user ID in the Federated Cloud ID.tipTo avoid a hashed Federated Cloud ID, deselect Use unique user ID and use
user_id
for the User ID mapping.
-
-
Log in:
Once configured, single sign-on (SSO) login via authentik becomes available.
Making OIDC the default login method
Automatically redirect users to authentik when they access Nextcloud by running:
sudo -u www-data php var/www/nextcloud/occ config:app:set --value=0 user_oidc allow_multiple_user_backends
SAML Auth
Preparation
The following placeholders are used in this guide:
nextcloud.company
is the FQDN of the Nextcloud installation.authentik.company
is the FQDN of the authentik installation.
This documentation lists only the settings you need to change from their default values. Other changes might cause issues accessing your application.
-
Create an application in authentik:
Note the chosen slug as it will be used later. -
Create a SAML provider:
In authentik, navigate to Applications > Providers and create a SAML provider with the following settings:- ACS URL:
https://nextcloud.company/apps/user_saml/saml/acs
- Issuer:
https://authentik.company
- Service Provider Binding:
Post - Audience:
https://nextcloud.company/apps/user_saml/saml/metadata
- Signing certificate: Select any valid certificate.
- Property mappings: Select all managed mappings.
- ACS URL:
Depending on your Nextcloud configuration, you might need to use https://nextcloud.company/index.php/
instead of https://nextcloud.company/
.
Nextcloud configuration
-
Install the app:
In Nextcloud, ensure the SSO & SAML Authentication app is installed. Then navigate to Settings > SSO & SAML Authentication. -
Configure the following settings:
-
Attribute to map the UID to:
http://schemas.goauthentik.io/2021/02/saml/uid
dangerUsing the UID attribute as username is not recommended because of its mutable nature. If you map to the username instead, disable username changing and set the UID attribute to
http://schemas.goauthentik.io/2021/02/saml/username
. -
Optional display name:
authentik
-
Identifier of the IdP entity:
https://authentik.company
-
URL target for authentication requests:
https://authentik.company/application/saml/<application-slug>/sso/binding/redirect/
-
URL for SLO requests:
https://authentik.company/application/saml/<application-slug>/slo/binding/redirect/
-
Public X.509 certificate of the IdP:
Paste the PEM from your selected certificate.
-
-
Set attribute mapping:
Configure the following mappings:- Display name:
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name
- Email:
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress
- User groups:
http://schemas.xmlsoap.org/claims/Group
- Display name:
If Nextcloud is behind a reverse proxy, force HTTPS by adding 'overwriteprotocol' => 'https'
to the Nextcloud config/config.php
file. See this guide for more details.
Group quotas
-
Set up groups:
Create a group for each storage quota level and assign a custom attribute (e.g.,nextcloud_quota
) with values like15 GB
. -
Create a custom SAML property mapping:
Name the mapping SAML Nextcloud Quota with:-
SAML Attribute Name:
nextcloud_quota
-
Expression:
return user.group_attributes().get("nextcloud_quota", "1 GB")
(Here,
"1 GB"
is the default if no quota is set.)
-
-
Configure Nextcloud:
In Nextcloud under Settings > SSO & SAML Authentication, set the Attribute to map the quota to asnextcloud_quota
.
Admin group
To grant admin access to authentik users:
-
Create a custom SAML property mapping for admins:
Configure a mapping with:-
SAML Attribute Name:
http://schemas.xmlsoap.org/claims/Group
-
Expression:
for group in request.user.all_groups():
yield group.name
if ak_is_group_member(request.user, name="<authentik nextcloud admin group's name>"):
yield "admin"
-
-
Update the Nextcloud provider:
Replace the default Groups mapping with this custom mapping.