1
<?php
2

3
declare(strict_types=1);
4

5
namespace ReallySimpleJWT;
6

7
use ReallySimpleJWT\Jwt;
8

9
/**
10
 * This 'value object' is generated when the JWT has been parsed and validated,
11
 * it contains the original JWT value object, and the header and payload
12
 * associative arrays. The class also offers helper methods which provide
13
 * access to the header and payload claim data.
14
 */
15
class Parsed
16
{
17
    /**
18
     * The pre-parsed JWT value object
19
     *
20
     * @var Jwt
21
     */
22
    private $jwt;
23

24
    /**
25
     * Associative array of header claims
26
     *
27
     * @var mixed[]
28
     */
29
    private $header;
30

31
    /**
32
     * Associative array of payload claims
33
     *
34
     * @var mixed[]
35
     */
36
    private $payload;
37

38
    /**
39
     * The JWT signature string
40
     *
41
     * @var string
42
     */
43
    private $signature;
44

45
    /**
46
     * The Parsed constructor
47
     *
48
     * @param Jwt $jwt
49
     * @param mixed[] $header
50
     * @param mixed[] $payload
51
     * @param string $signature
52
     */
53 1
    public function __construct(Jwt $jwt, array $header, array $payload, string $signature)
54
    {
55 1
        $this->jwt = $jwt;
56

57 1
        $this->header = $header;
58

59 1
        $this->payload = $payload;
60

61 1
        $this->signature = $signature;
62
    }
63

64
    /**
65
     * Return the original JWT value object.
66
     *
67
     * @return Jwt
68
     */
69 1
    public function getJwt(): Jwt
70
    {
71 1
        return $this->jwt;
72
    }
73

74
    /**
75
     * Get the header claims data as an associative array.
76
     *
77
     * @return mixed[]
78
     */
79 1
    public function getHeader(): array
80
    {
81 1
        return $this->header;
82
    }
83

84
    /**
85
     * Access the algorithm claim from the header.
86
     *
87
     * @return string
88
     */
89 1
    public function getAlgorithm(): string
90
    {
91 1
        return $this->header['alg'] ?? '';
92
    }
93

94
    /**
95
     * Access the type claim from the header.
96
     *
97
     * @return string
98
     */
99 1
    public function getType(): string
100
    {
101 1
        return $this->header['typ'] ?? '';
102
    }
103

104
    /**
105
     * Access the content type claim from the header.
106
     *
107
     * @return string
108
     */
109 1
    public function getContentType(): string
110
    {
111 1
        return $this->header['cty'] ?? '';
112
    }
113

114
    /**
115
     * Get the payload claims data as an associative array.
116
     *
117
     * @return mixed[]
118
     */
119 1
    public function getPayload(): array
120
    {
121 1
        return $this->payload;
122
    }
123

124
    /**
125
     * Access the issuer claim from the payload.
126
     *
127
     * @return string
128
     */
129 1
    public function getIssuer(): string
130
    {
131 1
        return $this->payload['iss'] ?? '';
132
    }
133

134
    /**
135
     * Access the subject claim from the payload.
136
     *
137
     * @return string
138
     */
139 1
    public function getSubject(): string
140
    {
141 1
        return $this->payload['sub'] ?? '';
142
    }
143

144
    /**
145
     * Access the audience claim from the payload. Can return a string or an
146
     * array. Will return an empty string if not set.
147
     *
148
     * @return string|string[]
149
     */
150 1
    public function getAudience()
151
    {
152 1
        return $this->payload['aud'] ?? '';
153
    }
154

155
    /**
156
     * Access the expiration claim from the payload.
157
     *
158
     * @return int
159
     */
160 1
    public function getExpiration(): int
161
    {
162 1
        return $this->payload['exp'] ?? 0;
163
    }
164

165
    /**
166
     * Calculate how long the token has until it expires.
167
     *
168
     * @return int
169
     */
170 1
    public function getExpiresIn(): int
171
    {
172 1
        $expiresIn = $this->getExpiration() - time();
173 1
        return $expiresIn > 0 ? $expiresIn : 0;
174
    }
175

176
    /**
177
     * Access the not before claim from the payload.
178
     *
179
     * @return int
180
     */
181 1
    public function getNotBefore(): int
182
    {
183 1
        return $this->payload['nbf'] ?? 0;
184
    }
185

186
    /**
187
     * Calculate how long until the Not Before claim expires and the token
188
     * is usable.
189
     *
190
     * @return int
191
     */
192 1
    public function getUsableIn(): int
193
    {
194 1
        $usableIn = $this->getNotBefore() - time();
195 1
        return $usableIn > 0 ? $usableIn : 0;
196
    }
197

198
    /**
199
     * Access the issued at claim from the payload.
200
     *
201
     * @return int
202
     */
203 1
    public function getIssuedAt(): int
204
    {
205 1
        return $this->payload['iat'] ?? 0;
206
    }
207

208
    /**
209
     * Access the JWT Id claim from the payload.
210
     *
211
     * @return string
212
     */
213 1
    public function getJwtId(): string
214
    {
215 1
        return $this->payload['jti'] ?? '';
216
    }
217

218
    /**
219
     * Get the JWT signature string if required.
220
     *
221
     * @return string
222
     */
223 1
    public function getSignature(): string
224
    {
225 1
        return $this->signature;
226
    }
227
}

Read our documentation on viewing source code .

Loading