|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +declare(strict_types=1); |
| 4 | + |
3 | 5 | /** |
4 | 6 | * Copyright (c) 2015 Thomas Müller <thomas.mueller@tmit.eu> |
5 | 7 | * This file is licensed under the Affero General Public License version 3 or |
|
9 | 11 |
|
10 | 12 | namespace OCA\UserExternal; |
11 | 13 |
|
| 14 | +use OCP\IDBConnection; |
| 15 | +use OCP\IGroupManager; |
| 16 | +use OCP\IUserManager; |
| 17 | +use Psr\Log\LoggerInterface; |
| 18 | + |
12 | 19 | class WebDavAuth extends Base { |
13 | | - private $webDavAuthUrl; |
| 20 | + private string $webDavAuthUrl; |
| 21 | + private string $authType; |
14 | 22 |
|
15 | | - public function __construct($webDavAuthUrl) { |
16 | | - parent::__construct($webDavAuthUrl); |
| 23 | + public function __construct( |
| 24 | + string $webDavAuthUrl, |
| 25 | + string $authType = 'basic', |
| 26 | + ?IDBConnection $db = null, |
| 27 | + ?IUserManager $userManager = null, |
| 28 | + ?IGroupManager $groupManager = null, |
| 29 | + ?LoggerInterface $logger = null, |
| 30 | + ) { |
| 31 | + parent::__construct($webDavAuthUrl, $db, $userManager, $groupManager, $logger); |
17 | 32 | $this->webDavAuthUrl = $webDavAuthUrl; |
| 33 | + $this->authType = $authType; |
18 | 34 | } |
19 | 35 |
|
20 | 36 | /** |
21 | | - * Check if the password is correct without logging in the user |
| 37 | + * Check if the password is correct without logging in the user. |
22 | 38 | * |
23 | 39 | * @param string $uid The username |
24 | 40 | * @param string $password The password |
25 | | - * |
26 | | - * @return true/false |
| 41 | + * @return string|false The uid on success, false on failure |
27 | 42 | */ |
28 | | - public function checkPassword($uid, $password) { |
| 43 | + public function checkPassword($uid, $password): string|false { |
29 | 44 | $uid = $this->resolveUid($uid); |
30 | 45 |
|
31 | 46 | $arr = explode('://', $this->webDavAuthUrl, 2); |
32 | | - if (! isset($arr) or count($arr) !== 2) { |
33 | | - $this->logger->error('ERROR: Invalid WebdavUrl: "' . $this->webDavAuthUrl . '" ', ['app' => 'user_external']); |
| 47 | + if (count($arr) !== 2) { |
| 48 | + $this->logger->error('Invalid WebDAV URL: "' . $this->webDavAuthUrl . '"', ['app' => 'user_external']); |
34 | 49 | return false; |
35 | 50 | } |
36 | 51 | [$protocol, $path] = $arr; |
37 | | - $url = $protocol . '://' . urlencode($uid) . ':' . urlencode($password) . '@' . $path; |
38 | | - $headers = get_headers($url); |
39 | | - if ($headers === false) { |
40 | | - $this->logger->error('ERROR: Not possible to connect to WebDAV Url: "' . $protocol . '://' . $path . '" ', ['app' => 'user_external']); |
| 52 | + $url = $protocol . '://' . $path; |
| 53 | + |
| 54 | + switch ($this->authType) { |
| 55 | + case 'basic': |
| 56 | + $responseHeaders = $this->fetchWithBasicAuth($url, $uid, $password); |
| 57 | + break; |
| 58 | + case 'digest': |
| 59 | + $responseHeaders = $this->fetchWithDigestAuth($url, $uid, $password); |
| 60 | + break; |
| 61 | + default: |
| 62 | + $this->logger->error( |
| 63 | + 'Invalid WebDAV auth type: "' . $this->authType . '". Expected "basic" or "digest".', |
| 64 | + ['app' => 'user_external'], |
| 65 | + ); |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + if ($responseHeaders === null) { |
41 | 70 | return false; |
42 | 71 | } |
43 | | - $returnCode = substr($headers[0], 9, 3); |
44 | 72 |
|
45 | | - if (substr($returnCode, 0, 1) === '2') { |
| 73 | + $returnCode = substr($responseHeaders[0], 9, 3); |
| 74 | + if (str_starts_with($returnCode, '2')) { |
46 | 75 | $this->storeUser($uid); |
47 | 76 | return $uid; |
| 77 | + } |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Perform a GET request with HTTP Basic authentication. |
| 83 | + * |
| 84 | + * @return string[]|null Response headers, or null on connection failure. |
| 85 | + */ |
| 86 | + protected function fetchWithBasicAuth(string $url, string $uid, string $password): ?array { |
| 87 | + $context = stream_context_create(['http' => [ |
| 88 | + 'method' => 'GET', |
| 89 | + 'header' => 'Authorization: Basic ' . base64_encode($uid . ':' . $password), |
| 90 | + 'ignore_errors' => true, |
| 91 | + ]]); |
| 92 | + return $this->fetchUrl($url, $context); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Perform a two-step GET request with HTTP Digest authentication. |
| 97 | + * |
| 98 | + * @return string[]|null Response headers, or null on connection failure or missing challenge. |
| 99 | + */ |
| 100 | + protected function fetchWithDigestAuth(string $url, string $uid, string $password): ?array { |
| 101 | + // Step 1: unauthenticated request to receive the server challenge |
| 102 | + $challengeHeaders = $this->fetchUrl($url); |
| 103 | + if ($challengeHeaders === null) { |
| 104 | + $this->logger->error('Not possible to connect to WebDAV URL: "' . $url . '"', ['app' => 'user_external']); |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + // Step 2: find the WWW-Authenticate: Digest header |
| 109 | + $authHeaderValue = null; |
| 110 | + foreach ($challengeHeaders as $header) { |
| 111 | + if (stripos($header, 'WWW-Authenticate: Digest ') === 0) { |
| 112 | + $authHeaderValue = substr($header, strlen('WWW-Authenticate: Digest ')); |
| 113 | + break; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + if ($authHeaderValue === null) { |
| 118 | + $this->logger->error('No Digest challenge received from WebDAV URL: "' . $url . '"', ['app' => 'user_external']); |
| 119 | + return null; |
| 120 | + } |
| 121 | + |
| 122 | + // Step 3: parse the challenge parameters |
| 123 | + $params = []; |
| 124 | + preg_match_all('/(\w+)="([^"]*)"/', $authHeaderValue, $matches, PREG_SET_ORDER); |
| 125 | + foreach ($matches as $m) { |
| 126 | + $params[$m[1]] = $m[2]; |
| 127 | + } |
| 128 | + |
| 129 | + if (!isset($params['realm'], $params['nonce'])) { |
| 130 | + $this->logger->error('Invalid Digest challenge from WebDAV URL: "' . $url . '"', ['app' => 'user_external']); |
| 131 | + return null; |
| 132 | + } |
| 133 | + |
| 134 | + // Step 4: compute the digest response |
| 135 | + $cnonce = bin2hex(random_bytes(8)); |
| 136 | + $nc = '00000001'; |
| 137 | + $A1 = md5($uid . ':' . $params['realm'] . ':' . $password); |
| 138 | + $A2 = md5('GET:' . $url); |
| 139 | + $response = md5($A1 . ':' . $params['nonce'] . ':' . $nc . ':' . $cnonce . ':auth:' . $A2); |
| 140 | + |
| 141 | + $digestHeader = sprintf( |
| 142 | + 'Authorization: Digest username="%s", realm="%s", nonce="%s", uri="%s", cnonce="%s", nc=%s, qop=auth, response="%s"', |
| 143 | + $uid, $params['realm'], $params['nonce'], $url, $cnonce, $nc, $response, |
| 144 | + ); |
| 145 | + if (isset($params['opaque'])) { |
| 146 | + $digestHeader .= sprintf(', opaque="%s"', $params['opaque']); |
| 147 | + } |
| 148 | + |
| 149 | + // Step 5: send the authenticated request |
| 150 | + $context = stream_context_create(['http' => [ |
| 151 | + 'method' => 'GET', |
| 152 | + 'header' => $digestHeader, |
| 153 | + 'ignore_errors' => true, |
| 154 | + ]]); |
| 155 | + return $this->fetchUrl($url, $context); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Perform a GET request and return the response headers. |
| 160 | + * Extracted so tests can stub network calls without hitting the wire. |
| 161 | + * |
| 162 | + * @return string[]|null Response headers, or null if the server is unreachable. |
| 163 | + */ |
| 164 | + protected function fetchUrl(string $url, mixed $context = null): ?array { |
| 165 | + if ($context !== null) { |
| 166 | + @file_get_contents($url, false, $context); |
48 | 167 | } else { |
49 | | - return false; |
| 168 | + @file_get_contents($url); |
50 | 169 | } |
| 170 | + return $http_response_header ?? null; |
51 | 171 | } |
52 | 172 | } |
0 commit comments