JwtService   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 149
ccs 33
cts 33
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 4
A createToken() 0 28 3
A parsePlainToken() 0 15 2
A verifyPlainToken() 0 6 1
A getSigner() 0 4 1
A getPrivateKey() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\Wallit\Service;
6
7
use Soluble\Wallit\Token\Exception as TokenException;
8
use Lcobucci\JWT\Builder;
9
use Lcobucci\JWT\Token;
10
use Lcobucci\JWT\Signer;
11
use Lcobucci\JWT\Parser;
12
13
class JwtService implements TokenServiceInterface
14
{
15
    /**
16
     * @var Signer
17
     */
18
    protected $signer;
19
20
    /**
21
     * @var Parser
22
     */
23
    protected $tokenParser;
24
25
    /**
26
     * @var string
27
     */
28
    protected $verificationKey;
29
30
    /**
31
     * @var string|null
32
     */
33
    protected $publicKey;
34
35
    /**
36
     * @var int
37
     */
38
    protected $expiration;
39
40
    /**
41
     * JwtService constructor.
42
     *
43
     * @param Signer $signer
44
     * @param string $verificationKey
45
     * @param string $publicKey       Only needed for asymmetric support
46
     */
47 20
    public function __construct(
48
        Signer $signer,
49
                                string $verificationKey,
50
                                string $publicKey = null
51
    ) {
52 20
        if (trim($verificationKey) === '') {
53 2
            throw new \InvalidArgumentException('Verification key (private key) cannot be empty');
54
        }
55
56 18
        if ($publicKey !== null && trim($publicKey) === '') {
57 1
            throw new \InvalidArgumentException('If public key is provided it cannot be empty');
58
        }
59
60 17
        $this->signer = $signer;
61 17
        $this->verificationKey = $verificationKey;
62 17
        $this->publicKey = $publicKey;
63 17
        $this->expiration = time() + 3600;
64 17
    }
65
66
    /**
67
     * Create new signed token.
68
     *
69
     * @param mixed[] $claims
70
     * @param int     $expiration
71
     *
72
     * @return Token
73
     */
74 7
    public function createToken(array $claims = [], int $expiration = null): Token
75
    {
76 7
        if ($expiration === null) {
77 6
            $expiration = $this->expiration;
78
        }
79
80 7
        $jwtBuilder = new Builder();
81
82 7
        foreach ($claims as $key => $value) {
83 5
            $jwtBuilder->set($key, $value);
84
        }
85
86 7
        $jwtBuilder->setIssuedAt(time()) // Configures the time that the token was issue (iat claim)
87 7
                   ->setExpiration($expiration); // Configures the expiration time of the token (nbf claim)
88
89
        /*
90
        ->setIssuer('http://example.com') // Configures the issuer (iss claim)
91
        ->setAudience('http://example.org') // Configures the audience (aud claim)
92
        ->setNotBefore(time() + 60) // Configures the time that the token can be used (nbf claim)
93
        ->setId('4f1g23a12aa', true) // Configures the id (jti claim), replicating as a header item
94
        //$jwtBuilder->set('uid', 1); // Configures a new claim, called "uid"
95
        */
96
97
        // This will change with lcobucci v4
98 7
        $jwtBuilder->sign($this->signer, $this->verificationKey);
99
100 7
        return $jwtBuilder->getToken();
101
    }
102
103
    /**
104
     * Parse a token string into a JWT\Token.
105
     *
106
     * @throws TokenException\InvalidTokenException
107
     *
108
     * @param string $tokenString
109
     *
110
     * @return Token
111
     */
112 12
    public function parsePlainToken(string $tokenString): Token
113
    {
114 12
        $tokenParser = new Parser();
115
        try {
116 12
            $token = $tokenParser->parse($tokenString);
117 3
        } catch (\Throwable $invalidToken) {
118 3
            throw new TokenException\InvalidTokenException('Cannot parse the JWT token', 1, $invalidToken);
119
        }
120
        /*
121
        if (!$token->validate(new ValidationData())) {
122
            throw new Exception\InvalidTokenException('Validation of JWT token failed', 2);
123
        }
124
        */
125 9
        return $token;
126
    }
127
128
    /**
129
     * Parse and verify a token.
130
     *
131
     * @throws TokenException\InvalidTokenException
132
     *
133
     * @param string $tokenString
134
     *
135
     * @return bool
136
     */
137 3
    public function verifyPlainToken(string $tokenString): bool
138
    {
139 3
        $token = $this->parsePlainToken($tokenString);
140
141 2
        return $token->verify($this->signer, $this->verificationKey);
142
    }
143
144
    /**
145
     * @return Signer
146
     */
147 5
    public function getSigner(): Signer
148
    {
149 5
        return $this->signer;
150
    }
151
152
    /**
153
     * Return private key.
154
     *
155
     * @return string
156
     */
157 5
    public function getPrivateKey(): string
158
    {
159 5
        return $this->verificationKey;
160
    }
161
}
162