Completed
Push — master ( 8ec568...54cae1 )
by claudio
03:56
created

GetUserFromToken   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 83.33%

Importance

Changes 6
Bugs 3 Features 0
Metric Value
wmc 7
c 6
b 3
f 0
lcom 1
cbo 4
dl 0
loc 40
ccs 15
cts 18
cp 0.8333
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C handle() 0 28 7
1
<?php
2
3
namespace Tymon\JWTAuth\Middleware;
4
5
use Tymon\JWTAuth\Exceptions\JWTException;
6
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
7
8
class GetUserFromToken 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 15
    public function handle($request, \Closure $next, $custom = '')
20
    {
21 15
        $custom = $this->convertToArray($custom);
22 15
        if($token = $this->auth->setRequest($request)->getToken()) {
23 11
        }else if ($this->auth->getUserModel()){
24
            $token = $this->auth->fromUser($this->auth->getUserModel(), $custom);
25
        }else {
26 3
            return $this->respond('tymon.jwt.absent', 'token_not_provided', 401);
27
        }
28
29
        try {
30 12
            $user = $this->auth->authenticate($token, $custom);
31 10
        } catch (TokenExpiredException $e) {
32 3
            return $this->respond('tymon.jwt.expired', 'token_expired', $e->getStatusCode(), [$e]);
33 3
        } 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 3
        } catch (JWTException $e) {
36 3
            return $this->respond('tymon.jwt.invalid', 'token_invalid', $e->getStatusCode(), [$e]);
37
        }
38
39 6
        if (! $user) {
40 3
            return $this->respond('tymon.jwt.user_not_found', 'user_not_found', 404);
41
        }
42
43 3
        $this->events->fire('tymon.jwt.valid', $user);
44
45 3
        return $next($request);
46
    }
47
}
48