ValidatesJWT::validateTimestamps()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 11
c 3
b 0
f 1
dl 0
loc 16
rs 9.2222
cc 6
nc 6
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the PHP-JWT package.
7
 *
8
 * (c) Jitendra Adhikari <[email protected]>
9
 *     <https://github.com/adhocore>
10
 *
11
 * Licensed under MIT license.
12
 */
13
14
namespace Ahc\Jwt;
15
16
/**
17
 * JSON Web Token (JWT) implementation in PHP7.
18
 *
19
 * @author   Jitendra Adhikari <[email protected]>
20
 * @license  MIT
21
 *
22
 * @link     https://github.com/adhocore/jwt
23
 */
24
trait ValidatesJWT
25
{
26
    /**
27
     * Throw up if input parameters invalid.
28
     *
29
     * @codeCoverageIgnore
30
     */
31
    protected function validateConfig($key, string $algo, int $maxAge, int $leeway)
32
    {
33
        if (empty($key)) {
34
            throw new JWTException('Signing key cannot be empty', static::ERROR_KEY_EMPTY);
0 ignored issues
show
Bug introduced by
The constant Ahc\Jwt\ValidatesJWT::ERROR_KEY_EMPTY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
35
        }
36
37
        if (!isset($this->algos[$algo])) {
38
            throw new JWTException('Unsupported algo ' . $algo, static::ERROR_ALGO_UNSUPPORTED);
0 ignored issues
show
Bug introduced by
The constant Ahc\Jwt\ValidatesJWT::ERROR_ALGO_UNSUPPORTED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
39
        }
40
41
        if ($maxAge < 1) {
42
            throw new JWTException('Invalid maxAge: Should be greater than 0', static::ERROR_INVALID_MAXAGE);
0 ignored issues
show
Bug introduced by
The constant Ahc\Jwt\ValidatesJWT::ERROR_INVALID_MAXAGE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
43
        }
44
45
        if ($leeway < 0 || $leeway > 120) {
46
            throw new JWTException('Invalid leeway: Should be between 0-120', static::ERROR_INVALID_LEEWAY);
0 ignored issues
show
Bug introduced by
The constant Ahc\Jwt\ValidatesJWT::ERROR_INVALID_LEEWAY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
47
        }
48
    }
49
50
    /**
51
     * Throw up if header invalid.
52
     */
53
    protected function validateHeader(array $header)
54
    {
55
        if (empty($header['alg'])) {
56
            throw new JWTException('Invalid token: Missing header algo', static::ERROR_ALGO_MISSING);
0 ignored issues
show
Bug introduced by
The constant Ahc\Jwt\ValidatesJWT::ERROR_ALGO_MISSING was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
57
        }
58
        if (empty($this->algos[$header['alg']])) {
59
            throw new JWTException('Invalid token: Unsupported header algo', static::ERROR_ALGO_UNSUPPORTED);
0 ignored issues
show
Bug introduced by
The constant Ahc\Jwt\ValidatesJWT::ERROR_ALGO_UNSUPPORTED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
60
        }
61
62
        $this->validateKid($header);
63
    }
64
65
    /**
66
     * Throw up if kid exists and invalid.
67
     */
68
    protected function validateKid(array $header)
69
    {
70
        if (!isset($header['kid'])) {
71
            return;
72
        }
73
        if (empty($this->keys[$header['kid']])) {
0 ignored issues
show
Bug introduced by
The property keys does not exist on Ahc\Jwt\ValidatesJWT. Did you mean key?
Loading history...
74
            throw new JWTException('Invalid token: Unknown key ID', static::ERROR_KID_UNKNOWN);
0 ignored issues
show
Bug introduced by
The constant Ahc\Jwt\ValidatesJWT::ERROR_KID_UNKNOWN was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
75
        }
76
77
        $this->key = $this->keys[$header['kid']];
0 ignored issues
show
Bug Best Practice introduced by
The property key does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
78
    }
79
80
    /**
81
     * Throw up if timestamp claims like iat, exp, nbf are invalid.
82
     */
83
    protected function validateTimestamps(array $payload)
84
    {
85
        $timestamp = $this->timestamp ?: \time();
86
        $checks    = [
87
            ['exp', $this->leeway /*          */ , static::ERROR_TOKEN_EXPIRED, 'Expired'],
0 ignored issues
show
Bug introduced by
The constant Ahc\Jwt\ValidatesJWT::ERROR_TOKEN_EXPIRED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
88
            ['iat', $this->maxAge - $this->leeway, static::ERROR_TOKEN_EXPIRED, 'Expired'],
89
            ['nbf', $this->maxAge - $this->leeway, static::ERROR_TOKEN_NOT_NOW, 'Not now'],
0 ignored issues
show
Bug introduced by
The constant Ahc\Jwt\ValidatesJWT::ERROR_TOKEN_NOT_NOW was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
90
        ];
91
92
        foreach ($checks as list($key, $offset, $code, $error)) {
93
            if (isset($payload[$key])) {
94
                $offset += $payload[$key];
95
                $fail    = $key === 'nbf' ? $timestamp <= $offset : $timestamp >= $offset;
96
97
                if ($fail) {
98
                    throw new JWTException('Invalid token: ' . $error, $code);
99
                }
100
            }
101
        }
102
    }
103
104
    /**
105
     * Throw up if key is not resource or file path to private key.
106
     */
107
    protected function validateKey()
108
    {
109
        if (\is_string($key = $this->key)) {
110
            if (\substr($key, 0, 7) !== 'file://') {
111
                $key = 'file://' . $key;
112
            }
113
114
            $this->key = \openssl_get_privatekey($key, $this->passphrase ?: '');
0 ignored issues
show
Bug Best Practice introduced by
The property key does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
115
        }
116
117
        if (!\is_resource($this->key)) {
118
            throw new JWTException('Invalid key: Should be resource of private key', static::ERROR_KEY_INVALID);
0 ignored issues
show
Bug introduced by
The constant Ahc\Jwt\ValidatesJWT::ERROR_KEY_INVALID was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
119
        }
120
    }
121
122
    /**
123
     * Throw up if last json_encode/decode was a failure.
124
     */
125
    protected function validateLastJson()
126
    {
127
        if (\JSON_ERROR_NONE === \json_last_error()) {
128
            return;
129
        }
130
131
        throw new JWTException('JSON failed: ' . \json_last_error_msg(), static::ERROR_JSON_FAILED);
0 ignored issues
show
Bug introduced by
The constant Ahc\Jwt\ValidatesJWT::ERROR_JSON_FAILED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
132
    }
133
}
134