mirror of
https://github.com/Yubico/yubioath-flutter.git
synced 2025-01-08 20:08:45 +03:00
avoid unnecessary Toasts
This commit is contained in:
parent
b15bd954c4
commit
b04639f113
@ -306,7 +306,7 @@ class OathManager(
|
||||
return useOathSessionNfc(OathActionDescription.AddAccount) { session ->
|
||||
// We need to check for duplicates here since we haven't yet read the credentials
|
||||
if (session.credentials.any { it.id.contentEquals(credentialData.id) }) {
|
||||
throw Exception("A credential with this ID already exists!")
|
||||
throw IllegalArgumentException()
|
||||
}
|
||||
|
||||
val credential = session.putCredential(credentialData, requireTouch)
|
||||
|
@ -29,6 +29,7 @@ import '../../app/models.dart';
|
||||
import '../../app/state.dart';
|
||||
import '../../app/views/user_interaction.dart';
|
||||
import '../../core/models.dart';
|
||||
import '../../exception/apdu_exception.dart';
|
||||
import '../../exception/cancellation_exception.dart';
|
||||
import '../../exception/no_data_exception.dart';
|
||||
import '../../exception/platform_exception_decoder.dart';
|
||||
@ -130,6 +131,29 @@ 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();
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
final addCredentialToAnyProvider =
|
||||
Provider((ref) => (Uri credentialUri, {bool requireTouch = false}) async {
|
||||
try {
|
||||
@ -142,8 +166,7 @@ final addCredentialToAnyProvider =
|
||||
var result = jsonDecode(resultString);
|
||||
return OathCredential.fromJson(result['credential']);
|
||||
} on PlatformException catch (pe) {
|
||||
_log.error('Failed to add account.', pe);
|
||||
throw pe.decode();
|
||||
throw _decodeAddAccountException(pe);
|
||||
}
|
||||
});
|
||||
|
||||
@ -267,8 +290,7 @@ class _AndroidCredentialListNotifier extends OathCredentialListNotifier {
|
||||
var result = jsonDecode(resultString);
|
||||
return OathCredential.fromJson(result['credential']);
|
||||
} on PlatformException catch (pe) {
|
||||
_log.error('Failed to add account.', pe);
|
||||
throw pe.decode();
|
||||
throw _decodeAddAccountException(pe);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user