Passed
Push — master ( 9e2345...346dc0 )
by Sébastien
02:10
created

TokenManager   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 63
ccs 0
cts 45
cp 0
rs 10
c 0
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A isExpired() 0 3 1
A signToken() 0 4 1
A parseToken() 0 7 2
A createNewToken() 0 19 3
A __construct() 0 6 1
A verifySignature() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Service;
6
7
use App\Service\Exception\InvalidTokenException;
8
use Lcobucci\JWT\Builder;
9
use Lcobucci\JWT\Parser;
10
use Lcobucci\JWT\Signer\Hmac\Sha256;
11
use Lcobucci\JWT\Token;
12
use Ramsey\Uuid\Uuid;
13
14
class TokenManager
15
{
16
    private $signer;
17
    private $issuer;
18
    private $audience;
19
    private $privateKey;
20
21
    public function __construct(string $privateKey)
22
    {
23
        $this->signer     = new Sha256();
24
        $this->issuer     = $_SERVER['SERVER_NAME'];
25
        $this->audience   = $_SERVER['SERVER_NAME'];
26
        $this->privateKey = $privateKey;
27
    }
28
29
    public function createNewToken(array $customClaims = [], int $expiration = 3600, bool $autoSign = true): Token
30
    {
31
        $builder = (new Builder())
32
            ->setIssuer($this->issuer) // Configures the issuer (iss claim)
33
            ->setAudience($this->audience) // Configures the audience (aud claim)
34
            ->setId(Uuid::uuid1()->toString(), true) // Configures the id (jti claim), replicating as a header item
35
            ->setIssuedAt(time()) // Configures the time that the token was issue (iat claim)
36
            ->setNotBefore(time() + 0) // Configures the time that the token can be used (nbf claim)
37
            ->setExpiration(time() + $expiration); // Configures the expiration time of the token (exp claim)
38
39
        foreach ($customClaims as $key => $value) {
40
            $builder->set($key, $value);
41
        }
42
43
        if ($autoSign) {
44
            return $this->signToken($builder);
45
        }
46
47
        return $builder->getToken();
48
    }
49
50
    public function signToken(Builder $builder): Token
51
    {
52
        return $builder->sign($this->signer, $this->privateKey)
53
            ->getToken(); // Retrieves the generated token
54
    }
55
56
    /**
57
     * @throws InvalidTokenException
58
     */
59
    public function parseToken(string $token): Token
60
    {
61
        $tokenParser = new Parser();
62
        try {
63
            return $tokenParser->parse($token);
64
        } catch (\Throwable $e) {
65
            throw new InvalidTokenException($e->getMessage());
66
        }
67
    }
68
69
    public function verifySignature(Token $token): bool
70
    {
71
        return $token->verify($this->signer, $this->privateKey);
72
    }
73
74
    public function isExpired(Token $token): bool
75
    {
76
        return $token->isExpired();
77
    }
78
}
79