|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright (c) 2021 Sebastian Sterk <sebastian@wiuwiu.de> |
| 4 | + * This file is licensed under the Affero General Public License version 3 or |
| 5 | + * later. |
| 6 | + * See the COPYING-README file. |
| 7 | + */ |
| 8 | + |
| 9 | +namespace OCA\UserExternal; |
| 10 | + |
| 11 | +use OCP\Http\Client\IClientService; |
| 12 | + |
| 13 | +/** |
| 14 | + * User authentication against a generic HTTP auth interface |
| 15 | + * |
| 16 | + * @category Apps |
| 17 | + * @package UserExternal |
| 18 | + * @author Sebastian Sterk https://wiuwiu.de/Imprint |
| 19 | + * @license http://www.gnu.org/licenses/agpl AGPL |
| 20 | + */ |
| 21 | +class HTTP extends Base { |
| 22 | + private $hashAlgo; |
| 23 | + private $accessKey; |
| 24 | + private $authenticationEndpoint; |
| 25 | + private $httpClientService; |
| 26 | + |
| 27 | + /** |
| 28 | + * Create new HTTP authentication provider |
| 29 | + * |
| 30 | + * @param string $authenticationEndpoint The HTTP endpoint URL for authentication |
| 31 | + * @param string|false $hashAlgo Hash algorithm for password (false for plain text) |
| 32 | + * @param string $accessKey Access key for additional security |
| 33 | + */ |
| 34 | + public function __construct($authenticationEndpoint, $hashAlgo = false, $accessKey = '') { |
| 35 | + parent::__construct($authenticationEndpoint); |
| 36 | + $this->authenticationEndpoint = $authenticationEndpoint; |
| 37 | + $this->hashAlgo = $hashAlgo; |
| 38 | + $this->accessKey = $accessKey; |
| 39 | + $this->httpClientService = \OC::$server->get(IClientService::class); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Send user credentials to HTTP endpoint |
| 44 | + * |
| 45 | + * @param string $user The username |
| 46 | + * @param string $password The password |
| 47 | + * |
| 48 | + * @return bool True if authentication successful, false otherwise |
| 49 | + */ |
| 50 | + public function sendUserData($user, $password) { |
| 51 | + if ($this->hashAlgo !== false) { |
| 52 | + $password = $this->hashPassword($password); |
| 53 | + } |
| 54 | + |
| 55 | + try { |
| 56 | + $client = $this->httpClientService->newClient(); |
| 57 | + |
| 58 | + $response = $client->post($this->authenticationEndpoint, [ |
| 59 | + 'form_params' => [ |
| 60 | + 'accessKey' => $this->accessKey, |
| 61 | + 'user' => $user, |
| 62 | + 'password' => $password |
| 63 | + ], |
| 64 | + 'timeout' => 10, |
| 65 | + ]); |
| 66 | + |
| 67 | + $statusCode = $response->getStatusCode(); |
| 68 | + |
| 69 | + if ($statusCode === 202) { |
| 70 | + return true; |
| 71 | + } else { |
| 72 | + return false; |
| 73 | + } |
| 74 | + } catch (\Exception $e) { |
| 75 | + \OC::$server->getLogger()->error( |
| 76 | + 'ERROR: Could not connect to HTTP auth endpoint: ' . $e->getMessage(), |
| 77 | + ['app' => 'user_external'] |
| 78 | + ); |
| 79 | + return false; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Hash password using configured algorithm |
| 85 | + * |
| 86 | + * @param string $password The plain text password |
| 87 | + * |
| 88 | + * @return string The hashed password |
| 89 | + */ |
| 90 | + private function hashPassword($password) { |
| 91 | + return hash($this->hashAlgo, $password); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Check if the password is correct without logging in the user |
| 96 | + * |
| 97 | + * @param string $uid The username |
| 98 | + * @param string $password The password |
| 99 | + * |
| 100 | + * @return string|false The username on success, false on failure |
| 101 | + */ |
| 102 | + public function checkPassword($uid, $password) { |
| 103 | + if (isset($uid) && isset($password)) { |
| 104 | + $authenticationStatus = $this->sendUserData($uid, $password); |
| 105 | + if ($authenticationStatus) { |
| 106 | + $uid = mb_strtolower($uid); |
| 107 | + $this->storeUser($uid); |
| 108 | + return $uid; |
| 109 | + } else { |
| 110 | + return false; |
| 111 | + } |
| 112 | + } |
| 113 | + return false; |
| 114 | + } |
| 115 | +} |
0 commit comments