mirror of
https://github.com/mailcow/mailcow-dockerized.git
synced 2026-11-15 21:00:14 +00:00
Merge pull request #7544 from jorgeavlobo/fix/keycloak-admin-token-introspect
[Web] Keycloak: skip introspection of an expired admin token and reuse the new one
This commit is contained in:
1 file changed
+13
-1
@@ -3106,7 +3106,17 @@ function identity_provider($_action = null, $_data = null, $_extra = null) {
|
|||||||
case "get-keycloak-admin-token":
|
case "get-keycloak-admin-token":
|
||||||
// get access_token for service account of mailcow client
|
// get access_token for service account of mailcow client
|
||||||
if ($iam_settings['authsource'] !== 'keycloak') return false;
|
if ($iam_settings['authsource'] !== 'keycloak') return false;
|
||||||
if (isset($iam_settings['access_token'])) {
|
// only introspect a cached access_token that is not (about to be) expired,
|
||||||
|
// introspecting an expired token makes Keycloak log INTROSPECT_TOKEN_ERROR
|
||||||
|
$token_not_expired = false;
|
||||||
|
if (!empty($iam_settings['access_token'])) {
|
||||||
|
$token_parts = explode('.', $iam_settings['access_token']);
|
||||||
|
if (count($token_parts) === 3) {
|
||||||
|
$token_payload = json_decode(base64_decode(strtr($token_parts[1], '-_', '+/')), true);
|
||||||
|
$token_not_expired = is_array($token_payload) && isset($token_payload['exp']) && intval($token_payload['exp']) > time() + 30;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ($token_not_expired) {
|
||||||
// check if access_token is valid
|
// check if access_token is valid
|
||||||
$url = "{$iam_settings['server_url']}/realms/{$iam_settings['realm']}/protocol/openid-connect/token/introspect";
|
$url = "{$iam_settings['server_url']}/realms/{$iam_settings['realm']}/protocol/openid-connect/token/introspect";
|
||||||
$req = http_build_query(array(
|
$req = http_build_query(array(
|
||||||
@@ -3157,6 +3167,8 @@ function identity_provider($_action = null, $_data = null, $_extra = null) {
|
|||||||
':key' => 'access_token',
|
':key' => 'access_token',
|
||||||
':value' => $res['access_token']
|
':value' => $res['access_token']
|
||||||
));
|
));
|
||||||
|
// keep the cached token in sync, so subsequent calls in the same run reuse it
|
||||||
|
$iam_settings['access_token'] = $res['access_token'];
|
||||||
return $res['access_token'];
|
return $res['access_token'];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user