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