Completed
Push — master ( e792c1...df17fb )
by claudio
03:48
created

GetUserAndRefresh   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 0
cbo 3
dl 0
loc 59
ccs 0
cts 34
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C handle() 0 47 9
1
<?php
2
3
namespace plunner\Http\Middleware;
4
5
use Doctrine\Common\Util\Debug;
6
use Log;
7
use Tymon\JWTAuth\Exceptions\JWTException;
8
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
9
10
class GetUserAndRefresh extends BaseMiddleware
11
{
12
    /**
13
     * Handle an incoming request.
14
     * If an user mode is set I don't check custom
15
     *
16
     * @param  \Illuminate\Http\Request  $request
17
     * @param  \Closure  $next
18
     * @param  String $custom custom claims that must be equals (format: key1-ele1;key2-ele2)
19
     * @return mixed
20
     */
21
    public function handle($request, \Closure $next, $custom = '')
22
    {
23
        $custom = $this->convertToArray($custom);
24
        Debug::info('headers: '.implode('-',$request->all()));
0 ignored issues
show
Bug introduced by
The method info() does not seem to exist on object<Doctrine\Common\Util\Debug>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
25
        if($token = $this->auth->setRequest($request)->getToken()) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
26
        }else if ($this->auth->getUserModel()){
27
            $token = $this->auth->fromUser($this->auth->getUserModel(), $custom);
28
        }else {
29
            return $this->respond('tymon.jwt.absent', 'token_not_provided', 401);
30
        }
31
32
        try {
33
            $user = $this->auth->authenticate($token, $custom);
34
        } catch (TokenExpiredException $e) {
35
            return $this->respond('tymon.jwt.expired', 'token_expired', $e->getStatusCode(), [$e]);
36
        } catch(InvalidClaimException $e) {
0 ignored issues
show
Bug introduced by
The class plunner\Http\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...
37
            return $this->respond('tymon.jwt.invalid', 'claim_invalid', $e->getStatusCode(), [$e]);
38
        } catch (JWTException $e) {
39
            return $this->respond('tymon.jwt.invalid', 'token_invalid', $e->getStatusCode(), [$e]);
40
        }
41
42
        if (! $user) {
43
            return $this->respond('tymon.jwt.user_not_found', 'user_not_found', 404);
44
        }
45
46
        /**
47
         * refresh
48
         */
49
50
        $response = $next($request);
51
52
53
        $this->events->fire('tymon.jwt.valid', $user);
54
55
        try {
56
            $newToken = $this->auth->refresh($token, $custom);
57
        } catch (TokenExpiredException $e) {
58
            return $this->respond('tymon.jwt.expired', 'token_expired', $e->getStatusCode(), [$e]);
59
        } catch (JWTException $e) {
60
            return $this->respond('tymon.jwt.invalid', 'token_invalid', $e->getStatusCode(), [$e]);
61
        }
62
63
        // send the refreshed token back to the client
64
        $response->headers->set('Authorization', 'Bearer ' . $newToken);
65
66
        return $response;
67
    }
68
}
69