Passed
Pull Request — develop (#295)
by Peter
04:30
created

ExpirationHelper::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
nc 2
nop 3
dl 0
loc 8
rs 10
c 1
b 0
f 0
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
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class ExpirationHelper
Loading history...
28
{
29
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
30
     * @var CoreDateTime
31
     */
32
    private $now;
0 ignored issues
show
Coding Style introduced by
Private member variable "now" must be prefixed with an underscore
Loading history...
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;
0 ignored issues
show
Coding Style introduced by
Private member variable "gracePeriod" must be prefixed with an underscore
Loading history...
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;
0 ignored issues
show
Coding Style introduced by
Private member variable "cookieLifetime" must be prefixed with an underscore
Loading history...
47
48
    public function __construct(int $cookieLifetime, int $gracePeriod, CoreDateTime $now = null)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function isExpired()
Loading history...
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