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

Guard::refreshToken()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 7
nc 2
nop 1
crap 3
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