yubioath-flutter/lib/fido/views/locked_page.dart

197 lines
6.3 KiB
Dart
Raw Normal View History

2022-10-04 13:12:54 +03:00
/*
* Copyright (C) 2022 Yubico.
*
* 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.
*/
import 'package:flutter/material.dart';
2022-09-12 16:46:43 +03:00
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../app/models.dart';
import '../../app/views/app_page.dart';
2022-05-18 15:11:17 +03:00
import '../../app/views/graphics.dart';
import '../../app/views/message_page.dart';
import '../models.dart';
import '../state.dart';
import 'key_actions.dart';
class FidoLockedPage extends ConsumerWidget {
final DeviceNode node;
final FidoState state;
2022-05-12 10:56:55 +03:00
const FidoLockedPage(this.node, this.state, {super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
if (!state.hasPin) {
if (state.bioEnroll != null) {
return MessagePage(
2022-09-12 16:46:43 +03:00
title: Text(AppLocalizations.of(context)!.fido_webauthn),
2022-05-20 12:11:20 +03:00
graphic: noFingerprints,
2022-09-12 16:46:43 +03:00
header: AppLocalizations.of(context)!.fido_no_fingerprints,
message: AppLocalizations.of(context)!.fido_set_pin_fingerprints,
keyActionsBuilder: _buildActions,
);
} else {
return MessagePage(
2022-09-12 16:46:43 +03:00
title: Text(AppLocalizations.of(context)!.fido_webauthn),
graphic: manageAccounts,
2022-09-12 16:46:43 +03:00
header: state.credMgmt
? AppLocalizations.of(context)!.fido_no_discoverable_acc
: AppLocalizations.of(context)!.fido_ready_to_use,
message: AppLocalizations.of(context)!.fido_optionally_set_a_pin,
keyActionsBuilder: _buildActions,
);
}
}
if (!state.credMgmt && state.bioEnroll == null) {
return MessagePage(
2022-09-12 16:46:43 +03:00
title: Text(AppLocalizations.of(context)!.fido_webauthn),
graphic: manageAccounts,
2022-09-12 16:46:43 +03:00
header: AppLocalizations.of(context)!.fido_ready_to_use,
message: AppLocalizations.of(context)!.fido_register_as_a_key,
keyActionsBuilder: _buildActions,
);
}
return AppPage(
2022-09-12 16:46:43 +03:00
title: Text(AppLocalizations.of(context)!.fido_webauthn),
keyActionsBuilder: _buildActions,
child: Column(
children: [
_PinEntryForm(state, node),
],
),
);
}
Widget _buildActions(BuildContext context) =>
fidoBuildActions(context, node, state, -1);
}
class _PinEntryForm extends ConsumerStatefulWidget {
final FidoState _state;
final DeviceNode _deviceNode;
2022-05-12 10:56:55 +03:00
const _PinEntryForm(this._state, this._deviceNode);
@override
ConsumerState<_PinEntryForm> createState() => _PinEntryFormState();
}
class _PinEntryFormState extends ConsumerState<_PinEntryForm> {
final _pinController = TextEditingController();
bool _blocked = false;
int? _retries;
bool _pinIsWrong = false;
bool _isObscure = true;
void _submit() async {
setState(() {
_pinIsWrong = false;
_isObscure = true;
});
final result = await ref
.read(fidoStateProvider(widget._deviceNode.path).notifier)
.unlock(_pinController.text);
result.whenOrNull(failed: (retries, authBlocked) {
setState(() {
_pinController.clear();
_pinIsWrong = true;
_retries = retries;
_blocked = authBlocked;
});
});
}
String? _getErrorText() {
if (_retries == 0) {
2022-09-12 16:46:43 +03:00
return AppLocalizations.of(context)!.fido_pin_blocked_factory_reset;
}
if (_blocked) {
2022-09-12 16:46:43 +03:00
return AppLocalizations.of(context)!.fido_pin_temp_blocked;
}
if (_retries != null) {
2022-09-12 16:46:43 +03:00
return AppLocalizations.of(context)!.fido_wrong_pin_attempts(_retries!);
}
return null;
}
@override
Widget build(BuildContext context) {
final noFingerprints = widget._state.bioEnroll == false;
return Padding(
padding: const EdgeInsets.only(left: 18.0, right: 18, top: 32),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2022-09-12 16:46:43 +03:00
Text(AppLocalizations.of(context)!.fido_enter_fido2_pin),
Padding(
padding: const EdgeInsets.only(top: 16.0, bottom: 16.0),
child: TextField(
autofocus: true,
obscureText: _isObscure,
controller: _pinController,
decoration: InputDecoration(
border: const OutlineInputBorder(),
2022-09-12 16:46:43 +03:00
labelText: AppLocalizations.of(context)!.fido_pin,
helperText: '', // Prevents dialog resizing
errorText: _pinIsWrong ? _getErrorText() : null,
errorMaxLines: 3,
2022-06-09 12:23:34 +03:00
prefixIcon: const Icon(Icons.pin_outlined),
suffixIcon: IconButton(
icon: Icon(
_isObscure ? Icons.visibility : Icons.visibility_off,
color: IconTheme.of(context).color,
),
onPressed: () {
setState(() {
_isObscure = !_isObscure;
});
},
),
),
onChanged: (value) {
setState(() {
_pinIsWrong = false;
});
}, // Update state on change
onSubmitted: (_) => _submit(),
),
),
ListTile(
leading:
noFingerprints ? const Icon(Icons.warning_amber_rounded) : null,
title: noFingerprints
2022-09-12 16:46:43 +03:00
? Text(
AppLocalizations.of(context)!.fido_no_fp_added,
overflow: TextOverflow.fade,
)
: null,
dense: true,
contentPadding: const EdgeInsets.symmetric(horizontal: 0),
minLeadingWidth: 0,
2022-09-01 12:35:39 +03:00
trailing: ElevatedButton.icon(
icon: const Icon(Icons.lock_open),
2022-09-12 16:46:43 +03:00
label: Text(AppLocalizations.of(context)!.fido_unlock),
onPressed:
_pinController.text.isNotEmpty && !_blocked ? _submit : null,
),
),
],
),
);
}
}