1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2019 Spomky-Labs |
9
|
|
|
* |
10
|
|
|
* This software may be modified and distributed under the terms |
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Jose\Easy; |
15
|
|
|
|
16
|
|
|
use InvalidArgumentException; |
17
|
|
|
use Jose\Component\Core\Algorithm as JoseAlgorithm; |
18
|
|
|
use Jose\Component\Signature\Algorithm; |
19
|
|
|
use Throwable; |
20
|
|
|
|
21
|
|
|
abstract class AbstractBuilder |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var JWT |
25
|
|
|
*/ |
26
|
|
|
protected $jwt; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var JoseAlgorithm[] |
30
|
|
|
*/ |
31
|
|
|
protected $algorithms = []; |
32
|
|
|
|
33
|
|
|
public function __construct() |
34
|
|
|
{ |
35
|
|
|
$this->jwt = new JWT(); |
36
|
|
|
$map = $this->getAlgorithmMap(); |
37
|
|
|
foreach ($map as $class) { |
38
|
|
|
if (class_exists($class)) { |
39
|
|
|
try { |
40
|
|
|
$this->algorithms[] = new $class(); |
41
|
|
|
} catch (Throwable $throwable) { |
42
|
|
|
//does nothing |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function payload(array $payload): self |
49
|
|
|
{ |
50
|
|
|
$clone = clone $this; |
51
|
|
|
$clone->jwt->claims->replace($payload); |
52
|
|
|
|
53
|
|
|
return $clone; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function iss(string $iss, bool $inHeader = false): self |
57
|
|
|
{ |
58
|
|
|
return $this->claim('iss', $iss, $inHeader); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function sub(string $sub, bool $inHeader = false): self |
62
|
|
|
{ |
63
|
|
|
return $this->claim('sub', $sub, $inHeader); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function aud(string $aud, bool $inHeader = false): self |
67
|
|
|
{ |
68
|
|
|
$audience = $this->jwt->claims->has('aud') ? $this->jwt->claims->get('aud') : []; |
69
|
|
|
$audience[] = $aud; |
70
|
|
|
|
71
|
|
|
return $this->claim('aud', $audience, $inHeader); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function jti(string $jti, bool $inHeader = false): self |
75
|
|
|
{ |
76
|
|
|
return $this->claim('jti', $jti, $inHeader); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function exp(int $exp, bool $inHeader = false): self |
80
|
|
|
{ |
81
|
|
|
return $this->claim('exp', $exp, $inHeader); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function iat(?int $iat = null, bool $inHeader = false): self |
85
|
|
|
{ |
86
|
|
|
$iat = $iat ?? time(); |
87
|
|
|
|
88
|
|
|
return $this->claim('iat', $iat, $inHeader); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function nbf(?int $nbf = null, bool $inHeader = false): self |
92
|
|
|
{ |
93
|
|
|
$nbf = $nbf ?? time(); |
94
|
|
|
|
95
|
|
|
return $this->claim('nbf', $nbf, $inHeader); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param Algorithm\SignatureAlgorithm|string $alg |
100
|
|
|
* |
101
|
|
|
* @throws InvalidArgumentException if the algorithm is not a string or an instance of Jose\Component\Core\Algorithm |
102
|
|
|
*/ |
103
|
|
|
public function alg($alg): self |
104
|
|
|
{ |
105
|
|
|
$clone = clone $this; |
106
|
|
|
switch (true) { |
107
|
|
|
case $alg instanceof JoseAlgorithm: |
108
|
|
|
$clone->algorithms[] = $alg; |
109
|
|
|
$clone->jwt->header->set('alg', $alg->name()); |
110
|
|
|
|
111
|
|
|
break; |
112
|
|
|
case \is_string($alg): |
113
|
|
|
$clone->jwt->header->set('alg', $alg); |
114
|
|
|
|
115
|
|
|
break; |
116
|
|
|
default: |
117
|
|
|
throw new InvalidArgumentException('Invalid parameter "alg". Shall be a string or an algorithm instance.'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $clone; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function cty(string $cty): self |
124
|
|
|
{ |
125
|
|
|
return $this->header('cty', $cty); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function typ(string $typ): self |
129
|
|
|
{ |
130
|
|
|
return $this->header('typ', $typ); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function crit(array $crit): self |
134
|
|
|
{ |
135
|
|
|
return $this->header('crit', $crit); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param mixed $value |
140
|
|
|
*/ |
141
|
|
|
public function claim(string $key, $value, bool $inHeader = false): self |
142
|
|
|
{ |
143
|
|
|
$clone = clone $this; |
144
|
|
|
$clone->jwt->claims->set($key, $value); |
145
|
|
|
if ($inHeader) { |
146
|
|
|
$clone->jwt->header->set($key, $value); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $clone; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param mixed $value |
154
|
|
|
*/ |
155
|
|
|
public function header(string $key, $value): self |
156
|
|
|
{ |
157
|
|
|
$clone = clone $this; |
158
|
|
|
$clone->jwt->header->set($key, $value); |
159
|
|
|
|
160
|
|
|
return $clone; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
abstract protected function getAlgorithmMap(): array; |
164
|
|
|
} |
165
|
|
|
|