Completed
Pull Request — master (#1)
by Woody
13:01
created

FirebaseParser::parseToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 16
rs 9.4286
cc 2
eloc 11
nc 2
nop 1
1
<?php
2
namespace Equip\Auth\Jwt;
3
4
use Firebase\JWT\JWT;
5
use Firebase\JWT\ExpiredException;
6
use Equip\Auth\Jwt\Configuration;
7
use Equip\Auth\Exception\InvalidException;
8
use Equip\Auth\Token;
9
10
/**
11
 * Parser for JWT authentication token strings that uses the firebase/php-jwt
12
 * library.
13
 */
14
class FirebaseParser implements ParserInterface
15
{
16
    /**
17
     * @var Configuration
18
     */
19
    protected $config;
20
21
    /**
22
     * @param Configuration $config
23
     */
24
    public function __construct(Configuration $config)
25
    {
26
        $this->config = $config;
27
    }
28
29
    /**
30
     * @inheritDoc
31
     */
32
    public function parseToken($token)
33
    {
34
        try {
35
            $metadata = (array) JWT::decode(
36
                (string) $token,
37
                $this->config->getPublicKey(),
38
                [$this->config->getAlgorithm()]
39
            );
40
        } catch (ExpiredException $e) {
41
            throw new InvalidException(
42
                'Token has expired: ' . $token,
43
                InvalidException::CODE_TOKEN_EXPIRED
44
            );
45
        }
46
        return new Token($token, $metadata);
47
    }
48
}
49