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 DateTime as CoreDateTime; |
23
|
|
|
use Surfnet\StepupBundle\DateTime\DateTime; |
24
|
|
|
use Surfnet\StepupBundle\Exception\InvalidArgumentException; |
25
|
|
|
|
26
|
|
|
final class Otp |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $otp; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $phoneNumber; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var DateInterval |
40
|
|
|
*/ |
41
|
|
|
private $expiryInterval; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var CoreDateTime |
45
|
|
|
*/ |
46
|
|
|
private $issuedAt; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $otpString |
50
|
|
|
* @param string $phoneNumber |
51
|
|
|
* @param DateInterval $expiryInterval |
52
|
|
|
* @return Otp |
53
|
|
|
*/ |
54
|
|
|
public static function create($otpString, $phoneNumber, DateInterval $expiryInterval) |
55
|
|
|
{ |
56
|
|
|
if (!is_string($otpString) || empty($otpString)) { |
57
|
|
|
throw InvalidArgumentException::invalidType('string', 'otpString', $otpString); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (!is_string($phoneNumber) || empty($phoneNumber)) { |
61
|
|
|
throw InvalidArgumentException::invalidType('string', 'phoneNumber', $phoneNumber); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$otp = new self; |
65
|
|
|
$otp->otp = $otpString; |
66
|
|
|
$otp->phoneNumber = $phoneNumber; |
67
|
|
|
$otp->expiryInterval = $expiryInterval; |
68
|
|
|
$otp->issuedAt = DateTime::now(); |
69
|
|
|
|
70
|
|
|
return $otp; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function __construct() |
74
|
|
|
{ |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function verify($userOtp) |
78
|
|
|
{ |
79
|
|
|
if (!is_string($userOtp)) { |
80
|
|
|
throw InvalidArgumentException::invalidType('string', 'userOtp', $userOtp); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (strtoupper($userOtp) !== strtoupper($this->otp)) { |
84
|
|
|
return OtpVerification::noMatch(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$expiryTime = clone $this->issuedAt; |
88
|
|
|
$expiryTime->add($this->expiryInterval); |
89
|
|
|
|
90
|
|
|
if ($expiryTime <= DateTime::now()) { |
91
|
|
|
return OtpVerification::matchExpired(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return OtpVerification::foundMatch($this->phoneNumber); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $phoneNumber |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
public function hasPhoneNumber($phoneNumber) |
102
|
|
|
{ |
103
|
|
|
return $this->phoneNumber === $phoneNumber; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|