mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-03-05 02:17:23 +08:00
Simplify refresh method
This commit is contained in:
parent
23402ae812
commit
f2d320825a
@ -71,41 +71,56 @@ class OpenIdService extends ExternalAuthService
|
|||||||
|
|
||||||
$accessToken = new AccessToken(json_decode($json, true) ?? []);
|
$accessToken = new AccessToken(json_decode($json, true) ?? []);
|
||||||
|
|
||||||
// Check if both the access token and the ID token (if present) are unexpired
|
// If the token is not expired, refreshing isn't necessary
|
||||||
$idToken = $accessToken->getIdToken();
|
if ($this->isUnexpired($accessToken)) {
|
||||||
$accessTokenUnexpired = $accessToken->getExpires() && !$accessToken->hasExpired();
|
|
||||||
$idTokenUnexpired = !$idToken || !$idToken->isExpired();
|
|
||||||
if ($accessTokenUnexpired && $idTokenUnexpired) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If no refresh token available, logout
|
// Try to obtain refreshed access token
|
||||||
if ($accessToken->getRefreshToken() === null) {
|
|
||||||
$this->actionLogout();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ID token or access token is expired, we refresh it using the refresh token
|
|
||||||
try {
|
try {
|
||||||
$provider = $this->getProvider();
|
$newAccessToken = $this->refreshAccessToken($accessToken);
|
||||||
|
|
||||||
$accessToken = $provider->getAccessToken('refresh_token', [
|
|
||||||
'refresh_token' => $accessToken->getRefreshToken(),
|
|
||||||
]);
|
|
||||||
} catch (IdentityProviderException $e) {
|
|
||||||
// Refreshing failed, logout
|
|
||||||
$this->actionLogout();
|
|
||||||
return false;
|
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
// Unknown error, logout and throw
|
// Log out if an unknown problem arises
|
||||||
$this->actionLogout();
|
$this->actionLogout();
|
||||||
throw $e;
|
throw $e;
|
||||||
}
|
}
|
||||||
|
|
||||||
// A valid token was obtained, we update the access token
|
// If a token was obtained, update the access token, otherwise log out
|
||||||
session()->put('openid_token', json_encode($accessToken));
|
if ($newAccessToken !== null) {
|
||||||
|
session()->put('openid_token', json_encode($newAccessToken));
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
$this->actionLogout();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
protected function isUnexpired(AccessToken $accessToken): bool
|
||||||
|
{
|
||||||
|
$idToken = $accessToken->getIdToken();
|
||||||
|
|
||||||
|
$accessTokenUnexpired = $accessToken->getExpires() && !$accessToken->hasExpired();
|
||||||
|
$idTokenUnexpired = !$idToken || !$idToken->isExpired();
|
||||||
|
|
||||||
|
return $accessTokenUnexpired && $idTokenUnexpired;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function refreshAccessToken(AccessToken $accessToken): ?AccessToken
|
||||||
|
{
|
||||||
|
// If no refresh token available, abort
|
||||||
|
if ($accessToken->getRefreshToken() === null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ID token or access token is expired, we refresh it using the refresh token
|
||||||
|
try {
|
||||||
|
return $this->getProvider()->getAccessToken('refresh_token', [
|
||||||
|
'refresh_token' => $accessToken->getRefreshToken(),
|
||||||
|
]);
|
||||||
|
} catch (IdentityProviderException $e) {
|
||||||
|
// Refreshing failed
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user