Completed
Push — master ( 9d09aa...80b42f )
by Jacob
02:05
created

Guard   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
c 2
b 0
f 0
lcom 1
cbo 4
dl 0
loc 52
ccs 24
cts 24
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A refresh() 0 9 3
A refreshToken() 0 10 3
A universalUserLogin() 0 13 4
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Canis.io
4
 * @license   MIT
5
 */
6
namespace Canis\Lumen\Jwt;
7
8
use Illuminate\Contracts\Auth\Factory as AuthFactory;
9
use Canis\Lumen\Jwt\Contracts\Subject as SubjectContract;
10
11
class Guard
12
    extends BaseGuard
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
Coding Style introduced by
Expected 0 spaces between "BaseGuard" and comma; 1 found
Loading history...
13
    implements GuardInterface
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
14
{
15
16
    /**
17
     * @inheritdoc
18
     */
19 4
    public function refresh(AuthFactory $auth)
20
    {
21 4
        $token = $this->getBearerToken(true);
22 4
        if ($token !== false && $token->hasClaim(static::JWT_GUARD_CLAIM)) {
23 3
            $guard = $token->getClaim(static::JWT_GUARD_CLAIM);
24 3
            return $auth->guard($guard)->refreshToken($token);
25
        }
26 1
        return false;
27
    }
28
29
    /**
30
     * Refresh the token 
31
     * @param  Token  $token
32
     * @return Token|boolean  New token or false if old token can't be verified
33
     */
34 3
    public function refreshToken(Token $token)
35
    {
36 3
        $user = $this->getProvider()->retrieveById($token->getClaim('sub'));
37 3
        $claimValidation = [static::JWT_GUARD_CLAIM => $this->id];
38 3
        if (!($user instanceof SubjectContract)
39 3
            || !$token->ensureClaimValues(array_merge($user->getJWTClaimValidation(), $claimValidation))) {
40 1
            return false;
41
        }
42 2
        return $this->generateToken($user);
43
    }
44
45
46
    /**
47
     * @inheritdoc
48
     */
49 3
    public function universalUserLogin(AuthFactory $auth)
50
    {
51 3
        $token = $this->getBearerToken();
52 3
        $guard = false;
53 3
        if ($token !== false && $token->hasClaim(static::JWT_GUARD_CLAIM)) {
54 3
            $guard = $token->getClaim(static::JWT_GUARD_CLAIM);
55 3
            $user = $auth->guard($guard)->user();
56 3
            if ($user === null) {
57 1
                $guard = false;
58 1
            }
59 3
        }
60 3
        return $guard;
61
    }
62
}
63