|
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; |
|
22
|
|
|
|
|
23
|
|
|
use Surfnet\StepupBundle\Command\SendSmsChallengeCommand as StepupSendSmsChallengeCommand; |
|
24
|
|
|
use Surfnet\StepupBundle\Command\VerifyPossessionOfPhoneCommand; |
|
25
|
|
|
use Surfnet\StepupBundle\Service\Exception\TooManyChallengesRequestedException; |
|
26
|
|
|
use Surfnet\StepupBundle\Service\SmsSecondFactorService as StepupSmsSecondFactorService; |
|
27
|
|
|
use Surfnet\StepupBundle\Value\PhoneNumber\InternationalPhoneNumber; |
|
28
|
|
|
use Surfnet\StepupBundle\Value\PhoneNumber\PhoneNumber; |
|
29
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Identity\Command\ProvePhonePossessionCommand; |
|
30
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Uuid\Uuid; |
|
31
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Command\SendSmsChallengeCommand; |
|
32
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Command\VerifySmsChallengeCommand; |
|
33
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Service\SmsSecondFactor\ProofOfPossessionResult; |
|
34
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
|
|
|
|
|
37
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) - Quite some commands and VOs are used here. |
|
38
|
|
|
*/ |
|
|
|
|
|
|
39
|
|
|
class SmsSecondFactorService implements SmsSecondFactorServiceInterface |
|
40
|
|
|
{ |
|
41
|
|
|
public function __construct( |
|
|
|
|
|
|
42
|
|
|
private readonly StepupSmsSecondFactorService $smsSecondFactorService, |
|
43
|
|
|
private readonly TranslatorInterface $translator, |
|
44
|
|
|
private readonly CommandService $commandService, |
|
45
|
|
|
) { |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function getOtpRequestsRemainingCount(string $identifier): int |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
|
|
return $this->smsSecondFactorService->getOtpRequestsRemainingCount($identifier); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function getMaximumOtpRequestsCount(): int |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
return $this->smsSecondFactorService->getMaximumOtpRequestsCount(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function hasSmsVerificationState(string $secondFactorId): bool |
|
|
|
|
|
|
59
|
|
|
{ |
|
60
|
|
|
return $this->smsSecondFactorService->hasSmsVerificationState($secondFactorId); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function clearSmsVerificationState(string $secondFactorId): void |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
|
|
$this->smsSecondFactorService->clearSmsVerificationState($secondFactorId); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
|
|
|
|
|
69
|
|
|
* @throws TooManyChallengesRequestedException |
|
70
|
|
|
*/ |
|
|
|
|
|
|
71
|
|
|
public function sendChallenge(SendSmsChallengeCommand $command): bool |
|
72
|
|
|
{ |
|
73
|
|
|
$phoneNumber = new InternationalPhoneNumber( |
|
74
|
|
|
$command->country->getCountryCode(), |
|
75
|
|
|
new PhoneNumber($command->subscriber) |
|
76
|
|
|
); |
|
77
|
|
|
|
|
78
|
|
|
$stepupCommand = new StepupSendSmsChallengeCommand(); |
|
79
|
|
|
$stepupCommand->phoneNumber = $phoneNumber; |
|
80
|
|
|
$stepupCommand->body = $this->translator->trans('ss.registration.sms.challenge_body'); |
|
81
|
|
|
$stepupCommand->identity = $command->identity; |
|
82
|
|
|
$stepupCommand->institution = $command->institution; |
|
83
|
|
|
$stepupCommand->secondFactorId = $command->secondFactorId; |
|
84
|
|
|
|
|
85
|
|
|
return $this->smsSecondFactorService->sendChallenge($stepupCommand); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function provePossession(VerifySmsChallengeCommand $challengeCommand): ProofOfPossessionResult |
|
|
|
|
|
|
89
|
|
|
{ |
|
90
|
|
|
$stepupCommand = new VerifyPossessionOfPhoneCommand(); |
|
91
|
|
|
$stepupCommand->challenge = $challengeCommand->challenge; |
|
92
|
|
|
$stepupCommand->secondFactorId = $challengeCommand->secondFactorId; |
|
93
|
|
|
|
|
94
|
|
|
$verification = $this->smsSecondFactorService->verifyPossession($stepupCommand); |
|
95
|
|
|
|
|
96
|
|
|
if ($verification->didOtpExpire()) { |
|
97
|
|
|
return ProofOfPossessionResult::challengeExpired(); |
|
98
|
|
|
} elseif ($verification->wasAttemptedTooManyTimes()) { |
|
99
|
|
|
return ProofOfPossessionResult::tooManyAttempts(); |
|
100
|
|
|
} elseif (!$verification->wasSuccessful()) { |
|
101
|
|
|
return ProofOfPossessionResult::incorrectChallenge(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$command = new ProvePhonePossessionCommand(); |
|
105
|
|
|
$command->identityId = $challengeCommand->identity; |
|
106
|
|
|
$command->secondFactorId = Uuid::generate(); |
|
107
|
|
|
$command->phoneNumber = $verification->getPhoneNumber(); |
|
108
|
|
|
|
|
109
|
|
|
$result = $this->commandService->execute($command); |
|
110
|
|
|
|
|
111
|
|
|
if (!$result->isSuccessful()) { |
|
112
|
|
|
return ProofOfPossessionResult::proofOfPossessionCommandFailed(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return ProofOfPossessionResult::secondFactorCreated($command->secondFactorId); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|