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-01-24 12:59:09 +03:00
|
|
|
import 'package:flutter/material.dart';
|
2022-09-06 15:30:18 +03:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2022-01-24 12:59:09 +03:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2022-06-13 17:45:26 +03:00
|
|
|
import 'package:logging/logging.dart';
|
2024-03-08 11:30:47 +03:00
|
|
|
import 'package:material_symbols_icons/symbols.dart';
|
2022-01-24 12:59:09 +03:00
|
|
|
|
2022-06-13 17:45:26 +03:00
|
|
|
import '../../app/logging.dart';
|
2022-03-25 17:43:32 +03:00
|
|
|
import '../../app/message.dart';
|
2022-06-10 13:23:52 +03:00
|
|
|
import '../../app/models.dart';
|
2023-08-18 15:54:55 +03:00
|
|
|
import '../../app/state.dart';
|
2022-06-13 17:45:26 +03:00
|
|
|
import '../../desktop/models.dart';
|
2023-11-10 17:24:53 +03:00
|
|
|
import '../../exception/cancellation_exception.dart';
|
2023-12-14 19:40:29 +03:00
|
|
|
import '../../widgets/app_input_decoration.dart';
|
2023-11-27 13:41:05 +03:00
|
|
|
import '../../widgets/app_text_form_field.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';
|
2023-11-10 17:24:53 +03:00
|
|
|
import '../keys.dart' as keys;
|
2022-01-24 12:59:09 +03:00
|
|
|
import '../models.dart';
|
|
|
|
import '../state.dart';
|
2022-01-31 13:02:34 +03:00
|
|
|
import 'utils.dart';
|
2022-01-24 12:59:09 +03:00
|
|
|
|
2022-06-13 17:45:26 +03:00
|
|
|
final _log = Logger('oath.view.rename_account_dialog');
|
|
|
|
|
2022-01-24 12:59:09 +03:00
|
|
|
class RenameAccountDialog extends ConsumerStatefulWidget {
|
2024-01-17 18:29:28 +03:00
|
|
|
final DevicePath devicePath;
|
2023-08-18 15:54:55 +03:00
|
|
|
final String? issuer;
|
|
|
|
final String name;
|
|
|
|
final OathType oathType;
|
|
|
|
final int period;
|
|
|
|
final List<(String? issuer, String name)> existing;
|
|
|
|
final Future<dynamic> Function(String? issuer, String name) rename;
|
|
|
|
|
|
|
|
const RenameAccountDialog({
|
2024-01-17 18:29:28 +03:00
|
|
|
required this.devicePath,
|
2023-08-18 15:54:55 +03:00
|
|
|
required this.issuer,
|
|
|
|
required this.name,
|
|
|
|
required this.oathType,
|
|
|
|
this.period = defaultPeriod,
|
|
|
|
this.existing = const [],
|
|
|
|
required this.rename,
|
|
|
|
super.key,
|
|
|
|
});
|
2022-01-24 12:59:09 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
ConsumerState<ConsumerStatefulWidget> createState() =>
|
|
|
|
_RenameAccountDialogState();
|
2023-08-18 15:54:55 +03:00
|
|
|
|
|
|
|
factory RenameAccountDialog.forOathCredential(
|
|
|
|
WidgetRef ref,
|
2024-01-17 18:29:28 +03:00
|
|
|
DevicePath devicePath,
|
2023-08-18 15:54:55 +03:00
|
|
|
OathCredential credential,
|
|
|
|
List<(String? issuer, String name)> existing) {
|
|
|
|
return RenameAccountDialog(
|
2024-01-17 18:29:28 +03:00
|
|
|
devicePath: devicePath,
|
2023-08-18 15:54:55 +03:00
|
|
|
issuer: credential.issuer,
|
|
|
|
name: credential.name,
|
|
|
|
oathType: credential.oathType,
|
|
|
|
period: credential.period,
|
|
|
|
existing: existing,
|
|
|
|
rename: (issuer, name) async {
|
|
|
|
final withContext = ref.read(withContextProvider);
|
|
|
|
try {
|
|
|
|
// Rename credentials
|
|
|
|
final renamed = await ref
|
2024-01-17 18:29:28 +03:00
|
|
|
.read(credentialListProvider(devicePath).notifier)
|
2023-08-18 15:54:55 +03:00
|
|
|
.renameAccount(credential, issuer, name);
|
|
|
|
|
|
|
|
// Update favorite
|
|
|
|
ref
|
|
|
|
.read(favoritesProvider.notifier)
|
|
|
|
.renameCredential(credential.id, renamed.id);
|
|
|
|
|
|
|
|
await withContext((context) async => showMessage(
|
|
|
|
context, AppLocalizations.of(context)!.s_account_renamed));
|
|
|
|
return renamed;
|
|
|
|
} 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;
|
|
|
|
} else {
|
|
|
|
errorMessage = e.toString();
|
|
|
|
}
|
|
|
|
await withContext((context) async => showMessage(
|
|
|
|
context,
|
|
|
|
AppLocalizations.of(context)!
|
|
|
|
.l_account_add_failed(errorMessage),
|
|
|
|
duration: const Duration(seconds: 4),
|
|
|
|
));
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2022-01-24 12:59:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
class _RenameAccountDialogState extends ConsumerState<RenameAccountDialog> {
|
2022-01-31 13:02:34 +03:00
|
|
|
late String _issuer;
|
2023-08-18 15:54:55 +03:00
|
|
|
late String _name;
|
2022-01-24 12:59:09 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2023-08-18 15:54:55 +03:00
|
|
|
_issuer = widget.issuer?.trim() ?? '';
|
|
|
|
_name = widget.name.trim();
|
2022-01-24 12:59:09 +03:00
|
|
|
}
|
|
|
|
|
2022-09-06 09:50:42 +03:00
|
|
|
void _submit() async {
|
2023-08-18 15:54:55 +03:00
|
|
|
FocusUtils.unfocus(context);
|
|
|
|
final nav = Navigator.of(context);
|
|
|
|
final renamed =
|
|
|
|
await widget.rename(_issuer.isNotEmpty ? _issuer : null, _name);
|
|
|
|
nav.pop(renamed);
|
2022-09-06 09:50:42 +03:00
|
|
|
}
|
|
|
|
|
2022-01-24 12:59:09 +03:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-02-28 21:05:46 +03:00
|
|
|
final l10n = AppLocalizations.of(context)!;
|
2022-01-24 12:59:09 +03:00
|
|
|
|
2023-05-22 12:20:30 +03:00
|
|
|
final (issuerRemaining, nameRemaining) = getRemainingKeySpace(
|
2023-08-18 15:54:55 +03:00
|
|
|
oathType: widget.oathType,
|
|
|
|
period: widget.period,
|
2022-01-31 13:02:34 +03:00
|
|
|
issuer: _issuer,
|
2023-08-18 15:54:55 +03:00
|
|
|
name: _name,
|
2022-01-31 13:02:34 +03:00
|
|
|
);
|
2022-09-02 19:24:55 +03:00
|
|
|
|
2023-08-18 15:54:55 +03:00
|
|
|
// are the name/issuer values different from original
|
|
|
|
final didChange = (widget.issuer ?? '') != _issuer || widget.name != _name;
|
|
|
|
|
|
|
|
// is this credentials name/issuer pair different from all other, or initial value?
|
|
|
|
final isUnique = !widget.existing.contains((_issuer, _name)) || !didChange;
|
2022-09-02 19:24:55 +03:00
|
|
|
|
|
|
|
// is this credential name/issuer of valid format
|
2023-08-18 15:54:55 +03:00
|
|
|
final nameNotEmpty = _name.isNotEmpty;
|
2022-09-02 19:24:55 +03:00
|
|
|
|
|
|
|
// can we rename with the new values
|
2023-08-18 15:54:55 +03:00
|
|
|
final isValid = isUnique && nameNotEmpty;
|
2022-01-25 16:41:41 +03:00
|
|
|
|
2022-03-15 20:04:26 +03:00
|
|
|
return ResponsiveDialog(
|
2023-03-02 14:45:55 +03:00
|
|
|
title: Text(l10n.s_rename_account),
|
2022-05-12 09:34:51 +03:00
|
|
|
actions: [
|
|
|
|
TextButton(
|
2022-09-06 09:50:42 +03:00
|
|
|
onPressed: didChange && isValid ? _submit : null,
|
2022-09-12 13:58:17 +03:00
|
|
|
key: keys.saveButton,
|
2023-03-02 14:45:55 +03:00
|
|
|
child: Text(l10n.s_save),
|
2022-05-12 09:34:51 +03:00
|
|
|
),
|
|
|
|
],
|
2022-09-07 11:25:22 +03:00
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 18.0),
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
2023-08-18 15:54:55 +03:00
|
|
|
Text(l10n.q_rename_target(widget.issuer != null
|
|
|
|
? '${widget.issuer} (${widget.name})'
|
|
|
|
: widget.name)),
|
2023-02-28 21:05:46 +03:00
|
|
|
Text(l10n.p_rename_will_change_account_displayed),
|
2023-11-10 17:24:53 +03:00
|
|
|
AppTextFormField(
|
2022-09-07 11:25:22 +03:00
|
|
|
initialValue: _issuer,
|
|
|
|
enabled: issuerRemaining > 0,
|
|
|
|
maxLength: issuerRemaining > 0 ? issuerRemaining : null,
|
|
|
|
buildCounter: buildByteCounterFor(_issuer),
|
|
|
|
inputFormatters: [limitBytesLength(issuerRemaining)],
|
2022-09-12 13:58:17 +03:00
|
|
|
key: keys.issuerField,
|
2023-12-14 19:21:46 +03:00
|
|
|
decoration: AppInputDecoration(
|
2022-09-07 11:25:22 +03:00
|
|
|
border: const OutlineInputBorder(),
|
2023-03-02 14:45:55 +03:00
|
|
|
labelText: l10n.s_issuer_optional,
|
2022-09-07 11:25:22 +03:00
|
|
|
helperText: '', // Prevents dialog resizing when disabled
|
2024-03-08 11:30:47 +03:00
|
|
|
prefixIcon: const Icon(Symbols.business),
|
2022-09-07 11:25:22 +03:00
|
|
|
),
|
|
|
|
textInputAction: TextInputAction.next,
|
|
|
|
onChanged: (value) {
|
|
|
|
setState(() {
|
|
|
|
_issuer = value.trim();
|
|
|
|
});
|
|
|
|
},
|
2024-03-07 17:01:46 +03:00
|
|
|
).init(),
|
2023-11-10 17:24:53 +03:00
|
|
|
AppTextFormField(
|
2023-08-18 15:54:55 +03:00
|
|
|
initialValue: _name,
|
2022-09-07 11:25:22 +03:00
|
|
|
maxLength: nameRemaining,
|
|
|
|
inputFormatters: [limitBytesLength(nameRemaining)],
|
2023-08-18 15:54:55 +03:00
|
|
|
buildCounter: buildByteCounterFor(_name),
|
2022-09-12 13:58:17 +03:00
|
|
|
key: keys.nameField,
|
2023-12-14 19:21:46 +03:00
|
|
|
decoration: AppInputDecoration(
|
2022-09-07 11:25:22 +03:00
|
|
|
border: const OutlineInputBorder(),
|
2023-03-02 14:45:55 +03:00
|
|
|
labelText: l10n.s_account_name,
|
2022-09-07 11:25:22 +03:00
|
|
|
helperText: '', // Prevents dialog resizing when disabled
|
2023-08-18 15:54:55 +03:00
|
|
|
errorText: !nameNotEmpty
|
2023-02-28 21:05:46 +03:00
|
|
|
? l10n.l_account_name_required
|
2022-09-07 11:25:22 +03:00
|
|
|
: !isUnique
|
2023-02-28 21:05:46 +03:00
|
|
|
? l10n.l_name_already_exists
|
2022-09-07 11:25:22 +03:00
|
|
|
: null,
|
2024-03-08 11:30:47 +03:00
|
|
|
prefixIcon: const Icon(Symbols.people_alt),
|
2022-09-07 11:25:22 +03:00
|
|
|
),
|
2022-09-11 12:05:00 +03:00
|
|
|
textInputAction: TextInputAction.done,
|
|
|
|
onChanged: (value) {
|
|
|
|
setState(() {
|
2023-08-18 15:54:55 +03:00
|
|
|
_name = value.trim();
|
2022-09-11 12:05:00 +03:00
|
|
|
});
|
|
|
|
},
|
|
|
|
onFieldSubmitted: (_) {
|
|
|
|
if (didChange && isValid) {
|
|
|
|
_submit();
|
|
|
|
}
|
|
|
|
},
|
2024-03-07 17:01:46 +03:00
|
|
|
).init(),
|
2022-09-07 11:25:22 +03:00
|
|
|
]
|
|
|
|
.map((e) => Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(vertical: 8.0),
|
|
|
|
child: e,
|
|
|
|
))
|
|
|
|
.toList(),
|
|
|
|
),
|
2022-01-24 12:59:09 +03:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|