Completed
Push — master ( 7a1c5d...542831 )
by Jacob
02:09
created

Processor   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
c 2
b 0
f 0
lcom 0
cbo 6
dl 0
loc 39
ccs 26
cts 26
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
D __invoke() 0 31 10
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
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
     * @inheritdoc
20
     */
21 16
    final public function __invoke($tokenString, $validateClaims = [])
22
    {
23 16
        $token = (new Parser())->parse((string) $tokenString);
24 16
        $signer = new Sha256();
25 16
        if (!$token->verify($signer, $this->config['secret'])) {
26 1
            return false;
27
        }
28 15
        if (!$this->checkRequiredClaims(array_keys($token->getClaims()))) {
29 1
            return false;
30 6
        };
31 14
        $data = new ValidationData();
32 14
        if (isset($this->config['issuer'])) {
33 14
            $data->setIssuer($this->config['issuer']);
34 14
        }
35 14
        if (isset($this->config['audience'])) {
36 1
            $data->setAudience($this->config['audience']);
37 1
        }
38 14
        if (!$token->validate($data)) {
39 2
            return false;
40
        }
41 12
        $claims = $token->getClaims();
42 12
        foreach ($claims as $key => $value) {
43 12
            $claims[$key] = $value->getValue();
44 12
        }
45 12
        foreach ($validateClaims as $claim => $value) {
46 10
            if (!isset($claims[$claim]) || $claims[$claim] !== $value) {
47 3
                return false;
48
            }
49 9
        }
50 9
        return new Token((string) $token, $claims);
51
    }
52
}
53