Completed
Pull Request — master (#1)
by Woody
23:31 queued 01:08
created

LcobucciParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4286
cc 1
eloc 9
nc 1
nop 4
crap 1
1
<?php
2
namespace Equip\Auth\Jwt;
3
4
use Lcobucci\JWT\Parser;
5
use Lcobucci\JWT\Signer;
6
use Lcobucci\JWT\Token as ParsedToken;
7
use Lcobucci\JWT\ValidationData;
8
use Equip\Auth\Exception\InvalidException;
9
use Equip\Auth\Token;
10
11
/**
12
 * Parser for JWT authentication token strings that uses the lcobucci/jwt
13
 * library.
14
 */
15
class LcobucciParser implements ParserInterface
16
{
17
    /**
18
     * @var Parser $parser
19
     */
20
    protected $parser;
21
22
    /**
23
     * @var Signer
24
     */
25
    protected $signer;
26
27
    /**
28
     * @var Configuration
29
     */
30
    protected $config;
31
32
    /**
33
     * @var ValidationData
34
     */
35
    protected $validation;
36
37
    /**
38
     * @param Parser $parser
39
     * @param Signer $signer
40
     * @param Configuration $config
41
     * @param ValidationData $validation
42
     */
43 3
    public function __construct(
44
        Parser $parser,
45
        Signer $signer,
46
        Configuration $config,
47
        ValidationData $validation
48
    )
49
    {
50 3
        $this->parser = $parser;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
51 3
        $this->signer = $signer;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
52 3
        $this->config = $config;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
53 3
        $this->validation = $validation;
54 3
    }
55
56
    /**
57
     * @inheritDoc
58
     */
59 3
    public function parseToken($token)
60
    {
61 3
        $parsed = $this->getParsedToken($token);
62 3
        $this->verifyParsedToken($parsed);
63 2
        $this->validateParsedToken($parsed);
64 1
        $metadata = $this->getTokenMetadata($parsed);
65 1
        return new Token($token, $metadata);
66
    }
67
68
    /**
69
     * @param string $token
70
     * @return ParsedToken
71
     * @throws InvalidException if token can't be parsed
72
     */
73 3
    protected function getParsedToken($token)
74
    {
75
        try {
76 3
            return $this->parser->parse((string) $token);
77 1
        } catch (\InvalidArgumentException $e) {
78 1
            throw new InvalidException(
79 1
                'Could not parse token: ' . $e->getMessage(),
80 1
                InvalidException::CODE_TOKEN_INVALID,
81
                $e
82
            );
83
        }
84
    }
85
86
    /**
87
     * @param ParsedToken $parsed
88
     * @throws InvalidException if token validation fails
89
     */
90 3
    protected function verifyParsedToken(ParsedToken $parsed)
91
    {
92 3
        if ($parsed->verify($this->signer, $this->config->getPublicKey())) {
93 2
            return;
94
        }
95 1
        throw new InvalidException(
96 1
            'Token signature is not valid',
97 1
            InvalidException::CODE_TOKEN_INVALID
98
        );
99
    }
100
101
    /**
102
     * @param ParsedToken $parsed
103
     * @throws InvalidException if token validation fails
104
     */
105 2
    protected function validateParsedToken(ParsedToken $parsed)
106
    {
107 2
        if ($parsed->validate($this->validation)) {
108 1
            return;
109
        }
110 1
        throw new InvalidException(
111 1
            'Token is expired or otherwise invalid',
112 1
            InvalidException::CODE_TOKEN_EXPIRED
113
        );
114
    }
115
116
    /**
117
     * @param ParsedToken $parsed
118
     * @return array
119
     */
120 1
    protected function getTokenMetadata(ParsedToken $parsed)
121
    {
122 1
        $metadata = [];
123 1
        foreach ($parsed->getClaims() as $name => $claim) {
124 1
            $metadata[$name] = $claim->getValue();
125
        }
126 1
        return $metadata;
127
    }
128
}
129