1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2016 Canis.io |
4
|
|
|
* @license MIT |
5
|
|
|
*/ |
6
|
|
|
namespace Canis\Lumen\Jwt\Adapters\Lcobucci; |
7
|
|
|
|
8
|
|
|
use Lcobucci\JWT\ValidationData; |
|
|
|
|
9
|
|
|
use Lcobucci\JWT\Builder; |
10
|
|
|
use Lcobucci\JWT\Signer\Hmac\Sha256; |
11
|
|
|
use Lcobucci\JWT\Parser; |
12
|
|
|
use Canis\Lumen\Jwt\Token; |
13
|
|
|
use Canis\Lumen\Jwt\Contracts\Generator as GeneratorContract; |
14
|
|
|
|
15
|
|
|
class Generator |
16
|
|
|
extends HelperBase |
|
|
|
|
17
|
|
|
implements GeneratorContract |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Generates the token |
21
|
|
|
* @param array $claims |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
27 |
|
final public function __invoke(array $claims) |
25
|
|
|
{ |
26
|
27 |
|
$signer = new Sha256(); |
27
|
27 |
|
$builder = new Builder(); |
28
|
27 |
|
$claims = array_merge($this->getDefaultClaims(), $claims, $this->getForcedClaims()); |
29
|
27 |
|
if (!$this->checkRequiredClaims(array_keys($claims))) { |
30
|
2 |
|
return false; |
31
|
13 |
|
}; |
32
|
25 |
|
foreach ($claims as $claim => $value) { |
33
|
25 |
|
if ($this->isBadClaim($claim)) { |
34
|
1 |
|
continue; |
35
|
|
|
} |
36
|
25 |
|
$builder->set($claim, $value); |
37
|
25 |
|
} |
38
|
25 |
|
$jti = substr(hash('sha256', serialize($claims) . openssl_random_pseudo_bytes(20)), 0, 16); |
39
|
25 |
|
$builder->setId($jti, $this->config['jtiInHeader']); |
40
|
25 |
|
$builder->sign($signer, $this->config['secret']); |
41
|
25 |
|
$token = $builder->getToken(); |
42
|
25 |
|
$generatedClaims = $token->getClaims(); |
43
|
25 |
|
foreach ($generatedClaims as $key => $value) { |
44
|
25 |
|
$generatedClaims[$key] = $value->getValue(); |
45
|
25 |
|
} |
46
|
25 |
|
return new Token((string) $token, $generatedClaims); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Default claims (can be overriden) |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
27 |
|
protected function getDefaultClaims() |
55
|
|
|
{ |
56
|
27 |
|
$default = []; |
57
|
27 |
|
$default['nbf'] = time() + $this->config['nbfOffset']; |
58
|
27 |
|
$default['exp'] = time() + $this->config['expOffset']; |
59
|
27 |
|
if (!empty($this->config['issuer'])) { |
60
|
27 |
|
$default['iss'] = $this->config['issuer']; |
61
|
27 |
|
} |
62
|
27 |
|
if (!empty($this->config['audience'])) { |
63
|
2 |
|
$default['aud'] = $this->config['audience']; |
64
|
2 |
|
} |
65
|
27 |
|
return $default; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Forced claims |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
27 |
|
private function getForcedClaims() |
74
|
|
|
{ |
75
|
|
|
return [ |
76
|
27 |
|
'iat' => time() |
77
|
27 |
|
]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Checks if claim is bad |
82
|
|
|
* |
83
|
|
|
* @param string $claim |
84
|
|
|
* @return boolean |
85
|
|
|
*/ |
86
|
25 |
|
private function isBadClaim($claim) |
87
|
|
|
{ |
88
|
25 |
|
return in_array($claim, ['jti']); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: