1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Zortje\MVC\Storage\Cookie; |
5
|
|
|
|
6
|
|
|
use Lcobucci\JWT\Builder; |
7
|
|
|
use Lcobucci\JWT\Claim; |
8
|
|
|
use Lcobucci\JWT\Parser; |
9
|
|
|
use Lcobucci\JWT\Signer\Hmac\Sha256; |
10
|
|
|
use Lcobucci\JWT\ValidationData; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Cookie |
14
|
|
|
* |
15
|
|
|
* @package Zortje\MVC\Storage |
16
|
|
|
*/ |
17
|
|
|
class Cookie |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
const ISSUER = 'zortje/mvc'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string[] Internal cookie values |
24
|
|
|
*/ |
25
|
|
|
protected $values = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Cookie constructor. |
29
|
|
|
* |
30
|
|
|
* @param string $token JWT token string |
31
|
|
|
*/ |
32
|
2 |
|
public function __construct(string $token = '') |
33
|
|
|
{ |
34
|
|
|
// @todo SECRET key should be in configuration |
35
|
2 |
|
$secret = 'super-secret-key'; |
36
|
|
|
|
37
|
|
|
try { |
38
|
2 |
|
$token = (new Parser())->parse($token); |
39
|
|
|
|
40
|
2 |
|
$data = new ValidationData(); // It will use the current time to validate (iat, nbf and exp) |
41
|
2 |
|
$data->setIssuer(self::ISSUER); |
42
|
|
|
|
43
|
2 |
|
if ($token->validate($data) && $token->verify(new Sha256(), $secret)) { |
44
|
|
|
/** |
45
|
|
|
* @var Claim $claim |
46
|
|
|
*/ |
47
|
1 |
|
$ignored = array_fill_keys(['iss', 'exp'], true); |
48
|
|
|
|
49
|
1 |
|
foreach ($token->getClaims() as $claim) { |
50
|
1 |
|
if (isset($ignored[$claim->getName()])) { |
51
|
1 |
|
continue; |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
$this->values[$claim->getName()] = $claim->getValue(); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} catch (\InvalidArgumentException $e) { |
|
|
|
|
58
|
|
|
} |
59
|
2 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Set value in cookie |
63
|
|
|
* |
64
|
|
|
* @param string $key Cookie key |
65
|
|
|
* @param string $value Cookie value |
66
|
|
|
*/ |
67
|
2 |
|
public function set(string $key, string $value) |
68
|
|
|
{ |
69
|
2 |
|
$this->values[$key] = $value; |
70
|
2 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get value from cookie |
74
|
|
|
* |
75
|
|
|
* @param string $key Cookie key |
76
|
|
|
* |
77
|
|
|
* @return string Cookie value |
78
|
|
|
*/ |
79
|
1 |
|
public function get(string $key): string |
80
|
|
|
{ |
81
|
1 |
|
return $this->values[$key]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getTokenString(): string |
85
|
|
|
{ |
86
|
|
|
// @todo SECRET key should be in configuration |
87
|
|
|
$secret = 'super-secret-key'; |
88
|
|
|
|
89
|
|
|
// @todo should cookie TTL be set in a configuration? |
90
|
|
|
$cookieTTL = '+1 hour'; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Build Token |
94
|
|
|
*/ |
95
|
|
|
$builder = (new Builder()); |
96
|
|
|
$builder->setIssuer(self::ISSUER); |
97
|
|
|
$builder->setExpiration((new \DateTime($cookieTTL))->getTimestamp()); |
98
|
|
|
|
99
|
|
|
foreach ($this->values as $key => $value) { |
100
|
|
|
$builder->set($key, $value); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Sign and generate new token |
105
|
|
|
*/ |
106
|
|
|
$builder->sign(new Sha256(), $secret); |
107
|
|
|
|
108
|
|
|
$token = $builder->getToken(); |
109
|
|
|
|
110
|
|
|
return (string) $token; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|