RobDWaller / ReallySimpleJWT
1
<?php
2

3
declare(strict_types=1);
4

5
namespace ReallySimpleJWT;
6

7
use ReallySimpleJWT\Tokens;
8
use ReallySimpleJWT\Build;
9
use ReallySimpleJWT\Parse;
10
use ReallySimpleJWT\Validate;
11

12
/**
13
 * A simple Package for creating JSON Web Tokens that uses HMAC SHA256 to sign
14
 * signatures.
15
 *
16
 * Exposes a simple interface to allow you to create a token that stores a user
17
 * identifier. The Package is set up to allow extension and the use of larger
18
 * payloads. You can use your own encoding if you choose.
19
 *
20
 * For more information on JSON Web Tokens please see https://jwt.io
21
 * along with the RFC https://tools.ietf.org/html/rfc7519
22
 *
23
 * @author Rob Waller <rdwaller1984@gmail.com>
24
 */
25
class Token
26
{
27
    /**
28
     * Create a JSON Web Token that contains a user identifier and a basic
29
     * payload including issued at, expiration and issuer.
30
     *
31
     * @see Tokens::create()
32
     * @param string|int $userId
33
     */
34 16
    public static function create($userId, string $secret, int $expiration, string $issuer): string
35
    {
36 16
        $tokens = new Tokens();
37 16
        return $tokens->create(
38 16
            'user_id',
39
            $userId,
40
            $secret,
41
            $expiration,
42
            $issuer
43 16
        )->getToken();
44
    }
45

46
    /**
47
     * @see Tokens::customPayload()
48
     * @param mixed[] $payload
49
     */
50 16
    public static function customPayload(array $payload, string $secret): string
51
    {
52 16
        $tokens = new Tokens();
53 16
        return $tokens->customPayload($payload, $secret)->getToken();
54
    }
55

56
    /**
57
     * @see Tokens::validate()
58
     */
59 16
    public static function validate(string $token, string $secret): bool
60
    {
61 16
        $tokens = new Tokens();
62 16
        return $tokens->validate($token, $secret);
63
    }
64

65
    /**
66
     * @see Tokens::getHeader()
67
     * @return mixed[]
68
     */
69 16
    public static function getHeader(string $token, string $secret): array
70
    {
71 16
        $tokens = new Tokens();
72 16
        return $tokens->getHeader($token, $secret);
73
    }
74

75
    /**
76
     * @see Tokens::getPayload()
77
     * @return mixed[]
78
     */
79 16
    public static function getPayload(string $token, string $secret): array
80
    {
81 16
        $tokens = new Tokens();
82 16
        return $tokens->getPayload($token, $secret);
83
    }
84

85
    /**
86
     * @see Tokens::builder()
87
     */
88 16
    public static function builder(): Build
89
    {
90 16
        $tokens = new Tokens();
91 16
        return $tokens->builder();
92
    }
93

94
    /**
95
     * @see Tokens::parser()
96
     */
97 16
    public static function parser(string $token, string $secret): Parse
98
    {
99 16
        $tokens = new Tokens();
100 16
        return $tokens->parser($token, $secret);
101
    }
102

103
    /**
104
     * @see Tokens::validator()
105
     */
106 16
    public static function validator(string $token, string $secret): Validate
107
    {
108 16
        $tokens = new Tokens();
109 16
        return $tokens->validator($token, $secret);
110
    }
111

112
    /**
113
     * @see Tokens::validateExpiration()
114
     */
115 16
    public static function validateExpiration(string $token, string $secret): bool
116
    {
117 16
        $tokens = new Tokens();
118 16
        return $tokens->validateExpiration($token, $secret);
119
    }
120

121
    /**
122
     * @see Tokens::validateNotBefore()
123
     */
124 16
    public static function validateNotBefore(string $token, string $secret): bool
125
    {
126 16
        $tokens = new Tokens();
127 16
        return $tokens->validateNotBefore($token, $secret);
128
    }
129
}

Read our documentation on viewing source code .

Loading