Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix re-login app when token expires in OIDC #2034

Merged
merged 2 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion env.file
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
SERVER_URL=http://localhost/
DOMAIN_REDIRECT_URL=http://localhost:3000
WEB_OIDC_CLIENT_ID=teammail-web
OIDC_SCOPES=openid,profile,email
OIDC_SCOPES=openid,profile,email,offline_access
APP_GRID_AVAILABLE=supported
FCM_AVAILABLE=supported
IOS_FCM=supported
Original file line number Diff line number Diff line change
Expand Up @@ -75,49 +75,53 @@ class AuthorizationInterceptors extends InterceptorsWrapper {

@override
void onError(DioError err, ErrorInterceptorHandler handler) async {
final requestOptions = err.requestOptions;
log('AuthorizationInterceptors::onError(): $err');
if (_isTokenExpired() &&
err.response?.statusCode == 401 &&
try {
if (_isTokenExpired() &&
(err.response == null || err.response?.statusCode == 401) &&
_isRefreshTokenNotEmpty() &&
_isAuthenticationOidcValid()) {
try {
_isAuthenticationOidcValid()
) {
final newToken = await _authenticationClient.refreshingTokensOIDC(
_configOIDC!.clientId,
_configOIDC!.redirectUrl,
_configOIDC!.discoveryUrl,
_configOIDC!.scopes,
_token!.refreshToken);
_configOIDC!.clientId,
_configOIDC!.redirectUrl,
_configOIDC!.discoveryUrl,
_configOIDC!.scopes,
_token!.refreshToken);

final accountCurrent = await _accountCacheManager.getSelectedAccount();

await _accountCacheManager.deleteSelectedAccount(_token!.tokenIdHash);

await Future.wait([
_tokenOidcCacheManager.persistOneTokenOidc(newToken),
_accountCacheManager.deleteSelectedAccount(_token!.tokenIdHash),
_accountCacheManager.setSelectedAccount(PersonalAccount(
newToken.tokenIdHash,
AuthenticationType.oidc,
isSelected: true,
accountId: accountCurrent.accountId,
apiUrl: accountCurrent.apiUrl,
userName: accountCurrent.userName))
_accountCacheManager.setSelectedAccount(
PersonalAccount(
newToken.tokenIdHash,
AuthenticationType.oidc,
isSelected: true,
accountId: accountCurrent.accountId,
apiUrl: accountCurrent.apiUrl,
userName: accountCurrent.userName
)
)
]);

log('AuthorizationInterceptors::onError(): refreshToken: $newToken');
log('AuthorizationInterceptors::setToken(): refreshTokenId: ${newToken.tokenIdHash}');

_updateNewToken(newToken.toToken());

requestOptions.headers[HttpHeaders.authorizationHeader] =
_getTokenAsBearerHeader(newToken.token);
final requestOptions = err.requestOptions;
requestOptions.headers[HttpHeaders.authorizationHeader] = _getTokenAsBearerHeader(newToken.token);

final response = await _dio.fetch(requestOptions);
return handler.resolve(response);
} catch(e) {
log('AuthorizationInterceptors::onError(): $e');
} else {
super.onError(err, handler);
}
} else {
} catch (e) {
log('AuthorizationInterceptors::onError():Exception: $e');
super.onError(err, handler);
}
}
Expand Down
Loading