Skip to content

Commit a84440f

Browse files
committed
Store both the id- and the accessToken
1 parent 31090e5 commit a84440f

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
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.4.0",
66
"minimum-stability": "stable",
77
"prefer-stable": true,
88
"require": {

src/Http/Middleware/OpenIDConnectAuthenticated.php

+13-4
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,15 @@ public function handle(Request $request, \Closure $next): mixed
4343
return $next($request);
4444
}
4545

46+
$idToken = $this->tokenStorageAdaptor->find(TokenStorageAdaptor::ID_TOKEN_STORAGE_KEY);
4647
$accessToken = $this->tokenStorageAdaptor->find(TokenStorageAdaptor::ACCESS_TOKEN_STORAGE_KEY);
4748

48-
if ($accessToken !== null) {
49-
return $this->handleExistingToken($accessToken, $request, $next);
49+
if ($idToken !== null && $accessToken !== null) {
50+
return $this->handleExistingToken(
51+
accessToken: $accessToken,
52+
request: $request,
53+
next: $next,
54+
);
5055
}
5156

5257
try {
@@ -58,9 +63,11 @@ public function handle(Request $request, \Closure $next): mixed
5863

5964
$openIDClient->authenticate();
6065

66+
$accessToken = $jwtVerifier->parser()->parse($openIDClient->getAccessToken());
6167
$idToken = $jwtVerifier->parser()->parse($openIDClient->getIdToken());
6268
$this->tokenStorageAdaptor->put(
63-
accessToken: $idToken,
69+
idToken: $idToken,
70+
accessToken: $accessToken,
6471
refreshToken: $openIDClient->getRefreshToken(),
6572
);
6673

@@ -130,9 +137,11 @@ private function handleExistingToken(
130137

131138
$jwtVerifier = $this->jwtVerifierBuilder->execute();
132139
$newIdToken = $jwtVerifier->parser()->parse($openIDClient->getIdToken());
140+
$newAccessToken = $jwtVerifier->parser()->parse($openIDClient->getAccessToken());
133141

134142
$this->tokenStorageAdaptor->put(
135-
accessToken: $newIdToken,
143+
idToken: $newIdToken,
144+
accessToken: $newAccessToken,
136145
refreshToken: $openIDClient->getRefreshToken(),
137146
);
138147
}

src/Service/Token/Storage/IlluminateSessionAdaptorToken.php

+8
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ public function get(string $type): Token
3737
}
3838

3939
public function put(
40+
Token $idToken,
4041
Token $accessToken,
4142
?string $refreshToken = null,
4243
): void {
44+
$this->sessionStore->put($this->getIdTokenStorageKey(), $idToken);
4345
$this->sessionStore->put($this->getAccessTokenStorageKey(), $accessToken);
4446
if ($refreshToken !== null) {
4547
$this->sessionStore->put($this->getRefreshTokenStorageKey(), $refreshToken);
@@ -48,11 +50,17 @@ public function put(
4850

4951
public function forget(): void
5052
{
53+
$this->sessionStore->forget($this->getIdTokenStorageKey());
5154
$this->sessionStore->forget($this->getAccessTokenStorageKey());
5255
$this->sessionStore->forget($this->getRefreshTokenStorageKey());
5356
$this->sessionStore->save();
5457
}
5558

59+
public function getIdTokenStorageKey(): string
60+
{
61+
return self::ID_TOKEN_STORAGE_KEY;
62+
}
63+
5664
public function getAccessTokenStorageKey(): string
5765
{
5866
return self::ACCESS_TOKEN_STORAGE_KEY;

src/Service/Token/Storage/TokenStorageAdaptor.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
interface TokenStorageAdaptor
1111
{
12+
public const ID_TOKEN_STORAGE_KEY = 'oidc_session_key_id_token';
1213
public const ACCESS_TOKEN_STORAGE_KEY = 'oidc_session_key_access_token';
1314
public const REFRESH_TOKEN_STORAGE_KEY = 'oidc_session_key_refresh_token';
1415

@@ -20,13 +21,13 @@ public function find(string $type): ?Token;
2021
public function get(string $type): Token;
2122

2223
public function put(
24+
Token $idToken,
2325
Token $accessToken,
2426
?string $refreshToken = null,
2527
): void;
2628

2729
public function forget(): void;
2830

29-
3031
public function getAccessTokenStorageKey(): string;
3132

3233
public function getRefreshTokenStorageKey(): string;

0 commit comments

Comments
 (0)