Completed
Push — develop ( 2f6a53...9bf2e6 )
by Sean
04:08 queued 10s
created

JWT::setToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 2
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
     * Get the authenticated user
194
     *
195
     * @return \Tymon\JWTAuth\Contracts\JWTSubject
196
     */
197 4
    public function user()
198
    {
199 4
        return $this->auth->user();
0 ignored issues
show
Bug introduced by
The property auth does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
200
    }
201
202
    /**
203
     * Set the token.
204
     *
205
     * @param  Token|string  $token
206
     *
207
     * @return JWTAuth
208
     */
209 18
    public function setToken($token)
210
    {
211 18
        $this->token = $token instanceof Token ? $token : new Token($token);
212
213 18
        return $this;
214
    }
215
216
    /**
217
     * Unset the current token.
218
     *
219
     * @return JWTAuth
220
     */
221
    public function unsetToken()
222
    {
223
        $this->token = null;
224
225
        return $this;
226
    }
227
228
    /**
229
     * Ensure that a token is available.
230
     *
231
     * @throws \Tymon\JWTAuth\Exceptions\JWTException
232
     */
233 16
    protected function requireToken()
234
    {
235 16
        if (! $this->token) {
236 2
            throw new JWTException('A token is required');
237
        }
238 14
    }
239
240
    /**
241
     * Set the request instance.
242
     *
243
     * @param \Illuminate\Http\Request $request
244
     *
245
     * @return JWTAuth
246
     */
247 2
    public function setRequest(Request $request)
248
    {
249 2
        $this->parser->setRequest($request);
250
251 2
        return $this;
252
    }
253
254
    /**
255
     * Get the Manager instance.
256
     *
257
     * @return \Tymon\JWTAuth\Manager
258
     */
259 4
    public function manager()
260
    {
261 4
        return $this->manager;
262
    }
263
264
    /**
265
     * Get the Parser instance
266
     *
267
     * @return \Tymon\JWTAuth\Http\Parser
268
     */
269 2
    public function parser()
270
    {
271 2
        return $this->parser;
272
    }
273
274
    /**
275
     * Get the Payload Factory
276
     *
277
     * @return \Tymon\JWTAuth\Factory
278
     */
279 4
    public function factory()
280
    {
281 4
        return $this->manager->getPayloadFactory();
282
    }
283
284
    /**
285
     * Get the Blacklist
286
     *
287
     * @return \Tymon\JWTAuth\Blacklist
288
     */
289
    public function blacklist()
290
    {
291
        return $this->manager->getBlacklist();
292
    }
293
294
    /**
295
     * Magically call the JWT Manager.
296
     *
297
     * @param string $method
298
     * @param array  $parameters
299
     *
300
     * @return mixed
301
     *
302
     * @throws \BadMethodCallException
303
     */
304 2
    public function __call($method, $parameters)
305
    {
306 2
        if (method_exists($this->manager, $method)) {
307 2
            return call_user_func_array([$this->manager, $method], $parameters);
308
        }
309
310
        throw new \BadMethodCallException("Method [$method] does not exist.");
311
    }
312
}
313