|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace LM\AuthAbstractor\Factory; |
|
6
|
|
|
|
|
7
|
|
|
use LM\AuthAbstractor\Configuration\IApplicationConfiguration; |
|
8
|
|
|
use LM\AuthAbstractor\Enum\AuthenticationProcess\Status; |
|
9
|
|
|
use LM\AuthAbstractor\Model\AuthenticationProcess; |
|
10
|
|
|
use LM\AuthAbstractor\Model\IAuthenticationProcess; |
|
11
|
|
|
use LM\AuthAbstractor\Model\IU2fRegistration; |
|
12
|
|
|
use LM\AuthAbstractor\Model\PersistOperation; |
|
13
|
|
|
use LM\Common\DataStructure\TypedMap; |
|
14
|
|
|
use LM\Common\Enum\Scalar; |
|
15
|
|
|
use LM\Common\Model\IntegerObject; |
|
16
|
|
|
use LM\Common\Model\ArrayObject; |
|
17
|
|
|
use LM\Common\Model\StringObject; |
|
18
|
|
|
use InvalidArgumentException; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* This is a factory class aiming at making new authentication processes |
|
22
|
|
|
* easier for the application. |
|
23
|
|
|
* |
|
24
|
|
|
* It takes care of choosing an implementation of IAuthenticationProcess, and |
|
25
|
|
|
* sets up its internals. |
|
26
|
|
|
* |
|
27
|
|
|
* @see \LM\AuthAbstractor\Model\IAuthenticationProcess |
|
28
|
|
|
* @see \LM\AuthAbstractor\Model\AuthenticationProcess |
|
29
|
|
|
*/ |
|
30
|
|
|
class AuthenticationProcessFactory |
|
31
|
|
|
{ |
|
32
|
|
|
/** @var IApplicationConfiguration */ |
|
33
|
|
|
private $appConfig; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @internal |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(IApplicationConfiguration $appConfig) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->appConfig = $appConfig; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* This method can be used to instantiate new authentication processes. |
|
45
|
|
|
* |
|
46
|
|
|
* @api |
|
47
|
|
|
* @param string[] $challenges An array of FQCNs of challenges |
|
48
|
|
|
* (implementations of IChallenge). |
|
49
|
|
|
* @param int $maxNFailedAttempts The maximum number of attempts users |
|
50
|
|
|
* can try in a row before the authentication process fails. |
|
51
|
|
|
* @param null|string $username The username of the user, null if the user |
|
52
|
|
|
* is not logged in yet. |
|
53
|
|
|
* @param mixed[] $additionalData Additional data that can is |
|
54
|
|
|
* application-specific and can be retrieved later by the callback. This |
|
55
|
|
|
* data shouldn't be used by any challenge. |
|
56
|
|
|
* @return AuthenticationProcess A new authentication process. |
|
57
|
|
|
* @see \LM\AuthAbstractor\Challenge\IChallenge |
|
58
|
|
|
* @todo Put $additionalData in a separate scope. |
|
59
|
|
|
* @todo Check $additionalData's format is correct. |
|
60
|
|
|
*/ |
|
61
|
|
|
public function createProcess( |
|
62
|
|
|
array $challenges, |
|
63
|
|
|
int $maxNFailedAttempts = 3, |
|
64
|
|
|
?string $username = null, |
|
65
|
|
|
array $additionalData = [] |
|
66
|
|
|
): IAuthenticationProcess { |
|
67
|
|
|
$dataArray = array_merge($additionalData, [ |
|
68
|
|
|
'used_u2f_key_public_keys' => new ArrayObject([], Scalar::_STR), |
|
|
|
|
|
|
69
|
|
|
'challenges' => new ArrayObject($challenges, Scalar::_STR), |
|
|
|
|
|
|
70
|
|
|
'max_n_failed_attempts' => new IntegerObject($maxNFailedAttempts), |
|
71
|
|
|
'n_failed_attempts' => new IntegerObject(0), |
|
72
|
|
|
'persist_operations' => new ArrayObject([], PersistOperation::class), |
|
|
|
|
|
|
73
|
|
|
'status' => new Status(Status::ONGOING), |
|
74
|
|
|
'new_u2f_registrations' => new ArrayObject([], IU2fRegistration::class), |
|
|
|
|
|
|
75
|
|
|
'n_u2f_registrations' => new IntegerObject(0), |
|
76
|
|
|
]); |
|
77
|
|
|
if (null !== $username) { |
|
78
|
|
|
$dataArray['username'] = new StringObject($username); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if ( |
|
82
|
|
|
!isset($additionalData['u2f_registrations']) && |
|
83
|
|
|
null !== $username |
|
84
|
|
|
) { |
|
85
|
|
|
$dataArray['u2f_registrations'] = $this->appConfig->getU2fRegistrations($username); |
|
86
|
|
|
} elseif ( |
|
87
|
|
|
!isset($additionalData['u2f_registrations']) && |
|
88
|
|
|
null === $username |
|
89
|
|
|
) { |
|
90
|
|
|
$dataArray['u2f_registrations'] = []; |
|
91
|
|
|
} elseif (isset($additionalData['u2f_registrations'])) { |
|
92
|
|
|
$dataArray['u2f_registrations'] = $additionalData['u2f_registrations']; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if (isset($additionalData['used_u2f_key_public_keys'])) { |
|
96
|
|
|
if (!is_array($additionalData['used_u2f_key_public_keys'])) { |
|
97
|
|
|
throw new InvalidArgumentException('used_u2f_key_public_keys'); |
|
98
|
|
|
} |
|
99
|
|
|
foreach ($additionalData['used_u2f_key_public_keys'] as $pb) { |
|
100
|
|
|
if (!is_string($pb)) { |
|
101
|
|
|
throw new InvalidArgumentException('Public key must be string'); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
$data['used_u2f_key_public_keys'] = $additionalData['used_u2f_key_public_keys']; |
|
|
|
|
|
|
105
|
|
|
} else { |
|
106
|
|
|
$data['used_u2f_key_public_keys'] = []; |
|
107
|
|
|
} |
|
108
|
|
|
$typedMap = new TypedMap($dataArray); |
|
109
|
|
|
|
|
110
|
|
|
return new AuthenticationProcess($typedMap); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|