Skip to content

Commit

Permalink
TF-2810 Handle crashed when no browser available for OIDC
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangdat committed Apr 23, 2024
1 parent 5a22ea0 commit d2fdbfa
Show file tree
Hide file tree
Showing 9 changed files with 286 additions and 19 deletions.
19 changes: 18 additions & 1 deletion lib/features/login/domain/exceptions/login_exception.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@

import 'package:flutter/services.dart';

class NotFoundDataResourceRecordException implements Exception {}

class NotFoundUrlException implements Exception {}
class NotFoundUrlException implements Exception {}

class NoSuitableBrowserForOIDCException implements Exception {

static const noBrowserAvailableCode = 'no_browser_available';

static bool verifyException(dynamic exception) {
if (exception is PlatformException) {
if (exception.code == noBrowserAvailableCode) {
return true;
}
}
return false;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import 'package:core/presentation/state/failure.dart';
import 'package:core/presentation/state/success.dart';
import 'package:core/utils/app_logger.dart';
import 'package:dartz/dartz.dart';
import 'package:flutter/services.dart';
import 'package:model/account/authentication_type.dart';
import 'package:model/account/personal_account.dart';
import 'package:model/oidc/oidc_configuration.dart';
import 'package:model/oidc/token_oidc.dart';
import 'package:tmail_ui_user/features/login/domain/exceptions/login_exception.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/authentication_oidc_repository.dart';
Expand Down Expand Up @@ -44,6 +46,13 @@ class GetTokenOIDCInteractor {
)
);
yield Right<Failure, Success>(GetTokenOIDCSuccess(tokenOIDC, config));
} on PlatformException catch (e) {
logError('GetTokenOIDCInteractor::execute(): PlatformException ${e.message} - ${e.stacktrace}');
if (NoSuitableBrowserForOIDCException.verifyException(e)) {
yield Left<Failure, Success>(GetTokenOIDCFailure(NoSuitableBrowserForOIDCException()));
} else {
yield Left<Failure, Success>(GetTokenOIDCFailure(e));
}
} catch (e) {
logError('GetTokenOIDCInteractor::execute(): $e');
yield Left<Failure, Success>(GetTokenOIDCFailure(e));
Expand Down
43 changes: 29 additions & 14 deletions lib/features/login/presentation/login_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import 'package:tmail_ui_user/features/base/reloadable/reloadable_controller.dar
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';
import 'package:tmail_ui_user/features/login/domain/exceptions/authentication_exception.dart';
import 'package:tmail_ui_user/features/login/domain/exceptions/login_exception.dart';
import 'package:tmail_ui_user/features/login/domain/model/login_constants.dart';
import 'package:tmail_ui_user/features/login/domain/model/recent_login_url.dart';
import 'package:tmail_ui_user/features/login/domain/model/recent_login_username.dart';
Expand Down Expand Up @@ -121,18 +122,18 @@ class LoginController extends ReloadableController {

@override
void handleFailureViewState(Failure failure) {
log('LoginController::handleFailureViewState(): $failure');
if (failure is GetAuthenticationInfoFailure) {
getAuthenticatedAccountAction();
} else if (failure is CheckOIDCIsAvailableFailure ||
failure is GetStoredOidcConfigurationFailure ||
failure is GetOIDCIsAvailableFailure ||
failure is GetOIDCConfigurationFailure ||
failure is GetTokenOIDCFailure) {
if (PlatformInfo.isMobile && loginFormType.value == LoginFormType.dnsLookupForm) {
_showPasswordForm();
} else {
_showCredentialForm();
}
failure is GetOIDCConfigurationFailure
) {
_handleCommonOIDCFailure();
} else if (failure is GetTokenOIDCFailure) {
_handleNoSuitableBrowserOIDC(failure)
.map((stillFailed) => _handleCommonOIDCFailure());
} else if (failure is GetAuthenticatedAccountFailure) {
_checkOIDCIsAvailable();
} else if (failure is GetSessionFailure) {
Expand Down Expand Up @@ -176,13 +177,11 @@ class LoginController extends ReloadableController {
if (failure is CheckOIDCIsAvailableFailure ||
failure is GetStoredOidcConfigurationFailure ||
failure is GetOIDCConfigurationFailure ||
failure is GetOIDCIsAvailableFailure ||
failure is GetTokenOIDCFailure) {
if (PlatformInfo.isMobile && loginFormType.value == LoginFormType.dnsLookupForm) {
_showPasswordForm();
} else {
_showCredentialForm();
}
failure is GetOIDCIsAvailableFailure) {
_handleCommonOIDCFailure();
} else if (failure is GetTokenOIDCFailure) {
_handleNoSuitableBrowserOIDC(failure)
.map((stillFailed) => _handleCommonOIDCFailure());
} else if (failure is GetSessionFailure) {
clearAllData();
} else {
Expand Down Expand Up @@ -420,6 +419,22 @@ class LoginController extends ReloadableController {
_checkOIDCIsAvailable();
}

void _handleCommonOIDCFailure() {
if (PlatformInfo.isMobile && loginFormType.value == LoginFormType.dnsLookupForm) {
_showPasswordForm();
} else {
_showCredentialForm();
}
}

Option<Failure> _handleNoSuitableBrowserOIDC(GetTokenOIDCFailure failure) {
if (failure.exception is NoSuitableBrowserForOIDCException) {
return const None();
} else {
return Some(failure);
}
}

void _showBaseUrlForm() {
clearState();
loginFormType.value = LoginFormType.baseUrlForm;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import 'package:core/presentation/utils/responsive_utils.dart';
import 'package:dartz/dartz.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tmail_ui_user/features/login/domain/exceptions/login_exception.dart';
import 'package:tmail_ui_user/features/login/domain/state/dns_lookup_to_get_jmap_url_state.dart';
import 'package:tmail_ui_user/features/login/domain/state/get_oidc_configuration_state.dart';
import 'package:tmail_ui_user/features/login/domain/state/get_token_oidc_state.dart';
import 'package:tmail_ui_user/features/login/presentation/login_form_type.dart';
import 'package:tmail_ui_user/main/localizations/app_localizations.dart';
import 'package:tmail_ui_user/main/utils/message_toast_utils.dart';
Expand Down Expand Up @@ -46,6 +48,8 @@ class LoginMessageWidget extends StatelessWidget {
return AppLocalizations.of(context).canNotVerifySSOConfiguration;
} else if (failure is DNSLookupToGetJmapUrlFailure) {
return AppLocalizations.of(context).dnsLookupLoginMessage;
} else if (failure is GetTokenOIDCFailure && failure.exception is NoSuitableBrowserForOIDCException) {
return AppLocalizations.of(context).noSuitableBrowserForOIDC;
} else if (failure is FeatureFailure) {
final errorMessage = MessageToastUtils.getMessageByException(context, failure.exception);
return errorMessage ?? AppLocalizations.of(context).unknownError;
Expand Down
8 changes: 7 additions & 1 deletion lib/l10n/intl_messages.arb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"@@last_modified": "2024-03-25T10:51:29.520399",
"@@last_modified": "2024-04-19T16:31:35.757887",
"initializing_data": "Initializing data...",
"@initializing_data": {
"type": "text",
Expand Down Expand Up @@ -3689,5 +3689,11 @@
"type": "text",
"placeholders_order": [],
"placeholders": {}
},
"noSuitableBrowserForOIDC": "No suitable browser for OIDC, please check with your system administrator",
"@noSuitableBrowserForOIDC": {
"type": "text",
"placeholders_order": [],
"placeholders": {}
}
}
7 changes: 7 additions & 0 deletions lib/main/localizations/app_localizations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3844,4 +3844,11 @@ class AppLocalizations {
name: 'selectAllMessagesOfThisPage',
);
}

String get noSuitableBrowserForOIDC {
return Intl.message(
'No suitable browser for OIDC, please check with your system administrator',
name: 'noSuitableBrowserForOIDC'
);
}
}
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -602,10 +602,10 @@ packages:
dependency: "direct main"
description:
name: flutter_appauth
sha256: "19031ea2438a513762e64ec7b58ae1f00cad6fb87f818e405325cd183dfb9fec"
sha256: f2696d4cf437f627fa09bc4864afdd8c80273f2e293fde544b18202a627754b1
url: "https://pub.dev"
source: hosted
version: "6.0.2"
version: "6.0.6"
flutter_appauth_platform_interface:
dependency: "direct main"
description:
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ dependencies:

better_open_file: 3.6.4

flutter_appauth: 6.0.2
flutter_appauth: 6.0.6

percent_indicator: 4.2.2

Expand Down
Loading

0 comments on commit d2fdbfa

Please sign in to comment.