Completed
Push — master ( 73f9ad...8ec568 )
by claudio
07:24 queued 04:52
created

GetUserFromToken::handle()   C

Complexity

Conditions 7
Paths 11

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 15.4039

Importance

Changes 6
Bugs 3 Features 0
Metric Value
c 6
b 3
f 0
dl 0
loc 28
ccs 8
cts 18
cp 0.4444
rs 6.7273
cc 7
eloc 19
nc 11
nop 3
crap 15.4039
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
            return $this->respond('tymon.jwt.absent', 'token_not_provided', 401);
27
        }
28
29
        try {
30 12
            $user = $this->auth->authenticate($token, $custom);
31 12
        } catch (TokenExpiredException $e) {
32
            return $this->respond('tymon.jwt.expired', 'token_expired', $e->getStatusCode(), [$e]);
33 12
        } 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 12
        } 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
        $this->events->fire('tymon.jwt.valid', $user);
44
45
        return $next($request);
46
    }
47
}
48