|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright (c) 2019 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 | +/** |
| 10 | + * User authentication against a generic HTTP auth interface |
| 11 | + * |
| 12 | + * @category Apps |
| 13 | + * @package UserExternal |
| 14 | + * @author Sebastian Sterk https://wiuwiu.de/Imprint |
| 15 | + * @license http://www.gnu.org/licenses/agpl AGPL |
| 16 | + */ |
| 17 | +class OC_User_HTTP extends \OCA\user_external\Base { |
| 18 | + private $hashAlgo; |
| 19 | + private $accessKey; |
| 20 | + private $authenticationEndpoint; |
| 21 | + |
| 22 | + public function __construct($authenticationEndpoint, $hashAlgo, $accessKey) { |
| 23 | + parent::__construct($authenticationEndpoint); |
| 24 | + $this->authenticationEndpoint = $authenticationEndpoint; |
| 25 | + $this->hashAlgo = $hashAlgo; |
| 26 | + $this->accessKey = $accessKey; |
| 27 | + } |
| 28 | + |
| 29 | + public function sendUserData($user, $password){ |
| 30 | + if($this->hashAlgo !== false){ |
| 31 | + $password = $this->hashPassword($password); |
| 32 | + } |
| 33 | + $ch = curl_init(); |
| 34 | + curl_setopt($ch, CURLOPT_URL, $this->authenticationEndpoint); |
| 35 | + curl_setopt($ch, CURLOPT_HEADER, true); |
| 36 | + curl_setopt($ch, CURLOPT_NOBODY, true); |
| 37 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 38 | + curl_setopt($ch, CURLOPT_POST, 1); |
| 39 | + curl_setopt($ch, CURLOPT_POSTFIELDS, |
| 40 | + http_build_query(array( |
| 41 | + 'accessKey' => $this->accessKey, |
| 42 | + 'user' => $user, |
| 43 | + 'password' => $password |
| 44 | + ) |
| 45 | + ) |
| 46 | + ); |
| 47 | + $response = curl_exec($ch); |
| 48 | + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 49 | + curl_close($ch); |
| 50 | + if($httpCode == 202){ |
| 51 | + return true; |
| 52 | + } else{ |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + public function hashPassword($password){ |
| 59 | + return hash($this->hashAlgo, $password); |
| 60 | + } |
| 61 | + |
| 62 | + public function checkPassword($uid, $password){ |
| 63 | + if(isset($uid) |
| 64 | + && isset($password)) { |
| 65 | + |
| 66 | + $authenticationStatus = $this->sendUserData($uid, $password); |
| 67 | + if ($authenticationStatus) { |
| 68 | + $uid = mb_strtolower($uid); |
| 69 | + $this->storeUser($uid); |
| 70 | + return $uid; |
| 71 | + } else { |
| 72 | + return false; |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments