1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2014 SURFnet bv |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
|
|
|
|
20
|
|
|
|
21
|
|
|
namespace Surfnet\StepupSelfService\SelfServiceBundle\Service\SelfAssertedTokens; |
22
|
|
|
|
23
|
|
|
use Psr\Log\LoggerInterface; |
24
|
|
|
use Surfnet\StepupMiddlewareClient\Identity\Dto\RecoveryToken; |
25
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\Identity; |
26
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\VerifiedSecondFactor; |
27
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Identity\Service\RecoveryTokenService as MiddlewareRecoveryTokenService; |
28
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Command\SafeStoreAuthenticationCommand; |
29
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Service\SelfAssertedTokens\Dto\ReturnTo; |
30
|
|
|
|
31
|
|
|
readonly class RecoveryTokenService |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
public function __construct( |
|
|
|
|
34
|
|
|
private MiddlewareRecoveryTokenService $recoveryTokenService, |
35
|
|
|
private SafeStoreService $safeStoreService, |
36
|
|
|
private RecoveryTokenState $stateStore, |
37
|
|
|
private RecoveryTokenConfig $config, |
38
|
|
|
private LoggerInterface $logger |
39
|
|
|
) { |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function hasRecoveryToken(Identity $identity): bool |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
return $this->recoveryTokenService->hasRecoveryToken($identity); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getRecoveryToken(string $recoveryTokenId): RecoveryToken |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
return $this->recoveryTokenService->findOne($recoveryTokenId); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
|
|
|
|
53
|
|
|
* @return RecoveryToken[] |
54
|
|
|
*/ |
55
|
|
|
public function getRecoveryTokensForIdentity(Identity $identity): array |
56
|
|
|
{ |
57
|
|
|
return $this->recoveryTokenService->findAllFor($identity); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getRemainingTokenTypes(Identity $identity): array |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
$tokens = $this->getRecoveryTokensForIdentity($identity); |
63
|
|
|
$tokenTypes = $this->excludeDisabledRecoveryTokens( |
64
|
|
|
$this->recoveryTokenService->getAvailableRecoveryTokenTypes() |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
/** @var RecoveryToken $token */ |
|
|
|
|
68
|
|
|
foreach ($tokens as $token) { |
69
|
|
|
if (in_array($token->type, $tokenTypes)) { |
70
|
|
|
unset($tokenTypes[$token->type]); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
return $tokenTypes; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function delete(RecoveryToken $recoveryToken): void |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$this->recoveryTokenService->delete($recoveryToken); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
|
|
|
|
82
|
|
|
* Verify the password hash with the secret specified on the command. |
83
|
|
|
*/ |
|
|
|
|
84
|
|
|
public function authenticateSafeStore(SafeStoreAuthenticationCommand $command): bool |
85
|
|
|
{ |
86
|
|
|
return $this->safeStoreService->authenticate($command->secret, $command->recoveryToken->identifier); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getAvailableTokens(Identity $identity, VerifiedSecondFactor $secondFactor): array |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
$tokens = $this->getRecoveryTokensForIdentity($identity); |
92
|
|
|
if ($secondFactor->type === 'sms' && array_key_exists('sms', $tokens)) { |
93
|
|
|
// Check if the phone number of the recovery token is the same as that of the second factor token |
94
|
|
|
$smsRecoveryToken = $tokens['sms']; |
95
|
|
|
if ($smsRecoveryToken->identifier === $secondFactor->secondFactorIdentifier) { |
96
|
|
|
$this->logger->info( |
97
|
|
|
sprintf( |
98
|
|
|
'Filtering the SMS recovery token from the available recovery tokens: [%s]. As the phone ' . |
99
|
|
|
' numbers are the same for both second factor and recovery tokens.', |
100
|
|
|
implode(', ', array_keys($tokens)) |
101
|
|
|
) |
102
|
|
|
); |
103
|
|
|
unset($tokens['sms']); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
if ($tokens === []) { |
107
|
|
|
$this->logger->info('No recovery tokens are available for second factor registration'); |
108
|
|
|
} |
109
|
|
|
return $tokens; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function startStepUpRequest(string $requestId): void |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
$this->stateStore->startStepUpRequest($requestId); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function hasStepUpRequest(): bool |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
return $this->stateStore->hasStepUpRequest(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function getStepUpRequest(): string |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
return $this->stateStore->getStepUpRequest(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function deleteStepUpRequest(): void |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
$this->stateStore->deleteStepUpRequest(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function wasStepUpGiven(): bool |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
return $this->stateStore->getStepUpGiven(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function stepUpGiven(): void |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$this->stateStore->setStepUpGiven(true); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function setReturnTo(string $route, array $parameters = []): void |
|
|
|
|
143
|
|
|
{ |
144
|
|
|
$this->stateStore->setReturnTo($route, $parameters); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function returnTo(): ReturnTo |
|
|
|
|
148
|
|
|
{ |
149
|
|
|
return $this->stateStore->returnTo(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function resetReturnTo() :void |
|
|
|
|
153
|
|
|
{ |
154
|
|
|
$this->stateStore->resetReturnTo(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function resetStepUpGiven(): void |
|
|
|
|
158
|
|
|
{ |
159
|
|
|
$this->stateStore->resetStepUpGiven(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
private function excludeDisabledRecoveryTokens(array $availableRecoveryTokenTypes): array |
|
|
|
|
163
|
|
|
{ |
164
|
|
|
foreach ($availableRecoveryTokenTypes as $identifier => $token) { |
165
|
|
|
if ($token === 'sms' && $this->config->isSmsDisabled()) { |
166
|
|
|
unset($availableRecoveryTokenTypes[$identifier]); |
167
|
|
|
} |
168
|
|
|
if ($token === 'safe-store' && $this->config->isSafeStoreCodeDisabled()) { |
169
|
|
|
unset($availableRecoveryTokenTypes[$identifier]); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
return $availableRecoveryTokenTypes; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|