serialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
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
 */
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\RecoveryTokenId;
28
use Surfnet\Stepup\Identity\Value\RecoveryTokenIdentifier;
29
use Surfnet\Stepup\Identity\Value\RecoveryTokenType;
30
use Surfnet\Stepup\Identity\Value\SafeStore;
31
use Surfnet\StepupMiddleware\CommandHandlingBundle\SensitiveData\Forgettable;
32
use Surfnet\StepupMiddleware\CommandHandlingBundle\SensitiveData\RightToObtainDataInterface;
33
use Surfnet\StepupMiddleware\CommandHandlingBundle\SensitiveData\SensitiveData;
34
35
/**
36
 * SafeStoreSecretRecoveryTokenPossessionPromisedEvent
37
 *
38
 * This event is recorded when the user promised it stored the password
39
 * (displayed only once to the user) in a safe location.
40
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
41
class SafeStoreSecretRecoveryTokenPossessionPromisedEvent extends IdentityEvent implements
42
    Forgettable,
43
    RightToObtainDataInterface
44
{
45
    /**
46
     * @var string[]
47
     */
48
    private array $allowlist = [
49
        'identity_id',
50
        'identity_institution',
51
        'recovery_token_id',
52
        'preferred_locale',
53
        'email',
54
        'common_name',
55
    ];
56
57
    public function __construct(
58
        IdentityId $identityId,
59
        Institution $identityInstitution,
60
        public RecoveryTokenId $recoveryTokenId,
61
        public RecoveryTokenIdentifier $secret,
62
        public CommonName $commonName,
63
        public Email $email,
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
        // In the audit log we do not show the secret (hashed)
75
        $metadata->recoveryTokenId = new RecoveryTokenId((string) SafeStore::hidden());
76
        $metadata->recoveryTokenType = RecoveryTokenType::safeStore();
77
        return $metadata;
78
    }
79
80
    public static function deserialize(array $data): self
81
    {
82
        return new self(
83
            new IdentityId($data['identity_id']),
84
            new Institution($data['identity_institution']),
85
            new RecoveryTokenId($data['recovery_token_id']),
86
            SafeStore::unknown(),
87
            CommonName::unknown(),
88
            Email::unknown(),
89
            new Locale($data['preferred_locale']),
90
        );
91
    }
92
93
    /**
94
     * The data ending up in the event_stream, be careful not to include sensitive data here!
95
     *
96
     * @return array<string, mixed>
97
     */
98
    public function serialize(): array
99
    {
100
        return [
101
            'identity_id' => (string)$this->identityId,
102
            'identity_institution' => (string)$this->identityInstitution,
103
            'recovery_token_id' => (string)$this->recoveryTokenId,
104
            'recovery_token_type' => RecoveryTokenType::TYPE_SAFE_STORE,
105
            'preferred_locale' => (string)$this->preferredLocale,
106
        ];
107
    }
108
109
    public function getSensitiveData(): SensitiveData
110
    {
111
        return (new SensitiveData)
112
            ->withCommonName($this->commonName)
113
            ->withEmail($this->email)
114
            ->withRecoveryTokenSecret($this->secret, RecoveryTokenType::safeStore());
115
    }
116
117
    public function setSensitiveData(SensitiveData $sensitiveData): void
118
    {
119
        $secret = $sensitiveData->getRecoveryTokenIdentifier();
120
        if ($secret === null) {
121
            $secret = SafeStore::unknown();
122
        }
123
124
        $this->email = $sensitiveData->getEmail();
125
        $this->commonName = $sensitiveData->getCommonName();
126
        $this->secret = $secret;
127
    }
128
129
    public function obtainUserData(): array
130
    {
131
        $serializedPublicUserData = $this->serialize();
132
        $serializedSensitiveUserData = $this->getSensitiveData()->serialize();
133
        return array_merge($serializedPublicUserData, $serializedSensitiveUserData);
134
    }
135
136
    /**
137
     * @return string[]
138
     */
139
    public function getAllowlist(): array
140
    {
141
        return $this->allowlist;
142
    }
143
}
144