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; |
20
|
|
|
|
21
|
|
|
use Surfnet\StepupBundle\Command\SendSmsChallengeCommand; |
22
|
|
|
use Surfnet\StepupBundle\Command\SendSmsCommand; |
23
|
|
|
use Surfnet\StepupBundle\Command\VerifyPossessionOfPhoneCommand; |
24
|
|
|
use Surfnet\StepupBundle\Exception\InvalidArgumentException; |
25
|
|
|
use Surfnet\StepupBundle\Service\SmsSecondFactor\OtpVerification; |
26
|
|
|
use Surfnet\StepupBundle\Service\SmsSecondFactor\SmsVerificationStateHandler; |
27
|
|
|
|
28
|
|
|
class SmsSecondFactorService implements SmsSecondFactorServiceInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var \Surfnet\StepupBundle\Service\SmsService |
32
|
|
|
*/ |
33
|
|
|
private $smsService; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var \Surfnet\StepupBundle\Service\SmsSecondFactor\SmsVerificationStateHandler |
37
|
|
|
*/ |
38
|
|
|
private $smsVerificationStateHandler; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $originator; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param SmsService $smsService |
47
|
|
|
* @param SmsVerificationStateHandler $smsVerificationStateHandler |
48
|
|
|
* @param string $originator |
49
|
|
|
*/ |
50
|
|
|
public function __construct( |
51
|
|
|
SmsService $smsService, |
52
|
|
|
SmsVerificationStateHandler $smsVerificationStateHandler, |
53
|
|
|
$originator |
54
|
|
|
) { |
55
|
|
|
if (!is_string($originator)) { |
56
|
|
|
throw InvalidArgumentException::invalidType('string', 'originator', $originator); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (!preg_match('~^[a-z0-9]{1,11}$~i', $originator)) { |
60
|
|
|
throw new InvalidArgumentException( |
61
|
|
|
'Invalid SMS originator given: may only contain alphanumerical characters.' |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->smsService = $smsService; |
66
|
|
|
$this->smsVerificationStateHandler = $smsVerificationStateHandler; |
67
|
|
|
$this->originator = $originator; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return int |
72
|
|
|
*/ |
73
|
|
|
public function getOtpRequestsRemainingCount() |
74
|
|
|
{ |
75
|
|
|
return $this->smsVerificationStateHandler->getOtpRequestsRemainingCount(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return int |
80
|
|
|
*/ |
81
|
|
|
public function getMaximumOtpRequestsCount() |
82
|
|
|
{ |
83
|
|
|
return $this->smsVerificationStateHandler->getMaximumOtpRequestsCount(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
public function hasSmsVerificationState() |
90
|
|
|
{ |
91
|
|
|
return $this->smsVerificationStateHandler->hasState(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function clearSmsVerificationState() |
95
|
|
|
{ |
96
|
|
|
$this->smsVerificationStateHandler->clearState(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param SendSmsChallengeCommand $command |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
|
public function sendChallenge(SendSmsChallengeCommand $command) |
104
|
|
|
{ |
105
|
|
|
$challenge = $this->smsVerificationStateHandler->requestNewOtp((string) $command->phoneNumber); |
106
|
|
|
|
107
|
|
|
$smsCommand = new SendSmsCommand(); |
108
|
|
|
$smsCommand->recipient = $command->phoneNumber->toMSISDN(); |
109
|
|
|
$smsCommand->originator = $this->originator; |
110
|
|
|
$smsCommand->body = str_replace('%challenge%', $challenge, $command->body); |
111
|
|
|
$smsCommand->identity = $command->identity; |
112
|
|
|
$smsCommand->institution = $command->institution; |
113
|
|
|
|
114
|
|
|
return $this->smsService->sendSms($smsCommand); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param VerifyPossessionOfPhoneCommand $command |
119
|
|
|
* @return OtpVerification |
120
|
|
|
*/ |
121
|
|
|
public function verifyPossession(VerifyPossessionOfPhoneCommand $command) |
122
|
|
|
{ |
123
|
|
|
return $this->smsVerificationStateHandler->verify($command->challenge); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|