Completed
Push — develop ( 971674...43bfaf )
by Sean
02:49
created

JWT::__call()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
crap 2.0625
1
<?php
2
3
/*
4
 * This file is part of jwt-auth
5
 *
6
 * (c) Sean Tymon <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tymon\JWTAuth;
13
14
use Illuminate\Http\Request;
15
use Tymon\JWTAuth\Http\Parser;
16
use Tymon\JWTAuth\Support\CustomClaims;
17
use Tymon\JWTAuth\Contracts\JWTSubject;
18
use Tymon\JWTAuth\Exceptions\JWTException;
19
20
class JWT
21
{
22
    use CustomClaims;
23
24
    /**
25
     * @var \Tymon\JWTAuth\Manager
26
     */
27
    protected $manager;
28
29
    /**
30
     * @var \Tymon\JWTAuth\Http\Parser
31
     */
32
    protected $parser;
33
34
    /**
35
     * @var \Tymon\JWTAuth\Token
36
     */
37
    protected $token;
38
39
    /**
40
     * @param \Tymon\JWTAuth\Manager                   $manager
41
     * @param \Tymon\JWTAuth\Http\Parser               $parser
42
     */
43 38
    public function __construct(Manager $manager, Parser $parser)
44
    {
45 38
        $this->manager = $manager;
46 38
        $this->parser = $parser;
47 38
    }
48
49
    /**
50
     * Generate a token using the user identifier as the subject claim.
51
     *
52
     * @param \Tymon\JWTAuth\Contracts\JWTSubject $user
53
     *
54
     * @return string
55
     */
56 4
    public function fromUser(JWTSubject $user)
57
    {
58 4
        $payload = $this->makePayload($user);
59
60 4
        return $this->manager->encode($payload)->get();
61
    }
62
63
    /**
64
     * Refresh an expired token.
65
     *
66
     * @return string
67
     */
68 2
    public function refresh()
69
    {
70 2
        $this->requireToken();
71
72 2
        return $this->manager->customClaims($this->getCustomClaims())->refresh($this->token)->get();
73
    }
74
75
    /**
76
     * Invalidate a token (add it to the blacklist).
77
     *
78
     * @param  boolean  $forceForever
79
     *
80
     * @return boolean
81
     */
82 4
    public function invalidate($forceForever = false)
83
    {
84 4
        $this->requireToken();
85
86 4
        return $this->manager->invalidate($this->token, $forceForever);
87
    }
88
89
    /**
90
     * Alias to get the payload, and as a result checks that
91
     * the token is valid i.e. not expired or blacklisted
92
     *
93
     * @throws JWTException
94
     *
95
     * @return \Tymon\JWTAuth\Payload
96
     */
97 4
    public function checkOrFail()
98
    {
99 4
        return $this->getPayload();
100
    }
101
102
    /**
103
     * Check that the token is valid
104
     *
105
     * @return boolean
106
     */
107 4
    public function check()
108
    {
109
        try {
110 4
            $this->checkOrFail();
111 4
        } catch (JWTException $e) {
112 2
            return false;
113
        }
114
115 2
        return true;
116
    }
117
118
    /**
119
     * Get the token.
120
     *
121
     * @return false|Token
122
     */
123 6
    public function getToken()
124
    {
125 6
        if (! $this->token) {
126
            try {
127 4
                $this->parseToken();
128 4
            } catch (JWTException $e) {
129 2
                return false;
130
            }
131 2
        }
132
133 4
        return $this->token;
134
    }
135
136
    /**
137
     * Parse the token from the request.
138
     *
139
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
140
     *
141
     * @return JWTAuth
142
     */
143 12
    public function parseToken()
144
    {
145 12
        if (! $token = $this->parser->parseToken()) {
146 4
            throw new JWTException('The token could not be parsed from the request');
147
        }
148
149 8
        return $this->setToken($token);
150
    }
151
152
    /**
153
     * Get the raw Payload instance.
154
     *
155
     * @return \Tymon\JWTAuth\Payload
156
     */
157 10
    public function getPayload()
158
    {
159 10
        $this->requireToken();
160
161 8
        return $this->manager->decode($this->token);
162
    }
163
164
    /**
165
     * Create a Payload instance.
166
     *
167
     * @param \Tymon\JWTAuth\Contracts\JWTSubject $user
168
     *
169
     * @return \Tymon\JWTAuth\Payload
170
     */
171 4
    public function makePayload(JWTSubject $user)
172
    {
173 4
        return $this->factory()->customClaims($this->getClaimsArray($user))->make();
174
    }
175
176
    /**
177
     * Build the claims array and return it
178
     *
179
     * @param \Tymon\JWTAuth\Contracts\JWTSubject $user
180
     *
181
     * @return array
182
     */
183 4
    protected function getClaimsArray(JWTSubject $user)
184
    {
185 4
        return array_merge(
186 4
            ['sub' => $user->getJWTIdentifier()],
187 4
            $this->customClaims,
188 4
            $user->getJWTCustomClaims()
189 4
        );
190
    }
191
192
    /**
193
     * Set the token.
194
     *
195
     * @param  Token|string  $token
196
     *
197
     * @return JWTAuth
198
     */
199 18
    public function setToken($token)
200
    {
201 18
        $this->token = $token instanceof Token ? $token : new Token($token);
202
203 18
        return $this;
204
    }
205
206
    /**
207
     * Unset the current token.
208
     *
209
     * @return JWTAuth
210
     */
211
    public function unsetToken()
212
    {
213
        $this->token = null;
214
215
        return $this;
216
    }
217
218
    /**
219
     * Ensure that a token is available.
220
     *
221
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
222
     */
223 16
    protected function requireToken()
224
    {
225 16
        if (! $this->token) {
226 2
            throw new JWTException('A token is required');
227
        }
228 14
    }
229
230
    /**
231
     * Set the request instance.
232
     *
233
     * @param \Illuminate\Http\Request $request
234
     *
235
     * @return JWTAuth
236
     */
237 2
    public function setRequest(Request $request)
238
    {
239 2
        $this->parser->setRequest($request);
240
241 2
        return $this;
242
    }
243
244
    /**
245
     * Get the Manager instance.
246
     *
247
     * @return \Tymon\JWTAuth\Manager
248
     */
249 4
    public function manager()
250
    {
251 4
        return $this->manager;
252
    }
253
254
    /**
255
     * Get the Parser instance
256
     *
257
     * @return \Tymon\JWTAuth\Http\Parser
258
     */
259 2
    public function parser()
260
    {
261 2
        return $this->parser;
262
    }
263
264
    /**
265
     * Get the Payload Factory
266
     *
267
     * @return \Tymon\JWTAuth\Factory
268
     */
269 4
    public function factory()
270
    {
271 4
        return $this->manager->getPayloadFactory();
272
    }
273
274
    /**
275
     * Get the Blacklist
276
     *
277
     * @return \Tymon\JWTAuth\Blacklist
278
     */
279
    public function blacklist()
280
    {
281
        return $this->manager->getBlacklist();
282
    }
283
284
    /**
285
     * Magically call the JWT Manager.
286
     *
287
     * @param string $method
288
     * @param array  $parameters
289
     *
290
     * @return mixed
291
     *
292
     * @throws \BadMethodCallException
293
     */
294 2
    public function __call($method, $parameters)
295
    {
296 2
        if (method_exists($this->manager, $method)) {
297 2
            return call_user_func_array([$this->manager, $method], $parameters);
298
        }
299
300
        throw new \BadMethodCallException("Method [$method] does not exist.");
301
    }
302
}
303