Issues (10)

src/JWTManager.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace JWTAuth;
4
5
use \Firebase\JWT\JWT;
0 ignored issues
show
The type \Firebase\JWT\JWT was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Firebase\JWT\Key;
7
use JWTAuth\Contracts\JWTManagerContract;
8
use JWTAuth\Contracts\JWTPayloadContract;
9
10
/**
11
 * Class JWTManager
12
 * @package JWTAuth
13
 */
14
class JWTManager implements JWTManagerContract
15
{
16
17
    /**
18
     * Path to public key
19
     *
20
     * @var string
21
     */
22
    protected string $publicKey;
23
24
    /**
25
     * Path to private key
26
     *
27
     * @var string
28
     */
29
    protected string $privateKey;
30
31
    /**
32
     * Payload data
33
     *
34
     * @var JWTPayloadContract
35
     */
36
    protected JWTPayloadContract $payload;
37
38
    /**
39
     * String token
40
     *
41
     * @var string
42
     */
43
    protected string $token;
44
45
46 5
    public function __construct(string $publicKey = '', string $privateKey = '')
47
    {
48 5
        $this->publicKey  = $publicKey;
49 5
        $this->privateKey = $privateKey;
50
51 5
        $this->payload = new JWTPayload();
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57 1
    public function decode(string $token): JWTManagerContract
58
    {
59 1
        $publicKey = file_get_contents(storage_path($this->publicKey));
60
61 1
        $payloadArray = (array) JWT::decode($token, new Key($publicKey, 'RS256'));
62
63 1
        $this->payload = new JWTPayload($payloadArray);
64
65 1
        $this->token = $token;
66
67 1
        return $this;
68
    }
69
70
    /**
71
     * @inheritDoc
72
     */
73 4
    public function encode(): string
74
    {
75 4
        $privateKey = file_get_contents(storage_path($this->privateKey));
76
77 4
        $this->token = JWT::encode($this->payload->toArray(), $privateKey, 'RS256');
78
79 4
        return $this->token;
80
    }
81
82
    /**
83
     * @inheritDoc
84
     */
85 4
    public function setPayload(array|JWTPayloadContract $payload): JWTManagerContract
86
    {
87 4
        $this->payload = is_array($payload) ? new JWTPayload($payload) : $payload;
88
89 4
        $this->token = '';
90
91 4
        return $this;
92
    }
93
94
    /**
95
     * @inheritDoc
96
     */
97 3
    public function payload(): JWTPayloadContract
98
    {
99 3
        return $this->payload;
100
    }
101
102
    /**
103
     * @inheritDoc
104
     */
105 3
    public function getToken(): string
106
    {
107 3
        if (!$this->token) {
108 1
            $this->encode();
109
        }
110
111 3
        return $this->token;
112
    }
113
114 1
    public function toArray(): array
115
    {
116 1
        return $this->payload->toArray();
117
    }
118
119 1
    public function toJson($options = 0)
120
    {
121 1
        return json_encode($this->toArray(), $options);
122
    }
123
124 1
    public function jsonSerialize(): mixed
125
    {
126 1
        return $this->toArray();
127
    }
128
}
129