Completed
Push — master ( 57aa9a...9d09aa )
by Jacob
02:21
created

Processor::validateToken()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 17
ccs 14
cts 14
cp 1
rs 8.8571
cc 5
eloc 11
nc 16
nop 2
crap 5
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Canis.io
4
 * @license   MIT
5
 */
6
namespace Canis\Lumen\Jwt\Adapters\Lcobucci;
7
8
use Canis\Lumen\Jwt\Token;
9
use Canis\Lumen\Jwt\Contracts\Processor as ProcessorContract;
10
use Lcobucci\JWT\Signer\Hmac\Sha256;
11
use Lcobucci\JWT\Parser;
12
use Lcobucci\JWT\Token as JwtToken;
13
14
class Processor
15
    extends HelperBase
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 "HelperBase" and comma; 1 found
Loading history...
16
    implements ProcessorContract
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
17
{
18
19
    /**
20
     * @inheritdoc
21
     */
22 19
    final public function __invoke($tokenString, $isRefresh = false)
23
    {
24 19
        $token = (new Parser())->parse((string) $tokenString);
25 19
        $signer = new Sha256();
26 19
        $claims = $token->getClaims();
27
        if (
28 19
                !$token->verify($signer, $this->config['secret']) 
29 19
            ||  !$this->checkRequiredClaims(array_keys($claims))
30 18
            ||  !$this->validateToken($token, $isRefresh)
31 19
        ) {
32 6
            return false;
33 4
        };
34 13
        foreach ($claims as $key => $value) {
35 13
            $claims[$key] = $value->getValue();
36 13
        }
37 13
        return new Token((string) $token, $claims);
38
    }
39
40
    /**
41
     * Validate token with validation data
42
     * 
43
     * @param  JwtToken $token
44
     * @param  boolean $isRefresh   Is a token refresh happening
45
     * @return boolean
46
     */
47 17
    private function validateToken(JwtToken $token, $isRefresh = false)
48
    {
49 17
        $data = new ValidationData();
50 17
        if (isset($this->config['issuer'])) {
51 17
            $data->setIssuer($this->config['issuer']);
52 17
        }
53 17
        if (isset($this->config['audience'])) {
54 1
            $data->setAudience($this->config['audience']);
55 1
        }
56 17
        if ($isRefresh) {
57 7
            $data->setExpiration(time() - $this->config['refreshOffsetAllowance']);
58 7
        }
59 17
        if (!$token->validate($data)) {
60 4
            return false;
61
        }
62 13
        return true;
63
    }
64
}
65