|
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 Symfony\Component\HttpFoundation\Session\SessionInterface; |
|
23
|
|
|
|
|
24
|
|
|
final class SessionSmsVerificationStateHandler implements SmsVerificationStateHandler |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var SessionInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $session; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
private $sessionKey; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var DateInterval |
|
38
|
|
|
*/ |
|
39
|
|
|
private $otpExpiryInterval; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var int |
|
43
|
|
|
*/ |
|
44
|
|
|
private $otpRequestMaximum; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param SessionInterface $session |
|
48
|
|
|
* @param string $sessionKey |
|
49
|
|
|
* @param int $otpExpiryInterval OTP's expiry interval in seconds |
|
50
|
|
|
* @param int $otpRequestMaximum |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct( |
|
53
|
|
|
SessionInterface $session, |
|
54
|
|
|
$sessionKey, |
|
55
|
|
|
$otpExpiryInterval, |
|
56
|
|
|
$otpRequestMaximum |
|
57
|
|
|
) { |
|
58
|
|
|
$this->session = $session; |
|
59
|
|
|
$this->sessionKey = $sessionKey; |
|
60
|
|
|
$this->otpExpiryInterval = new DateInterval(sprintf('PT%dS', $otpExpiryInterval)); |
|
61
|
|
|
$this->otpRequestMaximum = $otpRequestMaximum; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function hasState() |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->session->has($this->sessionKey); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function clearState() |
|
70
|
|
|
{ |
|
71
|
|
|
$this->session->remove($this->sessionKey); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function requestNewOtp($phoneNumber) |
|
75
|
|
|
{ |
|
76
|
|
|
/** @var SmsVerificationState|null $state */ |
|
77
|
|
|
$state = $this->session->get($this->sessionKey); |
|
78
|
|
|
|
|
79
|
|
|
if (!$state) { |
|
80
|
|
|
$state = new SmsVerificationState($this->otpExpiryInterval, $this->otpRequestMaximum); |
|
81
|
|
|
$this->session->set($this->sessionKey, $state); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $state->requestNewOtp($phoneNumber); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function getOtpRequestsRemainingCount() |
|
88
|
|
|
{ |
|
89
|
|
|
/** @var SmsVerificationState|null $state */ |
|
90
|
|
|
$state = $this->session->get($this->sessionKey); |
|
91
|
|
|
|
|
92
|
|
|
return $state ? $state->getOtpRequestsRemainingCount() : $this->otpRequestMaximum; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function getMaximumOtpRequestsCount() |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->otpRequestMaximum; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function verify($otp) |
|
101
|
|
|
{ |
|
102
|
|
|
/** @var SmsVerificationState|null $state */ |
|
103
|
|
|
$state = $this->session->get($this->sessionKey); |
|
104
|
|
|
|
|
105
|
|
|
if (!$state) { |
|
106
|
|
|
return OtpVerification::matchExpired(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$verification = $state->verify($otp); |
|
110
|
|
|
|
|
111
|
|
|
if ($verification->wasSuccessful()) { |
|
112
|
|
|
$this->session->remove($this->sessionKey); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $verification; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|