|
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 Broadway\EventHandling\EventBusInterface; |
|
22
|
|
|
use Rhumsaa\Uuid\Uuid; |
|
23
|
|
|
use Surfnet\Stepup\Configuration\Value\Institution; |
|
24
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Repository\InstitutionConfigurationOptionsRepository; |
|
25
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\IdentityRepository; |
|
26
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\UnverifiedSecondFactorRepository; |
|
27
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\VerifiedSecondFactorRepository; |
|
28
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Command\Command as MiddlewareCommand; |
|
29
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\VetSecondFactorCommand; |
|
30
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Pipeline\Pipeline; |
|
31
|
|
|
use Surfnet\StepupMiddleware\MiddlewareBundle\Service\DBALConnectionHelper; |
|
32
|
|
|
use Symfony\Component\Console\Command\Command; |
|
33
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
|
34
|
|
|
|
|
35
|
|
|
abstract class AbstractBootstrapCommand extends Command |
|
36
|
|
|
{ |
|
37
|
|
|
/** @var Pipeline */ |
|
38
|
|
|
private $pipeline; |
|
39
|
|
|
/** @var EventBusInterface */ |
|
40
|
|
|
private $eventBus; |
|
41
|
|
|
/** @var DBALConnectionHelper */ |
|
42
|
|
|
private $connection; |
|
43
|
|
|
/** @var IdentityRepository */ |
|
44
|
|
|
protected $identityRepository; |
|
45
|
|
|
/** @var UnverifiedSecondFactorRepository */ |
|
46
|
|
|
protected $unverifiedSecondFactorRepository; |
|
47
|
|
|
/** @var VerifiedSecondFactorRepository */ |
|
48
|
|
|
protected $verifiedSecondFactorRepository; |
|
49
|
|
|
/** @var InstitutionConfigurationOptionsRepository */ |
|
50
|
|
|
private $institutionConfigurationRepository; |
|
51
|
|
|
/** @var TokenStorageInterface */ |
|
52
|
|
|
protected $tokenStorage; |
|
53
|
|
|
|
|
54
|
|
|
public function __construct( |
|
55
|
|
|
Pipeline $pipeline, |
|
56
|
|
|
EventBusInterface $eventBus, |
|
57
|
|
|
DBALConnectionHelper $connection, |
|
58
|
|
|
IdentityRepository $identityRepository, |
|
59
|
|
|
UnverifiedSecondFactorRepository $unverifiedSecondFactorRepository, |
|
60
|
|
|
VerifiedSecondFactorRepository $verifiedSecondFactorRepository, |
|
61
|
|
|
InstitutionConfigurationOptionsRepository $institutionConfigurationOptionsRepository, |
|
62
|
|
|
TokenStorageInterface $tokenStorage |
|
63
|
|
|
) { |
|
64
|
|
|
$this->pipeline = $pipeline; |
|
65
|
|
|
$this->eventBus = $eventBus; |
|
66
|
|
|
$this->connection = $connection; |
|
67
|
|
|
$this->identityRepository = $identityRepository; |
|
68
|
|
|
$this->unverifiedSecondFactorRepository = $unverifiedSecondFactorRepository; |
|
69
|
|
|
$this->verifiedSecondFactorRepository = $verifiedSecondFactorRepository; |
|
70
|
|
|
$this->institutionConfigurationRepository = $institutionConfigurationOptionsRepository; |
|
71
|
|
|
$this->tokenStorage = $tokenStorage; |
|
72
|
|
|
parent::__construct(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
protected function beginTransaction() |
|
76
|
|
|
{ |
|
77
|
|
|
$this->connection->beginTransaction(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
protected function finishTransaction() |
|
81
|
|
|
{ |
|
82
|
|
|
$this->eventBus->flush(); |
|
|
|
|
|
|
83
|
|
|
$this->connection->commit(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
protected function rollback() |
|
87
|
|
|
{ |
|
88
|
|
|
$this->connection->rollBack(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
protected function process(MiddlewareCommand $command) |
|
92
|
|
|
{ |
|
93
|
|
|
$this->pipeline->process($command); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
protected function requiresMailVerification($institution) |
|
97
|
|
|
{ |
|
98
|
|
|
$configuration = $this->institutionConfigurationRepository->findConfigurationOptionsFor(new Institution($institution)); |
|
99
|
|
|
if ($configuration) { |
|
100
|
|
|
return $configuration->verifyEmailOption->isEnabled(); |
|
101
|
|
|
} |
|
102
|
|
|
return true; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
protected function vetSecondFactor($tokenType, $actorId, $identity, $secondFactorId, $verifiedSecondFactor, $phoneNumber) |
|
106
|
|
|
{ |
|
107
|
|
|
$command = new VetSecondFactorCommand(); |
|
108
|
|
|
$command->UUID = (string) Uuid::uuid4(); |
|
109
|
|
|
$command->authorityId = $actorId; |
|
110
|
|
|
$command->identityId = $identity->id; |
|
111
|
|
|
$command->secondFactorId = $secondFactorId; |
|
112
|
|
|
$command->registrationCode = $verifiedSecondFactor->registrationCode; |
|
113
|
|
|
$command->secondFactorType = $tokenType; |
|
114
|
|
|
$command->secondFactorIdentifier = $phoneNumber; |
|
115
|
|
|
$command->documentNumber = '123987'; |
|
116
|
|
|
$command->identityVerified = true; |
|
117
|
|
|
$this->pipeline->process($command); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: