2022-10-04 13:12:54 +03:00
|
|
|
/*
|
2023-05-31 11:24:41 +03:00
|
|
|
* Copyright (C) 2022-2023 Yubico.
|
2022-10-04 13:12:54 +03:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2022-09-13 13:55:17 +03:00
|
|
|
import 'dart:async';
|
2022-03-22 17:16:52 +03:00
|
|
|
import 'dart:convert';
|
2022-04-14 10:08:33 +03:00
|
|
|
import 'dart:math';
|
2022-01-28 19:05:05 +03:00
|
|
|
|
2021-11-22 11:49:52 +03:00
|
|
|
import 'package:flutter/material.dart';
|
2022-09-05 16:10:44 +03:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2021-11-22 11:49:52 +03:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2022-04-05 14:02:11 +03:00
|
|
|
import 'package:logging/logging.dart';
|
2021-11-22 11:49:52 +03:00
|
|
|
|
2022-09-14 10:48:37 +03:00
|
|
|
import '../../android/oath/state.dart';
|
2022-07-05 15:53:21 +03:00
|
|
|
import '../../app/logging.dart';
|
2022-03-25 17:43:32 +03:00
|
|
|
import '../../app/message.dart';
|
2021-11-22 11:49:52 +03:00
|
|
|
import '../../app/models.dart';
|
2022-04-14 10:08:33 +03:00
|
|
|
import '../../app/state.dart';
|
2022-09-13 13:55:17 +03:00
|
|
|
import '../../app/views/user_interaction.dart';
|
2023-12-13 21:35:17 +03:00
|
|
|
import '../../core/models.dart';
|
2022-09-15 12:47:39 +03:00
|
|
|
import '../../core/state.dart';
|
2022-06-13 17:45:26 +03:00
|
|
|
import '../../desktop/models.dart';
|
2023-11-14 18:44:14 +03:00
|
|
|
import '../../exception/apdu_exception.dart';
|
|
|
|
import '../../exception/cancellation_exception.dart';
|
2022-09-13 13:55:17 +03:00
|
|
|
import '../../management/models.dart';
|
2023-11-27 13:41:05 +03:00
|
|
|
import '../../widgets/app_text_field.dart';
|
2022-09-01 11:14:59 +03:00
|
|
|
import '../../widgets/choice_filter_chip.dart';
|
2022-03-23 12:46:35 +03:00
|
|
|
import '../../widgets/file_drop_target.dart';
|
2023-05-31 11:24:41 +03:00
|
|
|
import '../../widgets/focus_utils.dart';
|
2022-03-31 12:50:40 +03:00
|
|
|
import '../../widgets/responsive_dialog.dart';
|
2022-07-06 16:22:15 +03:00
|
|
|
import '../../widgets/utf8_utils.dart';
|
2022-09-12 13:58:17 +03:00
|
|
|
import '../keys.dart' as keys;
|
2022-03-15 20:04:26 +03:00
|
|
|
import '../models.dart';
|
2021-12-02 16:20:05 +03:00
|
|
|
import '../state.dart';
|
2022-09-14 14:19:39 +03:00
|
|
|
import 'unlock_form.dart';
|
2022-01-31 13:02:34 +03:00
|
|
|
import 'utils.dart';
|
2021-11-22 11:49:52 +03:00
|
|
|
|
2022-04-05 14:02:11 +03:00
|
|
|
final _log = Logger('oath.view.add_account_page');
|
|
|
|
|
2022-03-15 20:04:26 +03:00
|
|
|
class OathAddAccountPage extends ConsumerStatefulWidget {
|
2022-09-13 13:55:17 +03:00
|
|
|
final DevicePath? devicePath;
|
|
|
|
final OathState? state;
|
2022-09-05 16:02:36 +03:00
|
|
|
final List<OathCredential>? credentials;
|
2022-09-15 12:47:39 +03:00
|
|
|
final CredentialData? credentialData;
|
2023-11-14 18:44:14 +03:00
|
|
|
|
2022-09-05 16:02:36 +03:00
|
|
|
const OathAddAccountPage(
|
|
|
|
this.devicePath,
|
|
|
|
this.state, {
|
|
|
|
super.key,
|
|
|
|
required this.credentials,
|
2022-09-15 12:47:39 +03:00
|
|
|
this.credentialData,
|
2022-09-05 16:02:36 +03:00
|
|
|
});
|
2022-01-28 19:05:05 +03:00
|
|
|
|
2021-12-02 16:20:05 +03:00
|
|
|
@override
|
2022-03-15 20:04:26 +03:00
|
|
|
ConsumerState<ConsumerStatefulWidget> createState() =>
|
|
|
|
_OathAddAccountPageState();
|
2021-12-02 16:20:05 +03:00
|
|
|
}
|
|
|
|
|
2022-03-15 20:04:26 +03:00
|
|
|
class _OathAddAccountPageState extends ConsumerState<OathAddAccountPage> {
|
2022-02-11 16:56:35 +03:00
|
|
|
final _issuerController = TextEditingController();
|
|
|
|
final _accountController = TextEditingController();
|
|
|
|
final _secretController = TextEditingController();
|
|
|
|
final _periodController = TextEditingController(text: '$defaultPeriod');
|
2022-09-13 13:55:17 +03:00
|
|
|
UserInteractionController? _promptController;
|
|
|
|
Uri? _otpauthUri;
|
2021-12-02 16:20:05 +03:00
|
|
|
bool _touch = false;
|
2022-01-28 19:05:05 +03:00
|
|
|
OathType _oathType = defaultOathType;
|
|
|
|
HashAlgorithm _hashAlgorithm = defaultHashAlgorithm;
|
|
|
|
int _digits = defaultDigits;
|
2023-08-11 13:00:14 +03:00
|
|
|
int _counter = defaultCounter;
|
2023-11-24 16:37:37 +03:00
|
|
|
bool _validateSecret = false;
|
2023-08-17 17:51:57 +03:00
|
|
|
bool _dataLoaded = false;
|
2022-04-04 15:38:12 +03:00
|
|
|
bool _isObscure = true;
|
2022-04-05 13:28:31 +03:00
|
|
|
List<int> _periodValues = [20, 30, 45, 60];
|
|
|
|
List<int> _digitsValues = [6, 8];
|
2022-09-14 17:40:49 +03:00
|
|
|
List<OathCredential>? _credentials;
|
2022-02-11 16:56:35 +03:00
|
|
|
|
2022-09-06 09:58:26 +03:00
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_issuerController.dispose();
|
|
|
|
_accountController.dispose();
|
|
|
|
_secretController.dispose();
|
|
|
|
_periodController.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2022-09-15 12:47:39 +03:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
final cred = widget.credentialData;
|
|
|
|
if (cred != null) {
|
|
|
|
_loadCredentialData(cred);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-05 13:28:31 +03:00
|
|
|
_loadCredentialData(CredentialData data) {
|
|
|
|
setState(() {
|
2022-09-05 16:02:36 +03:00
|
|
|
_issuerController.text = data.issuer?.trim() ?? '';
|
|
|
|
_accountController.text = data.name.trim();
|
2022-04-05 13:28:31 +03:00
|
|
|
_secretController.text = data.secret;
|
|
|
|
_oathType = data.oathType;
|
|
|
|
_hashAlgorithm = data.hashAlgorithm;
|
|
|
|
_periodValues = [data.period];
|
|
|
|
_periodController.text = '${data.period}';
|
|
|
|
_digitsValues = [data.digits];
|
|
|
|
_digits = data.digits;
|
2023-08-11 13:00:14 +03:00
|
|
|
_counter = data.counter;
|
2022-04-05 13:28:31 +03:00
|
|
|
_isObscure = true;
|
2023-08-17 17:51:57 +03:00
|
|
|
_dataLoaded = true;
|
2022-04-05 13:28:31 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-09-14 10:48:37 +03:00
|
|
|
Future<void> _doAddCredential(
|
|
|
|
{DevicePath? devicePath, required Uri credUri}) async {
|
2023-02-28 13:34:29 +03:00
|
|
|
final l10n = AppLocalizations.of(context)!;
|
2022-09-13 13:55:17 +03:00
|
|
|
try {
|
2023-05-31 11:24:41 +03:00
|
|
|
FocusUtils.unfocus(context);
|
|
|
|
|
2022-09-14 10:48:37 +03:00
|
|
|
if (devicePath == null) {
|
2023-03-03 18:29:24 +03:00
|
|
|
assert(isAndroid, 'devicePath is only optional for Android');
|
2022-09-14 10:48:37 +03:00
|
|
|
await ref
|
|
|
|
.read(addCredentialToAnyProvider)
|
|
|
|
.call(credUri, requireTouch: _touch);
|
|
|
|
} else {
|
|
|
|
await ref
|
|
|
|
.read(credentialListProvider(devicePath).notifier)
|
|
|
|
.addAccount(credUri, requireTouch: _touch);
|
|
|
|
}
|
|
|
|
if (!mounted) return;
|
|
|
|
Navigator.of(context).pop();
|
2023-03-02 14:45:55 +03:00
|
|
|
showMessage(context, l10n.s_account_added);
|
2022-09-13 13:55:17 +03:00
|
|
|
} on CancellationException catch (_) {
|
|
|
|
// ignored
|
|
|
|
} catch (e) {
|
|
|
|
_log.error('Failed to add account', e);
|
|
|
|
final String errorMessage;
|
|
|
|
// TODO: Make this cleaner than importing desktop specific RpcError.
|
|
|
|
if (e is RpcError) {
|
|
|
|
errorMessage = e.message;
|
2023-01-09 19:22:34 +03:00
|
|
|
} else if (e is ApduException) {
|
|
|
|
errorMessage = e.message;
|
2022-09-13 13:55:17 +03:00
|
|
|
} else {
|
|
|
|
errorMessage = e.toString();
|
|
|
|
}
|
|
|
|
showMessage(
|
|
|
|
context,
|
2023-03-01 12:30:32 +03:00
|
|
|
l10n.l_account_add_failed(errorMessage),
|
2022-09-13 13:55:17 +03:00
|
|
|
duration: const Duration(seconds: 4),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-22 11:49:52 +03:00
|
|
|
@override
|
2021-12-02 16:20:05 +03:00
|
|
|
Widget build(BuildContext context) {
|
2023-02-28 13:34:29 +03:00
|
|
|
final l10n = AppLocalizations.of(context)!;
|
2022-09-13 13:55:17 +03:00
|
|
|
final deviceNode = ref.watch(currentDeviceProvider);
|
|
|
|
if (widget.devicePath != null && widget.devicePath != deviceNode?.path) {
|
|
|
|
// If the dialog was started for a specific device and it was
|
|
|
|
// changed/removed, close the dialog.
|
|
|
|
Navigator.of(context).popUntil((route) => route.isFirst);
|
|
|
|
}
|
|
|
|
|
|
|
|
final OathState? oathState;
|
|
|
|
if (widget.state == null && deviceNode != null) {
|
|
|
|
oathState = ref
|
|
|
|
.watch(oathStateProvider(deviceNode.path))
|
|
|
|
.maybeWhen(data: (data) => data, orElse: () => null);
|
2022-09-14 17:40:49 +03:00
|
|
|
_credentials = ref
|
|
|
|
.watch(credentialListProvider(deviceNode.path))
|
|
|
|
?.map((e) => e.credential)
|
|
|
|
.toList();
|
2022-09-13 13:55:17 +03:00
|
|
|
} else {
|
|
|
|
oathState = widget.state;
|
2022-09-14 17:40:49 +03:00
|
|
|
_credentials = widget.credentials;
|
2022-09-13 13:55:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
final otpauthUri = _otpauthUri;
|
2023-02-28 17:02:12 +03:00
|
|
|
_promptController?.updateContent(title: l10n.l_insert_yk);
|
2022-09-13 13:55:17 +03:00
|
|
|
if (otpauthUri != null && deviceNode != null) {
|
|
|
|
final deviceData = ref.watch(currentDeviceDataProvider);
|
|
|
|
deviceData.when(data: (data) {
|
|
|
|
if (Capability.oath.value ^
|
|
|
|
(data.info.config.enabledCapabilities[deviceNode.transport] ??
|
|
|
|
0) !=
|
|
|
|
0) {
|
|
|
|
if (oathState == null) {
|
2023-03-02 14:45:55 +03:00
|
|
|
_promptController?.updateContent(title: l10n.s_please_wait);
|
2022-09-13 13:55:17 +03:00
|
|
|
} else if (oathState.locked) {
|
2022-09-14 16:54:51 +03:00
|
|
|
_promptController?.close();
|
2022-09-13 13:55:17 +03:00
|
|
|
} else {
|
|
|
|
_otpauthUri = null;
|
|
|
|
_promptController?.close();
|
2022-09-14 10:48:37 +03:00
|
|
|
Timer.run(() => _doAddCredential(
|
2022-09-14 16:54:51 +03:00
|
|
|
devicePath: deviceNode.path,
|
|
|
|
credUri: otpauthUri,
|
|
|
|
));
|
2022-09-13 13:55:17 +03:00
|
|
|
}
|
|
|
|
} else {
|
2023-03-02 14:45:55 +03:00
|
|
|
_promptController?.updateContent(title: l10n.s_unsupported_yk);
|
2022-09-13 13:55:17 +03:00
|
|
|
}
|
|
|
|
}, error: (error, _) {
|
2023-03-02 14:45:55 +03:00
|
|
|
_promptController?.updateContent(title: l10n.s_unsupported_yk);
|
2022-09-13 13:55:17 +03:00
|
|
|
}, loading: () {
|
2023-03-02 14:45:55 +03:00
|
|
|
_promptController?.updateContent(title: l10n.s_please_wait);
|
2022-09-13 13:55:17 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-11 16:56:35 +03:00
|
|
|
final period = int.tryParse(_periodController.text) ?? -1;
|
2022-11-22 12:24:11 +03:00
|
|
|
final issuerText = _issuerController.text.trim();
|
|
|
|
final nameText = _accountController.text.trim();
|
2023-05-22 12:20:30 +03:00
|
|
|
final (issuerRemaining, nameRemaining) = getRemainingKeySpace(
|
2022-01-31 13:02:34 +03:00
|
|
|
oathType: _oathType,
|
2022-02-11 16:56:35 +03:00
|
|
|
period: period,
|
2022-11-22 12:24:11 +03:00
|
|
|
issuer: issuerText,
|
|
|
|
name: nameText,
|
2022-01-31 13:02:34 +03:00
|
|
|
);
|
2021-11-22 11:49:52 +03:00
|
|
|
|
2022-11-22 12:24:11 +03:00
|
|
|
final issuerMaxLength = max(issuerRemaining, 1);
|
|
|
|
final nameMaxLength = max(nameRemaining, 1);
|
|
|
|
|
2022-02-11 16:56:35 +03:00
|
|
|
final secret = _secretController.text.replaceAll(' ', '');
|
|
|
|
final secretLengthValid = secret.length * 5 % 8 < 5;
|
2023-11-24 16:37:37 +03:00
|
|
|
final secretFormatValid = Format.base32.isValid(secret);
|
2022-09-05 16:02:36 +03:00
|
|
|
|
|
|
|
// is this credentials name/issuer pair different from all other?
|
2022-09-14 17:40:49 +03:00
|
|
|
final isUnique = _credentials
|
2022-09-05 16:02:36 +03:00
|
|
|
?.where((element) =>
|
2022-11-22 12:24:11 +03:00
|
|
|
element.name == nameText &&
|
|
|
|
(element.issuer ?? '') == issuerText)
|
2022-09-05 16:02:36 +03:00
|
|
|
.isEmpty ??
|
2022-09-06 14:55:59 +03:00
|
|
|
true;
|
2022-10-21 11:31:40 +03:00
|
|
|
final issuerNoColon = !_issuerController.text.contains(':');
|
2022-09-05 16:02:36 +03:00
|
|
|
|
2022-09-14 14:19:39 +03:00
|
|
|
final isLocked = oathState?.locked ?? false;
|
|
|
|
|
|
|
|
final isValid = !isLocked &&
|
2022-11-22 12:24:11 +03:00
|
|
|
nameText.isNotEmpty &&
|
2022-04-01 16:05:26 +03:00
|
|
|
secret.isNotEmpty &&
|
2022-09-05 16:02:36 +03:00
|
|
|
isUnique &&
|
2022-10-21 11:31:40 +03:00
|
|
|
issuerNoColon &&
|
2022-04-01 16:05:26 +03:00
|
|
|
issuerRemaining >= -1 &&
|
|
|
|
nameRemaining >= 0 &&
|
|
|
|
period > 0;
|
2022-02-11 16:56:35 +03:00
|
|
|
|
2022-09-14 14:19:39 +03:00
|
|
|
final hashAlgorithms = HashAlgorithm.values
|
|
|
|
.where((alg) =>
|
|
|
|
alg != HashAlgorithm.sha512 ||
|
|
|
|
(oathState?.version.isAtLeast(4, 3, 1) ?? true))
|
|
|
|
.toList();
|
2022-09-13 13:55:17 +03:00
|
|
|
if (!hashAlgorithms.contains(_hashAlgorithm)) {
|
|
|
|
_hashAlgorithm = HashAlgorithm.sha1;
|
|
|
|
}
|
|
|
|
|
2022-09-14 14:19:39 +03:00
|
|
|
if (!(oathState?.version.isAtLeast(4, 2) ?? true)) {
|
|
|
|
// Touch not supported
|
|
|
|
_touch = false;
|
|
|
|
}
|
|
|
|
|
2022-06-09 13:30:19 +03:00
|
|
|
void submit() async {
|
2023-11-24 16:37:37 +03:00
|
|
|
if (secretLengthValid && secretFormatValid) {
|
2022-06-09 13:30:19 +03:00
|
|
|
final cred = CredentialData(
|
2022-11-22 12:24:11 +03:00
|
|
|
issuer: issuerText.isEmpty ? null : issuerText,
|
|
|
|
name: nameText,
|
2022-06-09 13:30:19 +03:00
|
|
|
secret: secret,
|
|
|
|
oathType: _oathType,
|
|
|
|
hashAlgorithm: _hashAlgorithm,
|
|
|
|
digits: _digits,
|
|
|
|
period: period,
|
2023-08-11 13:00:14 +03:00
|
|
|
counter: _counter,
|
2022-06-09 13:30:19 +03:00
|
|
|
);
|
|
|
|
|
2022-09-13 13:55:17 +03:00
|
|
|
final devicePath = deviceNode?.path;
|
|
|
|
if (devicePath != null) {
|
2022-09-14 10:48:37 +03:00
|
|
|
await _doAddCredential(devicePath: devicePath, credUri: cred.toUri());
|
2023-03-03 18:29:24 +03:00
|
|
|
} else if (isAndroid) {
|
2022-09-14 10:48:37 +03:00
|
|
|
// Send the credential to Android to be added to the next YubiKey
|
|
|
|
await _doAddCredential(devicePath: null, credUri: cred.toUri());
|
2022-09-13 13:55:17 +03:00
|
|
|
} else {
|
2022-09-14 10:48:37 +03:00
|
|
|
// Desktop. No YubiKey, prompt and store the cred.
|
2022-09-13 13:55:17 +03:00
|
|
|
_otpauthUri = cred.toUri();
|
|
|
|
_promptController = promptUserInteraction(
|
2022-06-13 17:45:26 +03:00
|
|
|
context,
|
2023-02-28 17:02:12 +03:00
|
|
|
title: l10n.l_insert_yk,
|
2023-03-02 14:45:55 +03:00
|
|
|
description: l10n.s_add_account,
|
2022-09-13 13:55:17 +03:00
|
|
|
icon: const Icon(Icons.usb),
|
|
|
|
onCancel: () {
|
|
|
|
_otpauthUri = null;
|
|
|
|
},
|
2022-06-13 17:45:26 +03:00
|
|
|
);
|
2022-06-09 13:30:19 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
setState(() {
|
2023-11-24 16:37:37 +03:00
|
|
|
_validateSecret = true;
|
2022-06-09 13:30:19 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 20:04:26 +03:00
|
|
|
return ResponsiveDialog(
|
2023-03-02 14:45:55 +03:00
|
|
|
title: Text(l10n.s_add_account),
|
2022-05-12 09:34:51 +03:00
|
|
|
actions: [
|
|
|
|
TextButton(
|
2022-06-09 13:30:19 +03:00
|
|
|
onPressed: isValid ? submit : null,
|
2023-03-02 14:45:55 +03:00
|
|
|
child: Text(l10n.s_save, key: keys.saveButton),
|
2022-05-12 09:34:51 +03:00
|
|
|
),
|
|
|
|
],
|
2022-03-23 12:46:35 +03:00
|
|
|
child: FileDropTarget(
|
|
|
|
onFileDropped: (fileData) async {
|
2023-08-17 17:51:57 +03:00
|
|
|
final qrScanner = ref.read(qrScannerProvider);
|
2022-03-23 12:46:35 +03:00
|
|
|
if (qrScanner != null) {
|
|
|
|
final b64Image = base64Encode(fileData);
|
|
|
|
final otpauth = await qrScanner.scanQr(b64Image);
|
2022-06-13 17:45:26 +03:00
|
|
|
if (otpauth == null) {
|
|
|
|
if (!mounted) return;
|
2023-02-28 17:02:12 +03:00
|
|
|
showMessage(context, l10n.l_qr_not_found);
|
2022-06-13 17:45:26 +03:00
|
|
|
} else {
|
2023-08-17 17:51:57 +03:00
|
|
|
try {
|
|
|
|
final data = CredentialData.fromOtpauth(Uri.parse(otpauth));
|
|
|
|
_loadCredentialData(data);
|
|
|
|
} catch (e) {
|
|
|
|
final String errorMessage;
|
|
|
|
// TODO: Make this cleaner than importing desktop specific RpcError.
|
|
|
|
if (e is RpcError) {
|
|
|
|
errorMessage = e.message;
|
|
|
|
} else {
|
|
|
|
errorMessage = e.toString();
|
|
|
|
}
|
|
|
|
if (!mounted) return;
|
|
|
|
showMessage(context, errorMessage);
|
|
|
|
}
|
2022-06-13 17:45:26 +03:00
|
|
|
}
|
2022-03-23 12:46:35 +03:00
|
|
|
}
|
|
|
|
},
|
2022-09-14 16:54:51 +03:00
|
|
|
child: isLocked
|
|
|
|
? Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(vertical: 18),
|
|
|
|
child:
|
|
|
|
UnlockForm(deviceNode!.path, keystore: oathState!.keystore),
|
|
|
|
)
|
|
|
|
: Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 18.0),
|
|
|
|
child: Column(
|
2022-09-14 14:19:39 +03:00
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
2023-11-10 17:24:53 +03:00
|
|
|
AppTextField(
|
2022-09-14 14:19:39 +03:00
|
|
|
key: keys.issuerField,
|
|
|
|
controller: _issuerController,
|
2022-09-15 12:47:39 +03:00
|
|
|
autofocus: widget.credentialData == null,
|
2022-09-14 14:19:39 +03:00
|
|
|
enabled: issuerRemaining > 0,
|
2022-11-22 12:24:11 +03:00
|
|
|
maxLength: issuerMaxLength,
|
2022-10-21 11:31:40 +03:00
|
|
|
inputFormatters: [
|
|
|
|
limitBytesLength(issuerRemaining),
|
|
|
|
],
|
2022-11-22 12:24:11 +03:00
|
|
|
buildCounter: buildByteCounterFor(issuerText),
|
2023-12-14 18:38:10 +03:00
|
|
|
decoration: AppInputDecoration(
|
2022-09-14 14:19:39 +03:00
|
|
|
border: const OutlineInputBorder(),
|
2023-03-02 14:45:55 +03:00
|
|
|
labelText: l10n.s_issuer_optional,
|
2023-11-24 16:37:37 +03:00
|
|
|
helperText:
|
|
|
|
'', // Prevents dialog resizing when disabled
|
2022-11-22 12:24:11 +03:00
|
|
|
errorText: (byteLength(issuerText) > issuerMaxLength)
|
|
|
|
? '' // needs empty string to render as error
|
|
|
|
: issuerNoColon
|
|
|
|
? null
|
2023-02-28 17:02:12 +03:00
|
|
|
: l10n.l_invalid_character_issuer,
|
2023-11-24 16:37:37 +03:00
|
|
|
prefixIcon: const Icon(Icons.business_outlined),
|
|
|
|
suffixIcon: (!issuerNoColon ||
|
|
|
|
byteLength(issuerText) > issuerMaxLength)
|
|
|
|
? const Icon(Icons.error)
|
|
|
|
: null,
|
2022-09-07 11:25:22 +03:00
|
|
|
),
|
2022-09-14 14:19:39 +03:00
|
|
|
textInputAction: TextInputAction.next,
|
|
|
|
onChanged: (value) {
|
2022-09-07 11:25:22 +03:00
|
|
|
setState(() {
|
2022-09-14 14:19:39 +03:00
|
|
|
// Update maxlengths
|
2022-09-07 11:25:22 +03:00
|
|
|
});
|
|
|
|
},
|
2022-09-14 14:19:39 +03:00
|
|
|
onSubmitted: (_) {
|
|
|
|
if (isValid) submit();
|
|
|
|
},
|
2022-04-04 13:09:34 +03:00
|
|
|
),
|
2023-11-10 17:24:53 +03:00
|
|
|
AppTextField(
|
2022-09-14 14:19:39 +03:00
|
|
|
key: keys.nameField,
|
|
|
|
controller: _accountController,
|
2022-11-22 12:24:11 +03:00
|
|
|
maxLength: nameMaxLength,
|
|
|
|
buildCounter: buildByteCounterFor(nameText),
|
2022-09-14 14:19:39 +03:00
|
|
|
inputFormatters: [limitBytesLength(nameRemaining)],
|
2023-12-14 18:38:10 +03:00
|
|
|
decoration: AppInputDecoration(
|
2022-09-14 14:19:39 +03:00
|
|
|
border: const OutlineInputBorder(),
|
2023-03-02 14:45:55 +03:00
|
|
|
labelText: l10n.s_account_name,
|
2023-11-14 18:44:14 +03:00
|
|
|
helperText: '',
|
|
|
|
// Prevents dialog resizing when disabled
|
2022-11-22 12:24:11 +03:00
|
|
|
errorText: (byteLength(nameText) > nameMaxLength)
|
|
|
|
? '' // needs empty string to render as error
|
|
|
|
: isUnique
|
|
|
|
? null
|
2023-03-02 11:34:01 +03:00
|
|
|
: l10n.l_name_already_exists,
|
2023-11-24 16:37:37 +03:00
|
|
|
prefixIcon: const Icon(Icons.person_outline),
|
|
|
|
suffixIcon:
|
|
|
|
(!isUnique || byteLength(nameText) > nameMaxLength)
|
|
|
|
? const Icon(Icons.error)
|
|
|
|
: null,
|
2022-09-14 14:19:39 +03:00
|
|
|
),
|
|
|
|
textInputAction: TextInputAction.next,
|
|
|
|
onChanged: (value) {
|
|
|
|
setState(() {
|
|
|
|
// Update maxlengths
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onSubmitted: (_) {
|
|
|
|
if (isValid) submit();
|
|
|
|
},
|
|
|
|
),
|
2023-11-10 17:24:53 +03:00
|
|
|
AppTextField(
|
2022-09-14 14:19:39 +03:00
|
|
|
key: keys.secretField,
|
|
|
|
controller: _secretController,
|
|
|
|
obscureText: _isObscure,
|
2023-03-14 19:12:20 +03:00
|
|
|
// avoid using autofill hints on Android otherwise Autofill service
|
|
|
|
// would hint to use saved passwords for this field
|
2023-05-22 12:20:30 +03:00
|
|
|
autofillHints:
|
|
|
|
isAndroid ? [] : const [AutofillHints.password],
|
2023-12-14 18:38:10 +03:00
|
|
|
decoration: AppInputDecoration(
|
|
|
|
border: const OutlineInputBorder(),
|
|
|
|
labelText: l10n.s_secret_key,
|
|
|
|
errorText: _validateSecret && !secretLengthValid
|
|
|
|
? l10n.s_invalid_length
|
|
|
|
: _validateSecret && !secretFormatValid
|
|
|
|
? l10n.l_invalid_format_allowed_chars(
|
|
|
|
Format.base32.allowedCharacters)
|
|
|
|
: null,
|
|
|
|
prefixIcon: const Icon(Icons.key_outlined),
|
|
|
|
suffixIcon: IconButton(
|
|
|
|
icon: Icon(
|
|
|
|
_isObscure
|
|
|
|
? Icons.visibility
|
|
|
|
: Icons.visibility_off,
|
|
|
|
color: !_validateSecret
|
|
|
|
? IconTheme.of(context).color
|
|
|
|
: null),
|
|
|
|
onPressed: () {
|
|
|
|
setState(() {
|
|
|
|
_isObscure = !_isObscure;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
tooltip: _isObscure
|
|
|
|
? l10n.s_show_secret_key
|
|
|
|
: l10n.s_hide_secret_key,
|
|
|
|
)),
|
2023-08-17 17:51:57 +03:00
|
|
|
readOnly: _dataLoaded,
|
2022-09-14 14:19:39 +03:00
|
|
|
textInputAction: TextInputAction.done,
|
|
|
|
onChanged: (value) {
|
2022-09-07 11:25:22 +03:00
|
|
|
setState(() {
|
2023-11-24 16:37:37 +03:00
|
|
|
_validateSecret = false;
|
2022-09-07 11:25:22 +03:00
|
|
|
});
|
|
|
|
},
|
2022-09-14 14:19:39 +03:00
|
|
|
onSubmitted: (_) {
|
|
|
|
if (isValid) submit();
|
|
|
|
},
|
2022-09-07 11:25:22 +03:00
|
|
|
),
|
2023-08-18 11:44:08 +03:00
|
|
|
const SizedBox(height: 8),
|
2022-09-14 14:19:39 +03:00
|
|
|
Wrap(
|
|
|
|
crossAxisAlignment: WrapCrossAlignment.center,
|
|
|
|
spacing: 4.0,
|
|
|
|
runSpacing: 8.0,
|
|
|
|
children: [
|
|
|
|
if (oathState?.version.isAtLeast(4, 2) ?? true)
|
|
|
|
FilterChip(
|
2023-11-14 18:44:14 +03:00
|
|
|
key: keys.requireTouchFilterChip,
|
2023-03-02 14:45:55 +03:00
|
|
|
label: Text(l10n.s_require_touch),
|
2022-09-14 14:19:39 +03:00
|
|
|
selected: _touch,
|
|
|
|
onSelected: (value) {
|
2022-09-07 11:25:22 +03:00
|
|
|
setState(() {
|
2022-09-14 14:19:39 +03:00
|
|
|
_touch = value;
|
2022-09-07 11:25:22 +03:00
|
|
|
});
|
2022-09-14 14:19:39 +03:00
|
|
|
},
|
|
|
|
),
|
|
|
|
ChoiceFilterChip<OathType>(
|
2023-11-14 18:44:14 +03:00
|
|
|
key: keys.oathTypeFilterChip,
|
2022-09-14 14:19:39 +03:00
|
|
|
items: OathType.values,
|
|
|
|
value: _oathType,
|
|
|
|
selected: _oathType != defaultOathType,
|
2023-11-14 18:44:14 +03:00
|
|
|
itemBuilder: (value) => Text(
|
|
|
|
value.getDisplayName(l10n),
|
|
|
|
key: value == OathType.totp
|
|
|
|
? keys.oathTypeTotpFilterValue
|
|
|
|
: keys.oathTypeHotpFilterValue),
|
2023-08-17 17:51:57 +03:00
|
|
|
onChanged: !_dataLoaded
|
2022-09-14 14:19:39 +03:00
|
|
|
? (value) {
|
|
|
|
setState(() {
|
|
|
|
_oathType = value;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
: null,
|
|
|
|
),
|
|
|
|
ChoiceFilterChip<HashAlgorithm>(
|
2023-11-14 18:44:14 +03:00
|
|
|
key: keys.hashAlgorithmFilterChip,
|
2022-09-14 14:19:39 +03:00
|
|
|
items: hashAlgorithms,
|
|
|
|
value: _hashAlgorithm,
|
|
|
|
selected: _hashAlgorithm != defaultHashAlgorithm,
|
2023-11-14 18:44:14 +03:00
|
|
|
itemBuilder: (value) => Text(value.displayName,
|
|
|
|
key: value == HashAlgorithm.sha1
|
|
|
|
? keys.hashAlgorithmSha1FilterValue
|
|
|
|
: value == HashAlgorithm.sha256
|
|
|
|
? keys.hashAlgorithmSha256FilterValue
|
|
|
|
: keys.hashAlgorithmSha512FilterValue),
|
2023-08-17 17:51:57 +03:00
|
|
|
onChanged: !_dataLoaded
|
2022-09-14 14:19:39 +03:00
|
|
|
? (value) {
|
|
|
|
setState(() {
|
|
|
|
_hashAlgorithm = value;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
: null,
|
|
|
|
),
|
|
|
|
if (_oathType == OathType.totp)
|
|
|
|
ChoiceFilterChip<int>(
|
2023-11-14 18:44:14 +03:00
|
|
|
key: keys.periodFilterChip,
|
2022-09-14 14:19:39 +03:00
|
|
|
items: _periodValues,
|
|
|
|
value: int.tryParse(_periodController.text) ??
|
|
|
|
defaultPeriod,
|
|
|
|
selected: int.tryParse(_periodController.text) !=
|
|
|
|
defaultPeriod,
|
2023-02-28 13:34:29 +03:00
|
|
|
itemBuilder: ((value) =>
|
2023-03-02 14:45:55 +03:00
|
|
|
Text(l10n.s_num_sec(value))),
|
2023-08-17 17:51:57 +03:00
|
|
|
onChanged: !_dataLoaded
|
2022-09-14 14:19:39 +03:00
|
|
|
? (period) {
|
|
|
|
setState(() {
|
|
|
|
_periodController.text = '$period';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
: null,
|
|
|
|
),
|
|
|
|
ChoiceFilterChip<int>(
|
2023-11-14 18:44:14 +03:00
|
|
|
key: keys.digitsFilterChip,
|
2022-09-14 14:19:39 +03:00
|
|
|
items: _digitsValues,
|
|
|
|
value: _digits,
|
|
|
|
selected: _digits != defaultDigits,
|
2023-02-28 13:34:29 +03:00
|
|
|
itemBuilder: (value) =>
|
2023-03-02 14:45:55 +03:00
|
|
|
Text(l10n.s_num_digits(value)),
|
2023-11-14 18:44:14 +03:00
|
|
|
// TODO: need to figure out how to add values for
|
|
|
|
// digits6FilterValue
|
|
|
|
// digits8FilterValue
|
2023-08-17 17:51:57 +03:00
|
|
|
onChanged: !_dataLoaded
|
2022-09-14 14:19:39 +03:00
|
|
|
? (digits) {
|
|
|
|
setState(() {
|
|
|
|
_digits = digits;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
: null,
|
|
|
|
),
|
|
|
|
],
|
2022-09-07 11:25:22 +03:00
|
|
|
),
|
2022-09-14 14:19:39 +03:00
|
|
|
]
|
|
|
|
.map((e) => Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(vertical: 8.0),
|
|
|
|
child: e,
|
|
|
|
))
|
|
|
|
.toList(),
|
|
|
|
),
|
2022-09-14 16:54:51 +03:00
|
|
|
),
|
2022-03-23 12:46:35 +03:00
|
|
|
),
|
2022-01-28 19:05:05 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|