|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* BEdita, API-first content management framework |
|
4
|
|
|
* Copyright 2022 ChannelWeb Srl, Chialab Srl |
|
5
|
|
|
* |
|
6
|
|
|
* This file is part of BEdita: you can redistribute it and/or modify |
|
7
|
|
|
* it under the terms of the GNU Lesser General Public License as published |
|
8
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
|
9
|
|
|
* (at your option) any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. |
|
12
|
|
|
*/ |
|
13
|
|
|
namespace App\Identifier; |
|
14
|
|
|
|
|
15
|
|
|
use App\Identifier\Resolver\ApiResolver; |
|
16
|
|
|
use Authentication\Identifier\AbstractIdentifier; |
|
17
|
|
|
use Authentication\Identifier\Resolver\ResolverAwareTrait; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Identifies authentication credentials through an API. |
|
21
|
|
|
*/ |
|
22
|
|
|
class ApiIdentifier extends AbstractIdentifier |
|
23
|
|
|
{ |
|
24
|
|
|
use ResolverAwareTrait; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Default configuration. |
|
28
|
|
|
* - `timezoneField` The field where timezone is stored in the identity. Default to 'timezone'. |
|
29
|
|
|
* - `resolver` The resolver implementation to use. |
|
30
|
|
|
* |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $_defaultConfig = [ |
|
34
|
|
|
'timezoneField' => 'timezone', |
|
35
|
|
|
'resolver' => ApiResolver::class, |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @inheritDoc |
|
40
|
|
|
*/ |
|
41
|
|
|
public function identify(array $credentials) |
|
42
|
|
|
{ |
|
43
|
|
|
if ( |
|
44
|
|
|
empty($credentials[self::CREDENTIAL_USERNAME]) && |
|
45
|
|
|
empty($credentials[self::CREDENTIAL_PASSWORD]) && |
|
46
|
|
|
empty($credentials[self::CREDENTIAL_TOKEN]) |
|
47
|
|
|
) { |
|
48
|
|
|
return null; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $this->findIdentity($credentials); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Retrieve user record using provided credentials. |
|
56
|
|
|
* |
|
57
|
|
|
* @param array $credentials The user credentials |
|
58
|
|
|
* @return array|\ArrayAccess|null |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function findIdentity(array $credentials) |
|
61
|
|
|
{ |
|
62
|
|
|
$identity = $this->getResolver()->find($credentials); |
|
63
|
|
|
if (empty($identity)) { |
|
64
|
|
|
return null; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// Store user timezone in identity |
|
68
|
|
|
$timezoneField = $this->getConfig('timezoneField', 'timezone'); |
|
69
|
|
|
if (!empty($credentials[$timezoneField])) { |
|
70
|
|
|
$identity[$timezoneField] = $this->userTimezone($credentials[$timezoneField]); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
// Flatten username and renew token in identity |
|
74
|
|
|
$identity[self::CREDENTIAL_USERNAME] = $identity['attributes']['username']; |
|
75
|
|
|
$identity[self::CREDENTIAL_TOKEN] = $identity['tokens']['renew']; |
|
76
|
|
|
|
|
77
|
|
|
return $identity; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Get user's timezone from offset. |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $offset Must contain UTC offset in seconds, plus Daylight Saving Time DST 0 or 1 like: '3600 1' or '7200 0'. Defaults to UTC. |
|
84
|
|
|
* @return string The timezone name. |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function userTimezone(string $offset = '0'): string |
|
87
|
|
|
{ |
|
88
|
|
|
$data = explode(' ', $offset); |
|
89
|
|
|
$dst = empty($data[1]) ? 0 : 1; |
|
90
|
|
|
|
|
91
|
|
|
return timezone_name_from_abbr('', intval($data[0]), $dst); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|