Skip to content

Commit 4e9a79b

Browse files
committed
Change accessToken to idToken for more available information
1 parent b7d07ef commit 4e9a79b

9 files changed

+44
-65
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "coddin-web/oidc-client-laravel-wrapper",
33
"description": "A Laravel wrapper of jumbojett's OpenID Connect Client",
44
"type": "library",
5-
"version": "1.3.3",
5+
"version": "1.3.4",
66
"minimum-stability": "stable",
77
"prefer-stable": true,
88
"require": {

src/Builder/JWTVerifierBuilder.php

-10
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@
66

77
use Coddin\OpenIDConnectClient\Helper\ConfigRepository;
88
use Coddin\OpenIDConnectClient\Helper\ConfigRepositoryException;
9-
use Lcobucci\Clock\FrozenClock;
109
use Lcobucci\JWT\Configuration;
1110
use Lcobucci\JWT\Signer\Key\InMemory;
1211
use Lcobucci\JWT\Signer\Rsa\Sha256;
1312
use Lcobucci\JWT\Validation\Constraint\IssuedBy;
14-
use Lcobucci\JWT\Validation\Constraint\SignedWith;
15-
use Lcobucci\JWT\Validation\Constraint\StrictValidAt;
1613

1714
final class JWTVerifierBuilder
1815
{
@@ -41,13 +38,6 @@ public function execute(): Configuration
4138
new IssuedBy(
4239
id: $this->configRepository->getAsString('oidc.provider.issuer'),
4340
),
44-
// new SignedWith(
45-
// signer: $signer,
46-
// key: $key,
47-
// ),
48-
// new StrictValidAt(
49-
// clock: new FrozenClock(new \DateTimeImmutable()),
50-
// ),
5141
);
5242

5343
return $configuration;

src/Http/Middleware/OpenIDConnectAuthenticated.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,13 @@ public function handle(Request $request, \Closure $next): mixed
5858

5959
$openIDClient->authenticate();
6060

61-
$accessToken = $jwtVerifier->parser()->parse($openIDClient->getIdToken());
61+
$idToken = $jwtVerifier->parser()->parse($openIDClient->getIdToken());
6262
$this->tokenStorageAdaptor->put(
63-
accessToken: $accessToken,
63+
accessToken: $idToken,
6464
refreshToken: $openIDClient->getRefreshToken(),
6565
);
6666

6767
/** @var Plain $idToken */
68-
$idToken = $jwtVerifier->parser()->parse($openIDClient->getIdToken());
6968
$userUuid = $idToken->claims()->get('sub');
7069
$userName = $idToken->claims()->get('nickname');
7170
$userEmail = $idToken->claims()->get('email');
@@ -130,9 +129,10 @@ private function handleExistingToken(
130129
$openIDClient->refreshToken($refreshToken->toString());
131130

132131
$jwtVerifier = $this->jwtVerifierBuilder->execute();
133-
$newAccessToken = $jwtVerifier->parser()->parse($openIDClient->getAccessToken());
132+
$newIdToken = $jwtVerifier->parser()->parse($openIDClient->getIdToken());
133+
134134
$this->tokenStorageAdaptor->put(
135-
accessToken: $newAccessToken,
135+
accessToken: $newIdToken,
136136
refreshToken: $openIDClient->getRefreshToken(),
137137
);
138138
}

src/Service/Token/Storage/IlluminateSessionAdaptorToken.php

+5-7
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@ public function __construct(
1717

1818
public function find(string $type): ?Token
1919
{
20-
if ($type !== $this->getAccessTokenStorageKey() && $type !== $this->getRefreshTokenStorageKey()) {
21-
return null;
22-
}
23-
24-
$token = $this->sessionStore->get($this->getAccessTokenStorageKey());
20+
$token = $this->sessionStore->get($type);
2521
if (!$token instanceof Token) {
2622
return null;
2723
}
@@ -40,8 +36,10 @@ public function get(string $type): Token
4036
return $token;
4137
}
4238

43-
public function put(Token $accessToken, ?string $refreshToken = null): void
44-
{
39+
public function put(
40+
Token $accessToken,
41+
?string $refreshToken = null,
42+
): void {
4543
$this->sessionStore->put($this->getAccessTokenStorageKey(), $accessToken);
4644
if ($refreshToken !== null) {
4745
$this->sessionStore->put($this->getRefreshTokenStorageKey(), $refreshToken);

src/Service/Token/Storage/TokenStorageAdaptor.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
interface TokenStorageAdaptor
1111
{
12-
public const ACCESS_TOKEN_STORAGE_KEY = 'oidc_id_access_token';
13-
public const REFRESH_TOKEN_STORAGE_KEY = 'oidc_id_refresh_token';
12+
public const ACCESS_TOKEN_STORAGE_KEY = 'oidc_session_key_access_token';
13+
public const REFRESH_TOKEN_STORAGE_KEY = 'oidc_session_key_refresh_token';
1414

1515
public function find(string $type): ?Token;
1616

@@ -19,10 +19,14 @@ public function find(string $type): ?Token;
1919
*/
2020
public function get(string $type): Token;
2121

22-
public function put(Token $accessToken, ?string $refreshToken = null): void;
22+
public function put(
23+
Token $accessToken,
24+
?string $refreshToken = null,
25+
): void;
2326

2427
public function forget(): void;
2528

29+
2630
public function getAccessTokenStorageKey(): string;
2731

2832
public function getRefreshTokenStorageKey(): string;

tests/Unit/Builder/JWTVerifierBuilderTest.php

+2-10
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
use Coddin\OpenIDConnectClient\Builder\JWTVerifierBuilder;
88
use Coddin\OpenIDConnectClient\Helper\ConfigRepository;
99
use Lcobucci\JWT\Validation\Constraint\IssuedBy;
10-
use Lcobucci\JWT\Validation\Constraint\SignedWith;
11-
use Lcobucci\JWT\Validation\Constraint\StrictValidAt;
1210
use PHPUnit\Framework\MockObject\MockObject;
1311
use PHPUnit\Framework\TestCase;
1412

@@ -46,20 +44,14 @@ public function execute(): void
4644

4745
$constraints = $jwtVerifier->validationConstraints();
4846

49-
self::assertCount(3, $constraints);
47+
self::assertCount(1, $constraints);
5048

51-
$expectedNrOfMatches = 3;
49+
$expectedNrOfMatches = 1;
5250
$countedNrOfMatches = 0;
5351
foreach ($constraints as $constraint) {
5452
if ($constraint instanceof IssuedBy) {
5553
$countedNrOfMatches++;
5654
}
57-
if ($constraint instanceof SignedWith) {
58-
$countedNrOfMatches++;
59-
}
60-
if ($constraint instanceof StrictValidAt) {
61-
$countedNrOfMatches++;
62-
}
6355
}
6456

6557
self::assertEquals($expectedNrOfMatches, $countedNrOfMatches, 'The constraint types do not match');

tests/Unit/Http/Middleware/OpenIDConnectAuthenticatedTest.php

+13-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
/** @noinspection PhpMissingFieldTypeInspection */
4+
35
declare(strict_types=1);
46

57
namespace Coddin\Tests\Unit\Http\Middleware;
@@ -30,20 +32,20 @@
3032
final class OpenIDConnectAuthenticatedTest extends TestCase
3133
{
3234
/** @var ResponseFactory & MockObject */
33-
private ResponseFactory|MockObject $responseFactory;
35+
private $responseFactory;
3436
/** @var OpenIDConnectClientBuilder & MockObject */
35-
private OpenIDConnectClientBuilder|MockObject $openIDConnectClientBuilder;
37+
private $openIDConnectClientBuilder;
3638
/** @var JWTVerifierBuilder & MockObject */
37-
private JWTVerifierBuilder|MockObject $jwtVerifierBuilder;
39+
private $jwtVerifierBuilder;
3840
/** @var TokenStorageAdaptor & MockObject */
39-
private TokenStorageAdaptor|MockObject $storageAdaptor;
41+
private $storageAdaptor;
4042
/** @var ConfigRepository & MockObject */
41-
private ConfigRepository|MockObject $configRepository;
43+
private $configRepository;
4244

4345
/** @var Request & MockObject */
44-
private Request|MockObject $request;
46+
private $request;
4547
/** @var ClosureTestClass & MockObject */
46-
private ClosureTestClass|MockObject $closure;
48+
private $closure;
4749

4850
protected function setUp(): void
4951
{
@@ -206,7 +208,7 @@ public function existing_token_almost_expired(): void
206208

207209
$openIDConnectClient
208210
->expects(self::once())
209-
->method('getAccessToken')
211+
->method('getIdToken')
210212
->willReturn('this_is_an_access_token');
211213

212214
$newAccessToken = $this->createMock(Token::class);
@@ -312,15 +314,10 @@ private function authenticateWithNewToken(): void
312314

313315
$parser = $this->createPartialMock(Parser::class, ['parse']);
314316
$jwtVerifier
315-
->expects(self::exactly(2))
317+
->expects(self::once())
316318
->method('parser')
317319
->willReturn($parser);
318320

319-
$openIDClient
320-
->expects(self::once())
321-
->method('getAccessToken')
322-
->willReturn('access_token.second_part.third_part');
323-
324321
$openIDClient
325322
->expects(self::once())
326323
->method('getIdToken')
@@ -341,23 +338,19 @@ private function authenticateWithNewToken(): void
341338
342339
);
343340

344-
$accessToken = $this->createPartialMock(Token\Plain::class, []);
345-
346341
$idToken = $this->createPartialMock(Token\Plain::class, ['claims']);
347342
$idToken
348343
->expects(self::exactly(3))
349344
->method('claims')
350345
->willReturn($dataSet);
351346

352347
$parser
353-
->expects(self::exactly(2))
348+
->expects(self::once())
354349
->method('parse')
355350
->withConsecutive(
356-
['access_token.second_part.third_part'],
357351
['id_token.second_part.third_part'],
358352
)
359353
->willReturnOnConsecutiveCalls(
360-
$accessToken,
361354
$idToken,
362355
);
363356

@@ -371,7 +364,7 @@ private function authenticateWithNewToken(): void
371364
->expects(self::once())
372365
->method('put')
373366
->with(
374-
$accessToken,
367+
$idToken,
375368
$refreshToken,
376369
);
377370
}

tests/Unit/Http/Middleware/TokenAuthenticatedTest.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
/** @noinspection PhpMissingFieldTypeInspection */
4+
35
declare(strict_types=1);
46

57
namespace Coddin\Tests\Unit\Http\Middleware;
@@ -24,14 +26,14 @@
2426
final class TokenAuthenticatedTest extends \Orchestra\Testbench\TestCase
2527
{
2628
/** @var TokenStorageAdaptor & MockObject */
27-
private TokenStorageAdaptor|MockObject $storageAdaptor;
29+
private $storageAdaptor;
2830
/** @var JWTVerifierBuilder & MockObject */
29-
private JWTVerifierBuilder|MockObject $jwtVerifierBuilder;
31+
private $jwtVerifierBuilder;
3032

3133
/** @var Request & MockObject */
32-
private Request|MockObject $request;
34+
private $request;
3335
/** @var ClosureTestClass & MockObject */
34-
private ClosureTestClass|MockObject $closure;
36+
private $closure;
3537

3638
protected function setUp(): void
3739
{

tests/Unit/Storage/IlluminateSessionAdaptorTokenTest.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function missing_token_in_session(): void
4040
$this->store
4141
->expects(self::once())
4242
->method('get')
43-
->with('oidc_id_access_token')
43+
->with('oidc_session_key_access_token')
4444
->willReturn(null);
4545

4646
self::expectException(MissingTokenException::class);
@@ -60,7 +60,7 @@ public function get_token(): void
6060
$this->store
6161
->expects(self::once())
6262
->method('get')
63-
->with('oidc_id_access_token')
63+
->with('oidc_session_key_access_token')
6464
->willReturn($token);
6565

6666
$tokenAdaptor = new IlluminateSessionAdaptorToken(
@@ -78,7 +78,7 @@ public function put_accessToken_only(): void
7878
$this->store
7979
->expects(self::once())
8080
->method('put')
81-
->with('oidc_id_access_token', $token);
81+
->with('oidc_session_key_access_token', $token);
8282

8383
$tokenAdaptor = new IlluminateSessionAdaptorToken(
8484
sessionStore: $this->store,
@@ -97,11 +97,11 @@ public function put_accessToken_and_refreshToken(): void
9797
->method('put')
9898
->withConsecutive(
9999
[
100-
'oidc_id_access_token',
100+
'oidc_session_key_access_token',
101101
$accessToken,
102102
],
103103
[
104-
'oidc_id_refresh_token',
104+
'oidc_session_key_refresh_token',
105105
$refreshToken,
106106
],
107107
);

0 commit comments

Comments
 (0)