Failed Conditions
Push — master ( 7c4f18...6ad094 )
by Florent
09:39
created

IssuedAtChecker::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace Jose\Component\Checker;
15
16
/**
17
 * Class IssuedAtChecker.
18
 */
19
final class IssuedAtChecker implements ClaimChecker
20
{
21
    private const CLAIM_NAME = 'iat';
22
23
    /**
24
     * @var int
25
     */
26
    private $allowedTimeDrift;
27
28
    /**
29
     * ExpirationTimeChecker constructor.
30
     *
31
     * @param int $allowedTimeDrift
32
     */
33
    public function __construct(int $allowedTimeDrift = 0)
34
    {
35
        $this->allowedTimeDrift = $allowedTimeDrift;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function checkClaim($value)
42
    {
43
        if (!is_int($value)) {
44
            throw new InvalidClaimException('The claim "iat" must be an integer.', self::CLAIM_NAME, $value);
45
        }
46
        if (time() < $value - $this->allowedTimeDrift) {
47
            throw new InvalidClaimException('The JWT is issued in the future.', self::CLAIM_NAME, $value);
48
        }
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function supportedClaim(): string
55
    {
56
        return self::CLAIM_NAME;
57
    }
58
}
59