Completed
Push — master ( 0d9a84...e6b01d )
by Jacob
02:04
created

Processor::validateClaims()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.2
cc 4
eloc 5
nc 3
nop 2
crap 4
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\ValidationData;
11
use Lcobucci\JWT\Signer\Hmac\Sha256;
12
use Lcobucci\JWT\Parser;
13
use Lcobucci\JWT\Token as JwtToken;
14
15
class Processor
16
    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...
17
    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...
18
{
19
20
    /**
21
     * @inheritdoc
22
     */
23 12
    final public function __invoke($tokenString)
24
    {
25 12
        $token = (new Parser())->parse((string) $tokenString);
26 12
        $signer = new Sha256();
27 12
        $claims = $token->getClaims();
28
        if (
29 12
                !$token->verify($signer, $this->config['secret']) 
30 12
            ||  !$this->checkRequiredClaims(array_keys($claims))
31 11
            ||  !$this->validateToken($token)
32 12
        ) {
33 4
            return false;
34 2
        };
35 8
        foreach ($claims as $key => $value) {
36 8
            $claims[$key] = $value->getValue();
37 8
        }
38 8
        return new Token((string) $token, $claims);
39
    }
40
41
    /**
42
     * Validate token with validation data
43
     * 
44
     * @param  JwtToken $token
45
     * @return boolean
46
     */
47 10
    private function validateToken(JwtToken $token)
48
    {
49 10
        $data = new ValidationData();
50 10
        if (isset($this->config['issuer'])) {
51 10
            $data->setIssuer($this->config['issuer']);
52 10
        }
53 10
        if (isset($this->config['audience'])) {
54 1
            $data->setAudience($this->config['audience']);
55 1
        }
56 10
        if (!$token->validate($data)) {
57 2
            return false;
58
        }
59 8
        return true;
60
    }
61
}
62