RobDWaller / ReallySimpleJWT
1
<?php
2

3
declare(strict_types=1);
4

5
namespace ReallySimpleJWT;
6

7
use ReallySimpleJWT\Jwt;
8
use ReallySimpleJWT\Validate;
9
use ReallySimpleJWT\Parsed;
10
use ReallySimpleJWT\Exception\ParseException;
11
use ReallySimpleJWT\Interfaces\Decode;
12

13
/**
14
 * This class parses a JSON Web Token.
15
 *
16
 * The token is housed in the Jwt value object. The class outputs a Parsed value
17
 * object to provide access to the data held within the JWT header and payload.
18
 */
19
class Parse
20
{
21
    /**
22
     * The JSON Web Token value object.
23
     */
24
    private Jwt $jwt;
25

26
    /**
27
     * A class to decode JWT tokens.
28
     */
29
    private Decode $decode;
30

31
    /**
32
     * Parse constructor
33
     */
34 16
    public function __construct(Jwt $jwt, Decode $decode)
35
    {
36 16
        $this->jwt = $jwt;
37

38 16
        $this->decode = $decode;
39
    }
40

41
    /**
42
     * Parse the JWT and generate the Parsed Value Object.
43
     */
44 16
    public function parse(): Parsed
45
    {
46 16
        return new Parsed(
47 16
            $this->jwt,
48 16
            $this->getDecodedHeader(),
49 16
            $this->getDecodedPayload(),
50 16
            $this->getSignature()
51
        );
52
    }
53

54
    /**
55
     * Split the JWT into it's component parts, the header, payload and
56
     * signature are all separated by a dot.
57
     *
58
     * @return string[]
59
     */
60 16
    private function splitToken(): array
61
    {
62 16
        return explode('.', $this->jwt->getToken());
63
    }
64

65
    /**
66
     * Get the header string from the JWT string. This is the first part of the
67
     * JWT string.
68
     */
69 16
    private function getHeader(): string
70
    {
71 16
        return $this->splitToken()[0] ?? '';
72
    }
73

74
    /**
75
     * Get the payload string from the JWT string. This is the second part of
76
     * the JWT string.
77
     */
78 16
    private function getPayload(): string
79
    {
80 16
        return $this->splitToken()[1] ?? '';
81
    }
82

83
    /**
84
     * Get the signature string from the JWT string. This is the third part of
85
     * the JWT string.
86
     */
87 16
    public function getSignature(): string
88
    {
89 16
        return $this->splitToken()[2] ?? '';
90
    }
91

92
    /**
93
     * Retrieve the expiration claim from the JWT.
94
     *
95
     * @throws ParseException
96
     */
97 16
    public function getExpiration(): int
98
    {
99 16
        $payload = $this->getDecodedPayload();
100

101 16
        if (isset($payload['exp'])) {
102 16
            return $payload['exp'];
103
        }
104

105 16
        throw new ParseException('Expiration claim is not set.', 6);
106
    }
107

108
    /**
109
     * Retrieve the not before claim from the JWT.
110
     *
111
     * @throws ParseException
112
     */
113 16
    public function getNotBefore(): int
114
    {
115 16
        $payload = $this->getDecodedPayload();
116

117 16
        if (isset($payload['nbf'])) {
118 16
            return $payload['nbf'];
119
        }
120

121 16
        throw new ParseException('Not Before claim is not set.', 7);
122
    }
123

124
    /**
125
     * Retrieve the audience claim from the JWT.
126
     *
127
     * @return string|string[]
128
     * @throws ParseException
129
     */
130 16
    public function getAudience()
131
    {
132 16
        $payload = $this->getDecodedPayload();
133

134 16
        if (isset($payload['aud'])) {
135 16
            return $payload['aud'];
136
        }
137

138 16
        throw new ParseException('Audience claim is not set.', 11);
139
    }
140

141
    /**
142
     * Retrieve the algorithm claim from the JWT.
143
     *
144
     * @throws ParseException
145
     */
146 16
    public function getAlgorithm(): string
147
    {
148 16
        $header = $this->getDecodedHeader();
149

150 16
        if (isset($header['alg'])) {
151 16
            return $header['alg'];
152
        }
153

154 16
        throw new ParseException('Algorithm claim is not set.', 13);
155
    }
156

157
    /**
158
     * Decode the JWT header string to an associative array.
159
     *
160
     * @return mixed[]
161
     */
162 16
    public function getDecodedHeader(): array
163
    {
164 16
        return $this->decode->decode(
165 16
            $this->getHeader()
166
        );
167
    }
168

169
    /**
170
     * Decode the JWT payload string to an associative array.
171
     *
172
     * @return mixed[]
173
     */
174 16
    public function getDecodedPayload(): array
175
    {
176 16
        return $this->decode->decode(
177 16
            $this->getPayload()
178
        );
179
    }
180

181
    /**
182
     * Retrieve the JSON Web Token string.
183
     */
184 16
    public function getToken(): string
185
    {
186 16
        return $this->jwt->getToken();
187
    }
188

189
    /**
190
     * Retrieve the JSON Web Token secret.
191
     */
192 16
    public function getSecret(): string
193
    {
194 16
        return $this->jwt->getSecret();
195
    }
196
}

Read our documentation on viewing source code .

Loading