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\StepupBundle\Value; |
20
|
|
|
|
21
|
|
|
use Surfnet\StepupBundle\Exception\InvalidArgumentException; |
22
|
|
|
|
23
|
|
|
final class YubikeyPublicId |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $value; |
29
|
|
|
|
30
|
|
|
public static function fromOtp(YubikeyOtp $otp) |
31
|
|
|
{ |
32
|
|
|
$hexadecimalId = strtr($otp->publicId, 'cbdefghijklnrtuv', '0123456789abcdef'); |
33
|
|
|
$gmpId = gmp_init($hexadecimalId, 16); |
34
|
|
|
|
35
|
|
|
return new self(sprintf('%08s', gmp_strval($gmpId, 10))); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function __construct($value) |
39
|
|
|
{ |
40
|
|
|
if (!is_string($value)) { |
41
|
|
|
throw InvalidArgumentException::invalidType('string', 'value', $value); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// Numeric IDs must be left-padded with zeroes until eight characters. Longer IDs may not be padded. |
45
|
|
|
if (!preg_match('~^\d+$~', $value)) { |
46
|
|
|
throw new InvalidArgumentException('Given Yubikey public ID is not a string of digits'); |
47
|
|
|
} |
48
|
|
|
if ($value !== sprintf('%08s', ltrim($value, '0'))) { |
49
|
|
|
throw new InvalidArgumentException( |
50
|
|
|
'Given Yubikey public ID is longer than 8 digits, yet left-padded with zeroes' |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// Yubikey public IDs, in their (mod)hex format, may be up to sixteen characters in length. Thus, this is their |
55
|
|
|
// maximum value. |
56
|
|
|
if (gmp_cmp(gmp_init($value, 10), gmp_init('ffffffffffffffff', 16)) > 0) { |
57
|
|
|
throw new InvalidArgumentException('Given Yubikey public ID is larger than 0xffffffffffffffff'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->value = $value; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getYubikeyPublicId() |
64
|
|
|
{ |
65
|
|
|
return $this->value; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function equals(YubikeyPublicId $other) |
69
|
|
|
{ |
70
|
|
|
return $this->value === $other->value; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function jsonSerialize() |
74
|
|
|
{ |
75
|
|
|
return $this->value; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function __toString() |
79
|
|
|
{ |
80
|
|
|
return $this->value; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|