GetUserFromToken::handle()   C
last analyzed

Complexity

Conditions 7
Paths 11

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 7.227

Importance

Changes 6
Bugs 3 Features 0
Metric Value
c 6
b 3
f 0
dl 0
loc 28
ccs 15
cts 18
cp 0.8333
rs 6.7273
cc 7
eloc 19
nc 11
nop 3
crap 7.227
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