Passed
Push — master ( bf6fa9...817f88 )
by Nikolaos
06:58
created

Validator::validateIssuedAt()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the Phalcon Framework.
5
 *
6
 * (c) Phalcon Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Phalcon\Security\JWT;
15
16
use Phalcon\Security\JWT\Exceptions\ValidatorException;
17
use Phalcon\Security\JWT\Signer\SignerInterface;
18
use Phalcon\Security\JWT\Token\Enum;
19
use Phalcon\Security\JWT\Token\Token;
20
21
use function in_array;
22
23
/**
24
 * Class Validator
25
 *
26
 * @property int   $timeShift
27
 * @property Token $token
28
 */
29
class Validator
30
{
31
    /**
32
     * @var int
33
     */
34
    private $timeShift = 0;
35
36
    /**
37
     * @var Token
38
     */
39
    private $token;
40
41
    /**
42
     * Validator constructor.
43
     *
44
     * @param Token $token
45
     * @param int   $timeShift
46
     */
47
    public function __construct(Token $token, int $timeShift = 0)
48
    {
49
        $this->token     = $token;
50
        $this->timeShift = $timeShift;
51
    }
52
53
    /**
54
     * @param Token $token
55
     *
56
     * @return Validator
57
     */
58
    public function setToken(Token $token): Validator
59
    {
60
        $this->token = $token;
61
62
        return $this;
63
    }
64
65
    /**
66
     * @param string $audience
67
     *
68
     * @return Validator
69
     * @throws ValidatorException
70
     */
71
    public function validateAudience(string $audience): Validator
72
    {
73
        if (!in_array($audience, $this->token->getClaims()->get(Enum::AUDIENCE, []))) {
0 ignored issues
show
Bug introduced by
It seems like $this->token->getClaims(...num::AUDIENCE, array()) can also be of type null; however, parameter $haystack of in_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
        if (!in_array($audience, /** @scrutinizer ignore-type */ $this->token->getClaims()->get(Enum::AUDIENCE, []))) {
Loading history...
74
            throw new ValidatorException(
75
                "Validation: audience not allowed"
76
            );
77
        }
78
79
        return $this;
80
    }
81
82
    /**
83
     * @param int $timestamp
84
     *
85
     * @return Validator
86
     * @throws ValidatorException
87
     */
88
    public function validateExpiration(int $timestamp): Validator
89
    {
90
        if (
91
            $this->token->getClaims()->has(Enum::EXPIRATION_TIME) &&
92
            $this->getTimestamp($timestamp) >= (int) $this->token->getClaims()->get(Enum::EXPIRATION_TIME)
93
        ) {
94
            throw new ValidatorException(
95
                "Validation: the token has expired"
96
            );
97
        }
98
99
        return $this;
100
    }
101
102
    /**
103
     * @param string $id
104
     *
105
     * @return Validator
106
     * @throws ValidatorException
107
     */
108
    public function validateId(string $id): Validator
109
    {
110
        if ($id !== (string) $this->token->getClaims()->get(Enum::ID)) {
111
            throw new ValidatorException(
112
                "Validation: incorrect Id"
113
            );
114
        }
115
116
        return $this;
117
    }
118
119
    /**
120
     * @param int $timestamp
121
     *
122
     * @return Validator
123
     * @throws ValidatorException
124
     */
125
    public function validateIssuedAt(int $timestamp): Validator
126
    {
127
        if ($this->getTimestamp($timestamp) <= (int) $this->token->getClaims()->get(Enum::ISSUED_AT)) {
128
            throw new ValidatorException(
129
                "Validation: the token cannot be used yet (future)"
130
            );
131
        }
132
133
        return $this;
134
    }
135
136
    /**
137
     * @param string $issuer
138
     *
139
     * @return Validator
140
     * @throws ValidatorException
141
     */
142
    public function validateIssuer(string $issuer): Validator
143
    {
144
        if ($issuer !== (string) $this->token->getClaims()->get(Enum::ISSUER)) {
145
            throw new ValidatorException(
146
                "Validation: incorrect issuer"
147
            );
148
        }
149
150
        return $this;
151
    }
152
153
    /**
154
     * @param int $timestamp
155
     *
156
     * @return Validator
157
     * @throws ValidatorException
158
     */
159
    public function validateNotBefore(int $timestamp): Validator
160
    {
161
        if ($this->getTimestamp($timestamp) <= (int) $this->token->getClaims()->get(Enum::NOT_BEFORE)) {
162
            throw new ValidatorException(
163
                "Validation: the token cannot be used yet (not before)"
164
            );
165
        }
166
167
        return $this;
168
    }
169
170
    /**
171
     * @param SignerInterface $signer
172
     * @param string          $passphrase
173
     *
174
     * @return Validator
175
     * @throws ValidatorException
176
     */
177
    public function validateSignature(SignerInterface $signer, string $passphrase): Validator
178
    {
179
        if (
180
            !$signer->verify(
181
                $this->token->getSignature()->getHash(),
182
                $this->token->getPayload(),
183
                $passphrase
184
            )
185
        ) {
186
            throw new ValidatorException(
187
                "Validation: the signature does not match"
188
            );
189
        }
190
191
        return $this;
192
    }
193
194
    /**
195
     * @param int $timestamp
196
     *
197
     * @return int
198
     */
199
    private function getTimestamp(int $timestamp): int
200
    {
201
        return $timestamp + $this->timeShift;
202
    }
203
}
204