1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2023 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\DateTime; |
20
|
|
|
|
21
|
|
|
use DateTime as CoreDateTime; |
22
|
|
|
use Surfnet\StepupBundle\DateTime\DateTime; |
23
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Sso2fa\Exception\InvalidAuthenticationTimeException; |
24
|
|
|
use Surfnet\StepupGateway\GatewayBundle\Sso2fa\ValueObject\CookieValueInterface; |
25
|
|
|
use TypeError; |
26
|
|
|
|
27
|
|
|
class ExpirationHelper implements ExpirationHelperInterface |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
/** |
|
|
|
|
30
|
|
|
* @var CoreDateTime |
31
|
|
|
*/ |
32
|
|
|
private $now; |
|
|
|
|
33
|
|
|
|
34
|
|
|
/* |
35
|
|
|
* The period in seconds that we still acknowledge the |
36
|
|
|
* cookie even tho the expiration was reached. This accounts |
37
|
|
|
* for server time/sync differences that may occur. |
38
|
|
|
*/ |
39
|
|
|
private $gracePeriod; |
|
|
|
|
40
|
|
|
|
41
|
|
|
/* |
42
|
|
|
* The SSO on 2FA cookie lifetime in seconds |
43
|
|
|
* |
44
|
|
|
* See: config/legacy/parameters.yaml sso_cookie_lifetime |
45
|
|
|
*/ |
46
|
|
|
private $cookieLifetime; |
|
|
|
|
47
|
|
|
|
48
|
|
|
public function __construct(int $cookieLifetime, int $gracePeriod, CoreDateTime $now = null) |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$this->cookieLifetime = $cookieLifetime; |
51
|
|
|
$this->gracePeriod = $gracePeriod; |
52
|
|
|
if (!$now) { |
53
|
|
|
$now = DateTime::now(); |
54
|
|
|
} |
55
|
|
|
$this->now = $now; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function isExpired(CookieValueInterface $cookieValue): bool |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
try { |
61
|
|
|
$authenticationTimestamp = $cookieValue->authenticationTime(); |
62
|
|
|
} catch (TypeError $error) { |
63
|
|
|
throw new InvalidAuthenticationTimeException( |
64
|
|
|
'The authentication time contained a non-int value', |
65
|
|
|
0, |
66
|
|
|
$error |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
if ($authenticationTimestamp < 0) { |
70
|
|
|
throw new InvalidAuthenticationTimeException( |
71
|
|
|
'The authentication time is from before the Unix timestamp epoch' |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
if ($authenticationTimestamp > $this->now->getTimestamp()) { |
75
|
|
|
throw new InvalidAuthenticationTimeException( |
76
|
|
|
'The authentication time is from the future, which indicates the clock settings ' . |
77
|
|
|
'are incorrect, or the time in the cookie value was tampered with.' |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$expirationTimestamp = $authenticationTimestamp + $this->cookieLifetime + $this->gracePeriod; |
82
|
|
|
$currentTimestamp = $this->now->getTimestamp(); |
83
|
|
|
// Is the current time greater than the expiration time? |
84
|
|
|
return $currentTimestamp > $expirationTimestamp; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|