Completed
Pull Request — develop (#223)
by
unknown
12:46 queued 04:45
created

serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
/**
4
 * Copyright 2018 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\DateTime\DateTime;
22
use Surfnet\Stepup\Identity\AuditLog\Metadata;
23
use Surfnet\Stepup\Identity\Value\CommonName;
24
use Surfnet\Stepup\Identity\Value\Email;
25
use Surfnet\Stepup\Identity\Value\IdentityId;
26
use Surfnet\Stepup\Identity\Value\Institution;
27
use Surfnet\Stepup\Identity\Value\Locale;
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\SensitiveData;
33
34 View Code Duplication
class YubikeyPossessionProvenAndVerifiedEvent extends IdentityEvent implements Forgettable, PossessionProvenAndVerified
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
{
36
    /**
37
     * @var \Surfnet\Stepup\Identity\Value\SecondFactorId
38
     */
39
    public $secondFactorId;
40
41
    /**
42
     * The Yubikey's public ID.
43
     *
44
     * @var \Surfnet\Stepup\Identity\Value\YubikeyPublicId
45
     */
46
    public $yubikeyPublicId;
47
48
    /**
49
     * @var \Surfnet\Stepup\Identity\Value\CommonName
50
     */
51
    public $commonName;
52
53
    /**
54
     * @var \Surfnet\Stepup\Identity\Value\Email
55
     */
56
    public $email;
57
58
    /**
59
     * @var \Surfnet\Stepup\Identity\Value\Locale Eg. "en_GB"
60
     */
61
    public $preferredLocale;
62
63
    /**
64
     * @var \Surfnet\Stepup\DateTime\DateTime
65
     */
66
    public $registrationRequestedAt;
67
68
    /**
69
     * @var string
70
     */
71
    public $registrationCode;
72
73
    /**
74
     * @param IdentityId              $identityId
75
     * @param Institution             $institution
76
     * @param SecondFactorId          $secondFactorId
77
     * @param YubikeyPublicId         $yubikeyPublicId
78
     * @param CommonName              $commonName
79
     * @param Email                   $email
80
     * @param Locale                  $locale
81
     * @param DateTime                $registrationRequestedAt
82
     * @param string                  $registrationCode
83
     */
84
    public function __construct(
85
        IdentityId $identityId,
86
        Institution $institution,
87
        SecondFactorId $secondFactorId,
88
        YubikeyPublicId $yubikeyPublicId,
89
        CommonName $commonName,
90
        Email $email,
91
        Locale $locale,
92
        DateTime $registrationRequestedAt,
93
        $registrationCode
94
    ) {
95
        parent::__construct($identityId, $institution);
96
97
        $this->secondFactorId            = $secondFactorId;
98
        $this->yubikeyPublicId           = $yubikeyPublicId;
99
        $this->commonName                = $commonName;
100
        $this->email                     = $email;
101
        $this->preferredLocale           = $locale;
102
        $this->registrationRequestedAt   = $registrationRequestedAt;
103
        $this->registrationCode          = $registrationCode;
104
    }
105
106
    public function getAuditLogMetadata()
107
    {
108
        $metadata                         = new Metadata();
109
        $metadata->identityId             = $this->identityId;
110
        $metadata->identityInstitution    = $this->identityInstitution;
111
        $metadata->secondFactorId         = $this->secondFactorId;
112
        $metadata->secondFactorType       = new SecondFactorType('yubikey');
113
        $metadata->secondFactorIdentifier = $this->yubikeyPublicId;
114
115
        return $metadata;
116
    }
117
118
    public static function deserialize(array $data)
119
    {
120
        // BC compatibility for test-environment only (2.8.0, fixed in 2.8.1)
121
        if (!isset($data['preferred_locale'])) {
122
            $data['preferred_locale'] = 'en_GB';
123
        }
124
125
        return new self(
126
            new IdentityId($data['identity_id']),
127
            new Institution($data['identity_institution']),
128
            new SecondFactorId($data['second_factor_id']),
129
            YubikeyPublicId::unknown(),
130
            CommonName::unknown(),
131
            Email::unknown(),
132
            new Locale($data['preferred_locale']),
133
            DateTime::fromString($data['registration_requested_at']),
134
            (string) $data['registration_code']
135
        );
136
    }
137
138
    public function serialize()
139
    {
140
        return [
141
            'identity_id'                 => (string) $this->identityId,
142
            'identity_institution'        => (string) $this->identityInstitution,
143
            'second_factor_id'            => (string) $this->secondFactorId,
144
            'registration_requested_at'   => (string) $this->registrationRequestedAt,
145
            'registration_code'           => $this->registrationCode,
146
            'preferred_locale'            => (string) $this->preferredLocale,
147
        ];
148
    }
149
150
    public function getSensitiveData()
151
    {
152
        return (new SensitiveData)
153
            ->withCommonName($this->commonName)
154
            ->withEmail($this->email)
155
            ->withSecondFactorIdentifier($this->yubikeyPublicId, new SecondFactorType('yubikey'));
156
    }
157
158
    public function setSensitiveData(SensitiveData $sensitiveData)
159
    {
160
        $this->yubikeyPublicId = $sensitiveData->getSecondFactorIdentifier();
161
        $this->email = $sensitiveData->getEmail();
162
        $this->commonName = $sensitiveData->getCommonName();
163
    }
164
}
165