add alert dialog

This commit is contained in:
Adam Velebil 2024-09-03 15:25:03 +02:00
parent 30fd742164
commit 75f74895cb
No known key found for this signature in database
GPG Key ID: C9B1E4A3CBBD2E10
13 changed files with 115 additions and 24 deletions

View File

@ -173,6 +173,7 @@ class FidoManager(
fidoChannel.setMethodCallHandler(null)
fidoViewModel.clearSessionState()
fidoViewModel.updateCredentials(null)
connectionHelper.cancelPending()
coroutineScope.cancel()
}

View File

@ -208,6 +208,7 @@ class OathManager(
oathChannel.setMethodCallHandler(null)
oathViewModel.clearSession()
oathViewModel.updateCredentials(mapOf())
pendingAction?.invoke(Result.failure(Exception()))
coroutineScope.cancel()
}

View File

@ -0,0 +1,7 @@
<?xml version='1.0' encoding='utf-8'?>
<resources>
<string name="p_ndef_set_otp">Đã sao chép mã OTP từ YubiKey vào clipboard.</string>
<string name="p_ndef_set_password">Đã sao chép mật khẩu từ YubiKey vào clipboard.</string>
<string name="p_ndef_parse_failure">Không thể phân tích mã OTP từ YubiKey.</string>
<string name="p_ndef_set_clip_failure">Không thể truy cập clipboard khi cố gắng sao chép mã OTP từ YubiKey.</string>
</resources>

View File

@ -0,0 +1,44 @@
/*
* Copyright (C) 2024 Yubico.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import 'package:flutter/material.dart';
import '../app/state.dart';
import 'tap_request_dialog.dart';
void showAlertDialog(ref, String title, String message) =>
ref.read(withContextProvider)((context) async {
ref.read(androidDialogProvider).closeDialog();
final l10n = ref.read(l10nProvider);
Navigator.of(context).popUntil((route) {
return route.isFirst;
});
await showDialog(
routeSettings: const RouteSettings(name: 'android_alert_dialog'),
context: context,
builder: (dialogContext) {
return AlertDialog(
title: Text(title),
actions: [
TextButton(
onPressed: () async {
Navigator.of(dialogContext).pop();
},
child: Text(l10n.s_close))
],
content: Text(message));
});
});

View File

@ -35,6 +35,7 @@ import '../../exception/no_data_exception.dart';
import '../../exception/platform_exception_decoder.dart';
import '../../oath/models.dart';
import '../../oath/state.dart';
import '../android_alert_dialog.dart';
final _log = Logger('android.oath.state');
@ -136,27 +137,33 @@ class _AndroidOathStateNotifier extends OathStateNotifier {
}
}
// Converts Platform exception during Add Account operation
// Returns CancellationException for situations we don't want to show a Toast
Exception _decodeAddAccountException(PlatformException platformException) {
final decodedException = platformException.decode();
// Auth required, the app will show Unlock dialog
if (decodedException is ApduException && decodedException.sw == 0x6982) {
_log.error('Add account failed: Auth required');
return CancellationException();
Exception handlePlatformException(PlatformException platformException, ref) {
final decoded = platformException.decode();
final l10n = ref.read(l10nProvider);
switch (decoded) {
case ApduException apduException:
if (apduException.sw == 0x6985) {
showAlertDialog(ref, l10n.l_account_add_failure_title,
l10n.p_account_add_failure_6985);
return CancellationException();
}
if (apduException.sw == 0x6982) {
showAlertDialog(ref, l10n.l_account_add_failure_title,
l10n.p_account_add_failure_6982);
return CancellationException();
}
case PlatformException pe:
if (pe.code == 'JobCancellationException') {
showAlertDialog(ref, l10n.l_account_add_failure_title,
l10n.p_account_add_failure_application_not_available);
return CancellationException();
} else if (pe.code == 'IllegalArgumentException') {
showAlertDialog(ref, l10n.l_account_add_failure_title,
l10n.p_account_add_failure_exists);
return CancellationException();
}
}
// Thrown in native code when the account already exists on the YubiKey
// The entry dialog will show an error message and that is why we convert
// this to CancellationException to avoid showing a Toast
if (platformException.code == 'IllegalArgumentException') {
_log.error('Add account failed: Account already exists');
return CancellationException();
}
// original exception
return decodedException;
return decoded;
}
final addCredentialToAnyProvider =
@ -171,7 +178,7 @@ final addCredentialToAnyProvider =
var result = jsonDecode(resultString);
return OathCredential.fromJson(result['credential']);
} on PlatformException catch (pe) {
throw _decodeAddAccountException(pe);
throw handlePlatformException(pe, ref);
}
});
@ -286,7 +293,7 @@ class _AndroidCredentialListNotifier extends OathCredentialListNotifier {
var result = jsonDecode(resultString);
return OathCredential.fromJson(result['credential']);
} on PlatformException catch (pe) {
throw _decodeAddAccountException(pe);
throw handlePlatformException(pe, _ref);
}
}

View File

@ -129,7 +129,7 @@ class _DialogProvider {
final args = jsonDecode(call.arguments);
switch (call.method) {
case 'close':
_closeDialog();
closeDialog();
break;
case 'show':
await _showDialog(args['title'], args['description'], args['icon']);
@ -147,7 +147,7 @@ class _DialogProvider {
});
}
void _closeDialog() {
void closeDialog() {
_controller?.close();
_controller = null;
}

View File

@ -69,6 +69,7 @@ class MainPage extends ConsumerWidget {
'oath_add_account',
'oath_icon_pack_dialog',
'android_qr_scanner_view',
'android_alert_dialog'
].contains(route.settings.name);
});
});

View File

@ -427,6 +427,11 @@
"message": {}
}
},
"l_account_add_failure_title": null,
"p_account_add_failure_6985": null,
"p_account_add_failure_6982": null,
"p_account_add_failure_exists": null,
"p_account_add_failure_application_not_available": null,
"l_account_name_required": "Ihr Konto muss einen Namen haben",
"l_name_already_exists": "Für diesen Aussteller existiert dieser Name bereits",
"l_account_already_exists": "Dieses Konto existiert bereits auf dem YubiKey",

View File

@ -427,6 +427,11 @@
"message": {}
}
},
"l_account_add_failure_title": "Add account failed",
"p_account_add_failure_6985": "The account could not be added to YubiKey. Please, set password first.",
"p_account_add_failure_6982": "The account could not be added to YubiKey. Please, unlock the key first.",
"p_account_add_failure_exists": "Account already exists on the key.",
"p_account_add_failure_application_not_available": "This YubiKey does not support OATH accounts.",
"l_account_name_required": "Your account must have a name",
"l_name_already_exists": "This name already exists for the issuer",
"l_account_already_exists": "This account already exists on the YubiKey",

View File

@ -427,6 +427,11 @@
"message": {}
}
},
"l_account_add_failure_title": null,
"p_account_add_failure_6985": null,
"p_account_add_failure_6982": null,
"p_account_add_failure_exists": null,
"p_account_add_failure_application_not_available": null,
"l_account_name_required": "Votre compte doit avoir un nom",
"l_name_already_exists": "Ce nom existe déjà pour l'émetteur",
"l_account_already_exists": "Ce compte existe déjà sur la YubiKey",

View File

@ -427,6 +427,11 @@
"message": {}
}
},
"l_account_add_failure_title": null,
"p_account_add_failure_6985": null,
"p_account_add_failure_6982": null,
"p_account_add_failure_exists": null,
"p_account_add_failure_application_not_available": null,
"l_account_name_required": "アカウントには名前が必要です",
"l_name_already_exists": "この名前は発行者にすでに存在します",
"l_account_already_exists": "このアカウントはYubiKeyにすでに存在します",

View File

@ -427,6 +427,11 @@
"message": {}
}
},
"l_account_add_failure_title": null,
"p_account_add_failure_6985": null,
"p_account_add_failure_6982": null,
"p_account_add_failure_exists": null,
"p_account_add_failure_application_not_available": null,
"l_account_name_required": "Twoje konto musi mieć nazwę",
"l_name_already_exists": "Ta nazwa już istnieje dla tego wydawcy",
"l_account_already_exists": "To konto już istnieje w YubiKey",

View File

@ -427,6 +427,11 @@
"message": {}
}
},
"l_account_add_failure_title": null,
"p_account_add_failure_6985": null,
"p_account_add_failure_6982": null,
"p_account_add_failure_exists": null,
"p_account_add_failure_application_not_available": null,
"l_account_name_required": "Tài khoản của bạn phải có tên",
"l_name_already_exists": "Tên này đã tồn tại cho nhà phát hành",
"l_account_already_exists": "Tài khoản này đã tồn tại trên YubiKey",