1 | <?php |
||
9 | class JWTManager |
||
10 | { |
||
11 | /** |
||
12 | * @var \Tymon\JWTAuth\Providers\JWT\JWTInterface |
||
13 | */ |
||
14 | protected $jwt; |
||
15 | |||
16 | /** |
||
17 | * @var \Tymon\JWTAuth\Blacklist |
||
18 | */ |
||
19 | protected $blacklist; |
||
20 | |||
21 | /** |
||
22 | * @var \Tymon\JWTAuth\PayloadFactory |
||
23 | */ |
||
24 | protected $payloadFactory; |
||
25 | |||
26 | /** |
||
27 | * @var boolean |
||
28 | */ |
||
29 | protected $blacklistEnabled = true; |
||
30 | |||
31 | /** |
||
32 | * @var boolean |
||
33 | */ |
||
34 | protected $refreshFlow = false; |
||
35 | |||
36 | /** |
||
37 | * @param \Tymon\JWTAuth\Providers\JWT\JWTInterface $jwt |
||
38 | * @param \Tymon\JWTAuth\Blacklist $blacklist |
||
39 | * @param \Tymon\JWTAuth\PayloadFactory $payloadFactory |
||
40 | */ |
||
41 | 27 | public function __construct(JWTInterface $jwt, Blacklist $blacklist, PayloadFactory $payloadFactory) |
|
42 | { |
||
43 | 27 | $this->jwt = $jwt; |
|
44 | 27 | $this->blacklist = $blacklist; |
|
45 | 27 | $this->payloadFactory = $payloadFactory; |
|
46 | 27 | } |
|
47 | |||
48 | /** |
||
49 | * Encode a Payload and return the Token |
||
50 | * |
||
51 | * @param \Tymon\JWTAuth\Payload $payload |
||
52 | * @return \Tymon\JWTAuth\Token |
||
53 | */ |
||
54 | 6 | public function encode(Payload $payload) |
|
55 | { |
||
56 | 6 | $token = $this->jwt->encode($payload->get()); |
|
57 | |||
58 | 6 | return new Token($token); |
|
59 | } |
||
60 | |||
61 | /** |
||
62 | * Decode a Token and return the Payload |
||
63 | * |
||
64 | * @param \Tymon\JWTAuth\Token $token |
||
65 | * @return Payload |
||
66 | * @throws TokenBlacklistedException |
||
67 | */ |
||
68 | 12 | public function decode(Token $token) |
|
69 | { |
||
70 | 12 | $payloadArray = $this->jwt->decode($token->get()); |
|
71 | |||
72 | 12 | $payload = $this->payloadFactory->setRefreshFlow($this->refreshFlow)->make($payloadArray); |
|
73 | |||
74 | 12 | if ($this->blacklistEnabled && $this->blacklist->has($payload)) { |
|
75 | 3 | throw new TokenBlacklistedException('The token has been blacklisted'); |
|
76 | } |
||
77 | |||
78 | 9 | return $payload; |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * Refresh a Token and return a new Token |
||
83 | * |
||
84 | * @param \Tymon\JWTAuth\Token $token |
||
85 | * @param Array $custom |
||
86 | * |
||
87 | * @return \Tymon\JWTAuth\Token |
||
88 | */ |
||
89 | 3 | public function refresh(Token $token, $custom= []) |
|
90 | { |
||
91 | 3 | $payload = $this->setRefreshFlow()->decode($token); |
|
92 | |||
93 | 3 | if ($this->blacklistEnabled) { |
|
94 | // invalidate old token |
||
95 | 3 | $this->blacklist->add($payload); |
|
96 | 2 | } |
|
97 | |||
98 | //add custom |
||
99 | 3 | $custom = array_merge($custom, ['sub' => $payload['sub'],'iat' => $payload['iat']]); |
|
100 | |||
101 | // return the new token |
||
102 | 3 | return $this->encode( |
|
103 | 3 | $this->payloadFactory->make($custom) |
|
104 | 2 | ); |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * Invalidate a Token by adding it to the blacklist |
||
109 | * |
||
110 | * @param Token $token |
||
111 | * @return boolean |
||
112 | */ |
||
113 | 6 | public function invalidate(Token $token) |
|
114 | { |
||
115 | 6 | if (! $this->blacklistEnabled) { |
|
116 | 3 | throw new JWTException('You must have the blacklist enabled to invalidate a token.'); |
|
117 | } |
||
118 | |||
119 | 3 | return $this->blacklist->add($this->decode($token)); |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * Get the PayloadFactory instance |
||
124 | * |
||
125 | * @return \Tymon\JWTAuth\PayloadFactory |
||
126 | */ |
||
127 | 3 | public function getPayloadFactory() |
|
128 | { |
||
129 | 3 | return $this->payloadFactory; |
|
130 | } |
||
131 | |||
132 | /** |
||
133 | * Get the JWTProvider instance |
||
134 | * |
||
135 | * @return \Tymon\JWTAuth\Providers\JWT\JWTInterface |
||
136 | */ |
||
137 | 3 | public function getJWTProvider() |
|
138 | { |
||
139 | 3 | return $this->jwt; |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * Get the Blacklist instance |
||
144 | * |
||
145 | * @return \Tymon\JWTAuth\Blacklist |
||
146 | */ |
||
147 | 3 | public function getBlacklist() |
|
148 | { |
||
149 | 3 | return $this->blacklist; |
|
150 | } |
||
151 | |||
152 | /** |
||
153 | * Set whether the blacklist is enabled |
||
154 | * |
||
155 | * @param bool $enabled |
||
156 | */ |
||
157 | 3 | public function setBlacklistEnabled($enabled) |
|
163 | |||
164 | /** |
||
165 | * Set the refresh flow |
||
166 | * |
||
167 | * @param boolean $refreshFlow |
||
168 | * @return $this |
||
169 | */ |
||
170 | 3 | public function setRefreshFlow($refreshFlow = true) |
|
176 | } |
||
177 |