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\CommandHandlingBundle\Identity\Command\ProveGssfPossessionCommand; |
27
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
28
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
29
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
30
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken; |
31
|
|
|
|
32
|
|
|
final class BootstrapGsspSecondFactorCommand extends AbstractBootstrapCommand |
33
|
|
|
{ |
34
|
|
View Code Duplication |
protected function configure() |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$this |
37
|
|
|
->setDescription('Creates a Generic SAML Second Factor (GSSF) second factor for a specified user') |
38
|
|
|
->addArgument('name-id', InputArgument::REQUIRED, 'The NameID of the identity to create') |
39
|
|
|
->addArgument('institution', InputArgument::REQUIRED, 'The institution of the identity to create') |
40
|
|
|
->addArgument( |
41
|
|
|
'gssp-token-type', |
42
|
|
|
InputArgument::REQUIRED, |
43
|
|
|
'The GSSP token type as defined in the GSSP config, for example tiqr or webauthn' |
44
|
|
|
) |
45
|
|
|
->addArgument( |
46
|
|
|
'gssp-token-identifier', |
47
|
|
|
InputArgument::REQUIRED, |
48
|
|
|
'The identifier of the token as registered at the GSSP' |
49
|
|
|
) |
50
|
|
|
->addArgument( |
51
|
|
|
'registration-status', |
52
|
|
|
InputArgument::REQUIRED, |
53
|
|
|
'Valid arguments: unverified, verified, vetted' |
54
|
|
|
) |
55
|
|
|
->addArgument('actor-id', InputArgument::REQUIRED, 'The id of the vetting actor'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
59
|
|
|
{ |
60
|
|
|
$this->tokenStorage->setToken( |
61
|
|
|
new AnonymousToken('cli.bootstrap-gssp-token', 'cli', ['ROLE_SS', 'ROLE_RA']) |
|
|
|
|
62
|
|
|
); |
63
|
|
|
$nameId = new NameId($input->getArgument('name-id')); |
64
|
|
|
$institutionText = $input->getArgument('institution'); |
65
|
|
|
$institution = new Institution($institutionText); |
|
|
|
|
66
|
|
|
$mailVerificationRequired = $this->requiresMailVerification($institutionText); |
67
|
|
|
$registrationStatus = $input->getArgument('registration-status'); |
68
|
|
|
$tokenType = $input->getArgument('gssp-token-type'); |
69
|
|
|
$tokenIdentifier = $input->getArgument('gssp-token-identifier'); |
70
|
|
|
$actorId = $input->getArgument('actor-id'); |
71
|
|
|
$this->enrichEventMetadata($actorId); |
72
|
|
|
if (!$this->tokenBootstrapService->hasIdentityWithNameIdAndInstitution($nameId, $institution)) { |
73
|
|
|
$output->writeln( |
74
|
|
|
sprintf( |
75
|
|
|
'<error>An identity with name ID "%s" from institution "%s" does not exist, create it first.</error>', |
76
|
|
|
$nameId->getNameId(), |
77
|
|
|
$institution->getInstitution() |
78
|
|
|
) |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
$identity = $this->tokenBootstrapService->findOneByNameIdAndInstitution($nameId, $institution); |
84
|
|
|
$output->writeln(sprintf('<comment>Adding a %s %s GSSP token for %s</comment>', $registrationStatus, $tokenType, $identity->commonName)); |
85
|
|
|
$this->beginTransaction(); |
86
|
|
|
$secondFactorId = Uuid::uuid4()->toString(); |
87
|
|
|
|
88
|
|
|
try { |
89
|
|
|
switch ($registrationStatus) { |
90
|
|
|
case "unverified": |
91
|
|
|
$output->writeln(sprintf('<comment>Creating an unverified %s token</comment>', $tokenType)); |
92
|
|
|
$this->provePossession($secondFactorId, $identity, $tokenType, $tokenIdentifier); |
93
|
|
|
break; |
94
|
|
|
case "verified": |
95
|
|
|
$output->writeln(sprintf('<comment>Creating an unverified %s token</comment>', $tokenType)); |
96
|
|
|
$this->provePossession($secondFactorId, $identity, $tokenType, $tokenIdentifier); |
97
|
|
|
$unverifiedSecondFactor = $this->tokenBootstrapService->findUnverifiedToken($identity->id, $tokenType); |
98
|
|
|
if ($mailVerificationRequired) { |
99
|
|
|
$output->writeln(sprintf('<comment>Creating an verified %s token</comment>', $tokenType)); |
100
|
|
|
$this->verifyEmail($identity, $unverifiedSecondFactor); |
101
|
|
|
} |
102
|
|
|
break; |
103
|
|
|
case "vetted": |
104
|
|
|
$output->writeln(sprintf('<comment>Creating an unverified %s token</comment>', $tokenType)); |
105
|
|
|
$this->provePossession($secondFactorId, $identity, $tokenType, $tokenIdentifier); |
106
|
|
|
/** @var UnverifiedSecondFactor $unverifiedSecondFactor */ |
107
|
|
|
$unverifiedSecondFactor = $this->tokenBootstrapService->findUnverifiedToken($identity->id, $tokenType); |
108
|
|
|
if ($mailVerificationRequired) { |
109
|
|
|
$output->writeln(sprintf('<comment>Creating an verified %s token</comment>', $tokenType)); |
110
|
|
|
$this->verifyEmail($identity, $unverifiedSecondFactor); |
111
|
|
|
} |
112
|
|
|
$verifiedSecondFactor = $this->tokenBootstrapService->findVerifiedToken($identity->id, $tokenType); |
113
|
|
|
$output->writeln(sprintf('<comment>Vetting the verified %s token</comment>', $tokenType)); |
114
|
|
|
$this->vetSecondFactor( |
115
|
|
|
$tokenType, |
116
|
|
|
$actorId, |
117
|
|
|
$identity, |
118
|
|
|
$secondFactorId, |
119
|
|
|
$verifiedSecondFactor, |
120
|
|
|
$tokenIdentifier |
121
|
|
|
); |
122
|
|
|
break; |
123
|
|
|
} |
124
|
|
|
$this->finishTransaction(); |
125
|
|
|
} catch (Exception $e) { |
126
|
|
|
$output->writeln( |
127
|
|
|
sprintf( |
128
|
|
|
'<error>An Error occurred when trying to bootstrap the %s token: "%s"</error>', |
129
|
|
|
$tokenType, |
130
|
|
|
$e->getMessage() |
131
|
|
|
) |
132
|
|
|
); |
133
|
|
|
$this->rollback(); |
134
|
|
|
throw $e; |
135
|
|
|
} |
136
|
|
|
$output->writeln( |
137
|
|
|
sprintf( |
138
|
|
|
'<info>Successfully %s %s second factor with UUID %s</info>', |
139
|
|
|
$registrationStatus, |
140
|
|
|
$tokenType, |
141
|
|
|
$secondFactorId |
142
|
|
|
) |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
private function provePossession($secondFactorId, $identity, $tokenType, $tokenIdentifier) |
147
|
|
|
{ |
148
|
|
|
$command = new ProveGssfPossessionCommand(); |
149
|
|
|
$command->UUID = (string)Uuid::uuid4(); |
150
|
|
|
$command->secondFactorId = $secondFactorId; |
151
|
|
|
$command->identityId = $identity->id; |
152
|
|
|
$command->stepupProvider = $tokenType; |
153
|
|
|
$command->gssfId = $tokenIdentifier; |
154
|
|
|
$this->process($command); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.