Skip to content

Commit

Permalink
TF-2387 Handle signIn twake workplace via use company server
Browse files Browse the repository at this point in the history
  • Loading branch information
dab246 committed Nov 7, 2024
1 parent 7eaded5 commit ee6087d
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 28 deletions.
4 changes: 1 addition & 3 deletions env.file
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ APP_GRID_AVAILABLE=supported
FCM_AVAILABLE=supported
IOS_FCM=supported
FORWARD_WARNING_MESSAGE=
PLATFORM=other
REGISTRATION_URL=http://localhost/
SAAS_SERVER_URL=http://localhost/
PLATFORM=other
23 changes: 23 additions & 0 deletions lib/features/login/presentation/login_bindings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:tmail_ui_user/features/caching/clients/recent_login_username_cac
import 'package:tmail_ui_user/features/login/data/datasource/login_datasource.dart';
import 'package:tmail_ui_user/features/login/data/datasource_impl/hive_login_datasource_impl.dart';
import 'package:tmail_ui_user/features/login/data/datasource_impl/login_datasource_impl.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/dns_service.dart';
import 'package:tmail_ui_user/features/login/data/repository/login_repository_impl.dart';
import 'package:tmail_ui_user/features/login/domain/repository/account_repository.dart';
Expand All @@ -26,6 +27,11 @@ import 'package:tmail_ui_user/features/login/domain/usecases/get_token_oidc_inte
import 'package:tmail_ui_user/features/login/domain/usecases/save_login_url_on_mobile_interactor.dart';
import 'package:tmail_ui_user/features/login/domain/usecases/save_login_username_on_mobile_interactor.dart';
import 'package:tmail_ui_user/features/login/presentation/login_controller.dart';
import 'package:tmail_ui_user/features/starting_page/data/datasource/saas_authentication_datasource.dart';
import 'package:tmail_ui_user/features/starting_page/data/datasource_impl/saas_authentication_datasource_impl.dart';
import 'package:tmail_ui_user/features/starting_page/data/repository/saas_authentication_repository_impl.dart';
import 'package:tmail_ui_user/features/starting_page/domain/repository/saas_authentication_repository.dart';
import 'package:tmail_ui_user/features/starting_page/domain/usecase/sign_in_twake_workplace_interactor.dart';
import 'package:tmail_ui_user/main/exceptions/cache_exception_thrower.dart';
import 'package:tmail_ui_user/main/exceptions/remote_exception_thrower.dart';

Expand All @@ -47,12 +53,15 @@ class LoginBindings extends BaseBindings {
Get.find<SaveLoginUsernameOnMobileInteractor>(),
Get.find<GetAllRecentLoginUsernameOnMobileInteractor>(),
Get.find<DNSLookupToGetJmapUrlInteractor>(),
Get.find<SignInTwakeWorkplaceInteractor>(),
));
}

@override
void bindingsDataSource() {
Get.lazyPut<LoginDataSource>(() => Get.find<LoginDataSourceImpl>());
Get.lazyPut<SaasAuthenticationDataSource>(
() => Get.find<SaasAuthenticationDataSourceImpl>());
}

@override
Expand All @@ -66,6 +75,10 @@ class LoginBindings extends BaseBindings {
Get.find<DNSService>(),
Get.find<RemoteExceptionThrower>(),
));
Get.lazyPut(() => SaasAuthenticationDataSourceImpl(
Get.find<AuthenticationClientBase>(),
Get.find<RemoteExceptionThrower>(),
));
}

@override
Expand Down Expand Up @@ -98,11 +111,19 @@ class LoginBindings extends BaseBindings {
Get.lazyPut(() => SaveLoginUsernameOnMobileInteractor(Get.find<LoginRepository>(),));
Get.lazyPut(() => GetAllRecentLoginUsernameOnMobileInteractor(Get.find<LoginRepository>()));
Get.lazyPut(() => DNSLookupToGetJmapUrlInteractor(Get.find<LoginRepository>()));
Get.lazyPut(() => SignInTwakeWorkplaceInteractor(
Get.find<SaasAuthenticationRepository>(),
Get.find<AuthenticationOIDCRepository>(),
Get.find<AccountRepository>(),
Get.find<CredentialRepository>(),
));
}

@override
void bindingsRepository() {
Get.lazyPut<LoginRepository>(() => Get.find<LoginRepositoryImpl>());
Get.lazyPut<SaasAuthenticationRepository>(
() => Get.find<SaasAuthenticationRepositoryImpl>());
}

@override
Expand All @@ -113,5 +134,7 @@ class LoginBindings extends BaseBindings {
DataSourceType.network: Get.find<LoginDataSource>(),
}
));
Get.lazyPut(() => SaasAuthenticationRepositoryImpl(
Get.find<SaasAuthenticationDataSource>()));
}
}
62 changes: 51 additions & 11 deletions lib/features/login/presentation/login_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'package:model/account/password.dart';
import 'package:model/oidc/oidc_configuration.dart';
import 'package:model/oidc/request/oidc_request.dart';
import 'package:model/oidc/response/oidc_response.dart';
import 'package:model/oidc/token_oidc.dart';
import 'package:tmail_ui_user/features/base/reloadable/reloadable_controller.dart';
import 'package:tmail_ui_user/features/home/domain/state/get_session_state.dart';
import 'package:tmail_ui_user/features/login/data/network/oidc_error.dart';
Expand Down Expand Up @@ -49,6 +50,8 @@ import 'package:tmail_ui_user/features/login/domain/usecases/save_login_url_on_m
import 'package:tmail_ui_user/features/login/domain/usecases/save_login_username_on_mobile_interactor.dart';
import 'package:tmail_ui_user/features/login/presentation/login_form_type.dart';
import 'package:tmail_ui_user/features/login/presentation/model/login_arguments.dart';
import 'package:tmail_ui_user/features/starting_page/domain/state/sign_in_twake_workplace_state.dart';
import 'package:tmail_ui_user/features/starting_page/domain/usecase/sign_in_twake_workplace_interactor.dart';
import 'package:tmail_ui_user/main/routes/app_routes.dart';
import 'package:tmail_ui_user/main/routes/route_navigation.dart';
import 'package:tmail_ui_user/main/routes/route_utils.dart';
Expand All @@ -70,6 +73,7 @@ class LoginController extends ReloadableController {
final SaveLoginUsernameOnMobileInteractor _saveLoginUsernameOnMobileInteractor;
final GetAllRecentLoginUsernameOnMobileInteractor _getAllRecentLoginUsernameOnMobileInteractor;
final DNSLookupToGetJmapUrlInteractor _dnsLookupToGetJmapUrlInteractor;
final SignInTwakeWorkplaceInteractor _signInTwakeWorkplaceInteractor;

final TextEditingController urlInputController = TextEditingController();
final TextEditingController usernameInputController = TextEditingController();
Expand Down Expand Up @@ -99,6 +103,7 @@ class LoginController extends ReloadableController {
this._saveLoginUsernameOnMobileInteractor,
this._getAllRecentLoginUsernameOnMobileInteractor,
this._dnsLookupToGetJmapUrlInteractor,
this._signInTwakeWorkplaceInteractor,
);

@override
Expand Down Expand Up @@ -130,7 +135,8 @@ class LoginController extends ReloadableController {
_handleCheckOIDCIsAvailableFailure(failure);
} else if (failure is GetStoredOidcConfigurationFailure ||
failure is GetOIDCIsAvailableFailure ||
failure is GetOIDCConfigurationFailure
failure is GetOIDCConfigurationFailure ||
failure is SignInTwakeWorkplaceFailure
) {
_handleCommonOIDCFailure();
} else if (failure is GetTokenOIDCFailure) {
Expand Down Expand Up @@ -168,6 +174,12 @@ class LoginController extends ReloadableController {
_loginSuccessAction(success);
} else if (success is DNSLookupToGetJmapUrlSuccess) {
_handleDNSLookupToGetJmapUrlSuccess(success);
} else if (success is SignInTwakeWorkplaceSuccess) {
_synchronizeTokenAndGetSession(
baseUri: success.baseUri,
tokenOIDC: success.tokenOIDC,
oidcConfiguration: success.oidcConfiguration,
);
} else {
super.handleSuccessViewState(success);
}
Expand All @@ -180,7 +192,9 @@ class LoginController extends ReloadableController {
_handleCheckOIDCIsAvailableFailure(failure);
} else if (failure is GetStoredOidcConfigurationFailure ||
failure is GetOIDCConfigurationFailure ||
failure is GetOIDCIsAvailableFailure) {
failure is GetOIDCIsAvailableFailure ||
failure is SignInTwakeWorkplaceFailure
) {
_handleCommonOIDCFailure();
} else if (failure is GetTokenOIDCFailure) {
_handleNoSuitableBrowserOIDC(failure)
Expand Down Expand Up @@ -326,11 +340,41 @@ class LoginController extends ReloadableController {
void _getOIDCConfigurationSuccess(GetOIDCConfigurationSuccess success) {
if (PlatformInfo.isWeb) {
_authenticateOidcOnBrowserAction(success.oidcConfiguration);
} else if (success.oidcConfiguration.authority == AppConfig.saasRegistrationUrl) {
_getTokenOIDCOnSaaSPlatform(success.oidcConfiguration);
} else {
_getTokenOIDCAction(success.oidcConfiguration);
}
}

void _getTokenOIDCOnSaaSPlatform(OIDCConfiguration oidcConfiguration) {
if (_currentBaseUrl != null) {
consumeState(_signInTwakeWorkplaceInteractor.execute(
baseUri: _currentBaseUrl!,
oidcConfiguration: oidcConfiguration,
));
} else {
dispatchState(Left(GetTokenOIDCFailure(CanNotFoundBaseUrl())));
}
}

void _synchronizeTokenAndGetSession({
required Uri baseUri,
required TokenOIDC tokenOIDC,
required OIDCConfiguration oidcConfiguration,
}) {
dynamicUrlInterceptors.setJmapUrl(baseUri.toString());
dynamicUrlInterceptors.changeBaseUrl(baseUri.toString());
authorizationInterceptors.setTokenAndAuthorityOidc(
newToken: tokenOIDC,
newConfig: oidcConfiguration);
authorizationIsolateInterceptors.setTokenAndAuthorityOidc(
newToken: tokenOIDC,
newConfig: oidcConfiguration);

getSessionAction();
}

void _getTokenOIDCAction(OIDCConfiguration config) {
if (_currentBaseUrl != null) {
consumeState(_getTokenOIDCInteractor.execute(_currentBaseUrl!, config));
Expand All @@ -357,15 +401,11 @@ class LoginController extends ReloadableController {
}

void _getTokenOIDCSuccess(GetTokenOIDCSuccess success) {
dynamicUrlInterceptors.setJmapUrl(_currentBaseUrl?.toString());
dynamicUrlInterceptors.changeBaseUrl(_currentBaseUrl?.toString());
authorizationInterceptors.setTokenAndAuthorityOidc(
newToken: success.tokenOIDC,
newConfig: success.configuration);
authorizationIsolateInterceptors.setTokenAndAuthorityOidc(
newToken: success.tokenOIDC,
newConfig: success.configuration);
getSessionAction();
_synchronizeTokenAndGetSession(
baseUri: _currentBaseUrl!,
tokenOIDC: success.tokenOIDC,
oidcConfiguration: success.configuration,
);
}

void _loginSuccessAction(AuthenticationUserSuccess success) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ class TwakeWelcomeController extends ReloadableController {
void onClickSignIn(BuildContext context) {
TipDialogHelper.loading(AppLocalizations.of(context).loadingPleaseWait);

if (AppConfig.saasJmapServerUrl.isEmpty) {
consumeState(Stream.value(Left(SignInTwakeWorkplaceFailure(CanNotFoundSaasServerUrl()))));
return;
}

final baseUri = Uri.tryParse(AppConfig.saasJmapServerUrl);

if (baseUri == null) {
Expand All @@ -71,11 +66,6 @@ class TwakeWelcomeController extends ReloadableController {
void onSignUpTwakeWorkplace(BuildContext context) {
TipDialogHelper.loading(AppLocalizations.of(context).loadingPleaseWait);

if (AppConfig.saasJmapServerUrl.isEmpty) {
consumeState(Stream.value(Left(SignUpTwakeWorkplaceFailure(CanNotFoundSaasServerUrl()))));
return;
}

final baseUri = Uri.tryParse(AppConfig.saasJmapServerUrl);

if (baseUri == null) {
Expand Down
6 changes: 2 additions & 4 deletions lib/main/utils/app_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class AppConfig {
static const String iOSKeychainSharingService = 'com.linagora.ios.teammail.sessions';
static const String saasPlatform = 'saas';
static const String linagoraPrivacyUrl = 'https://www.linagora.com/en/legal/privacy';
static const String saasRegistrationUrl = 'https://sign-up.stg.lin-saas.com';
static const String saasJmapServerUrl = 'https://jmap.stg.lin-saas.com';

static String get baseUrl => dotenv.get('SERVER_URL', fallback: '');
static String get domainRedirectUrl => dotenv.get('DOMAIN_REDIRECT_URL', fallback: '');
Expand Down Expand Up @@ -66,8 +68,4 @@ class AppConfig {
static String get _platformEnv => dotenv.get('PLATFORM', fallback: 'other');

static bool get isSaasPlatForm => _platformEnv.toLowerCase() == saasPlatform;

static String get saasRegistrationUrl => dotenv.get('SAAS_REGISTRATION_URL', fallback: '');

static String get saasJmapServerUrl => dotenv.get('SAAS_JMAP_SERVER_URL', fallback: '');
}
5 changes: 5 additions & 0 deletions test/features/login/presentation/login_controller_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import 'package:tmail_ui_user/features/login/presentation/login_controller.dart'
import 'package:tmail_ui_user/features/login/presentation/login_form_type.dart';
import 'package:tmail_ui_user/features/manage_account/data/local/language_cache_manager.dart';
import 'package:tmail_ui_user/features/manage_account/domain/usecases/log_out_oidc_interactor.dart';
import 'package:tmail_ui_user/features/starting_page/domain/usecase/sign_in_twake_workplace_interactor.dart';
import 'package:tmail_ui_user/main/bindings/network/binding_tag.dart';
import 'package:tmail_ui_user/main/utils/toast_manager.dart';
import 'package:uuid/uuid.dart';
Expand Down Expand Up @@ -62,6 +63,7 @@ import 'login_controller_test.mocks.dart';
MockSpec<SaveLoginUsernameOnMobileInteractor>(),
MockSpec<GetAllRecentLoginUsernameOnMobileInteractor>(),
MockSpec<DNSLookupToGetJmapUrlInteractor>(),
MockSpec<SignInTwakeWorkplaceInteractor>(),
MockSpec<GetSessionInteractor>(),
MockSpec<GetAuthenticatedAccountInteractor>(),
MockSpec<UpdateAccountCacheInteractor>(),
Expand All @@ -84,6 +86,7 @@ void main() {
late MockSaveLoginUsernameOnMobileInteractor mockSaveLoginUsernameOnMobileInteractor;
late MockGetAllRecentLoginUsernameOnMobileInteractor mockGetAllRecentLoginUsernameOnMobileInteractor;
late MockDNSLookupToGetJmapUrlInteractor mockDNSLookupToGetJmapUrlInteractor;
late MockSignInTwakeWorkplaceInteractor mockSignInTwakeWorkplaceInteractor;
late MockGetSessionInteractor mockGetSessionInteractor;
late MockGetAuthenticatedAccountInteractor mockGetAuthenticatedAccountInteractor;
late MockUpdateAccountCacheInteractor mockUpdateAccountCacheInteractor;
Expand Down Expand Up @@ -118,6 +121,7 @@ void main() {
mockSaveLoginUsernameOnMobileInteractor = MockSaveLoginUsernameOnMobileInteractor();
mockGetAllRecentLoginUsernameOnMobileInteractor = MockGetAllRecentLoginUsernameOnMobileInteractor();
mockDNSLookupToGetJmapUrlInteractor = MockDNSLookupToGetJmapUrlInteractor();
mockSignInTwakeWorkplaceInteractor = MockSignInTwakeWorkplaceInteractor();

// mock reloadable controller
mockGetSessionInteractor = MockGetSessionInteractor();
Expand Down Expand Up @@ -175,6 +179,7 @@ void main() {
mockSaveLoginUsernameOnMobileInteractor,
mockGetAllRecentLoginUsernameOnMobileInteractor,
mockDNSLookupToGetJmapUrlInteractor,
mockSignInTwakeWorkplaceInteractor,
);


Expand Down

0 comments on commit ee6087d

Please sign in to comment.