RobDWaller / ReallySimpleJWT
1
<?php
2

3
declare(strict_types=1);
4

5
namespace ReallySimpleJWT;
6

7
use ReallySimpleJWT\Build;
8
use ReallySimpleJWT\Helper\Validator;
9
use ReallySimpleJWT\Validate;
10
use ReallySimpleJWT\Encoders\EncodeHS256;
11
use ReallySimpleJWT\Decode;
12
use ReallySimpleJWT\Parse;
13
use ReallySimpleJWT\Jwt;
14
use ReallySimpleJWT\Exception\TokensException;
15
use ReallySimpleJWT\Exception\ValidateException;
16
use ReallySimpleJWT\Exception\ParseException;
17

18
/**
19
 * Core factory and interface class for creating basic JSON Web Tokens.
20
 */
21
class Tokens
22
{
23
    /**
24
     * Factory method to return an instance of the Build class for creating new
25
     * JSON Web Tokens.
26
     */
27 16
    public function builder(): Build
28
    {
29 16
        return new Build(
30 16
            'JWT',
31 16
            new Validator(),
32 16
            new Secret(),
33 16
            new EncodeHS256()
34
        );
35
    }
36

37
    /**
38
     * Factory method to return an instance of the Parse class for parsing a JWT
39
     * and gaining access to the token's header and payload claims data.
40
     */
41 16
    public function parser(string $token, string $secret): Parse
42
    {
43 16
        return new Parse(
44 16
            new Jwt(
45 16
                $token,
46
                $secret
47
            ),
48 16
            new Decode()
49
        );
50
    }
51

52
    /**
53
     * Factory method to return an instance of the Validate class to validate
54
     * the structure, signature and claims data of a JWT.
55
     */
56 16
    public function validator(string $token, string $secret): Validate
57
    {
58 16
        $parse = $this->parser($token, $secret);
59

60 16
        return new Validate(
61 16
            $parse,
62 16
            new EncodeHS256(),
63 16
            new Validator()
64
        );
65
    }
66

67
    /**
68
     * Return the header claims data from a JWT.
69
     *
70
     * @return mixed[]
71
     */
72 16
    public function getHeader(string $token, string $secret): array
73
    {
74 16
        $parser = $this->parser($token, $secret);
75 16
        return $parser->parse()->getHeader();
76
    }
77

78
    /**
79
     * Return the payload claims data from a JWT.
80
     *
81
     * @return mixed[]
82
     */
83 16
    public function getPayload(string $token, string $secret): array
84
    {
85 16
        $parser = $this->parser($token, $secret);
86 16
        return $parser->parse()->getPayload();
87
    }
88

89
    /**
90
     * Create a basic JSON Web Token for a user, define the user key and id to
91
     * identify the user along with an expiration and issuer.
92
     *
93
     * @param string|int $userId
94
     */
95 16
    public function create(string $userKey, $userId, string $secret, int $expiration, string $issuer): Jwt
96
    {
97 16
        $builder = $this->builder();
98

99 16
        return $builder->setPayloadClaim($userKey, $userId)
100 16
            ->setSecret($secret)
101 16
            ->setExpiration($expiration)
102 16
            ->setIssuer($issuer)
103 16
            ->setIssuedAt(time())
104 16
            ->build();
105
    }
106

107
    /**
108
     * Create a basic token based on an array of payload claims.
109
     * Format [string: mixed].
110
     *
111
     * @param mixed[] $payload
112
     */
113 16
    public function customPayload(array $payload, string $secret): Jwt
114
    {
115 16
        $builder = $this->builder();
116

117 16
        foreach ($payload as $key => $value) {
118 16
            if (is_int($key)) {
119 16
                throw new TokensException('Invalid payload claim.', 8);
120
            }
121

122 16
            $builder->setPayloadClaim($key, $value);
123
        }
124

125 16
        return $builder->setSecret($secret)
126 16
            ->build();
127
    }
128

129
    /**
130
     * Validate the token structure and signature.
131
     */
132 16
    public function validate(string $token, string $secret): bool
133
    {
134 16
        $validate = $this->validator($token, $secret);
135

136
        try {
137 16
            $validate->structure()
138 16
                ->algorithmNotNone()
139 16
                ->signature();
140 16
            return true;
141 16
        } catch (ValidateException $e) {
142 16
            return false;
143
        }
144
    }
145

146
    /**
147
     * Validate the expiration claim of a token to see if it has expired. Will
148
     * return false if the expiration (exp) claim is not set.
149
     */
150 16
    public function validateExpiration(string $token, string $secret): bool
151
    {
152 16
        $validate = $this->validator($token, $secret);
153

154
        try {
155 16
            $validate->expiration();
156 16
            return true;
157 16
        } catch (ValidateException $e) {
158 16
            return false;
159 16
        } catch (ParseException $e) {
160 16
            return false;
161
        }
162
    }
163

164
    /**
165
     * Validate the not before claim of a token to see if it is ready to use.
166
     * Will return false if the not before (nbf) claim is not set.
167
     */
168 16
    public function validateNotBefore(string $token, string $secret): bool
169
    {
170 16
        $validate = $this->validator($token, $secret);
171

172
        try {
173 16
            $validate->notBefore();
174 16
            return true;
175 16
        } catch (ValidateException $e) {
176 16
            return false;
177 16
        } catch (ParseException $e) {
178 16
            return false;
179
        }
180
    }
181
}

Read our documentation on viewing source code .

Loading