1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Caridea |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
7
|
|
|
* use this file except in compliance with the License. You may obtain a copy of |
8
|
|
|
* the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
15
|
|
|
* License for the specific language governing permissions and limitations under |
16
|
|
|
* the License. |
17
|
|
|
* |
18
|
|
|
* @copyright 2015-2018 LibreWorks contributors |
19
|
|
|
* @license Apache-2.0 |
20
|
|
|
*/ |
21
|
|
|
namespace Caridea\Auth\Adapter; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Abstract authentication adapter. |
25
|
|
|
* |
26
|
|
|
* @copyright 2015-2018 LibreWorks contributors |
27
|
|
|
* @license Apache-2.0 |
28
|
|
|
*/ |
29
|
|
|
abstract class AbstractAdapter implements \Caridea\Auth\Adapter |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Checks that a string argument isn't null, empty, or just whitespace. |
33
|
|
|
* |
34
|
|
|
* @param string $object The value to check for blankness |
35
|
|
|
* @param string $fieldName The name of the parameter (for Exception message) |
36
|
|
|
* @return mixed Returns `$object` |
37
|
|
|
* @throws \InvalidArgumentException if the value is null, empty, or whitespace |
38
|
|
|
*/ |
39
|
15 |
|
protected function checkBlank($object, string $fieldName) |
40
|
|
|
{ |
41
|
15 |
|
if ($object === null || strlen(trim($object)) === 0) { |
42
|
1 |
|
throw new \InvalidArgumentException("The \"$fieldName\" argument is required; it cannot be null, empty, nor containing only whitespace"); |
43
|
|
|
} |
44
|
14 |
|
return $object; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Throws a `MissingCredentials` if the value is empty. |
49
|
|
|
* |
50
|
|
|
* @param array $source The params array |
51
|
|
|
* @param string $key The array offset |
52
|
|
|
* @return mixed Returns the value of `$source[$key]` |
53
|
|
|
* @throws \Caridea\Auth\Exception\MissingCredentials If `$source[$key]` is empty |
54
|
|
|
*/ |
55
|
14 |
|
protected function ensure(array &$source, string $key) |
56
|
|
|
{ |
57
|
14 |
|
if (!isset($source[$key]) || !$source[$key]) { |
58
|
1 |
|
throw new \Caridea\Auth\Exception\MissingCredentials(); |
59
|
|
|
} |
60
|
13 |
|
return $source[$key]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Verifies a user-provided password against a hash. |
65
|
|
|
* |
66
|
|
|
* @param string $input The user-provided password |
67
|
|
|
* @param string $hash The stored password hash |
68
|
|
|
* @throws \Caridea\Auth\Exception\MissingCredentials If the user-provided password is empty |
69
|
|
|
* @throws \Caridea\Auth\Exception\InvalidPassword If the password fails to verify |
70
|
|
|
*/ |
71
|
4 |
|
protected function verify(string $input, string $hash) |
72
|
|
|
{ |
73
|
4 |
|
if (!password_verify($input, $hash)) { |
74
|
2 |
|
throw new \Caridea\Auth\Exception\InvalidPassword(); |
75
|
|
|
} |
76
|
2 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Gets a default set of details for web requests (includes User-Agent and IP). |
80
|
|
|
* |
81
|
|
|
* ```php |
82
|
|
|
* $details = $this->details($request, ['foo' => 'bar']) |
83
|
|
|
* // $details = [ |
84
|
|
|
* // 'ua' => 'My User Agent/1.0', |
85
|
|
|
* // 'ip' => '127.0.0.1', |
86
|
|
|
* // 'foo' => 'bar' |
87
|
|
|
* // ] |
88
|
|
|
* |
89
|
|
|
* ``` |
90
|
|
|
* |
91
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request The server request |
92
|
|
|
* @param array $details Any details to add |
93
|
|
|
* @return array The details |
94
|
|
|
*/ |
95
|
4 |
|
protected function details(\Psr\Http\Message\ServerRequestInterface $request, array $details): array |
96
|
|
|
{ |
97
|
4 |
|
$server = $request->getServerParams(); |
98
|
4 |
|
return array_merge([ |
99
|
4 |
|
'ua' => $server['HTTP_USER_AGENT'], |
100
|
4 |
|
'ip' => $server['REMOTE_ADDR'] |
101
|
4 |
|
], $details); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|