2022-10-04 13:12:54 +03:00
|
|
|
/*
|
2023-11-10 17:24:53 +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-14 14:19:39 +03:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
|
|
|
|
import '../../app/message.dart';
|
|
|
|
import '../../app/models.dart';
|
2023-12-14 19:40:29 +03:00
|
|
|
import '../../widgets/app_input_decoration.dart';
|
2023-11-10 17:24:53 +03:00
|
|
|
import '../../widgets/app_text_field.dart';
|
2022-09-14 14:19:39 +03:00
|
|
|
import '../keys.dart' as keys;
|
2023-11-10 17:24:53 +03:00
|
|
|
import '../models.dart';
|
2022-09-14 14:19:39 +03:00
|
|
|
import '../state.dart';
|
|
|
|
|
|
|
|
class UnlockForm extends ConsumerStatefulWidget {
|
|
|
|
final DevicePath _devicePath;
|
|
|
|
final KeystoreState keystore;
|
|
|
|
const UnlockForm(this._devicePath, {required this.keystore, super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
ConsumerState<UnlockForm> createState() => _UnlockFormState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _UnlockFormState extends ConsumerState<UnlockForm> {
|
|
|
|
final _passwordController = TextEditingController();
|
|
|
|
bool _remember = false;
|
|
|
|
bool _passwordIsWrong = false;
|
|
|
|
bool _isObscure = true;
|
|
|
|
|
|
|
|
void _submit() async {
|
|
|
|
setState(() {
|
|
|
|
_passwordIsWrong = false;
|
|
|
|
});
|
2023-05-22 12:20:30 +03:00
|
|
|
final (success, remembered) = await ref
|
2022-09-14 14:19:39 +03:00
|
|
|
.read(oathStateProvider(widget._devicePath).notifier)
|
|
|
|
.unlock(_passwordController.text, remember: _remember);
|
|
|
|
if (!mounted) return;
|
2023-05-22 12:20:30 +03:00
|
|
|
if (!success) {
|
2022-09-14 14:19:39 +03:00
|
|
|
setState(() {
|
|
|
|
_passwordIsWrong = true;
|
|
|
|
_passwordController.clear();
|
|
|
|
});
|
2023-05-22 12:20:30 +03:00
|
|
|
} else if (_remember && !remembered) {
|
2023-02-28 17:02:12 +03:00
|
|
|
showMessage(context, AppLocalizations.of(context)!.l_remember_pw_failed);
|
2022-09-14 14:19:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-02-28 21:05:46 +03:00
|
|
|
final l10n = AppLocalizations.of(context)!;
|
2022-09-14 14:19:39 +03:00
|
|
|
final keystoreFailed = widget.keystore == KeystoreState.failed;
|
|
|
|
return Column(
|
|
|
|
children: [
|
|
|
|
Padding(
|
2023-12-20 17:09:31 +03:00
|
|
|
padding: const EdgeInsets.only(left: 18.0, right: 18, top: 14),
|
2022-09-14 14:19:39 +03:00
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
Text(
|
2023-02-28 21:05:46 +03:00
|
|
|
l10n.l_enter_oath_pw,
|
2022-09-14 14:19:39 +03:00
|
|
|
),
|
|
|
|
const SizedBox(height: 16.0),
|
2023-11-10 17:24:53 +03:00
|
|
|
AppTextField(
|
2022-09-14 14:19:39 +03:00
|
|
|
key: keys.passwordField,
|
|
|
|
controller: _passwordController,
|
|
|
|
autofocus: true,
|
|
|
|
obscureText: _isObscure,
|
2023-03-14 17:24:41 +03:00
|
|
|
autofillHints: const [AutofillHints.password],
|
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_password,
|
|
|
|
errorText: _passwordIsWrong ? l10n.s_wrong_password : null,
|
2022-09-14 14:19:39 +03:00
|
|
|
helperText: '', // Prevents resizing when errorText shown
|
|
|
|
prefixIcon: const Icon(Icons.password_outlined),
|
2023-12-14 18:38:10 +03:00
|
|
|
suffixIcon: IconButton(
|
|
|
|
icon: Icon(
|
2023-12-21 16:28:24 +03:00
|
|
|
_isObscure ? Icons.visibility : Icons.visibility_off),
|
2023-12-14 18:38:10 +03:00
|
|
|
onPressed: () {
|
|
|
|
setState(() {
|
|
|
|
_isObscure = !_isObscure;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
tooltip: _isObscure
|
|
|
|
? l10n.s_show_password
|
|
|
|
: l10n.s_hide_password,
|
2022-09-14 14:19:39 +03:00
|
|
|
),
|
|
|
|
),
|
|
|
|
onChanged: (_) => setState(() {
|
|
|
|
_passwordIsWrong = false;
|
|
|
|
}), // Update state on change
|
|
|
|
onSubmitted: (_) => _submit(),
|
|
|
|
),
|
2023-12-13 14:07:02 +03:00
|
|
|
const SizedBox(height: 8.0),
|
|
|
|
Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
|
|
children: [
|
|
|
|
Wrap(
|
|
|
|
alignment: WrapAlignment.spaceBetween,
|
|
|
|
crossAxisAlignment: WrapCrossAlignment.center,
|
|
|
|
spacing: 4.0,
|
|
|
|
runSpacing: 8.0,
|
2023-11-29 15:31:30 +03:00
|
|
|
children: [
|
2023-12-13 14:07:02 +03:00
|
|
|
keystoreFailed
|
|
|
|
? Wrap(
|
|
|
|
crossAxisAlignment: WrapCrossAlignment.center,
|
|
|
|
spacing: 4.0,
|
|
|
|
runSpacing: 8.0,
|
|
|
|
children: [
|
|
|
|
const Icon(Icons.warning_amber_rounded),
|
|
|
|
Text(l10n.l_keystore_unavailable)
|
|
|
|
],
|
|
|
|
)
|
|
|
|
: FilterChip(
|
|
|
|
label: Text(l10n.s_remember_password),
|
|
|
|
selected: _remember,
|
|
|
|
onSelected: (value) {
|
|
|
|
setState(() {
|
|
|
|
_remember = value;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
),
|
|
|
|
ElevatedButton.icon(
|
|
|
|
key: keys.unlockButton,
|
|
|
|
label: Text(l10n.s_unlock),
|
|
|
|
icon: const Icon(Icons.lock_open),
|
|
|
|
onPressed: _passwordController.text.isNotEmpty
|
|
|
|
? _submit
|
|
|
|
: null,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
2022-09-14 14:19:39 +03:00
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|