Completed
Push — master ( 5467e4...183ea4 )
by Boy
05:06 queued 01:02
created

YubikeyPublicId::getValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 1
cc 1
eloc 2
nc 1
nop 0
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
 */
18
19
namespace Surfnet\Stepup\Identity\Value;
20
21
use Surfnet\Stepup\Exception\InvalidArgumentException;
22
23
final class YubikeyPublicId implements SecondFactorIdentifier
24
{
25
    const UNKNOWN = '—';
26
27
    /**
28
     * @var string
29
     */
30
    private $value;
31
32
    public static function unknown()
33
    {
34
        return new self(self::UNKNOWN);
35
    }
36
37
    public function __construct($value)
38
    {
39
        if ($value === self::UNKNOWN) {
40
            $this->value = $value;
41
            return;
42
        }
43
44
        if (!is_string($value)) {
45
            throw InvalidArgumentException::invalidType('string', 'value', $value);
46
        }
47
48
        // Numeric IDs must be left-padded with zeroes until eight characters. Longer IDs, up to twenty characters, may
49
        // not be padded.
50
        if (!preg_match('~^[0-9]{8,20}$~', $value)) {
51
            throw new InvalidArgumentException('Given Yubikey public ID is not a string of 8 to 20 digits');
52
        }
53
        if ($value !== sprintf('%08s', ltrim($value, '0'))) {
54
            throw new InvalidArgumentException(
55
                'Given Yubikey public ID is longer than 8 digits, yet left-padded with zeroes'
56
            );
57
        }
58
59
        // Yubikey public IDs, in their (mod)hex format, may be up to sixteen characters in length. Thus, this is their
60
        // maximum value.
61
        if (gmp_cmp(gmp_init($value, 10), gmp_init('ffffffffffffffff', 16)) > 0) {
62
            throw new InvalidArgumentException('Given Yubikey public ID is larger than 0xffffffffffffffff');
63
        }
64
65
        $this->value = $value;
66
    }
67
68
    public function getValue()
69
    {
70
        return $this->value;
71
    }
72
73
    public function __toString()
74
    {
75
        return $this->value;
76
    }
77
78
    public function equals(SecondFactorIdentifier $other)
79
    {
80
        return $other instanceof self && $this->value === $other->value;
81
    }
82
83
    public function jsonSerialize()
84
    {
85
        return $this->value;
86
    }
87
}
88