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-03-25 16:40:45 +03:00
|
|
|
import 'package:flutter/material.dart';
|
2022-09-05 16:10:44 +03:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2022-03-25 16:40:45 +03:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2024-03-08 11:30:47 +03:00
|
|
|
import 'package:material_symbols_icons/symbols.dart';
|
2022-03-25 16:40:45 +03:00
|
|
|
|
2022-03-25 17:43:32 +03:00
|
|
|
import '../../app/message.dart';
|
2022-03-25 16:40:45 +03:00
|
|
|
import '../../app/models.dart';
|
2024-02-26 17:37:08 +03:00
|
|
|
import '../../app/state.dart';
|
2024-07-09 12:33:41 +03:00
|
|
|
import '../../management/models.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_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';
|
2023-11-10 17:24:53 +03:00
|
|
|
import '../keys.dart' as keys;
|
2022-03-25 16:40:45 +03:00
|
|
|
import '../models.dart';
|
|
|
|
import '../state.dart';
|
|
|
|
|
|
|
|
class ManagePasswordDialog extends ConsumerStatefulWidget {
|
|
|
|
final DevicePath path;
|
|
|
|
final OathState state;
|
2022-05-12 10:56:55 +03:00
|
|
|
const ManagePasswordDialog(this.path, this.state, {super.key});
|
2022-03-25 16:40:45 +03:00
|
|
|
|
|
|
|
@override
|
2022-09-09 13:55:56 +03:00
|
|
|
ConsumerState<ConsumerStatefulWidget> createState() =>
|
|
|
|
_ManagePasswordDialogState();
|
2022-03-25 16:40:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
class _ManagePasswordDialogState extends ConsumerState<ManagePasswordDialog> {
|
2024-03-06 23:16:17 +03:00
|
|
|
final _currentPasswordController = TextEditingController();
|
|
|
|
final _currentPasswordFocus = FocusNode();
|
2022-03-25 16:40:45 +03:00
|
|
|
String _newPassword = '';
|
|
|
|
String _confirmPassword = '';
|
|
|
|
bool _currentIsWrong = false;
|
2023-11-27 17:12:57 +03:00
|
|
|
bool _isObscureCurrent = true;
|
|
|
|
bool _isObscureNew = true;
|
|
|
|
bool _isObscureConfirm = true;
|
2022-03-25 16:40:45 +03:00
|
|
|
|
2024-03-06 23:16:17 +03:00
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_currentPasswordController.dispose();
|
|
|
|
_currentPasswordFocus.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2022-04-06 12:52:57 +03:00
|
|
|
_submit() async {
|
2023-05-31 11:24:41 +03:00
|
|
|
FocusUtils.unfocus(context);
|
|
|
|
|
2022-09-09 13:55:56 +03:00
|
|
|
final result = await ref
|
|
|
|
.read(oathStateProvider(widget.path).notifier)
|
2024-03-06 23:16:17 +03:00
|
|
|
.setPassword(_currentPasswordController.text, _newPassword);
|
2022-04-06 12:52:57 +03:00
|
|
|
if (result) {
|
2024-02-26 17:37:08 +03:00
|
|
|
if (mounted) {
|
|
|
|
await ref.read(withContextProvider)((context) async {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
showMessage(context, AppLocalizations.of(context)!.s_password_set);
|
|
|
|
});
|
|
|
|
}
|
2022-04-06 12:52:57 +03:00
|
|
|
} else {
|
2024-03-06 23:16:17 +03:00
|
|
|
_currentPasswordController.selection = TextSelection(
|
|
|
|
baseOffset: 0, extentOffset: _currentPasswordController.text.length);
|
|
|
|
_currentPasswordFocus.requestFocus();
|
2022-04-06 12:52:57 +03:00
|
|
|
setState(() {
|
|
|
|
_currentIsWrong = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-25 16:40:45 +03:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-07-09 12:33:41 +03:00
|
|
|
final fipsCapable = ref.watch(currentDeviceDataProvider).maybeWhen(
|
|
|
|
data: (data) => data.info.getFipsStatus(Capability.oath).$1,
|
|
|
|
orElse: () => false);
|
2023-02-28 21:05:46 +03:00
|
|
|
final l10n = AppLocalizations.of(context)!;
|
2024-03-06 23:16:17 +03:00
|
|
|
final isValid = !_currentIsWrong &&
|
|
|
|
_newPassword.isNotEmpty &&
|
2022-04-06 12:52:57 +03:00
|
|
|
_newPassword == _confirmPassword &&
|
2024-03-06 23:16:17 +03:00
|
|
|
(!widget.state.hasKey || _currentPasswordController.text.isNotEmpty);
|
2022-04-06 12:52:57 +03:00
|
|
|
|
2022-03-25 16:40:45 +03:00
|
|
|
return ResponsiveDialog(
|
2024-02-13 15:11:01 +03:00
|
|
|
title: Text(
|
|
|
|
widget.state.hasKey ? l10n.s_manage_password : l10n.s_set_password),
|
2022-05-12 09:34:51 +03:00
|
|
|
actions: [
|
|
|
|
TextButton(
|
|
|
|
onPressed: isValid ? _submit : null,
|
2022-09-12 13:58:17 +03:00
|
|
|
key: keys.savePasswordButton,
|
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: [
|
|
|
|
if (widget.state.hasKey) ...[
|
2023-02-28 21:05:46 +03:00
|
|
|
Text(l10n.p_enter_current_password_or_reset),
|
2023-11-10 17:24:53 +03:00
|
|
|
AppTextField(
|
2022-09-07 11:25:22 +03:00
|
|
|
autofocus: true,
|
2023-11-27 17:12:57 +03:00
|
|
|
obscureText: _isObscureCurrent,
|
2023-03-14 17:24:41 +03:00
|
|
|
autofillHints: const [AutofillHints.password],
|
2022-09-12 13:58:17 +03:00
|
|
|
key: keys.currentPasswordField,
|
2024-03-06 23:16:17 +03:00
|
|
|
controller: _currentPasswordController,
|
|
|
|
focusNode: _currentPasswordFocus,
|
2023-12-14 18:38:10 +03:00
|
|
|
decoration: AppInputDecoration(
|
|
|
|
border: const OutlineInputBorder(),
|
|
|
|
labelText: l10n.s_current_password,
|
|
|
|
errorText: _currentIsWrong ? l10n.s_wrong_password : null,
|
|
|
|
errorMaxLines: 3,
|
2024-03-08 11:30:47 +03:00
|
|
|
prefixIcon: const Icon(Symbols.password),
|
2023-12-14 18:38:10 +03:00
|
|
|
suffixIcon: IconButton(
|
|
|
|
icon: Icon(_isObscureCurrent
|
2024-03-08 11:30:47 +03:00
|
|
|
? Symbols.visibility
|
|
|
|
: Symbols.visibility_off),
|
2023-12-14 18:38:10 +03:00
|
|
|
onPressed: () {
|
|
|
|
setState(() {
|
|
|
|
_isObscureCurrent = !_isObscureCurrent;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
tooltip: _isObscureCurrent
|
|
|
|
? l10n.s_show_password
|
|
|
|
: l10n.s_hide_password),
|
|
|
|
),
|
2022-09-07 11:25:22 +03:00
|
|
|
textInputAction: TextInputAction.next,
|
|
|
|
onChanged: (value) {
|
|
|
|
setState(() {
|
|
|
|
_currentIsWrong = false;
|
|
|
|
});
|
|
|
|
},
|
2024-03-07 17:01:46 +03:00
|
|
|
).init(),
|
2022-09-07 11:25:22 +03:00
|
|
|
Wrap(
|
|
|
|
spacing: 4.0,
|
|
|
|
runSpacing: 8.0,
|
|
|
|
children: [
|
2024-07-09 12:33:41 +03:00
|
|
|
if (!fipsCapable)
|
|
|
|
OutlinedButton(
|
|
|
|
key: keys.removePasswordButton,
|
|
|
|
onPressed: _currentPasswordController.text.isNotEmpty &&
|
|
|
|
!_currentIsWrong
|
|
|
|
? () async {
|
|
|
|
final result = await ref
|
|
|
|
.read(oathStateProvider(widget.path).notifier)
|
|
|
|
.unsetPassword(
|
|
|
|
_currentPasswordController.text);
|
|
|
|
if (result) {
|
|
|
|
if (mounted) {
|
|
|
|
await ref.read(withContextProvider)(
|
|
|
|
(context) async {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
showMessage(
|
|
|
|
context, l10n.s_password_removed);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_currentPasswordController.selection =
|
|
|
|
TextSelection(
|
|
|
|
baseOffset: 0,
|
|
|
|
extentOffset: _currentPasswordController
|
|
|
|
.text.length);
|
|
|
|
_currentPasswordFocus.requestFocus();
|
|
|
|
setState(() {
|
|
|
|
_currentIsWrong = true;
|
2024-02-26 17:37:08 +03:00
|
|
|
});
|
|
|
|
}
|
2022-09-07 11:25:22 +03:00
|
|
|
}
|
2024-07-09 12:33:41 +03:00
|
|
|
: null,
|
|
|
|
child: Text(l10n.s_remove_password),
|
|
|
|
),
|
2022-09-07 11:25:22 +03:00
|
|
|
if (widget.state.remembered)
|
|
|
|
OutlinedButton(
|
2023-03-02 14:45:55 +03:00
|
|
|
child: Text(l10n.s_clear_saved_password),
|
2022-09-07 11:25:22 +03:00
|
|
|
onPressed: () async {
|
|
|
|
await ref
|
|
|
|
.read(oathStateProvider(widget.path).notifier)
|
|
|
|
.forgetPassword();
|
2024-02-26 17:37:08 +03:00
|
|
|
if (mounted) {
|
|
|
|
await ref.read(withContextProvider)((context) async {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
showMessage(context, l10n.s_password_forgotten);
|
|
|
|
});
|
|
|
|
}
|
2022-09-07 11:25:22 +03:00
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
2023-02-28 21:05:46 +03:00
|
|
|
Text(l10n.p_enter_new_password),
|
2023-11-10 17:24:53 +03:00
|
|
|
AppTextField(
|
2022-09-12 13:58:17 +03:00
|
|
|
key: keys.newPasswordField,
|
2022-09-07 11:25:22 +03:00
|
|
|
autofocus: !widget.state.hasKey,
|
2023-11-27 17:12:57 +03:00
|
|
|
obscureText: _isObscureNew,
|
2023-03-14 17:24:41 +03:00
|
|
|
autofillHints: const [AutofillHints.newPassword],
|
2023-12-14 18:38:10 +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_new_password,
|
2024-03-08 11:30:47 +03:00
|
|
|
prefixIcon: const Icon(Symbols.password),
|
2023-12-14 18:38:10 +03:00
|
|
|
suffixIcon: IconButton(
|
|
|
|
icon: Icon(_isObscureNew
|
2024-03-08 11:30:47 +03:00
|
|
|
? Symbols.visibility
|
|
|
|
: Symbols.visibility_off),
|
2023-12-14 18:38:10 +03:00
|
|
|
onPressed: () {
|
|
|
|
setState(() {
|
|
|
|
_isObscureNew = !_isObscureNew;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
tooltip: _isObscureNew
|
|
|
|
? l10n.s_show_password
|
|
|
|
: l10n.s_hide_password),
|
2024-03-06 23:16:17 +03:00
|
|
|
enabled: !widget.state.hasKey ||
|
|
|
|
_currentPasswordController.text.isNotEmpty,
|
2022-09-07 11:25:22 +03:00
|
|
|
),
|
2022-08-12 10:54:47 +03:00
|
|
|
textInputAction: TextInputAction.next,
|
2022-03-25 16:40:45 +03:00
|
|
|
onChanged: (value) {
|
|
|
|
setState(() {
|
2022-09-07 11:25:22 +03:00
|
|
|
_newPassword = value;
|
2022-03-25 16:40:45 +03:00
|
|
|
});
|
|
|
|
},
|
2022-09-07 11:25:22 +03:00
|
|
|
onSubmitted: (_) {
|
|
|
|
if (isValid) {
|
|
|
|
_submit();
|
|
|
|
}
|
|
|
|
},
|
2024-03-07 17:01:46 +03:00
|
|
|
).init(),
|
2023-11-10 17:24:53 +03:00
|
|
|
AppTextField(
|
2022-09-12 13:58:17 +03:00
|
|
|
key: keys.confirmPasswordField,
|
2023-11-27 17:12:57 +03:00
|
|
|
obscureText: _isObscureConfirm,
|
2023-03-14 17:24:41 +03:00
|
|
|
autofillHints: const [AutofillHints.newPassword],
|
2023-12-14 18:38:10 +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_confirm_password,
|
2024-03-08 11:30:47 +03:00
|
|
|
prefixIcon: const Icon(Symbols.password),
|
2023-12-14 18:38:10 +03:00
|
|
|
suffixIcon: IconButton(
|
|
|
|
icon: Icon(_isObscureConfirm
|
2024-03-08 11:30:47 +03:00
|
|
|
? Symbols.visibility
|
|
|
|
: Symbols.visibility_off),
|
2023-12-14 18:38:10 +03:00
|
|
|
onPressed: () {
|
|
|
|
setState(() {
|
|
|
|
_isObscureConfirm = !_isObscureConfirm;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
tooltip: _isObscureConfirm
|
|
|
|
? l10n.s_show_password
|
|
|
|
: l10n.s_hide_password),
|
2024-03-06 23:16:17 +03:00
|
|
|
enabled: (!widget.state.hasKey ||
|
|
|
|
_currentPasswordController.text.isNotEmpty) &&
|
|
|
|
_newPassword.isNotEmpty,
|
2024-03-07 11:38:38 +03:00
|
|
|
errorText: _newPassword.length == _confirmPassword.length &&
|
|
|
|
_newPassword != _confirmPassword
|
|
|
|
? l10n.l_password_mismatch
|
|
|
|
: null,
|
|
|
|
helperText: '', // Prevents resizing when errorText shown
|
2022-09-07 11:25:22 +03:00
|
|
|
),
|
|
|
|
textInputAction: TextInputAction.done,
|
|
|
|
onChanged: (value) {
|
|
|
|
setState(() {
|
|
|
|
_confirmPassword = value;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onSubmitted: (_) {
|
|
|
|
if (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-03-25 16:40:45 +03:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|