Passed
Push — master ( 85c6cc...2afdeb )
by Nikolaos
04:50 queued 10s
created

Validator::validateSignature()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 2
b 0
f 0
nc 2
nop 2
dl 0
loc 15
ccs 7
cts 7
cp 1
crap 2
rs 10
1
<?php
2
3
/**
4
 * This file is part of the Phalcon Framework.
5
 *
6
 * For the full copyright and license information, please view the LICENSE.md
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Phalcon\Http\JWT;
13
14
use Phalcon\Http\JWT\Exceptions\ValidatorException;
15
use Phalcon\Http\JWT\Signer\SignerInterface;
16
use Phalcon\Http\JWT\Token\Enum;
17
use Phalcon\Http\JWT\Token\Token;
18
19
/**
20
 * Class Validator
21
 *
22
 * @property int   $timeShift
23
 * @property Token $token
24
 */
25
class Validator
26
{
27
    /**
28
     * @var int
29
     */
30
    private $timeShift = 0;
31
32
    /**
33
     * @var Token
34
     */
35
    private $token;
36
37
    /**
38
     * Validator constructor.
39
     *
40
     * @param Token $token
41
     * @param int   $timeShift
42
     */
43 10
    public function __construct(Token $token, int $timeShift = 0)
44
    {
45 10
        $this->token     = $token;
46 10
        $this->timeShift = $timeShift;
47 10
    }
48
49
    /**
50
     * @param Token $token
51
     *
52
     * @return Validator
53
     */
54 1
    public function setToken(Token $token): Validator
55
    {
56 1
        $this->token = $token;
57
58 1
        return $this;
59
    }
60
61
    /**
62
     * @param string $audience
63
     *
64
     * @return Validator
65
     * @throws ValidatorException
66
     */
67 2
    public function validateAudience(string $audience): Validator
68
    {
69 2
        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

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