nextcloud / nextcloud/server

Client Certificate Authentication Support for both Nextcloud and Collabora etc

Open
#52,198 3 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

0. Needs triage enhancement feature: authentication
Dominant language
PHP
Stars
36.9k
Forks
5.2k
Avg merge
2d 3h
Merged PRs (30d)
713

Description

** Nextcloud does not have internal support for client certificates being used for http authentication **

This is a problem when accessing the /settings/admin/overview page as it cannot connect to my server (403 error)
This is also a problem when integrating with 3rd party apps such as Collabora on my site (also protected with client certificates

** Proposed solution **

I have edited the Client.php file: /app/www/public/lib/private/Http/Client/Client.php to add support. The diff below will allow users to enable this in the main nextcloud config.php file and also specify a path to appropriate certificate, key, and if required, password for that key (all in pem format). It would be great if you could take a look at the diff and merge this into the code for nextcloud for an upcoming release as I've seen that others also desire this feature. Many thanks.

** Usage **

  1. change the /app/www/public/lib/private/Http/Client/Client.php file as shown below
  2. One of a or b
    2a. copy the client ssl certificate to $SERVERROOT/config/client_ssl/cert.pem and the key to $SERVERROOT/config/client_ssl/key.pem
    2b. edit config.php (for me: /config/www/nextcloud/config/config.php) to set the locaton for these files. e.g.
    'internal_client_authentication_cert' => '/config/client_ssl/nextcloud_server_cert.pem',
    'internal_client_authentication_key' => '/config/client_ssl/nextcloud_server_key.pem',
    'internal_client_authentication_key_pass' => 'my password',
  3. edit config.php (for me: /config/www/nextcloud/config/config.php) to enable client authentication support
    'client_authentication_enabled' => true,
+++ Client.php
@@ -5,6 +5,8 @@
  * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  * SPDX-License-Identifier: AGPL-3.0-only
+ *
+ * /app/www/public/lib/private/Http/Client/Client.php
  */
 namespace OC\Http\Client;

@@ -80,6 +82,14 @@

                $options = array_merge($defaults, $options);

+               if ($this->isClientAuthenticationEnabled($options)) {
+                    $client_auth_options = [
+                        RequestOptions::CERT => $this->getClientAuthenticationCert($options),
+                        RequestOptions::SSL_KEY => $this->getClientAuthenticationKey($options),
+                    ];
+                    $options = array_merge($client_auth_options, $options);
+                }
+
                if (!isset($options[RequestOptions::HEADERS]['User-Agent'])) {
                        $options[RequestOptions::HEADERS]['User-Agent'] = 'Nextcloud Server Crawler';
                }
@@ -107,6 +117,37 @@

                return $this->certificateManager->getAbsoluteBundlePath();
        }
+
+        private function isClientAuthenticationEnabled(array $options): bool {
+                if (($options['nextcloud']['client_authentication_enabled'] ?? false) ||
+                        $this->config->getSystemValueBool('client_authentication_enabled', false)) {
+                        return true;
+                }
+
+                return false;
+        }
+
+        private function getClientAuthenticationCert(array $options): ?string {
+                $clientCert = $this->config->getSystemValueString('internal_client_authentication_cert', \OC::$SERVERROOT . '/config/client_ssl/cert.pem');
+                if ($clientCert === '') {
+                        return null;
+                }
+                return $clientCert;
+        }
+
+/* PHP8+        private function getClientAuthenticationKey(array $options): ?array|?string {*/
+        private function getClientAuthenticationKey(array $options) {
+                $clientKey = $this->config->getSystemValueString('internal_client_authentication_key', \OC::$SERVERROOT . '/config/client_ssl/key.pem');
+                $clientKeyPass = $this->config->getSystemValueString('internal_client_authentication_key_pass', '<not specified>');
+                if ($clientKey === '') {
+                        return null;
+                }
+                if ($clientKeyPass === '<not specified>') {
+                        return $clientKey;
+                } else {
+                        return array($clientKey, $clientKeyPass);
+                }
+        }

        /**
         * Returns a null or an associative array specifying the proxy URI for```

**Here is the full file for your convenience:**

[Client.php.txt](https://github.com/user-attachments/files/19754845/Client.php.txt)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with lib/private/Http/Client/Client.php and the attached Client.php.txt, then trace the existing request options used by the admin overview and Collabora integrations. No test file is named, so locate the relevant HTTP-client tests and define done as configurable certificate, key, and optional passphrase support that preserves existing requests.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.