TF-1974 Fix refresh token oidc failed

(cherry picked from commit 0bed553bfad291d4a6501f9ac935cfe701703b06)
This commit is contained in:
dab246
2023-08-22 10:14:23 +07:00
committed by Dat Vu
parent 000e500051
commit f5d5791fdf
@@ -13,7 +13,7 @@ import 'package:tmail_ui_user/features/login/data/local/account_cache_manager.da
import 'package:tmail_ui_user/features/login/data/local/token_oidc_cache_manager.dart'; import 'package:tmail_ui_user/features/login/data/local/token_oidc_cache_manager.dart';
import 'package:tmail_ui_user/features/login/data/network/authentication_client/authentication_client_base.dart'; import 'package:tmail_ui_user/features/login/data/network/authentication_client/authentication_client_base.dart';
class AuthorizationInterceptors extends InterceptorsWrapper { class AuthorizationInterceptors extends QueuedInterceptorsWrapper {
final Dio _dio; final Dio _dio;
final AuthenticationClientBase _authenticationClient; final AuthenticationClientBase _authenticationClient;
@@ -41,9 +41,7 @@ class AuthorizationInterceptors extends InterceptorsWrapper {
_token = newToken; _token = newToken;
_configOIDC = newConfig; _configOIDC = newConfig;
_authenticationType = AuthenticationType.oidc; _authenticationType = AuthenticationType.oidc;
log('AuthorizationInterceptors::setToken(): newToken: $newToken'); log('AuthorizationInterceptors::setToken(): newToken: $newToken | configOIDC: $_configOIDC');
log('AuthorizationInterceptors::setToken(): tokenId: ${newToken?.tokenIdHash}');
log('AuthorizationInterceptors::setToken(): EXPIRE_DATE: ${newToken?.expiredTime?.toIso8601String()}');
} }
void _updateNewToken(Token newToken) { void _updateNewToken(Token newToken) {
@@ -75,19 +73,17 @@ class AuthorizationInterceptors extends InterceptorsWrapper {
@override @override
void onError(DioError err, ErrorInterceptorHandler handler) async { void onError(DioError err, ErrorInterceptorHandler handler) async {
log('AuthorizationInterceptors::onError(): $err'); log('AuthorizationInterceptors::onError():dioType: ${err.type} | statusCode: ${err.response?.statusCode} | message: ${err.message} | statusMessage: ${err.response?.statusMessage}');
try { try {
if (_isTokenExpired() && if (_validateToRefreshToken(err)) {
(err.response == null || err.response?.statusCode == 401) && log('AuthorizationInterceptors::onError:RefreshTokenCalled:configOIDC: $_configOIDC | refreshTokenCurrent: ${_token?.refreshToken}');
_isRefreshTokenNotEmpty() &&
_isAuthenticationOidcValid()
) {
final newToken = await _authenticationClient.refreshingTokensOIDC( final newToken = await _authenticationClient.refreshingTokensOIDC(
_configOIDC!.clientId, _configOIDC!.clientId,
_configOIDC!.redirectUrl, _configOIDC!.redirectUrl,
_configOIDC!.discoveryUrl, _configOIDC!.discoveryUrl,
_configOIDC!.scopes, _configOIDC!.scopes,
_token!.refreshToken); _token!.refreshToken
);
final accountCurrent = await _accountCacheManager.getSelectedAccount(); final accountCurrent = await _accountCacheManager.getSelectedAccount();
@@ -106,10 +102,7 @@ class AuthorizationInterceptors extends InterceptorsWrapper {
) )
) )
]); ]);
log('AuthorizationInterceptors::onError():NewToken: $newToken');
log('AuthorizationInterceptors::onError(): refreshToken: $newToken');
log('AuthorizationInterceptors::setToken(): refreshTokenId: ${newToken.tokenIdHash}');
_updateNewToken(newToken.toToken()); _updateNewToken(newToken.toToken());
final requestOptions = err.requestOptions; final requestOptions = err.requestOptions;
@@ -146,6 +139,16 @@ class AuthorizationInterceptors extends InterceptorsWrapper {
bool _isRefreshTokenNotEmpty() => _token != null && _token!.refreshToken.isNotEmpty; bool _isRefreshTokenNotEmpty() => _token != null && _token!.refreshToken.isNotEmpty;
bool _validateToRefreshToken(DioError dioError) {
if (_isTokenExpired() &&
(dioError.response == null || dioError.response?.statusCode == 401) &&
_isRefreshTokenNotEmpty() &&
_isAuthenticationOidcValid()) {
return true;
}
return false;
}
String _getAuthorizationAsBasicHeader(String? authorization) => 'Basic $authorization'; String _getAuthorizationAsBasicHeader(String? authorization) => 'Basic $authorization';
String _getTokenAsBearerHeader(String token) => 'Bearer $token'; String _getTokenAsBearerHeader(String token) => 'Bearer $token';