Skip to content

Commit 680463f

Browse files
committed
update tests for the new interface
1 parent bd4aff9 commit 680463f

File tree

1 file changed

+83
-87
lines changed

1 file changed

+83
-87
lines changed

tests/unit/TokenGeneratorTest.php

Lines changed: 83 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ final public function testRegistrationAccessTokenGeneration(): void
203203
}
204204

205205
/**
206-
* @testdox Token Generator SHOULD complain WHEN asked to generate a IdToken without accessToken
206+
* @testdox Token Generator SHOULD complain WHEN asked to generate a IdToken without clientId
207207
*
208208
* @covers ::generateIdToken
209209
*/
@@ -217,7 +217,7 @@ final public function testIdTokenGenerationWithoutAccesToken(): void
217217
}
218218

219219
/**
220-
* @testdox Token Generator SHOULD complain WHEN asked to generate a IdToken without clientId
220+
* @testdox Token Generator SHOULD complain WHEN asked to generate a IdToken without subject
221221
*
222222
* @covers ::generateIdToken
223223
*/
@@ -227,54 +227,7 @@ final public function testIdTokenGenerationWithoutClientId(): void
227227

228228
$this->expectArgumentCountError(2);
229229

230-
$tokenGenerator->generateIdToken('mock access token');
231-
}
232-
233-
/**
234-
* @testdox Token Generator SHOULD complain WHEN asked to generate a IdToken without subject
235-
*
236-
* @covers ::generateIdToken
237-
*/
238-
final public function testIdTokenGenerationWithoutSubject(): void
239-
{
240-
$tokenGenerator = $this->createTokenGenerator();
241-
242-
$this->expectArgumentCountError(3);
243-
244-
$tokenGenerator->generateIdToken('mock access token', 'mock clientId');
245-
}
246-
247-
/**
248-
* @testdox Token Generator SHOULD complain WHEN asked to generate a IdToken without nonce
249-
*
250-
* @covers ::generateIdToken
251-
*/
252-
final public function testIdTokenGenerationWithoutNonce(): void
253-
{
254-
$tokenGenerator = $this->createTokenGenerator();
255-
256-
$this->expectArgumentCountError(4);
257-
258-
$tokenGenerator->generateIdToken('mock access token', 'mock clientId', 'mock subject');
259-
}
260-
261-
/**
262-
* @testdox Token Generator SHOULD complain WHEN asked to generate a IdToken without privateKey, $dpopKey
263-
*
264-
* @covers ::generateIdToken
265-
*/
266-
final public function testIdTokenGenerationWithoutPrivateKey(): void
267-
{
268-
$tokenGenerator = $this->createTokenGenerator();
269-
270-
$this->expectArgumentCountError(5);
271-
272-
$tokenGenerator->generateIdToken(
273-
'mock access token',
274-
'mock clientId',
275-
'mock subject',
276-
'mock nonce'
277-
);
230+
$tokenGenerator->generateIdToken('mock clientId');
278231
}
279232

280233
/**
@@ -290,7 +243,6 @@ final public function testIdTokenGenerationWithoutDpopKey(): void
290243

291244
$tokenGenerator = $this->createTokenGenerator($validFor);
292245

293-
294246
$mockServer = $this->getMockBuilder(ServerInterface::class)
295247
->disableOriginalConstructor()
296248
->getMock()
@@ -307,56 +259,68 @@ final public function testIdTokenGenerationWithoutDpopKey(): void
307259
->willReturn('mock issuer')
308260
;
309261

262+
$privateKey = file_get_contents(__DIR__.'/../fixtures/keys/private.key');
310263
$publicKey = file_get_contents(__DIR__.'/../fixtures/keys/public.key');
311-
264+
265+
$mockPrivateKey = $this->getMockBuilder(\League\OAuth2\Server\CryptKey::class)
266+
->disableOriginalConstructor()
267+
->getMock()
268+
;
312269
$mockPublicKey = $this->getMockBuilder(\Lcobucci\JWT\Signer\Key::class)
270+
->disableOriginalConstructor()
313271
->getMock()
314272
;
315273

274+
$mockPrivateKey->expects($this->once())
275+
->method('getKeyContents')
276+
->willReturn($privateKey)
277+
;
278+
316279
$mockPublicKey->expects($this->once())
317280
->method('contents')
318281
->willReturn($publicKey)
319282
;
320283

284+
$this->mockKeys->expects($this->once())
285+
->method('getPrivateKey')
286+
->willReturn($mockPrivateKey)
287+
;
288+
321289
$this->mockKeys->expects($this->once())
322290
->method('getPublicKey')
323291
->willReturn($mockPublicKey)
324292
;
325293

326-
$privateKey = file_get_contents(__DIR__.'/../fixtures/keys/private.key');
327-
328-
$now = new \DateTimeImmutable('1234-01-01 12:34:56.789');
294+
$this->mockConfig->expects($this->atLeast(1))
295+
->method('getKeys')
296+
->willReturn($this->mockKeys)
297+
;
329298

330-
$token = $tokenGenerator->generateIdToken(
331-
'mock access token',
299+
$idToken = $tokenGenerator->generateIdToken(
332300
'mock clientId',
333-
'mock subject',
334-
'mock nonce',
335-
$privateKey,
336-
null,
337-
$now,
301+
'mock subject'
338302
);
303+
$idToken = $tokenGenerator->bindAccessToken('mock access token', $idToken);
304+
$idToken = $tokenGenerator->signToken($idToken);
339305

340306
$this->assertJwtEquals([
341307
[
342-
'typ' => 'JWT',
308+
// 'typ' => 'JWT',
343309
'alg' => 'RS256',
344310
'kid' => '0c3932ca20f3a00ad2eb72035f6cc9cb'
345311
],
346312
[
347313
'at_hash' => '1EZBnvsFWlK8ESkgHQsrIQ',
348314
'aud' => 'mock clientId',
349315
'azp' => 'mock clientId',
350-
'c_hash' => '1EZBnvsFWlK8ESkgHQsrIQ',
351-
'exp' => -23225829903.789,
352-
'iat' => -23225829904.789,
316+
'exp' => 4834,
317+
'iat' => 1234,
353318
'iss' => 'mock issuer',
354319
'jti' => '4dc20036dbd8313ed055',
355-
'nbf' => -23225829905.789,
356-
'nonce' => 'mock nonce',
320+
// 'nonce' => 'mock nonce',
357321
'sub' => 'mock subject',
358322
],
359-
], $token);
323+
], $idToken);
360324
}
361325

362326
/**
@@ -370,7 +334,7 @@ final public function testIdTokenGeneration(): void
370334
{
371335
$validFor = new \DateInterval('PT1S');
372336

373-
$tokenGenerator = $this->createTokenGenerator($validFor, self::MOCK_JKT);
337+
$tokenGenerator = $this->createTokenGenerator($validFor);
374338

375339
$mockServer = $this->getMockBuilder(ServerInterface::class)
376340
->disableOriginalConstructor()
@@ -388,8 +352,6 @@ final public function testIdTokenGeneration(): void
388352
->willReturn('mock issuer')
389353
;
390354

391-
$privateKey = file_get_contents(__DIR__.'/../fixtures/keys/private.key');
392-
393355
$now = new \DateTimeImmutable('1234-01-01 12:34:56.789');
394356

395357
$encodedDpop = vsprintf("%s.%s.%s", [
@@ -398,32 +360,66 @@ final public function testIdTokenGeneration(): void
398360
'signature' => Base64Url::encode('mock signature')
399361
]);
400362

401-
$actual = $tokenGenerator->generateIdToken(
402-
'mock access token',
363+
$privateKey = file_get_contents(__DIR__.'/../fixtures/keys/private.key');
364+
$publicKey = file_get_contents(__DIR__.'/../fixtures/keys/public.key');
365+
366+
$mockPrivateKey = $this->getMockBuilder(\League\OAuth2\Server\CryptKey::class)
367+
->disableOriginalConstructor()
368+
->getMock()
369+
;
370+
$mockPublicKey = $this->getMockBuilder(\Lcobucci\JWT\Signer\Key::class)
371+
->disableOriginalConstructor()
372+
->getMock()
373+
;
374+
375+
$mockPrivateKey->expects($this->once())
376+
->method('getKeyContents')
377+
->willReturn($privateKey)
378+
;
379+
380+
$mockPublicKey->expects($this->once())
381+
->method('contents')
382+
->willReturn($publicKey)
383+
;
384+
385+
$this->mockKeys->expects($this->once())
386+
->method('getPrivateKey')
387+
->willReturn($mockPrivateKey)
388+
;
389+
390+
$this->mockKeys->expects($this->once())
391+
->method('getPublicKey')
392+
->willReturn($mockPublicKey)
393+
;
394+
395+
$this->mockConfig->expects($this->atLeast(1))
396+
->method('getKeys')
397+
->willReturn($this->mockKeys)
398+
;
399+
400+
$idToken = $tokenGenerator->generateIdToken(
403401
'mock clientId',
404-
'mock subject',
405-
'mock nonce',
406-
$privateKey,
407-
$encodedDpop,
408-
$now
402+
'mock subject'
409403
);
404+
$idToken = $tokenGenerator->bindAccessToken('mock access token', $idToken);
405+
$idToken = $tokenGenerator->signToken($idToken);
410406

411407
$this->assertJwtEquals([[
412408
"alg"=>"RS256",
413-
"typ"=>"JWT",
409+
'kid' => '0c3932ca20f3a00ad2eb72035f6cc9cb'
410+
// "typ"=>"JWT",
414411
],[
415412
'at_hash' => '1EZBnvsFWlK8ESkgHQsrIQ',
416413
'aud' => 'mock clientId',
417414
'azp' => 'mock clientId',
418-
'c_hash' => '1EZBnvsFWlK8ESkgHQsrIQ',
419-
'cnf' => ["jkt" => self::MOCK_JKT],
420-
'exp' => -23225829903.789,
421-
'iat' => -23225829904.789,
415+
// 'cnf' => ["jkt" => self::MOCK_JKT],
416+
'exp' => 4834,
417+
'iat' => 1234,
422418
'iss' => 'mock issuer',
423419
'jti' => '4dc20036dbd8313ed055',
424-
'nbf' => -23225829905.789,
425-
'nonce' => 'mock nonce',
420+
// 'nbf' => -23225829905.789,
421+
// 'nonce' => 'mock nonce',
426422
'sub' => 'mock subject',
427-
]], $actual);
423+
]], $idToken);
428424
}
429425
}

0 commit comments

Comments
 (0)