YubikeySecondFactorBootstrappedEvent   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 51
dl 0
loc 110
rs 10
c 3
b 0
f 0
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A deserialize() 0 11 1
A getSensitiveData() 0 6 1
A getAllowlist() 0 3 1
A getAuditLogMetadata() 0 10 1
A setSensitiveData() 0 7 1
A serialize() 0 8 1
A obtainUserData() 0 5 1
A __construct() 0 12 1
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
 */
0 ignored issues
show
Coding Style introduced by
Missing @link tag in file comment
Loading history...
18
19
namespace Surfnet\Stepup\Identity\Event;
20
21
use Surfnet\Stepup\Identity\AuditLog\Metadata;
22
use Surfnet\Stepup\Identity\Value\CommonName;
23
use Surfnet\Stepup\Identity\Value\Email;
24
use Surfnet\Stepup\Identity\Value\IdentityId;
25
use Surfnet\Stepup\Identity\Value\Institution;
26
use Surfnet\Stepup\Identity\Value\Locale;
0 ignored issues
show
Bug introduced by
The type Surfnet\Stepup\Identity\Value\Locale was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
use Surfnet\Stepup\Identity\Value\NameId;
28
use Surfnet\Stepup\Identity\Value\SecondFactorId;
29
use Surfnet\Stepup\Identity\Value\YubikeyPublicId;
30
use Surfnet\StepupBundle\Value\SecondFactorType;
31
use Surfnet\StepupMiddleware\CommandHandlingBundle\SensitiveData\Forgettable;
32
use Surfnet\StepupMiddleware\CommandHandlingBundle\SensitiveData\RightToObtainDataInterface;
33
use Surfnet\StepupMiddleware\CommandHandlingBundle\SensitiveData\SensitiveData;
34
35
final class YubikeySecondFactorBootstrappedEvent extends IdentityEvent implements
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class YubikeySecondFactorBootstrappedEvent
Loading history...
36
    Forgettable,
37
    RightToObtainDataInterface
38
{
39
    /**
40
     * @var string[]
41
     */
42
    private array $allowlist = [
43
        'identity_id',
44
        'name_id',
45
        'identity_institution',
46
        'preferred_locale',
47
        'second_factor_id',
48
        'second_factor_identifier',
49
        'second_factor_type',
50
        'email',
51
        'common_name',
52
    ];
53
54
    /**
55
     * @var Institution
56
     */
57
    public Institution $institution;
58
59
    public function __construct(
60
        IdentityId $identityId,
61
        public NameId $nameId,
62
        Institution $institution,
63
        public CommonName $commonName,
64
        public Email $email,
65
        public Locale $preferredLocale,
66
        public SecondFactorId $secondFactorId,
67
        public YubikeyPublicId $yubikeyPublicId,
68
    ) {
69
        parent::__construct($identityId, $institution);
70
        $this->institution = $institution;
71
    }
72
73
    public function getAuditLogMetadata(): Metadata
74
    {
75
        $metadata = new Metadata();
76
        $metadata->identityId = $this->identityId;
77
        $metadata->identityInstitution = $this->identityInstitution;
78
        $metadata->secondFactorId = $this->secondFactorId;
79
        $metadata->secondFactorType = new SecondFactorType('yubikey');
80
        $metadata->secondFactorIdentifier = $this->yubikeyPublicId;
81
82
        return $metadata;
83
    }
84
85
    /**
86
     * The data ending up in the event_stream, be careful not to include sensitive data here!
87
     *
88
     * @return array<string, mixed>
89
     */
90
    public function serialize(): array
91
    {
92
        return [
93
            'identity_id' => (string)$this->identityId,
94
            'name_id' => (string)$this->nameId,
95
            'identity_institution' => (string)$this->identityInstitution,
96
            'preferred_locale' => (string)$this->preferredLocale,
97
            'second_factor_id' => (string)$this->secondFactorId,
98
        ];
99
    }
100
101
    public static function deserialize(array $data): self
102
    {
103
        return new self(
104
            new IdentityId($data['identity_id']),
105
            new NameId($data['name_id']),
106
            new Institution($data['identity_institution']),
107
            CommonName::unknown(),
108
            Email::unknown(),
109
            new Locale($data['preferred_locale']),
110
            new SecondFactorId($data['second_factor_id']),
111
            YubikeyPublicId::unknown(),
112
        );
113
    }
114
115
    public function getSensitiveData(): SensitiveData
116
    {
117
        return (new SensitiveData)
118
            ->withCommonName($this->commonName)
119
            ->withEmail($this->email)
120
            ->withSecondFactorIdentifier($this->yubikeyPublicId, new SecondFactorType('yubikey'));
121
    }
122
123
    public function setSensitiveData(SensitiveData $sensitiveData): void
124
    {
125
        $this->email = $sensitiveData->getEmail();
126
        $this->commonName = $sensitiveData->getCommonName();
127
        $yubikeyPublicId = $sensitiveData->getSecondFactorIdentifier();
128
        assert($yubikeyPublicId instanceof YubikeyPublicId);
129
        $this->yubikeyPublicId = $yubikeyPublicId;
130
    }
131
132
    public function obtainUserData(): array
133
    {
134
        $serializedPublicUserData = $this->serialize();
135
        $serializedSensitiveUserData = $this->getSensitiveData()->serialize();
136
        return array_merge($serializedPublicUserData, $serializedSensitiveUserData);
137
    }
138
139
    /**
140
     * @return string[]
141
     */
142
    public function getAllowlist(): array
143
    {
144
        return $this->allowlist;
145
    }
146
}
147