LcobucciParser::getParsedToken()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
namespace Equip\Auth\Jwt;
3
4
use Equip\Auth\Exception\InvalidException;
5
use Equip\Auth\Token;
6
use Lcobucci\JWT\Parser;
7
use Lcobucci\JWT\Signer;
8
use Lcobucci\JWT\Token as ParsedToken;
9
use Lcobucci\JWT\ValidationData;
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 4
    public function __construct(
44
        Parser $parser,
45
        Signer $signer,
46
        Configuration $config,
47
        ValidationData $validation
48
    )
49
    {
50 4
        $this->parser = $parser;
51 4
        $this->signer = $signer;
52 4
        $this->config = $config;
53 4
        $this->validation = $validation;
54 4
    }
55
56
    /**
57
     * @inheritDoc
58
     */
59 4
    public function parseToken($token)
60
    {
61 4
        $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 4
    protected function getParsedToken($token)
74
    {
75
        try {
76 4
            return $this->parser->parse((string) $token);
77 1
        } catch (\InvalidArgumentException $e) {
78 1
            throw InvalidException::tokenUnparseable($token, $e);
79
        }
80
    }
81
82
    /**
83
     * @param ParsedToken $parsed
84
     * @throws InvalidException if token validation fails
85
     */
86 3
    protected function verifyParsedToken(ParsedToken $parsed)
87
    {
88 3
        if ($parsed->verify($this->signer, $this->config->getPublicKey())) {
89 2
            return;
90
        }
91 1
        throw InvalidException::invalidSignature((string) $parsed);
92
    }
93
94
    /**
95
     * @param ParsedToken $parsed
96
     * @throws InvalidException if token validation fails
97
     */
98 2
    protected function validateParsedToken(ParsedToken $parsed)
99
    {
100 2
        if ($parsed->validate($this->validation)) {
101 1
            return;
102
        }
103 1
        throw InvalidException::invalidToken((string) $parsed);
104
    }
105
106
    /**
107
     * @param ParsedToken $parsed
108
     * @return array
109
     */
110 1
    protected function getTokenMetadata(ParsedToken $parsed)
111
    {
112 1
        $metadata = [];
113 1
        foreach ($parsed->getClaims() as $name => $claim) {
114 1
            $metadata[$name] = $claim->getValue();
115 1
        }
116 1
        return $metadata;
117
    }
118
}
119