1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2020 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\StepupMiddleware\MiddlewareBundle\Console\Command; |
20
|
|
|
|
21
|
|
|
use Exception; |
22
|
|
|
use Rhumsaa\Uuid\Uuid; |
23
|
|
|
use Surfnet\Stepup\Identity\Value\Institution; |
24
|
|
|
use Surfnet\Stepup\Identity\Value\NameId; |
25
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\UnverifiedSecondFactor; |
26
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\VerifiedSecondFactor; |
27
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\CreateIdentityCommand; |
28
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\ProvePhonePossessionCommand; |
29
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\VerifyEmailCommand; |
30
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
31
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
32
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
33
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveClassLength) |
37
|
|
|
*/ |
38
|
|
|
final class BootstrapIdentityWithSmsSecondFactorCommand extends AbstractBootstrapCommand |
39
|
|
|
{ |
40
|
|
|
protected function configure() |
41
|
|
|
{ |
42
|
|
|
$this |
43
|
|
|
->setDescription('Creates an identity with a SMS second factor') |
44
|
|
|
->addArgument('name-id', InputArgument::REQUIRED, 'The NameID of the identity to create') |
45
|
|
|
->addArgument('institution', InputArgument::REQUIRED, 'The institution of the identity to create') |
46
|
|
|
->addArgument('common-name', InputArgument::REQUIRED, 'The Common Name of the identity to create') |
47
|
|
|
->addArgument('email', InputArgument::REQUIRED, 'The e-mail address of the identity to create') |
48
|
|
|
->addArgument('preferred-locale', InputArgument::REQUIRED, 'The preferred locale of the identity to create') |
49
|
|
|
->addArgument( |
50
|
|
|
'phone-number', |
51
|
|
|
InputArgument::REQUIRED, |
52
|
|
|
'The phone number of the user should be formatted like "+31 (0) 612345678"' |
53
|
|
|
) |
54
|
|
|
->addArgument( |
55
|
|
|
'registration-status', |
56
|
|
|
InputArgument::REQUIRED, |
57
|
|
|
'Valid arguments: unverified, verified, vetted' |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
62
|
|
|
{ |
63
|
|
|
$this->tokenStorage->setToken( |
64
|
|
|
new AnonymousToken('cli.bootstrap-identity-with-sms-token', 'cli', ['ROLE_SS', 'ROLE_RA']) |
|
|
|
|
65
|
|
|
); |
66
|
|
|
$nameId = new NameId($input->getArgument('name-id')); |
67
|
|
|
$institutionText = $input->getArgument('institution'); |
68
|
|
|
$institution = new Institution($institutionText); |
|
|
|
|
69
|
|
|
$mailVerificationRequired = $this->requiresMailVerification($institutionText); |
70
|
|
|
$commonName = $input->getArgument('common-name'); |
71
|
|
|
$email = $input->getArgument('email'); |
72
|
|
|
$preferredLocale = $input->getArgument('preferred-locale'); |
73
|
|
|
$registrationStatus = $input->getArgument('registration-status'); |
74
|
|
|
$phoneNumber = $input->getArgument('phone-number'); |
75
|
|
|
$identity = false; |
76
|
|
|
$output->writeln(sprintf('<notice>Adding a %s SMS token for %s</notice>', $registrationStatus, $commonName)); |
77
|
|
|
|
78
|
|
|
if ($this->identityRepository->hasIdentityWithNameIdAndInstitution($nameId, $institution)) { |
79
|
|
|
$output->writeln( |
80
|
|
|
sprintf( |
81
|
|
|
'<notice>An identity with name ID "%s" from institution "%s" already exists, using that identity</notice>', |
82
|
|
|
$nameId->getNameId(), |
83
|
|
|
$institution->getInstitution() |
84
|
|
|
) |
85
|
|
|
); |
86
|
|
|
$identity = $this->identityRepository->findOneByNameIdAndInstitution($nameId, $institution); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->beginTransaction(); |
90
|
|
|
$secondFactorId = Uuid::uuid4()->toString(); |
91
|
|
|
|
92
|
|
|
if (!$identity) { |
93
|
|
|
$output->writeln('<notice>Creating a new identity</notice>'); |
94
|
|
|
$identity = $this->createIdentity($institution, $nameId, $commonName, $email, $preferredLocale); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
try { |
98
|
|
|
switch ($registrationStatus) { |
99
|
|
|
case "unverified": |
100
|
|
|
$output->writeln('<notice>Creating an unverified SMS token</notice>'); |
101
|
|
|
$this->provePossession($secondFactorId, $identity, $phoneNumber); |
102
|
|
|
break; |
103
|
|
|
case "verified": |
104
|
|
|
$output->writeln('<notice>Creating an unverified SMS token</notice>'); |
105
|
|
|
$this->provePossession($secondFactorId, $identity, $phoneNumber); |
106
|
|
|
/** @var UnverifiedSecondFactor $unverifiedSecondFactor */ |
107
|
|
|
$unverifiedSecondFactor = $this->unverifiedSecondFactorRepository->findOneBy( |
108
|
|
|
['identityId' => $identity->id, 'type' => 'sms'] |
109
|
|
|
); |
110
|
|
|
if ($mailVerificationRequired) { |
111
|
|
|
$output->writeln('<notice>Creating a verified SMS token</notice>'); |
112
|
|
|
$this->verifyEmail($identity, $unverifiedSecondFactor); |
113
|
|
|
} |
114
|
|
|
break; |
115
|
|
|
case "vetted": |
116
|
|
|
$output->writeln('<notice>Creating an unverified SMS token</notice>'); |
117
|
|
|
$this->provePossession($secondFactorId, $identity, $phoneNumber); |
118
|
|
|
/** @var UnverifiedSecondFactor $unverifiedSecondFactor */ |
119
|
|
|
$unverifiedSecondFactor = $this->unverifiedSecondFactorRepository->findOneBy( |
120
|
|
|
['identityId' => $identity->id, 'type' => 'sms'] |
121
|
|
|
); |
122
|
|
|
if ($mailVerificationRequired) { |
123
|
|
|
$output->writeln('<notice>Creating a verified SMS token</notice>'); |
124
|
|
|
$this->verifyEmail($identity, $unverifiedSecondFactor); |
125
|
|
|
} |
126
|
|
|
/** @var VerifiedSecondFactor $verifiedSecondFactor */ |
127
|
|
|
$verifiedSecondFactor = $this->verifiedSecondFactorRepository->findOneBy( |
128
|
|
|
['identityId' => $identity->id, 'type' => 'sms'] |
129
|
|
|
); |
130
|
|
|
$output->writeln('<notice>Vetting the verified SMS token</notice>'); |
131
|
|
|
$this->vetSecondFactor( |
132
|
|
|
'sms', |
133
|
|
|
'db9b8bdf-720c-44ba-a4c4-154953e45f14', |
134
|
|
|
$identity, |
135
|
|
|
$secondFactorId, |
136
|
|
|
$verifiedSecondFactor, |
137
|
|
|
$phoneNumber |
138
|
|
|
); |
139
|
|
|
break; |
140
|
|
|
} |
141
|
|
|
$this->finishTransaction(); |
142
|
|
|
} catch (Exception $e) { |
143
|
|
|
$output->writeln( |
144
|
|
|
sprintf( |
145
|
|
|
'<error>An Error occurred when trying to bootstrap the identity: "%s"</error>', |
146
|
|
|
$e->getMessage() |
147
|
|
|
) |
148
|
|
|
); |
149
|
|
|
$this->rollback(); |
150
|
|
|
throw $e; |
151
|
|
|
} |
152
|
|
|
$output->writeln( |
153
|
|
|
sprintf( |
154
|
|
|
'<info>Successfully created identity with UUID %s and %s second factor with UUID %s</info>', |
155
|
|
|
$identity->id, |
156
|
|
|
$registrationStatus, |
157
|
|
|
$secondFactorId |
158
|
|
|
) |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
View Code Duplication |
private function provePossession($secondFactorId, $identity, $phoneNumber) |
|
|
|
|
163
|
|
|
{ |
164
|
|
|
$command = new ProvePhonePossessionCommand(); |
165
|
|
|
$command->UUID = (string)Uuid::uuid4(); |
166
|
|
|
$command->secondFactorId = $secondFactorId; |
167
|
|
|
$command->identityId = $identity->id; |
168
|
|
|
$command->phoneNumber = $phoneNumber; |
169
|
|
|
$this->process($command); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
View Code Duplication |
private function verifyEmail($identity, $unverifiedSecondFactor) |
|
|
|
|
173
|
|
|
{ |
174
|
|
|
$command = new VerifyEmailCommand(); |
175
|
|
|
$command->UUID = (string)Uuid::uuid4(); |
176
|
|
|
$command->identityId = $identity->id; |
177
|
|
|
$command->verificationNonce = $unverifiedSecondFactor->verificationNonce; |
178
|
|
|
$this->process($command); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
protected function createIdentity( |
182
|
|
|
Institution $institution, |
183
|
|
|
NameId $nameId, |
184
|
|
|
$commonName, |
185
|
|
|
$email, |
186
|
|
|
$preferredLocale |
187
|
|
|
) { |
188
|
|
|
$identity = new CreateIdentityCommand(); |
189
|
|
|
$identity->UUID = (string)Uuid::uuid4(); |
190
|
|
|
$identity->id = (string)Uuid::uuid4(); |
191
|
|
|
$identity->institution = $institution->getInstitution(); |
192
|
|
|
$identity->nameId = $nameId->getNameId(); |
193
|
|
|
$identity->commonName = $commonName; |
194
|
|
|
$identity->email = $email; |
195
|
|
|
$identity->preferredLocale = $preferredLocale; |
196
|
|
|
$this->process($identity); |
197
|
|
|
|
198
|
|
|
return $identity; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: