obtainUserData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright 2022 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\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\PhoneNumber;
28
use Surfnet\Stepup\Identity\Value\RecoveryTokenId;
29
use Surfnet\Stepup\Identity\Value\RecoveryTokenType;
30
use Surfnet\StepupMiddleware\CommandHandlingBundle\SensitiveData\Forgettable;
31
use Surfnet\StepupMiddleware\CommandHandlingBundle\SensitiveData\RightToObtainDataInterface;
32
use Surfnet\StepupMiddleware\CommandHandlingBundle\SensitiveData\SensitiveData;
33
34
/**
35
 * PhoneRecoveryTokenPossessionProvenEvent
36
 *
37
 * This event is recorded when the user prove possession of a Phone (SMS)
38
 * based Recovery Token.
39
 */
40
class PhoneRecoveryTokenPossessionProvenEvent extends IdentityEvent implements Forgettable, RightToObtainDataInterface
41
{
42
    /**
43
     * @var string[]
44
     */
45
    private array $allowlist = [
46
        'identity_id',
47
        'identity_institution',
48
        'recovery_token_id',
49
        'preferred_locale',
50
        'email',
51
        'common_name',
52
    ];
53
54
    public function __construct(
55
        IdentityId $identityId,
56
        Institution $identityInstitution,
57
        public RecoveryTokenId $recoveryTokenId,
58
        public PhoneNumber $phoneNumber,
59
        public CommonName $commonName,
60
        public Email $email,
61
        /**
62
         * @var Locale Eg. "en_GB"
63
         */
64
        public Locale $preferredLocale,
65
    ) {
66
        parent::__construct($identityId, $identityInstitution);
67
    }
68
69
    public function getAuditLogMetadata(): Metadata
70
    {
71
        $metadata = new Metadata();
72
        $metadata->identityId = $this->identityId;
73
        $metadata->identityInstitution = $this->identityInstitution;
74
        $metadata->recoveryTokenId = new RecoveryTokenId((string) $this->phoneNumber);
75
        $metadata->recoveryTokenType = RecoveryTokenType::sms();
76
        return $metadata;
77
    }
78
79
    public static function deserialize(array $data): self
80
    {
81
        return new self(
82
            new IdentityId($data['identity_id']),
83
            new Institution($data['identity_institution']),
84
            new RecoveryTokenId($data['recovery_token_id']),
85
            PhoneNumber::unknown(),
86
            CommonName::unknown(),
87
            Email::unknown(),
88
            new Locale($data['preferred_locale']),
89
        );
90
    }
91
92
    /**
93
     * The data ending up in the event_stream, be careful not to include sensitive data here!
94
     *
95
     * @return array<string, mixed>
96
     */
97
    public function serialize(): array
98
    {
99
        return [
100
            'identity_id' => (string)$this->identityId,
101
            'identity_institution' => (string)$this->identityInstitution,
102
            'recovery_token_id' => (string)$this->recoveryTokenId,
103
            'recovery_token_type' => RecoveryTokenType::TYPE_SMS,
104
            'preferred_locale' => (string)$this->preferredLocale,
105
        ];
106
    }
107
108
    public function getSensitiveData(): SensitiveData
109
    {
110
        return (new SensitiveData)
0 ignored issues
show
Coding Style introduced by
Parentheses must be used when instantiating a new class
Loading history...
111
            ->withCommonName($this->commonName)
112
            ->withEmail($this->email)
113
            ->withRecoveryTokenSecret($this->phoneNumber, RecoveryTokenType::sms());
114
    }
115
116
    public function setSensitiveData(SensitiveData $sensitiveData): void
117
    {
118
        $this->email = $sensitiveData->getEmail();
119
        $this->commonName = $sensitiveData->getCommonName();
120
        $phoneNumber = $sensitiveData->getRecoveryTokenIdentifier();
121
        assert($phoneNumber instanceof PhoneNumber);
122
        $this->phoneNumber = $phoneNumber;
123
    }
124
125
    public function obtainUserData(): array
126
    {
127
        $serializedPublicUserData = $this->serialize();
128
        $serializedSensitiveUserData = $this->getSensitiveData()->serialize();
129
        return array_merge($serializedPublicUserData, $serializedSensitiveUserData);
130
    }
131
132
    /**
133
     * @return string[]
134
     */
135
    public function getAllowlist(): array
136
    {
137
        return $this->allowlist;
138
    }
139
}
140