Completed
Push — master ( 098c1d...89670e )
by Jacob
02:08
created

Processor::__invoke()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
ccs 18
cts 18
cp 1
rs 8.6737
cc 6
eloc 15
nc 10
nop 1
crap 6
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\Contracts\Processor as ProcessorContract;
9
use Canis\Lumen\Jwt\Exceptions\InvalidTokenException;
10
use Lcobucci\JWT\ValidationData;
11
use Lcobucci\JWT\Builder;
12
use Lcobucci\JWT\Signer\Hmac\Sha256;
13
use Lcobucci\JWT\Parser;
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
     * Parses and validates token
21
     * @param  string $tokenString
22
     * @return Token
23
     */
24 6
    final public function __invoke($tokenString)
25
    {
26 6
        $token = (new Parser())->parse((string) $tokenString);
27 6
        $signer = new Sha256();
28 6
        if (!$token->verify($signer, $this->config['secret'])) {
29 1
            return false;
30
        }
31 5
        if (!$this->checkRequiredClaims(array_keys($token->getClaims()))) {
32 1
            return false;
33 4
        };
34 4
        $data = new ValidationData();
35 4
        if (isset($this->config['issuer'])) {
36 4
            $data->setIssuer($this->config['issuer']);
37 4
        }
38 4
        if (isset($this->config['audience'])) {
39 1
            $data->setAudience($this->config['audience']);
40 1
        }
41 4
        if (!$token->validate($data)) {
42 2
            return false;
43
        }
44 2
        return $token;
45
    }
46
}
47