Passed
Push — master ( ad05d4...e3df8f )
by Vince
01:36
created

jwt::getExpires()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * ==================================
4
 * Responsible PHP API
5
 * ==================================
6
 *
7
 * @link Git https://github.com/vince-scarpa/responsibleAPI.git
8
 *
9
 * @api Responible API
10
 * @package responsible\core\auth
11
 *
12
 * @author Vince scarpa <[email protected]>
13
 *
14
 */
15
namespace responsible\core\auth;
16
17
use responsible\core\auth;
18
use responsible\core\exception;
19
use responsible\core\keys;
20
use responsible\core\configuration;
21
use responsible\core\headers\header;
22
23
class jwt extends \responsible\core\auth\authorise
24
{
25
    /**
26
     * [CTY - Json Web Token content type]
27
     * @link https://tools.ietf.org/html/rfc7519#section-5.2
28
     */
29
    const CYT = 'JWT';
30
31
    /**
32
     * [$TIMESTAMP Set the current timestamp]
33
     * @var integer
34
     */
35
    protected static $TIMESTAMP;
36
37
    /**
38
     * [$LEEWAY Cater for time skew in sever time differences]
39
     * @var integer
40
     */
41
    protected static $LEEWAY = 10;
42
43
    /**
44
     * [$EXPIRES Default token expiry]
45
     * 300 = 5 minutes
46
     * @var integer
47
     */
48
    protected $EXPIRES = 300;
49
50
    /**
51
     * [$algorithyms Supported algorithms]
52
     * @var array
53
     */
54
    protected static $ALGORITHMS = [
55
        'HS256','sha256',
56
        'HS384','sha384',
57
        'HS512','sha512',
58
    ];
59
60
    /**
61
     * [$ALGORITHMS_ACRONYM Get the JWT acronym support]
62
     * @var array
63
     */
64
    protected static $ALGORITHMS_ACRONYM = [
65
        'sha256' => ['hash' => 'sha256'],
66
        'sha384' => ['hash' => 'sha384'],
67
        'sha512' => ['hash' => 'sha512'],
68
        'HS256' => ['hash' => 'sha256'],
69
        'HS384' => ['hash' => 'sha384'],
70
        'HS512' => ['hash' => 'sha512'],
71
    ];
72
73
    /**
74
     * [$token]
75
     * @var string
76
     */
77
    protected $token;
78
79
    /**
80
     * [$key Client secret key]
81
     * @var string
82
     */
83
    protected $key;
84
85
    /**
86
     * [$payload Clients payload]
87
     * @var array
88
     */
89
    protected $payload;
90
91
    /**
92
     * Responsible API options
93
     */
94
    protected static $options;
95
96
    /**
97
     * [__construct]
98
     */
99
    public function __construct()
100
    {
101
        self::$TIMESTAMP = (new \DateTime('now'))->getTimestamp();
102
    }
103
104
    /**
105
     * [encode]
106
     * @return string
107
     */
108
    public function encode()
109
    {
110
        $encode = new auth\jwtEncoder;
111
112
        $encoded =
113
        $encode->setPayload($this->getPayload())
114
            ->key($this->getKey())
115
            ->encode()
116
        ;
117
118
        return $encoded;
119
    }
120
121
    /**
122
     * [decode Decode the token]
123
     * @return array
124
     */
125
    public function decode()
126
    {
127
        $decode = new auth\jwtDecoder;
128
129
        $decoded =
130
        $decode->token($this->getToken())
131
            ->key($this->getKey())
132
            ->decode()
133
        ;
134
135
        return $decoded;
136
    }
137
138
    /**
139
     * [token Set the token]
140
     *
141
     * @param  string
142
     * @return self
143
     */
144
    public function token($token = null)
145
    {
146
        if (is_null($token) || empty($token) || !is_string($token)) {
147
            $this->setUnauthorised();
148
        }
149
150
        $this->token = $token;
151
152
        return $this;
153
    }
154
155
    /**
156
     * [key - Set the secret key]
157
     *
158
     * @param  string
159
     * @return self
160
     */
161
    public function key($key = null)
162
    {
163
        if (is_null($key) || empty($key) || !is_string($key)) {
164
            $this->setUnauthorised();
165
        }
166
167
        $this->key = $key;
168
169
        return $this;
170
    }
171
172
    /**
173
     * [setUnauthorised Render unauthorised response]
174
     */
175
    protected function setUnauthorised()
176
    {
177
        $header = new header;
178
        $header->setOptions($this->getOptions());
179
        $header->unauthorised();
180
        // @codeCoverageIgnoreStart
181
    }
182
    // @codeCoverageIgnoreEnd
183
184
    /**
185
     * [payload Set the clients payload]
186
     * @param  array $payload
187
     * @return self
188
     */
189
    public function setPayload($payload)
190
    {
191
        $this->payload = $payload;
192
        return $this;
193
    }
194
195
    /**
196
     * [getToken Get the Json Web Token]
197
     * @return string
198
     */
199
    protected function getToken()
200
    {
201
        return $this->token;
202
    }
203
204
    /**
205
     * [getKey Get the client secret key]
206
     * @return string
207
     */
208
    protected function getKey()
209
    {
210
        return $this->key;
211
    }
212
213
    /**
214
     * [getPayload Get the clients payload]
215
     * @return array
216
     */
217
    protected function getPayload()
218
    {
219
        return $this->payload;
220
    }
221
222
    /**
223
     * [getLeeway Get the default leeway]
224
     * @return integer
225
     */
226
    public static function getLeeway()
227
    {
228
        return self::$LEEWAY;
229
    }
230
231
    /**
232
     * [getLeeway Get the default expiry]
233
     * @return integer
234
     */
235
    public function getExpires()
236
    {
237
        return $this->EXPIRES;
238
    }
239
240
    /**
241
     * [getTimestamp Get the current timestamp]
242
     * @return integer
243
     */
244
    public static function getCurrentTimestamp()
245
    {
246
        return self::$TIMESTAMP;
247
    }
248
249
    /**
250
     * [setOptions Inherit Responsible API options]
251
     * @param array $options
252
     */
253
    public function setOptions($options)
254
    {
255
        parent::setOptions($options);
256
        self::$options = $options;
257
        return $this;
258
    }
259
260
    /**
261
     * [getOptions]
262
     * @return array
263
     */
264
    public function getOptions():?array
265
    {
266
        return self::$options;
267
    }
268
269
    /**
270
     * [messages Common error messages]
271
     * @param  string $type [message type]
272
     * @return array
273
     */
274
    protected static function messages($type)
275
    {
276
        $error = [];
277
278
        switch ($type) {
279
            case 'denied_token':
280
                $error = [
281
                    'error' => 'invalid token',
282
                    'description' => 'Permission denied - invalid token'
283
                ];
284
                break;
285
286
            case 'denied_key':
287
                $error = [
288
                    'error' => 'invalid key',
289
                    'description' => 'Permission denied - invalid key'
290
                ];
291
                break;
292
293
            case 'expired':
294
                $error = [
295
                    'error' => 'expired',
296
                    'description' => 'Token expired'
297
                ];
298
                break;
299
300
            case 'not_ready':
301
                $error = [
302
                    'error' => 'not ready',
303
                    'description' => 'The token supplied is not ready to be accessed at the moment.'
304
                ];
305
                break;
306
        }
307
308
        return $error;
309
    }
310
311
    /**
312
     * [getAlgorithm Check if the algorithm in the header is supported by the Responsible API]
313
     * @param  string $type [Algorithm hash]
314
     * @return mixed
315
     */
316
    public static function getAlgorithm($type = '')
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

316
    public static function getAlgorithm(/** @scrutinizer ignore-unused */ $type = '')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
317
    {
318
        return self::resolveAlgorithm();
319
    }
320
321
    /**
322
     * [resolveAlgorithm Resolve the algorythm to use in the JWT header]
323
     * @return array
324
     */
325
    protected static function resolveAlgorithm()
326
    {
327
        $algoKey = (self::$options['jwt']['algo']) ?? 'HS256';
328
        $algoKey = (isset(self::$ALGORITHMS_ACRONYM[$algoKey])) ? $algoKey : 'HS256';
329
330
        $ALGO = [
331
            'header' => $algoKey,
332
            'hash' => self::$ALGORITHMS_ACRONYM[$algoKey]['hash'],
333
        ];
334
335
        if (array_search($algoKey, self::$ALGORITHMS) !== FALSE) {
336
            $ALGO = [
337
                'header' => $algoKey,
338
                'hash' => self::$ALGORITHMS_ACRONYM[$algoKey]['hash'],
339
            ];
340
        }
341
342
        return $ALGO;
343
    }
344
}
345