|
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\StepupMiddleware\CommandHandlingBundle\SensitiveData; |
|
20
|
|
|
|
|
21
|
|
|
use Broadway\Serializer\Serializable as SerializableInterface; |
|
22
|
|
|
use Surfnet\Stepup\Identity\Value\CommonName; |
|
23
|
|
|
use Surfnet\Stepup\Identity\Value\Email; |
|
24
|
|
|
use Surfnet\Stepup\Identity\Value\RecoveryTokenIdentifier; |
|
25
|
|
|
use Surfnet\Stepup\Identity\Value\RecoveryTokenIdentifierFactory; |
|
26
|
|
|
use Surfnet\Stepup\Identity\Value\RecoveryTokenType; |
|
27
|
|
|
use Surfnet\Stepup\Identity\Value\SecondFactorIdentifier; |
|
28
|
|
|
use Surfnet\Stepup\Identity\Value\SecondFactorIdentifierFactory; |
|
29
|
|
|
use Surfnet\Stepup\Identity\Value\UnknownVettingType; |
|
30
|
|
|
use Surfnet\Stepup\Identity\Value\VettingType; |
|
31
|
|
|
use Surfnet\Stepup\Identity\Value\VettingTypeFactory; |
|
32
|
|
|
use Surfnet\StepupBundle\Value\SecondFactorType; |
|
33
|
|
|
|
|
34
|
|
|
class SensitiveData implements SerializableInterface |
|
|
|
|
|
|
35
|
|
|
{ |
|
36
|
|
|
private ?CommonName $commonName = null; |
|
37
|
|
|
|
|
38
|
|
|
private ?Email $email = null; |
|
39
|
|
|
|
|
40
|
|
|
private ?SecondFactorIdentifier $secondFactorIdentifier = null; |
|
41
|
|
|
|
|
42
|
|
|
private ?SecondFactorType $secondFactorType = null; |
|
43
|
|
|
|
|
44
|
|
|
private ?VettingType $vettingType = null; |
|
45
|
|
|
|
|
46
|
|
|
private ?RecoveryTokenType $recoveryTokenType = null; |
|
47
|
|
|
|
|
48
|
|
|
private ?RecoveryTokenIdentifier $recoveryTokenIdentifier = null; |
|
49
|
|
|
|
|
50
|
|
|
public function withCommonName(CommonName $commonName): static |
|
51
|
|
|
{ |
|
52
|
|
|
$clone = clone $this; |
|
53
|
|
|
$clone->commonName = $commonName; |
|
54
|
|
|
|
|
55
|
|
|
return $clone; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function withEmail(Email $email): static |
|
59
|
|
|
{ |
|
60
|
|
|
$clone = clone $this; |
|
61
|
|
|
$clone->email = $email; |
|
62
|
|
|
|
|
63
|
|
|
return $clone; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function withSecondFactorIdentifier( |
|
67
|
|
|
SecondFactorIdentifier $secondFactorIdentifier, |
|
68
|
|
|
SecondFactorType $secondFactorType, |
|
69
|
|
|
): static { |
|
70
|
|
|
$clone = clone $this; |
|
71
|
|
|
$clone->secondFactorType = $secondFactorType; |
|
72
|
|
|
$clone->secondFactorIdentifier = $secondFactorIdentifier; |
|
73
|
|
|
|
|
74
|
|
|
return $clone; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function withRecoveryTokenSecret( |
|
78
|
|
|
RecoveryTokenIdentifier $recoveryTokenIdentifier, |
|
79
|
|
|
RecoveryTokenType $type, |
|
80
|
|
|
): SensitiveData { |
|
81
|
|
|
$clone = clone $this; |
|
82
|
|
|
$clone->recoveryTokenType = $type; |
|
83
|
|
|
$clone->recoveryTokenIdentifier = $recoveryTokenIdentifier; |
|
84
|
|
|
|
|
85
|
|
|
return $clone; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function withVettingType(VettingType $vettingType): self |
|
89
|
|
|
{ |
|
90
|
|
|
$clone = clone $this; |
|
91
|
|
|
$clone->vettingType = $vettingType; |
|
92
|
|
|
|
|
93
|
|
|
return $clone; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Returns an instance in which all sensitive data is forgotten. |
|
98
|
|
|
*/ |
|
|
|
|
|
|
99
|
|
|
public function forget(): self |
|
100
|
|
|
{ |
|
101
|
|
|
$forgotten = new self(); |
|
102
|
|
|
$forgotten->secondFactorType = $this->secondFactorType; |
|
103
|
|
|
|
|
104
|
|
|
return $forgotten; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function getCommonName(): CommonName |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->commonName ?: CommonName::unknown(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function getEmail(): Email |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->email ?: Email::unknown(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function getSecondFactorIdentifier(): SecondFactorIdentifier |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->secondFactorIdentifier ?: SecondFactorIdentifierFactory::unknownForType($this->secondFactorType); |
|
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function getRecoveryTokenIdentifier(): ?RecoveryTokenIdentifier |
|
123
|
|
|
{ |
|
124
|
|
|
if ($this->recoveryTokenIdentifier instanceof RecoveryTokenIdentifier) { |
|
125
|
|
|
return $this->recoveryTokenIdentifier; |
|
126
|
|
|
} |
|
127
|
|
|
if ($this->recoveryTokenType instanceof RecoveryTokenType) { |
|
128
|
|
|
return RecoveryTokenIdentifierFactory::unknownForType($this->recoveryTokenType); |
|
129
|
|
|
} |
|
130
|
|
|
return null; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function getVettingType(): VettingType |
|
134
|
|
|
{ |
|
135
|
|
|
return $this->vettingType ?: new UnknownVettingType(); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public static function deserialize(array $data): SensitiveData |
|
139
|
|
|
{ |
|
140
|
|
|
$self = new self; |
|
141
|
|
|
|
|
142
|
|
|
if (isset($data['common_name'])) { |
|
143
|
|
|
$self->commonName = new CommonName($data['common_name']); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
if (isset($data['email'])) { |
|
147
|
|
|
$self->email = new Email($data['email']); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
if (isset($data['second_factor_type'])) { |
|
151
|
|
|
$self->secondFactorType = new SecondFactorType($data['second_factor_type']); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
if (isset($data['second_factor_identifier'])) { |
|
155
|
|
|
$self->secondFactorIdentifier = |
|
|
|
|
|
|
156
|
|
|
SecondFactorIdentifierFactory::forType($self->secondFactorType, $data['second_factor_identifier']); |
|
|
|
|
|
|
157
|
|
|
} |
|
158
|
|
|
if (isset($data['recovery_token_type'])) { |
|
159
|
|
|
$self->recoveryTokenType = new RecoveryTokenType($data['recovery_token_type']); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
if (isset($data['recovery_token_identifier'])) { |
|
163
|
|
|
$self->recoveryTokenIdentifier = RecoveryTokenIdentifierFactory::forType( |
|
164
|
|
|
$self->recoveryTokenType, |
|
|
|
|
|
|
165
|
|
|
$data['recovery_token_identifier'], |
|
166
|
|
|
); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
if (isset($data['document_number']) || isset($data['vetting_type'])) { |
|
170
|
|
|
$self->vettingType = VettingTypeFactory::fromData($data); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
return $self; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
public function serialize(): array |
|
177
|
|
|
{ |
|
178
|
|
|
$vettingType = (is_null($this->vettingType)) ? null : $this->vettingType->jsonSerialize(); |
|
179
|
|
|
return array_filter([ |
|
|
|
|
|
|
180
|
|
|
'common_name' => $this->commonName, |
|
181
|
|
|
'email' => $this->email, |
|
182
|
|
|
'second_factor_type' => $this->secondFactorType, |
|
183
|
|
|
'second_factor_identifier' => $this->secondFactorIdentifier, |
|
184
|
|
|
'recovery_token_type' => (string)$this->recoveryTokenType, |
|
185
|
|
|
'recovery_token_identifier' => $this->recoveryTokenIdentifier, |
|
186
|
|
|
'vetting_type' => $vettingType, |
|
187
|
|
|
]); |
|
|
|
|
|
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|