TF-2461 Remove logic retry in authorizationInterceptors
Signed-off-by: dab246 <tdvu@linagora.com>
This commit is contained in:
@@ -12,7 +12,7 @@ import 'package:model/email/attachment.dart';
|
|||||||
import 'package:model/oidc/token_oidc.dart';
|
import 'package:model/oidc/token_oidc.dart';
|
||||||
import 'package:tmail_ui_user/features/email/domain/repository/email_repository.dart';
|
import 'package:tmail_ui_user/features/email/domain/repository/email_repository.dart';
|
||||||
import 'package:tmail_ui_user/features/email/domain/state/download_attachments_state.dart';
|
import 'package:tmail_ui_user/features/email/domain/state/download_attachments_state.dart';
|
||||||
import 'package:tmail_ui_user/features/login/data/network/config/authorization_interceptors.dart';
|
import 'package:tmail_ui_user/features/login/data/network/interceptors/authorization_interceptors.dart';
|
||||||
import 'package:tmail_ui_user/features/login/domain/exceptions/authentication_exception.dart';
|
import 'package:tmail_ui_user/features/login/domain/exceptions/authentication_exception.dart';
|
||||||
import 'package:tmail_ui_user/features/login/domain/extensions/oidc_configuration_extensions.dart';
|
import 'package:tmail_ui_user/features/login/domain/extensions/oidc_configuration_extensions.dart';
|
||||||
import 'package:tmail_ui_user/features/login/domain/repository/account_repository.dart';
|
import 'package:tmail_ui_user/features/login/domain/repository/account_repository.dart';
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ import 'package:tmail_ui_user/features/email/domain/usecases/move_to_mailbox_int
|
|||||||
import 'package:tmail_ui_user/features/email/domain/usecases/store_opened_email_interactor.dart';
|
import 'package:tmail_ui_user/features/email/domain/usecases/store_opened_email_interactor.dart';
|
||||||
import 'package:tmail_ui_user/features/email/presentation/controller/email_supervisor_controller.dart';
|
import 'package:tmail_ui_user/features/email/presentation/controller/email_supervisor_controller.dart';
|
||||||
import 'package:tmail_ui_user/features/email/presentation/controller/single_email_controller.dart';
|
import 'package:tmail_ui_user/features/email/presentation/controller/single_email_controller.dart';
|
||||||
import 'package:tmail_ui_user/features/login/data/network/config/authorization_interceptors.dart';
|
import 'package:tmail_ui_user/features/login/data/network/interceptors/authorization_interceptors.dart';
|
||||||
import 'package:tmail_ui_user/features/login/domain/repository/account_repository.dart';
|
import 'package:tmail_ui_user/features/login/domain/repository/account_repository.dart';
|
||||||
import 'package:tmail_ui_user/features/login/domain/repository/authentication_oidc_repository.dart';
|
import 'package:tmail_ui_user/features/login/domain/repository/authentication_oidc_repository.dart';
|
||||||
import 'package:tmail_ui_user/features/login/domain/repository/credential_repository.dart';
|
import 'package:tmail_ui_user/features/login/domain/repository/credential_repository.dart';
|
||||||
|
|||||||
+34
-49
@@ -21,9 +21,6 @@ import 'package:tmail_ui_user/main/utils/ios_sharing_manager.dart';
|
|||||||
|
|
||||||
class AuthorizationInterceptors extends QueuedInterceptorsWrapper {
|
class AuthorizationInterceptors extends QueuedInterceptorsWrapper {
|
||||||
|
|
||||||
static const int _maxRetryCount = 3;
|
|
||||||
static const String _retryKey = 'Retry';
|
|
||||||
|
|
||||||
final Dio _dio;
|
final Dio _dio;
|
||||||
final AuthenticationClientBase _authenticationClient;
|
final AuthenticationClientBase _authenticationClient;
|
||||||
final TokenOidcCacheManager _tokenOidcCacheManager;
|
final TokenOidcCacheManager _tokenOidcCacheManager;
|
||||||
@@ -52,9 +49,11 @@ class AuthorizationInterceptors extends QueuedInterceptorsWrapper {
|
|||||||
_token = newToken;
|
_token = newToken;
|
||||||
_configOIDC = newConfig;
|
_configOIDC = newConfig;
|
||||||
_authenticationType = AuthenticationType.oidc;
|
_authenticationType = AuthenticationType.oidc;
|
||||||
|
log('AuthorizationInterceptors::setTokenAndAuthorityOidc: TOKEN_INITIAL = $newToken');
|
||||||
}
|
}
|
||||||
|
|
||||||
void _updateNewToken(TokenOIDC newToken) {
|
void _updateNewToken(TokenOIDC newToken) {
|
||||||
|
log('AuthorizationInterceptors::_updateNewToken: NEW_TOKEN = $newToken');
|
||||||
_token = newToken;
|
_token = newToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,7 +63,6 @@ class AuthorizationInterceptors extends QueuedInterceptorsWrapper {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
|
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
|
||||||
log('AuthorizationInterceptors::onRequest():url: ${options.uri} | data: ${options.data} | header: ${options.headers}');
|
|
||||||
switch(_authenticationType) {
|
switch(_authenticationType) {
|
||||||
case AuthenticationType.basic:
|
case AuthenticationType.basic:
|
||||||
if (_authorization != null) {
|
if (_authorization != null) {
|
||||||
@@ -79,27 +77,38 @@ class AuthorizationInterceptors extends QueuedInterceptorsWrapper {
|
|||||||
case AuthenticationType.none:
|
case AuthenticationType.none:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
log('AuthorizationInterceptors::onRequest(): URL = ${options.uri} | HEADER = ${options.headers} | DATA = ${options.data}');
|
||||||
super.onRequest(options, handler);
|
super.onRequest(options, handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void onResponse(Response response, ResponseInterceptorHandler handler) {
|
||||||
|
log('AuthorizationInterceptors::response(): STATUS_CODE = ${response.statusCode} | DATA = ${response.data}');
|
||||||
|
super.onResponse(response, handler);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void onError(DioError err, ErrorInterceptorHandler handler) async {
|
void onError(DioError err, ErrorInterceptorHandler handler) async {
|
||||||
logError('AuthorizationInterceptors::onError(): $err');
|
logError('AuthorizationInterceptors::onError(): DIO_ERROR = $err');
|
||||||
try {
|
try {
|
||||||
final requestOptions = err.requestOptions;
|
if (validateToRefreshToken(responseStatusCode: err.response?.statusCode)) {
|
||||||
final extraInRequest = requestOptions.extra;
|
log('AuthorizationInterceptors::onError:_validateToRefreshToken');
|
||||||
var retries = extraInRequest[_retryKey] ?? 0;
|
final requestOptions = err.requestOptions;
|
||||||
|
final extraInRequest = requestOptions.extra;
|
||||||
|
|
||||||
if (_validateToRefreshToken(err)) {
|
final newTokenOidc = PlatformInfo.isIOS
|
||||||
log('AuthorizationInterceptors::onError:>> _validateToRefreshToken');
|
? await _handleRefreshTokenOnIOSPlatform()
|
||||||
|
: await _handleRefreshTokenOnOtherPlatform();
|
||||||
|
|
||||||
if (PlatformInfo.isIOS) {
|
if (newTokenOidc.token == _token?.token) {
|
||||||
await _handleRefreshTokenOnIOSPlatform();
|
log('AuthorizationInterceptors::onError: TokenOIDC duplicated');
|
||||||
} else {
|
return super.onError(err, handler);
|
||||||
await _handleRefreshTokenOnOtherPlatform();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_updateNewToken(newTokenOidc);
|
||||||
|
|
||||||
if (extraInRequest.containsKey(FileUploader.uploadAttachmentExtraKey)) {
|
if (extraInRequest.containsKey(FileUploader.uploadAttachmentExtraKey)) {
|
||||||
|
log('AuthorizationInterceptors::onError: Perform upload attachment request');
|
||||||
final uploadExtra = extraInRequest[FileUploader.uploadAttachmentExtraKey];
|
final uploadExtra = extraInRequest[FileUploader.uploadAttachmentExtraKey];
|
||||||
|
|
||||||
requestOptions.headers[HttpHeaders.authorizationHeader] = _getTokenAsBearerHeader(_token!.token);
|
requestOptions.headers[HttpHeaders.authorizationHeader] = _getTokenAsBearerHeader(_token!.token);
|
||||||
@@ -120,26 +129,18 @@ class AuthorizationInterceptors extends QueuedInterceptorsWrapper {
|
|||||||
|
|
||||||
return handler.resolve(response);
|
return handler.resolve(response);
|
||||||
} else {
|
} else {
|
||||||
|
log('AuthorizationInterceptors::onError: Perform normal request');
|
||||||
requestOptions.headers[HttpHeaders.authorizationHeader] = _getTokenAsBearerHeader(_token!.token);
|
requestOptions.headers[HttpHeaders.authorizationHeader] = _getTokenAsBearerHeader(_token!.token);
|
||||||
|
|
||||||
final response = await _dio.fetch(requestOptions);
|
final response = await _dio.fetch(requestOptions);
|
||||||
return handler.resolve(response);
|
return handler.resolve(response);
|
||||||
}
|
}
|
||||||
} else if (_validateToRetry(err, retries)) {
|
|
||||||
log('AuthorizationInterceptors::onError:>> _validateToRetry | retries: $retries');
|
|
||||||
retries++;
|
|
||||||
|
|
||||||
requestOptions.headers[HttpHeaders.authorizationHeader] = _getTokenAsBearerHeader(_token!.token);
|
|
||||||
requestOptions.extra = {_retryKey: retries};
|
|
||||||
|
|
||||||
final response = await _dio.fetch(requestOptions);
|
|
||||||
return handler.resolve(response);
|
|
||||||
} else {
|
} else {
|
||||||
super.onError(err, handler);
|
return super.onError(err, handler);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logError('AuthorizationInterceptors::onError:Exception: $e');
|
logError('AuthorizationInterceptors::onError:Exception: $e');
|
||||||
super.onError(err.copyWith(error: e), handler);
|
return super.onError(err.copyWith(error: e), handler);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -160,9 +161,10 @@ class AuthorizationInterceptors extends QueuedInterceptorsWrapper {
|
|||||||
|
|
||||||
bool _isRefreshTokenNotEmpty() => _token?.refreshToken.isNotEmpty == true;
|
bool _isRefreshTokenNotEmpty() => _token?.refreshToken.isNotEmpty == true;
|
||||||
|
|
||||||
bool _validateToRefreshToken(DioError dioError) {
|
bool validateToRefreshToken({int? responseStatusCode}) {
|
||||||
if (dioError.response?.statusCode == 401 &&
|
if (responseStatusCode == 401 &&
|
||||||
_isAuthenticationOidcValid() &&
|
_isAuthenticationOidcValid() &&
|
||||||
|
_isTokenNotEmpty() &&
|
||||||
_isRefreshTokenNotEmpty() &&
|
_isRefreshTokenNotEmpty() &&
|
||||||
_isTokenExpired()
|
_isTokenExpired()
|
||||||
) {
|
) {
|
||||||
@@ -171,17 +173,6 @@ class AuthorizationInterceptors extends QueuedInterceptorsWrapper {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool _validateToRetry(DioError dioError, int retryCount) {
|
|
||||||
if (dioError.type == DioErrorType.badResponse &&
|
|
||||||
dioError.response?.statusCode == 401 &&
|
|
||||||
_isTokenNotEmpty() &&
|
|
||||||
retryCount < _maxRetryCount
|
|
||||||
) {
|
|
||||||
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';
|
||||||
@@ -250,30 +241,24 @@ class AuthorizationInterceptors extends QueuedInterceptorsWrapper {
|
|||||||
return newToken;
|
return newToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future _handleRefreshTokenOnIOSPlatform() async {
|
Future<TokenOIDC> _handleRefreshTokenOnIOSPlatform() async {
|
||||||
final keychainToken = await _getTokenInKeychain(_token!);
|
final keychainToken = await _getTokenInKeychain(_token!);
|
||||||
|
|
||||||
if (keychainToken == null) {
|
if (keychainToken == null) {
|
||||||
final newToken = await _invokeRefreshTokenFromServer();
|
final newToken = await _invokeRefreshTokenFromServer();
|
||||||
|
|
||||||
_updateNewToken(newToken);
|
|
||||||
|
|
||||||
final newAccount = await _updateCurrentAccount(tokenOIDC: newToken);
|
final newAccount = await _updateCurrentAccount(tokenOIDC: newToken);
|
||||||
|
|
||||||
await _iosSharingManager.saveKeyChainSharingSession(newAccount);
|
await _iosSharingManager.saveKeyChainSharingSession(newAccount);
|
||||||
|
return newToken;
|
||||||
} else {
|
} else {
|
||||||
_updateNewToken(keychainToken);
|
|
||||||
|
|
||||||
await _updateCurrentAccount(tokenOIDC: keychainToken);
|
await _updateCurrentAccount(tokenOIDC: keychainToken);
|
||||||
|
return keychainToken;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future _handleRefreshTokenOnOtherPlatform() async {
|
Future<TokenOIDC> _handleRefreshTokenOnOtherPlatform() async {
|
||||||
final newToken = await _invokeRefreshTokenFromServer();
|
final newToken = await _invokeRefreshTokenFromServer();
|
||||||
|
|
||||||
_updateNewToken(newToken);
|
|
||||||
|
|
||||||
await _updateCurrentAccount(tokenOIDC: newToken);
|
await _updateCurrentAccount(tokenOIDC: newToken);
|
||||||
|
return newToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
void clear() {
|
void clear() {
|
||||||
@@ -20,7 +20,7 @@ import 'package:tmail_ui_user/features/email/domain/usecases/unsubscribe_email_i
|
|||||||
import 'package:tmail_ui_user/features/home/domain/extensions/session_extensions.dart';
|
import 'package:tmail_ui_user/features/home/domain/extensions/session_extensions.dart';
|
||||||
import 'package:tmail_ui_user/features/home/domain/state/get_session_state.dart';
|
import 'package:tmail_ui_user/features/home/domain/state/get_session_state.dart';
|
||||||
import 'package:tmail_ui_user/features/home/domain/usecases/get_session_interactor.dart';
|
import 'package:tmail_ui_user/features/home/domain/usecases/get_session_interactor.dart';
|
||||||
import 'package:tmail_ui_user/features/login/data/network/config/authorization_interceptors.dart';
|
import 'package:tmail_ui_user/features/login/data/network/interceptors/authorization_interceptors.dart';
|
||||||
import 'package:tmail_ui_user/features/login/domain/state/get_authenticated_account_state.dart';
|
import 'package:tmail_ui_user/features/login/domain/state/get_authenticated_account_state.dart';
|
||||||
import 'package:tmail_ui_user/features/login/domain/state/get_credential_state.dart';
|
import 'package:tmail_ui_user/features/login/domain/state/get_credential_state.dart';
|
||||||
import 'package:tmail_ui_user/features/login/domain/state/get_stored_token_oidc_state.dart';
|
import 'package:tmail_ui_user/features/login/domain/state/get_stored_token_oidc_state.dart';
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ import 'package:tmail_ui_user/features/home/domain/extensions/session_extensions
|
|||||||
import 'package:tmail_ui_user/features/home/domain/state/get_session_state.dart';
|
import 'package:tmail_ui_user/features/home/domain/state/get_session_state.dart';
|
||||||
import 'package:tmail_ui_user/features/home/domain/usecases/get_session_interactor.dart';
|
import 'package:tmail_ui_user/features/home/domain/usecases/get_session_interactor.dart';
|
||||||
import 'package:tmail_ui_user/features/home/presentation/home_bindings.dart';
|
import 'package:tmail_ui_user/features/home/presentation/home_bindings.dart';
|
||||||
import 'package:tmail_ui_user/features/login/data/network/config/authorization_interceptors.dart';
|
import 'package:tmail_ui_user/features/login/data/network/interceptors/authorization_interceptors.dart';
|
||||||
import 'package:tmail_ui_user/features/login/domain/state/get_authenticated_account_state.dart';
|
import 'package:tmail_ui_user/features/login/domain/state/get_authenticated_account_state.dart';
|
||||||
import 'package:tmail_ui_user/features/login/domain/state/get_credential_state.dart';
|
import 'package:tmail_ui_user/features/login/domain/state/get_credential_state.dart';
|
||||||
import 'package:tmail_ui_user/features/login/domain/state/get_stored_token_oidc_state.dart';
|
import 'package:tmail_ui_user/features/login/domain/state/get_stored_token_oidc_state.dart';
|
||||||
|
|||||||
Reference in New Issue
Block a user