Skip to content

Commit 5126141

Browse files
wouldsminasbenaddi
authored andcommitted
Gestion décodage JWT
1 parent f235745 commit 5126141

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,8 @@ OIDC_CLIENT_ID=deming
106106
OIDC_CLIENT_SECRET=deming
107107
OIDC_BASE_URL=http://auth.lan
108108
OIDC_SUFFIX=""
109+
OIDC_USE_ID_TOKEN=false # true pour décoder le JWT
110+
OIDC_JWT_ALG=RS256 # RS256 ou HS256. utile uniquement avec OIDC_USE_ID_TOKEN=true
111+
OIDC_JWT_SECRET_OR_KEY="" # secret pour HS256 ou clé au format PEM pour RS256
109112
OIDC_REDIRECT_URI=${APP_URL}auth/callback/oidc
110113
APP_VERSION=2025.08.13

app/Providers/Socialite/GenericSocialiteProvider.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use Laravel\Socialite\Two\AbstractProvider;
77
use Laravel\Socialite\Two\ProviderInterface;
88
use Laravel\Socialite\Two\User;
9+
use Firebase\JWT\JWT;
10+
use Firebase\JWT\Key;
911
use Log;
1012

1113
/**
@@ -40,6 +42,7 @@ class GenericSocialiteProvider extends AbstractProvider implements ProviderInter
4042
* {@inheritdoc}
4143
*/
4244
protected $scopeSeparator = ' ';
45+
protected $idToken;
4346

4447
/**
4548
* Return provider Url.
@@ -96,6 +99,19 @@ protected function getTokenUrl()
9699
return $this->getOIDCUrl() . '/token';
97100
}
98101

102+
/**
103+
* Get the access token response for the given code.
104+
*
105+
* @param string $code
106+
* @return mixed
107+
*/
108+
public function getAccessTokenResponse($code)
109+
{
110+
$response = parent::getAccessTokenResponse($code);
111+
$this->idToken = $response['id_token'] ?? null;
112+
return $response;
113+
}
114+
99115
/**
100116
* @param string $token
101117
*
@@ -105,6 +121,15 @@ protected function getTokenUrl()
105121
*/
106122
protected function getUserByToken($token)
107123
{
124+
$useIdToken = config('services.oidc.use_id_token', false);
125+
126+
if ($useIdToken) {
127+
if (!$this->idToken) {
128+
throw new \Exception('OIDC_USE_ID_TOKEN=true but id_token not received');
129+
}
130+
return $this->decodeIdToken($this->idToken);
131+
}
132+
108133
$base_url = $this->getOIDCUrl() . '/userinfo';
109134
// If userinfo endpoint set, use it instead
110135
if (config('services.oidc.userinfo_endpoint')) {
@@ -140,4 +165,22 @@ protected function mapUserToObject(array $user)
140165
}
141166
return (new User())->setRaw($user)->map($socialite_user);
142167
}
168+
169+
protected function decodeIdToken($idToken)
170+
{
171+
$alg = config('services.oidc.jwt_alg', 'RS256');
172+
$key = config('services.oidc.jwt_secret_or_key');
173+
174+
if (!$key) {
175+
throw new \Exception('JWT secret or public key not configured');
176+
}
177+
178+
try {
179+
$decoded = JWT::decode($idToken, new Key($key, $alg));
180+
} catch (\Exception $e) {
181+
throw new \Exception('Failed to decode ID token: '.$e->getMessage(), 0, $e);
182+
}
183+
184+
return json_decode(json_encode($decoded), true);
185+
}
143186
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"license": "GPLv3",
77
"require": {
88
"php": "^8.2",
9+
"firebase/php-jwt": "^7.0",
910
"directorytree/ldaprecord-laravel": "^3.4",
1011
"erusev/parsedown": "^1.7",
1112
"laravel/framework": "^11.9",

config/services.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@
7171
'authorize_endpoint' => env('OIDC_AUTHORIZE_ENDPOINT', null),
7272
'token_endpoint' => env('OIDC_TOKEN_ENDPOINT', null),
7373
'userinfo_endpoint' => env('OIDC_USERINFO_ENDPOINT', null),
74+
'use_id_token' => env('OIDC_USE_ID_TOKEN', false),
75+
'jwt_alg' => env('OIDC_JWT_ALG', 'RS256'),
76+
'jwt_secret_or_key' => env('OIDC_JWT_SECRET_OR_KEY', ''),
7477
'map_user_attr' => [
7578
'id' => 'sub',
7679
'name' => 'name',

0 commit comments

Comments
 (0)