1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2022 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\GatewayBundle\Sso2fa\ValueObject; |
20
|
|
|
|
21
|
|
|
use DateTime; |
22
|
|
|
use function strtolower; |
23
|
|
|
use function strtotime; |
24
|
|
|
|
25
|
|
|
class CookieValue implements CookieValueInterface |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
private $tokenId; |
|
|
|
|
28
|
|
|
private $identityId; |
|
|
|
|
29
|
|
|
private $loa; |
|
|
|
|
30
|
|
|
private $authenticationTime; |
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
|
|
|
|
33
|
|
|
* The cookie value consists of: |
34
|
|
|
* - Token used: SecondFactorId from SecondFactor |
35
|
|
|
* - Identifier: IdentityId from SecondFactor |
36
|
|
|
* - The resolved LoA: LoA (resolved using Loa resolution service) |
37
|
|
|
* - Authentication time (Atom formatted date time string) |
38
|
|
|
*/ |
|
|
|
|
39
|
|
|
public static function from(string $identityId, string $secondFactorId, float $loa): self |
40
|
|
|
{ |
41
|
|
|
$cookieValue = new self; |
42
|
|
|
$cookieValue->tokenId = $secondFactorId; |
43
|
|
|
$cookieValue->identityId = $identityId; |
44
|
|
|
$cookieValue->loa = $loa; |
45
|
|
|
$dateTime = new DateTime(); |
46
|
|
|
$cookieValue->authenticationTime = $dateTime->format(DATE_ATOM); |
47
|
|
|
return $cookieValue; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public static function deserialize(string $serializedData): CookieValueInterface |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$data = json_decode($serializedData, true); |
53
|
|
|
$cookieValue = new self; |
54
|
|
|
$cookieValue->tokenId = $data['tokenId']; |
55
|
|
|
$cookieValue->identityId = $data['identityId']; |
56
|
|
|
$cookieValue->loa = (float) $data['loa']; |
57
|
|
|
$cookieValue->authenticationTime = $data['authenticationTime']; |
58
|
|
|
|
59
|
|
|
return $cookieValue; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function serialize(): string |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
return json_encode([ |
|
|
|
|
65
|
|
|
'tokenId' => $this->tokenId, |
66
|
|
|
'identityId' => $this->identityId, |
67
|
|
|
'loa' => $this->loa, |
68
|
|
|
'authenticationTime' => $this->authenticationTime, |
69
|
|
|
]); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function meetsRequiredLoa(float $requiredLoa): bool |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
return $this->loa >= $requiredLoa; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getLoa(): float |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
return $this->loa; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getIdentityId(): string |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
return $this->identityId; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function secondFactorId(): string |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
return $this->tokenId; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function issuedTo(string $identityNameId): bool |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
return strtolower($identityNameId) === strtolower($this->identityId); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function authenticationTime(): int |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
return strtotime($this->authenticationTime); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|