|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2014 SURFnet bv |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of 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, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace Surfnet\StepupBundle\Service\SmsSecondFactor; |
|
20
|
|
|
|
|
21
|
|
|
use DateInterval; |
|
22
|
|
|
use Surfnet\StepupBundle\Exception\InvalidArgumentException; |
|
23
|
|
|
use Surfnet\StepupBundle\Security\OtpGenerator; |
|
24
|
|
|
use Surfnet\StepupBundle\Service\Exception\TooManyChallengesRequestedException; |
|
25
|
|
|
|
|
26
|
|
|
final class SmsVerificationState |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* The maximum amount of attempts can be made, per OTP, to verify the OTP. |
|
30
|
|
|
*/ |
|
31
|
|
|
const MAXIMUM_VERIFICATION_ATTEMPTS = 10; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var DateInterval |
|
35
|
|
|
*/ |
|
36
|
|
|
private $expiryInterval; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var int |
|
40
|
|
|
*/ |
|
41
|
|
|
private $maximumOtpRequests; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var Otp[] |
|
45
|
|
|
*/ |
|
46
|
|
|
private $otps; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var int |
|
50
|
|
|
*/ |
|
51
|
|
|
private $verificationAttemptsMade; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param DateInterval $expiryInterval |
|
55
|
|
|
* @param int $maximumOtpRequests |
|
56
|
|
|
*/ |
|
57
|
|
|
public function __construct(DateInterval $expiryInterval, $maximumOtpRequests) |
|
58
|
|
|
{ |
|
59
|
|
|
if ($maximumOtpRequests <= 0) { |
|
60
|
|
|
throw new InvalidArgumentException('Expected greater-than-zero number of maximum OTP requests.'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$this->expiryInterval = $expiryInterval; |
|
64
|
|
|
$this->maximumOtpRequests= $maximumOtpRequests; |
|
65
|
|
|
$this->otps = []; |
|
66
|
|
|
$this->verificationAttemptsMade = 0; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $phoneNumber |
|
71
|
|
|
* @return string The generated OTP string. |
|
72
|
|
|
*/ |
|
73
|
|
|
public function requestNewOtp($phoneNumber) |
|
74
|
|
|
{ |
|
75
|
|
|
if (!is_string($phoneNumber) || empty($phoneNumber)) { |
|
76
|
|
|
throw InvalidArgumentException::invalidType('string', 'phoneNumber', $phoneNumber); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if (count($this->otps) >= $this->maximumOtpRequests) { |
|
80
|
|
|
throw new TooManyChallengesRequestedException( |
|
81
|
|
|
sprintf( |
|
82
|
|
|
'%d OTPs were requested, while only %d requests are allowed', |
|
83
|
|
|
count($this->otps) + 1, |
|
84
|
|
|
$this->maximumOtpRequests |
|
85
|
|
|
) |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$this->otps = array_filter($this->otps, function (Otp $otp) use ($phoneNumber) { |
|
90
|
|
|
return $otp->hasPhoneNumber($phoneNumber); |
|
91
|
|
|
}); |
|
92
|
|
|
|
|
93
|
|
|
$otp = OtpGenerator::generate(8); |
|
94
|
|
|
$this->otps[] = Otp::create($otp, $phoneNumber, $this->expiryInterval); |
|
95
|
|
|
|
|
96
|
|
|
return $otp; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param string $userOtp |
|
101
|
|
|
* @return OtpVerification |
|
102
|
|
|
*/ |
|
103
|
|
|
public function verify($userOtp) |
|
104
|
|
|
{ |
|
105
|
|
|
if ($this->verificationAttemptsMade >= self::MAXIMUM_VERIFICATION_ATTEMPTS) { |
|
106
|
|
|
return OtpVerification::tooManyAttempts(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$this->verificationAttemptsMade++; |
|
110
|
|
|
|
|
111
|
|
|
if (!is_string($userOtp)) { |
|
112
|
|
|
throw InvalidArgumentException::invalidType('string', 'userOtp', $userOtp); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
foreach ($this->otps as $otp) { |
|
116
|
|
|
$verification = $otp->verify($userOtp); |
|
117
|
|
|
|
|
118
|
|
|
if ($verification->didOtpMatch()) { |
|
119
|
|
|
return $verification; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return OtpVerification::noMatch(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return int |
|
128
|
|
|
*/ |
|
129
|
|
|
public function getOtpRequestsRemainingCount() |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->maximumOtpRequests - count($this->otps); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|