mirror of
https://github.com/linagora/tmail-flutter.git
synced 2026-12-05 08:05:07 +00:00
248 lines
10 KiB
Dart
248 lines
10 KiB
Dart
import 'package:core/presentation/state/failure.dart';
|
|
import 'package:core/presentation/state/success.dart';
|
|
import 'package:core/utils/app_logger.dart';
|
|
import 'package:core/utils/platform_info.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:jmap_dart_client/jmap/account_id.dart';
|
|
import 'package:jmap_dart_client/jmap/core/capability/capability_identifier.dart';
|
|
import 'package:jmap_dart_client/jmap/core/session/session.dart';
|
|
import 'package:jmap_dart_client/jmap/core/user_name.dart';
|
|
import 'package:model/account/password.dart';
|
|
import 'package:model/oidc/oidc_configuration.dart';
|
|
import 'package:model/oidc/token_oidc.dart';
|
|
import 'package:tmail_ui_user/features/base/base_controller.dart';
|
|
import 'package:tmail_ui_user/features/delegation/presentation/utils/sign_in_email_link.dart';
|
|
import 'package:tmail_ui_user/features/home/data/exceptions/session_exceptions.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/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_oidc_user_info_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/update_authentication_account_state.dart';
|
|
import 'package:tmail_ui_user/features/login/domain/usecases/get_authenticated_account_interactor.dart';
|
|
import 'package:tmail_ui_user/features/login/domain/usecases/get_oidc_user_info_interactor.dart';
|
|
import 'package:tmail_ui_user/features/login/domain/usecases/update_account_cache_interactor.dart';
|
|
import 'package:tmail_ui_user/features/manage_account/presentation/vacation/vacation_interactors_bindings.dart';
|
|
import 'package:tmail_ui_user/main/providers/workplace/drive_attachment_enabled_notifier.dart';
|
|
import 'package:tmail_ui_user/main/providers/workplace/fqdn/workplace_fqdn_user_info_notifier.dart';
|
|
import 'package:tmail_ui_user/main/error/capability_validator.dart';
|
|
import 'package:tmail_ui_user/main/providers/app_provider_container.dart';
|
|
import 'package:tmail_ui_user/main/exceptions/remote/authentication_exception.dart';
|
|
import 'package:tmail_ui_user/main/utils/app_config.dart';
|
|
|
|
abstract class ReloadableController extends BaseController {
|
|
final GetSessionInteractor getSessionInteractor = Get.find<GetSessionInteractor>();
|
|
final GetAuthenticatedAccountInteractor getAuthenticatedAccountInteractor = Get.find<GetAuthenticatedAccountInteractor>();
|
|
final UpdateAccountCacheInteractor _updateAccountCacheInteractor = Get.find<UpdateAccountCacheInteractor>();
|
|
final GetOidcUserInfoInteractor _getOidcUserInfoInteractor = Get.find<GetOidcUserInfoInteractor>();
|
|
|
|
bool _sessionLoadedFromCache = false;
|
|
|
|
/// True when the session just applied was read from cache (ADR-0114 offline start).
|
|
bool get sessionLoadedFromCache => _sessionLoadedFromCache;
|
|
|
|
@override
|
|
void handleFailureViewState(Failure failure) {
|
|
if (isNotSignedIn(failure)) {
|
|
logWarning('$runtimeType::handleFailureViewState():Failure = $failure');
|
|
keepSignInEmailLinkOfCurrentPage();
|
|
goToLogin();
|
|
} else if (failure is GetSessionFailure) {
|
|
logWarning('$runtimeType::handleFailureViewState():Failure = $failure');
|
|
handleGetSessionFailure(failure);
|
|
} else if (failure is UpdateAccountCacheFailure) {
|
|
logWarning('$runtimeType::handleFailureViewState():Failure = $failure');
|
|
_handleUpdateAccountCacheCompleted(
|
|
session: failure.session,
|
|
apiUrl: failure.apiUrl);
|
|
} else if (failure is GetOidcUserInfoFailure) {
|
|
twakeAppManager.clearOidcUserInfo();
|
|
appProviderContainer.read(workplaceFqdnUserInfoProvider.notifier).setFqdn(null);
|
|
appProviderContainer.read(driveAttachmentEnabledProvider.notifier).setEnabled(null);
|
|
} else {
|
|
super.handleFailureViewState(failure);
|
|
}
|
|
}
|
|
|
|
@override
|
|
void handleSuccessViewState(Success success) {
|
|
if (success is GetCredentialViewState) {
|
|
log('$runtimeType::handleSuccessViewState:Success = ${success.runtimeType}');
|
|
_handleGetCredentialSuccess(success);
|
|
} else if (success is GetStoredTokenOidcSuccess) {
|
|
log('$runtimeType::handleSuccessViewState:Success = ${success.runtimeType}');
|
|
_handleGetStoredTokenOidcSuccess(success);
|
|
} else if (success is GetSessionSuccess) {
|
|
log('$runtimeType::handleSuccessViewState:Success = ${success.runtimeType}');
|
|
_sessionLoadedFromCache = success.fromCache;
|
|
updateAccountCache(
|
|
session: success.session,
|
|
baseUrl: dynamicUrlInterceptors.baseUrl);
|
|
} else if (success is UpdateAccountCacheSuccess) {
|
|
log('$runtimeType::handleSuccessViewState:Success = ${success.runtimeType}');
|
|
_handleUpdateAccountCacheCompleted(
|
|
session: success.session,
|
|
apiUrl: success.apiUrl);
|
|
} else if (success is GetOidcUserInfoSuccess) {
|
|
log('$runtimeType::handleSuccessViewState:GetOidcUserInfoSuccess: OidcUserInfo = ${success.oidcUserInfo.toJson().toString()}');
|
|
twakeAppManager.setOidcUserInfo(success.oidcUserInfo);
|
|
appProviderContainer
|
|
.read(workplaceFqdnUserInfoProvider.notifier)
|
|
.setFqdn(success.oidcUserInfo.workplaceFqdn);
|
|
} else {
|
|
super.handleSuccessViewState(success);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* trigger reload by getting Credential again then setting up Interceptor and retrieving session
|
|
* */
|
|
void reload() {
|
|
getAuthenticatedAccountAction();
|
|
}
|
|
|
|
void getAuthenticatedAccountAction() {
|
|
consumeState(getAuthenticatedAccountInteractor.execute());
|
|
}
|
|
|
|
void _handleGetCredentialSuccess(GetCredentialViewState success) {
|
|
setDataToInterceptors(
|
|
baseUrl: success.baseUrl.toString(),
|
|
userName: success.userName,
|
|
password: success.password);
|
|
getSessionAction();
|
|
}
|
|
|
|
void _handleGetStoredTokenOidcSuccess(GetStoredTokenOidcSuccess success) {
|
|
setDataToInterceptors(
|
|
baseUrl: success.baseUrl.toString(),
|
|
tokenOIDC: success.tokenOidc,
|
|
oidcConfiguration: success.oidcConfiguration);
|
|
getSessionAction();
|
|
}
|
|
|
|
void _handleUpdateAccountCacheCompleted({required Session session, String? apiUrl}) {
|
|
dynamicUrlInterceptors.changeBaseUrl(apiUrl);
|
|
handleReloaded(session);
|
|
}
|
|
|
|
void setDataToInterceptors({
|
|
required String baseUrl,
|
|
UserName? userName,
|
|
Password? password,
|
|
TokenOIDC? tokenOIDC,
|
|
OIDCConfiguration? oidcConfiguration
|
|
}) {
|
|
dynamicUrlInterceptors.setJmapUrl(baseUrl);
|
|
dynamicUrlInterceptors.changeBaseUrl(baseUrl);
|
|
|
|
if (userName != null && password != null) {
|
|
authorizationInterceptors.setBasicAuthorization(userName, password);
|
|
authorizationIsolateInterceptors.setBasicAuthorization(userName, password);
|
|
}
|
|
|
|
if (tokenOIDC != null && oidcConfiguration != null) {
|
|
authorizationInterceptors.setTokenAndAuthorityOidc(newToken: tokenOIDC, newConfig: oidcConfiguration);
|
|
authorizationIsolateInterceptors.setTokenAndAuthorityOidc(newToken: tokenOIDC, newConfig: oidcConfiguration);
|
|
|
|
if (_shouldCallUserInfo) {
|
|
getOidcUserInfo(oidcConfiguration);
|
|
}
|
|
}
|
|
}
|
|
|
|
bool get _shouldCallUserInfo =>
|
|
!PlatformInfo.isWeb || AppConfig.isSaasPlatForm;
|
|
|
|
void getSessionAction() {
|
|
consumeState(getSessionInteractor.execute());
|
|
}
|
|
|
|
void getOidcUserInfo(OIDCConfiguration oidcConfiguration) {
|
|
consumeState(_getOidcUserInfoInteractor.execute(oidcConfiguration));
|
|
}
|
|
|
|
void handleGetSessionFailure(GetSessionFailure failure) {
|
|
final exception = failure.exception;
|
|
if (exception is! BadCredentialsException) {
|
|
toastManager.showMessageFailure(failure);
|
|
}
|
|
if (exception is BadCredentialsException ||
|
|
exception is RefreshTokenFailedException ||
|
|
exception is NotFoundSessionException) {
|
|
final authErrorType = _sessionLogoutAuthErrorType(exception);
|
|
logError(
|
|
'$runtimeType::handleGetSessionFailure: '
|
|
'auth_error_type=$authErrorType | will_logout=true — '
|
|
'session definitively dead — exception=${exception.runtimeType}',
|
|
exception: exception,
|
|
stackTrace: StackTrace.current,
|
|
extras: {'auth_error_type': authErrorType},
|
|
webConsoleEnabled: true,
|
|
);
|
|
keepSignInEmailLinkOfCurrentPage();
|
|
clearDataAndGoToLoginPage();
|
|
} else {
|
|
logWarning(
|
|
'$runtimeType::handleGetSessionFailure: NOT logging out — '
|
|
'exception=${exception.runtimeType} not in {BadCredentials, '
|
|
'RefreshTokenFailed, NotFoundSession}',
|
|
webConsoleEnabled: true,
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Tags the forced-logout cause from a definitively-dead session so every
|
|
/// logout is queryable in Sentry by a single `auth_error_type` field.
|
|
String _sessionLogoutAuthErrorType(Object? exception) {
|
|
if (exception is RefreshTokenFailedException) {
|
|
return 'forced_logout_session_refresh_token_failed';
|
|
}
|
|
if (exception is NotFoundSessionException) {
|
|
return 'forced_logout_session_not_found';
|
|
}
|
|
return 'forced_logout_session_bad_credentials';
|
|
}
|
|
|
|
void handleReloaded(Session session) {}
|
|
|
|
void injectVacationBindings(Session? session, AccountId? accountId) {
|
|
try {
|
|
requireCapability(session!, accountId!, [CapabilityIdentifier.jmapVacationResponse]);
|
|
VacationInteractorsBindings().dependencies();
|
|
} catch(e) {
|
|
logWarning('$runtimeType::injectVacationBindings(): exception: $e');
|
|
}
|
|
}
|
|
|
|
void updateAccountCache({
|
|
required Session session,
|
|
String? baseUrl
|
|
}) {
|
|
consumeState(_updateAccountCacheInteractor.execute(
|
|
session: session,
|
|
baseUrl: baseUrl
|
|
));
|
|
}
|
|
|
|
bool isNotSignedIn(Failure failure) {
|
|
return failure is GetCredentialFailure ||
|
|
failure is GetStoredTokenOidcFailure ||
|
|
failure is GetAuthenticatedAccountFailure;
|
|
}
|
|
|
|
void synchronizeTokenAndGetSession({
|
|
required Uri baseUri,
|
|
required TokenOIDC tokenOIDC,
|
|
required OIDCConfiguration oidcConfiguration,
|
|
}) {
|
|
setDataToInterceptors(
|
|
baseUrl: baseUri.toString(),
|
|
tokenOIDC: tokenOIDC,
|
|
oidcConfiguration: oidcConfiguration,
|
|
);
|
|
|
|
getSessionAction();
|
|
}
|
|
} |