YubikeyPublicId   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 56
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getValue() 0 3 1
A unknown() 0 3 1
A __toString() 0 3 1
A __construct() 0 25 5
A jsonSerialize() 0 3 1
A equals() 0 3 2
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\Value;
20
21
use Surfnet\Stepup\Exception\InvalidArgumentException;
22
23
final class YubikeyPublicId implements SecondFactorIdentifier
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class YubikeyPublicId
Loading history...
24
{
25
    public const UNKNOWN = '—';
26
27
    private string $value;
28
29
    public static function unknown(): static
30
    {
31
        return new self(self::UNKNOWN);
32
    }
33
34
    public function __construct(string $value)
35
    {
36
        if ($value === self::UNKNOWN) {
37
            $this->value = $value;
38
            return;
39
        }
40
41
        // Numeric IDs must be left-padded with zeroes until eight characters. Longer IDs, up to twenty characters, may
42
        // not be padded.
43
        if (!preg_match('~^\d{8,20}$~', $value)) {
44
            throw new InvalidArgumentException('Given Yubikey public ID is not a string of 8 to 20 digits');
45
        }
46
        if ($value !== sprintf('%08s', ltrim($value, '0'))) {
47
            throw new InvalidArgumentException(
48
                'Given Yubikey public ID is longer than 8 digits, yet left-padded with zeroes',
49
            );
50
        }
51
52
        // Yubikey public IDs, in their (mod)hex format, may be up to sixteen characters in length. Thus, this is their
53
        // maximum value.
54
        if (gmp_cmp(gmp_init($value, 10), gmp_init('ffffffffffffffff', 16)) > 0) {
55
            throw new InvalidArgumentException('Given Yubikey public ID is larger than 0xffffffffffffffff');
56
        }
57
58
        $this->value = $value;
59
    }
60
61
    public function getValue(): string
62
    {
63
        return $this->value;
64
    }
65
66
    public function __toString(): string
67
    {
68
        return $this->value;
69
    }
70
71
    public function equals(SecondFactorIdentifier $other): bool
72
    {
73
        return $other instanceof self && $this->value === $other->value;
74
    }
75
76
    public function jsonSerialize(): string
77
    {
78
        return $this->value;
79
    }
80
}
81