Passed
Push — master ( 106006...c1ddb5 )
by Thomas Mauro
06:38 queued 11s
created

AuthTimeChecker::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TMV\OpenIdClient\ClaimChecker;
6
7
use function is_int;
8
use Jose\Component\Checker\ClaimChecker;
9
use Jose\Component\Checker\InvalidClaimException;
10
use function time;
11
12
final class AuthTimeChecker implements ClaimChecker
13
{
14
    private const CLAIM_NAME = 'auth_time';
15
16
    /** @var int */
17
    private $maxAge;
18
19
    /** @var int */
20
    private $allowedTimeDrift;
21
22 28
    public function __construct(int $maxAge, int $allowedTimeDrift = 0)
23
    {
24 28
        $this->maxAge = $maxAge;
25 28
        $this->allowedTimeDrift = $allowedTimeDrift;
26 28
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 14
    public function checkClaim($value): void
32
    {
33 14
        if (! is_int($value)) {
34 1
            throw new InvalidClaimException('"auth_time" must be an integer.', self::CLAIM_NAME, $value);
35
        }
36
37 13
        if ($value + $this->maxAge < time() - $this->allowedTimeDrift) {
38 1
            throw new InvalidClaimException('Too much time has elapsed since the last End-User authentication.', self::CLAIM_NAME, $value);
39
        }
40 12
    }
41
42 24
    public function supportedClaim(): string
43
    {
44 24
        return self::CLAIM_NAME;
45
    }
46
}
47