1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015 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\StepupGateway\U2fVerificationBundle\Entity; |
20
|
|
|
|
21
|
|
|
use DateTime; |
22
|
|
|
use Doctrine\ORM\Mapping as ORM; |
23
|
|
|
use Surfnet\StepupGateway\U2fVerificationBundle\Exception\DomainException; |
24
|
|
|
use Surfnet\StepupGateway\U2fVerificationBundle\Exception\InvalidArgumentException; |
25
|
|
|
use Surfnet\StepupGateway\U2fVerificationBundle\Exception\SignCounterTooLowException; |
26
|
|
|
use Surfnet\StepupGateway\U2fVerificationBundle\Value\KeyHandle; |
27
|
|
|
use Surfnet\StepupGateway\U2fVerificationBundle\Value\PublicKey; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @ORM\Entity(repositoryClass="Surfnet\StepupGateway\U2fVerificationBundle\Repository\RegistrationRepository") |
31
|
|
|
*/ |
32
|
|
|
class Registration |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @ORM\Id |
36
|
|
|
* @ORM\GeneratedValue(strategy="NONE") |
37
|
|
|
* @ORM\Column(type="u2f_key_handle") |
38
|
|
|
* |
39
|
|
|
* @var KeyHandle |
40
|
|
|
*/ |
41
|
|
|
private $keyHandle; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @ORM\Column(type="u2f_public_key") |
45
|
|
|
* |
46
|
|
|
* @var PublicKey |
47
|
|
|
*/ |
48
|
|
|
private $publicKey; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @ORM\Column(type="integer") |
52
|
|
|
* |
53
|
|
|
* @var int |
54
|
|
|
*/ |
55
|
|
|
private $signCounter; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @ORM\Column(type="datetime") |
59
|
|
|
* |
60
|
|
|
* @var DateTime |
61
|
|
|
*/ |
62
|
|
|
private $lastUsed; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param KeyHandle $keyHandle |
66
|
|
|
* @param PublicKey $publicKey |
67
|
|
|
*/ |
68
|
|
|
public function __construct(KeyHandle $keyHandle, PublicKey $publicKey) |
69
|
|
|
{ |
70
|
|
|
$this->keyHandle = $keyHandle; |
71
|
|
|
$this->publicKey = $publicKey; |
72
|
|
|
$this->signCounter = 0; |
73
|
|
|
$this->lastUsed = new DateTime(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param int $newSignCounter |
78
|
|
|
* @throws DomainException |
79
|
|
|
*/ |
80
|
|
|
public function authenticationWasVerified($newSignCounter) |
81
|
|
|
{ |
82
|
|
|
if (!is_int($newSignCounter)) { |
83
|
|
|
throw InvalidArgumentException::invalidType('int', 'newSignCounter', $newSignCounter); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($newSignCounter <= $this->signCounter) { |
87
|
|
|
throw new SignCounterTooLowException( |
88
|
|
|
sprintf( |
89
|
|
|
'An authentication matching this registration was verified, but the sign counter "%d" was lower ' . |
90
|
|
|
'than or equal to the last known sign counter "%d". This registration must be invalidated.', |
91
|
|
|
$newSignCounter, |
92
|
|
|
$this->signCounter |
93
|
|
|
) |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->signCounter = $newSignCounter; |
98
|
|
|
$this->lastUsed = new DateTime(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return KeyHandle |
103
|
|
|
*/ |
104
|
|
|
public function getKeyHandle() |
105
|
|
|
{ |
106
|
|
|
return $this->keyHandle; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return PublicKey |
111
|
|
|
*/ |
112
|
|
|
public function getPublicKey() |
113
|
|
|
{ |
114
|
|
|
return $this->publicKey; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return int |
119
|
|
|
*/ |
120
|
|
|
public function getSignCounter() |
121
|
|
|
{ |
122
|
|
|
return $this->signCounter; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|