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

AuthTimeChecker   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 33
ccs 12
cts 12
cp 1
rs 10
c 1
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A supportedClaim() 0 3 1
A __construct() 0 4 1
A checkClaim() 0 8 3
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