Completed
Push — master ( 9a6f5b...ef0055 )
by Pierre
02:26
created

Token   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 215
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 54
dl 0
loc 215
ccs 58
cts 58
cp 1
rs 10
c 0
b 0
f 0
wmc 15

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setIssueAtDelay() 0 4 2
A getConfigAlgo() 0 3 1
A __construct() 0 8 1
A getToken() 0 3 1
A getConfig() 0 4 1
A setIssueAt() 0 4 2
A setToken() 0 4 1
A getToEncodePayload() 0 25 1
A getConfigSecret() 0 3 1
A setTtl() 0 4 2
A decode() 0 6 1
A encode() 0 8 1
1
<?php
2
3
/**
4
 * Description of App\Component\Jwt\Token
5
 *
6
 * @author pierrefromager
7
 */
8
9
namespace App\Component\Jwt;
10
11
use Firebase\JWT\JWT as Fjwt;
12
use App\Config;
13
use App\Http\Request;
14
15
class Token implements Interfaces\IToken
16
{
17
    /**
18
     * token time value to be issue
19
     *
20
     * @var int
21
     */
22
    private $issueAt;
23
24
    /**
25
     * token time to live
26
     *
27
     * @var int
28
     */
29
    private $ttl;
30
31
    /**
32
     * token delay before issue
33
     *
34
     * @var int
35
     */
36
    private $issueAtDelay;
37
38
    /**
39
     * config
40
     *
41
     * @var Config
42
     */
43
    private $config;
44
45
    /**
46
     * request
47
     *
48
     * @var Request
49
     */
50
    private $request;
51
52
    /**
53
     * token
54
     *
55
     * @var String
56
     */
57
    private $token;
58
59
    /**
60
     * instanciate
61
     *
62
     * @param Config $config
63
     * @param Request $request
64
     */
65 8
    public function __construct(Config $config, Request $request)
66
    {
67 8
        $this->config = $config;
68 8
        $this->request = $request;
69 8
        $this->token = '';
70 8
        $this->setIssueAt(0);
71 8
        $this->setIssueAtDelay(0);
72 8
        $this->setTtl(0);
73
    }
74
75
    /**
76
     * set token string
77
     *
78
     * @param string $token
79
     * @return Token
80
     */
81 1
    protected function setToken(string $token): Token
82
    {
83 1
        $this->token = $token;
84 1
        return $this;
85
    }
86
87
    /**
88
     * get token string
89
     *
90
     * @return string
91
     */
92 1
    protected function getToken(): string
93
    {
94 1
        return $this->token;
95
    }
96
97
    /**
98
     * encode
99
     *
100
     * @param string $uid
101
     * @param string $login
102
     * @param string $password
103
     * @return string
104
     */
105 2
    public function encode(int $uid, string $login, string $password): string
106
    {
107 2
        $this->setToken(Fjwt::encode(
108 2
            $this->getToEncodePayload($uid, $login, $password),
109 2
            $this->getConfigSecret(),
110 2
            $this->getConfigAlgo()
111
        ));
112 2
        return $this->token;
113
    }
114
115
    /**
116
     * return to encode payload datas
117
     *
118
     * @param integer $uid
119
     * @param string $login
120
     * @param string $password
121
     * @return array
122
     */
123 1
    protected function getToEncodePayload(
124
        int $uid,
125
        string $login,
126
        string $password
127
    ): array {
128 1
        $tokenId = base64_encode(
129 1
            openssl_random_pseudo_bytes(self::_RANDOM_BYTES_LEN)
130
        );
131 1
        $issuedAt = time() - 100;
132 1
        $notBefore = $issuedAt + $this->issueAtDelay; //Adding 10 seconds
133 1
        $expire = $notBefore + $this->ttl; // Adding 60 seconds
134 1
        $serverName = $this->request->getHost();
135
        return [
136 1
            self::_IAT => $issuedAt, // Issued at: time when the token was generated
137 1
            self::_JTI => $tokenId, // Json Token Id: an unique identifier for the token
138 1
            self::_ISS => $serverName, // Issuer
139 1
            self::_NBF => $notBefore, // Not before
140 1
            self::_EXP => $expire, // Expire
141 1
            self::_DATA => [// Data related to the signer user
142 1
                self::_DATA_ID => $uid, // userid from the users table
143 1
                self::_DATA_LOGIN => $login, // User name
144 1
                self::_DATA_PASSWORD_HASH => password_hash($password, PASSWORD_DEFAULT),
145 1
                self::_DATA_IAT_S => strftime('%c', $issuedAt),
146 1
                self::_DATA_NBF_S => strftime('%c', $notBefore),
147 1
                self::_DATA_EXP_S => strftime('%c', $expire), // Expire
148
            ]
149
        ];
150
    }
151
152
    /**
153
     * decode
154
     *
155
     * @param string $token
156
     * @return mixed
157
     */
158 1
    public function decode(string $token)
159
    {
160 1
        return Fjwt::decode(
161 1
            $token,
162 1
            $this->getConfigSecret(),
163 1
            [$this->getConfigAlgo()]
164
        );
165
    }
166
167
    /**
168
     * return secret from jwt config
169
     *
170
     * @return string
171
     */
172 1
    protected function getConfigSecret(): string
173
    {
174 1
        return $this->getConfig()[self::_SECRET];
175
    }
176
177
    /**
178
     * return algo from jwt config
179
     *
180
     * @return string
181
     */
182 1
    protected function getConfigAlgo(): string
183
    {
184 1
        return $this->getConfig()[self::_ALGO];
185
    }
186
187
    /**
188
     * set token issue at time
189
     *
190
     * @param integer $dateTime
191
     * @return Token
192
     */
193 8
    public function setIssueAt(int $dateTime): Token
194
    {
195 8
        $this->issueAt = ($dateTime > 0) ? $dateTime : time();
196 8
        return $this;
197
    }
198
199
    /**
200
     * setIssueAtDelay
201
     *
202
     * @param int $delay
203
     */
204 8
    public function setIssueAtDelay(int $delay): Token
205
    {
206 8
        $this->issueAtDelay = ($delay > 0) ? $delay : self::_ISSUE_AT_DELAY;
207 8
        return $this;
208
    }
209
210
    /**
211
     * set token ttl
212
     *
213
     * @param int $ttl
214
     */
215 8
    public function setTtl(int $ttl): Token
216
    {
217 8
        $this->ttl = ($ttl > 0) ? $ttl : self::_TTL;
218 8
        return $this;
219
    }
220
221
    /**
222
     * get token config
223
     *
224
     * @return array
225
     */
226 1
    protected function getConfig(): array
227
    {
228 1
        return $this->config->getSettings(
229 1
            self::_CONFIG_KEY
230
        );
231
    }
232
}
233