Failed Conditions
Push — master ( 8c9991...101c5c )
by Florent
01:56
created

NotBeforeChecker::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2017 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
namespace Jose\Checker;
13
14
use Assert\Assertion;
15
use Jose\Object\JWTInterface;
16
17
class NotBeforeChecker implements ClaimCheckerInterface
18
{
19
    /**
20
     * @var int
21
     */
22
    private $tolerance;
23
24
    /**
25
     * @param int $tolerance
26
     */
27
    public function __construct($tolerance = 0)
28
    {
29
        Assertion::greaterOrEqualThan($tolerance, 0, 'Tolerance value must be >=0');
30
        $this->tolerance = (int) $tolerance;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function checkClaim(JWTInterface $jwt)
37
    {
38
        if (!$jwt->hasClaim('nbf')) {
39
            return [];
40
        }
41
42
        $nbf = (int) $jwt->getClaim('nbf') - $this->tolerance;
43
        Assertion::lessOrEqualThan($nbf, time(), 'The JWT can not be used yet.');
44
45
        return ['nbf'];
46
    }
47
}
48