Failed Conditions
Pull Request — master (#308)
by Gareth
04:37
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
    private $tolerance;
20
21
    public function __construct($tolerance = 0)
22
    {
23
        Assertion::greaterOrEqualThan($tolerance, 0, 'Tolerance value must be >=0');
24
        $this->tolerance = (int) $tolerance;
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function checkClaim(JWTInterface $jwt)
31
    {
32
        if (!$jwt->hasClaim('nbf')) {
33
            return [];
34
        }
35
36
        $nbf = (int) $jwt->getClaim('nbf') - $this->tolerance;
37
        Assertion::lessOrEqualThan($nbf, time(), 'The JWT can not be used yet.');
38
39
        return ['nbf'];
40
    }
41
}
42