1 | <?php |
||
20 | class CheckerManagerTest extends TestCase |
||
21 | { |
||
22 | /** |
||
23 | * @expectedException \InvalidArgumentException |
||
24 | * @expectedExceptionMessage The JWT has expired. |
||
25 | */ |
||
26 | public function testExpiredJWT() |
||
27 | { |
||
28 | $jws = JWSFactory::createJWS( |
||
29 | [ |
||
30 | 'exp' => time() - 1, |
||
31 | ] |
||
32 | ); |
||
33 | $jws = $jws->addSignature( |
||
34 | new JWK(['kty'=>'none']), |
||
35 | [ |
||
36 | 'alg' => 'HS512', |
||
37 | ] |
||
38 | ); |
||
39 | |||
40 | $this->getCheckerManager()->checkJWS($jws, 0); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * @expectedException \InvalidArgumentException |
||
45 | * @expectedExceptionMessage The JWT is issued in the futur. |
||
46 | */ |
||
47 | public function testJWTIssuedInTheFuture() |
||
48 | { |
||
49 | $jws = JWSFactory::createJWS( |
||
50 | [ |
||
51 | 'exp' => time() + 3600, |
||
52 | 'iat' => time() + 100, |
||
53 | ] |
||
54 | ); |
||
55 | $jws = $jws->addSignature( |
||
56 | new JWK(['kty'=>'none']), |
||
57 | [ |
||
58 | 'alg' => 'HS512', |
||
59 | ] |
||
60 | ); |
||
61 | |||
62 | $this->getCheckerManager()->checkJWS($jws, 0); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @expectedException \InvalidArgumentException |
||
67 | * @expectedExceptionMessage The JWT can not be used yet. |
||
68 | */ |
||
69 | public function testJWTNotNow() |
||
70 | { |
||
71 | $jws = JWSFactory::createJWS( |
||
72 | [ |
||
73 | 'exp' => time() + 3600, |
||
74 | 'iat' => time() - 100, |
||
75 | 'nbf' => time() + 100, |
||
76 | ] |
||
77 | ); |
||
78 | $jws = $jws->addSignature( |
||
79 | new JWK(['kty'=>'none']), |
||
80 | [ |
||
81 | 'alg' => 'HS512', |
||
82 | ] |
||
83 | ); |
||
84 | |||
85 | $this->getCheckerManager()->checkJWS($jws, 0); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @expectedException \InvalidArgumentException |
||
90 | * @expectedExceptionMessage Bad audience. |
||
91 | */ |
||
92 | public function testJWTNotForAudience() |
||
93 | { |
||
94 | $jws = JWSFactory::createJWS( |
||
95 | [ |
||
96 | 'exp' => time() + 3600, |
||
97 | 'iat' => time() - 100, |
||
98 | 'nbf' => time() - 100, |
||
99 | 'aud' => 'Other Service', |
||
100 | ] |
||
101 | ); |
||
102 | $jws = $jws->addSignature( |
||
103 | new JWK(['kty'=>'none']), |
||
104 | [ |
||
105 | 'alg' => 'HS512', |
||
106 | ] |
||
107 | ); |
||
108 | |||
109 | $this->getCheckerManager()->checkJWS($jws, 0); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * @expectedException \InvalidArgumentException |
||
114 | * @expectedExceptionMessage One or more claims are marked as critical, but they are missing or have not been checked (["iss"]). |
||
115 | */ |
||
116 | public function testJWTHasCriticalClaimsNotSatisfied() |
||
117 | { |
||
118 | $jws = JWSFactory::createJWS( |
||
119 | [ |
||
120 | 'exp' => time() + 3600, |
||
121 | 'iat' => time() - 100, |
||
122 | 'nbf' => time() - 100, |
||
123 | ] |
||
124 | ); |
||
125 | $jws = $jws->addSignature( |
||
126 | new JWK(['kty'=>'none']), |
||
127 | [ |
||
128 | 'enc' => 'A256CBC-HS512', |
||
129 | 'alg' => 'HS512', |
||
130 | 'zip' => 'DEF', |
||
131 | 'crit' => ['exp', 'iss'], |
||
132 | ] |
||
133 | ); |
||
134 | |||
135 | $this->getCheckerManager()->checkJWS($jws, 0); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @expectedException \InvalidArgumentException |
||
140 | * @expectedExceptionMessage The issuer "foo" is not allowed. |
||
141 | */ |
||
142 | public function testJWTBadIssuer() |
||
143 | { |
||
144 | $jws = JWSFactory::createJWS( |
||
145 | [ |
||
146 | 'exp' => time() + 3600, |
||
147 | 'iat' => time() - 100, |
||
148 | 'nbf' => time() - 100, |
||
149 | 'iss' => 'foo', |
||
150 | ] |
||
151 | ); |
||
152 | $jws = $jws->addSignature( |
||
153 | new JWK(['kty'=>'none']), |
||
154 | [ |
||
155 | 'enc' => 'A256CBC-HS512', |
||
156 | 'alg' => 'HS512', |
||
157 | 'zip' => 'DEF', |
||
158 | 'crit' => ['exp', 'iss'], |
||
159 | ] |
||
160 | ); |
||
161 | |||
162 | $this->getCheckerManager()->checkJWS($jws, 0); |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * @expectedException \InvalidArgumentException |
||
167 | * @expectedExceptionMessage The subject "foo" is not allowed. |
||
168 | */ |
||
169 | public function testJWTBadSubject() |
||
170 | { |
||
171 | $jws = JWSFactory::createJWS( |
||
172 | [ |
||
173 | 'exp' => time() + 3600, |
||
174 | 'iat' => time() - 100, |
||
175 | 'nbf' => time() - 100, |
||
176 | 'iss' => 'ISS1', |
||
177 | 'sub' => 'foo', |
||
178 | ] |
||
179 | ); |
||
180 | $jws = $jws->addSignature( |
||
181 | new JWK(['kty'=>'none']), |
||
182 | [ |
||
183 | 'enc' => 'A256CBC-HS512', |
||
184 | 'alg' => 'HS512', |
||
185 | 'zip' => 'DEF', |
||
186 | 'crit' => ['exp', 'iss', 'sub', 'aud'], |
||
187 | ] |
||
188 | |||
189 | ); |
||
190 | |||
191 | $this->getCheckerManager()->checkJWS($jws, 0); |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * @expectedException \InvalidArgumentException |
||
196 | * @expectedExceptionMessage Invalid token ID "bad jti". |
||
197 | */ |
||
198 | public function testJWTBadTokenID() |
||
199 | { |
||
200 | $jws = JWSFactory::createJWS( |
||
201 | [ |
||
202 | 'jti' => 'bad jti', |
||
203 | 'exp' => time() + 3600, |
||
204 | 'iat' => time() - 100, |
||
205 | 'nbf' => time() - 100, |
||
206 | 'iss' => 'ISS1', |
||
207 | 'sub' => 'SUB1', |
||
208 | ] |
||
209 | ); |
||
210 | $jws = $jws->addSignature( |
||
211 | new JWK(['kty'=>'none']), |
||
212 | [ |
||
213 | 'enc' => 'A256CBC-HS512', |
||
214 | 'alg' => 'HS512', |
||
215 | 'zip' => 'DEF', |
||
216 | 'crit' => ['exp', 'iss', 'sub', 'aud', 'jti'], |
||
217 | ] |
||
218 | ); |
||
219 | |||
220 | $this->getCheckerManager()->checkJWS($jws, 0); |
||
221 | } |
||
222 | |||
223 | public function testJWTSuccessfullyCheckedWithCriticalHeaders() |
||
224 | { |
||
225 | $jws = JWSFactory::createJWS( |
||
226 | [ |
||
227 | 'jti' => 'JTI1', |
||
228 | 'exp' => time() + 3600, |
||
229 | 'iat' => time() - 100, |
||
230 | 'nbf' => time() - 100, |
||
231 | 'iss' => 'ISS1', |
||
232 | 'sub' => 'SUB1', |
||
233 | 'aud' => 'My Service', |
||
234 | ] |
||
235 | ); |
||
236 | $jws = $jws->addSignature( |
||
237 | new JWK(['kty'=>'none']), |
||
238 | [ |
||
239 | 'enc' => 'A256CBC-HS512', |
||
240 | 'alg' => 'HS512', |
||
241 | 'zip' => 'DEF', |
||
242 | 'crit' => ['exp', 'iss', 'sub', 'aud', 'jti'], |
||
243 | ] |
||
244 | |||
245 | ); |
||
246 | |||
247 | $this->getCheckerManager()->checkJWS($jws, 0); |
||
248 | } |
||
249 | |||
250 | public function testJWTSuccessfullyCheckedWithoutCriticalHeaders() |
||
251 | { |
||
252 | $jws = JWSFactory::createJWS( |
||
253 | [ |
||
254 | 'jti' => 'JTI1', |
||
255 | 'exp' => time() + 3600, |
||
256 | 'iat' => time() - 100, |
||
257 | 'nbf' => time() - 100, |
||
258 | 'iss' => 'ISS1', |
||
259 | 'sub' => 'SUB1', |
||
260 | 'aud' => 'My Service', |
||
261 | ] |
||
262 | ); |
||
263 | $jws = $jws->addSignature( |
||
264 | new JWK(['kty'=>'none']), |
||
265 | [ |
||
266 | 'enc' => 'A256CBC-HS512', |
||
267 | 'alg' => 'HS512', |
||
268 | 'zip' => 'DEF', |
||
269 | ] |
||
270 | ); |
||
271 | |||
272 | $this->getCheckerManager()->checkJWS($jws, 0); |
||
273 | } |
||
274 | |||
275 | public function testJWTSuccessfullyCheckedWithUnsupportedClaims() |
||
276 | { |
||
277 | $jws = JWSFactory::createJWS( |
||
278 | [ |
||
279 | 'foo' => 'bar', |
||
280 | ] |
||
281 | ); |
||
282 | $jws = $jws->addSignature( |
||
283 | new JWK(['kty'=>'none']), |
||
284 | [ |
||
285 | 'enc' => 'A256CBC-HS512', |
||
286 | 'alg' => 'HS512', |
||
287 | 'zip' => 'DEF', |
||
288 | ] |
||
289 | ); |
||
290 | |||
291 | $this->getCheckerManager()->checkJWS($jws, 0); |
||
292 | } |
||
293 | } |
||
294 |