Completed
Push — master ( c5bfa7...0d9a84 )
by Jacob
02:05
created

Processor   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 14
c 3
b 0
f 0
lcom 1
cbo 6
dl 0
loc 66
ccs 33
cts 33
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 20 6
A validateToken() 0 14 4
A validateClaims() 0 9 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
     * @inheritdoc
21
     */
22 16
    final public function __invoke($tokenString, $validateClaims = [])
23
    {
24 16
        $token = (new Parser())->parse((string) $tokenString);
25 16
        $signer = new Sha256();
26 16
        $claims = $token->getClaims();
27
        if (
28 16
                !$token->verify($signer, $this->config['secret']) 
29 16
            ||  !$this->checkRequiredClaims(array_keys($claims))
30 15
            ||  !$this->validateToken($token)
31 16
        ) {
32 4
            return false;
33 4
        };
34 12
        foreach ($claims as $key => $value) {
35 12
            $claims[$key] = $value->getValue();
36 12
        }
37 12
        if (!$this->validateClaims($claims, $validateClaims)) {
38 3
            return false;
39
        }
40 9
        return new Token((string) $token, $claims);
41
    }
42
43
    /**
44
     * Validate token with validation data
45
     * 
46
     * @param  JwtToken $token
47
     * @return boolean
48
     */
49 14
    private function validateToken(JwtToken $token)
50
    {
51 14
        $data = new ValidationData();
52 14
        if (isset($this->config['issuer'])) {
53 14
            $data->setIssuer($this->config['issuer']);
54 14
        }
55 14
        if (isset($this->config['audience'])) {
56 1
            $data->setAudience($this->config['audience']);
57 1
        }
58 14
        if (!$token->validate($data)) {
59 2
            return false;
60
        }
61 12
        return true;
62
    }
63
64
    /**
65
     * Validate the claims of the token
66
     * 
67
     * @param  array $claims
68
     * @param  array $validateClaims
69
     * @return boolean
70
     */
71 12
    private function validateClaims($claims, $validateClaims)
72
    {
73 12
        foreach ($validateClaims as $claim => $value) {
74 9
            if (!isset($claims[$claim]) || $claims[$claim] !== $value) {
75 3
                return false;
76
            }
77 9
        }
78 9
        return true;
79
    }
80
}
81