GetUserAndRefresh::handle()   C
last analyzed

Complexity

Conditions 9
Paths 15

Size

Total Lines 46
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 4
Bugs 3 Features 0
Metric Value
c 4
b 3
f 0
dl 0
loc 46
ccs 0
cts 33
cp 0
rs 5.0943
cc 9
eloc 27
nc 15
nop 3
crap 90
1
<?php
2
3
namespace Tymon\JWTAuth\Middleware;
4
5
use Tymon\JWTAuth\Exceptions\JWTException;
6
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
7
8
class GetUserAndRefresh extends BaseMiddleware
9
{
10
    /**
11
     * Handle an incoming request.
12
     * If an user mode is set I don't check custom
13
     *
14
     * @param  \Illuminate\Http\Request  $request
15
     * @param  \Closure  $next
16
     * @param  String $custom custom claims that must be equals (format: key1-ele1;key2-ele2)
17
     * @return mixed
18
     */
19
    public function handle($request, \Closure $next, $custom = '')
20
    {
21
        $custom = $this->convertToArray($custom);
22
        if($token = $this->auth->setRequest($request)->getToken()) {
23
        }else if ($this->auth->getUserModel()){
24
            $token = $this->auth->fromUser($this->auth->getUserModel(), $custom);
25
        }else {
26
            return $this->respond('tymon.jwt.absent', 'token_not_provided', 401);
27
        }
28
29
        try {
30
            $user = $this->auth->authenticate($token, $custom);
31
        } catch (TokenExpiredException $e) {
32
            return $this->respond('tymon.jwt.expired', 'token_expired', $e->getStatusCode(), [$e]);
33
        } catch(InvalidClaimException $e) {
0 ignored issues
show
Bug introduced by
The class Tymon\JWTAuth\Middleware\InvalidClaimException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
34
            return $this->respond('tymon.jwt.invalid', 'claim_invalid', $e->getStatusCode(), [$e]);
35
        } catch (JWTException $e) {
36
            return $this->respond('tymon.jwt.invalid', 'token_invalid', $e->getStatusCode(), [$e]);
37
        }
38
39
        if (! $user) {
40
            return $this->respond('tymon.jwt.user_not_found', 'user_not_found', 404);
41
        }
42
43
        /**
44
         * refresh
45
         */
46
47
        $response = $next($request);
48
49
50
        $this->events->fire('tymon.jwt.valid', $user);
51
52
        try {
53
            $newToken = $this->auth->refresh($token, $custom);
54
        } catch (TokenExpiredException $e) {
55
            return $this->respond('tymon.jwt.expired', 'token_expired', $e->getStatusCode(), [$e]);
56
        } catch (JWTException $e) {
57
            return $this->respond('tymon.jwt.invalid', 'token_invalid', $e->getStatusCode(), [$e]);
58
        }
59
60
        // send the refreshed token back to the client
61
        $response->headers->set('Authorization', 'Bearer ' . $newToken);
62
63
        return $response;
64
    }
65
}
66