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.
|
|
|
|
*/
|
|
|
|
|
2022-04-03 12:05:37 +03:00
|
|
|
import 'package:flutter/material.dart';
|
2022-09-12 16:46:43 +03:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2022-04-03 12:05:37 +03:00
|
|
|
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';
|
2022-04-05 12:46:22 +03:00
|
|
|
import '../../app/views/message_page.dart';
|
2022-04-03 12:05:37 +03:00
|
|
|
import '../models.dart';
|
2022-04-04 10:56:34 +03:00
|
|
|
import '../state.dart';
|
2023-01-12 10:56:08 +03:00
|
|
|
import 'key_actions.dart';
|
2022-04-03 12:05:37 +03:00
|
|
|
|
|
|
|
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});
|
2022-04-03 12:05:37 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
2023-02-28 21:05:46 +03:00
|
|
|
final l10n = AppLocalizations.of(context)!;
|
2022-04-05 12:46:22 +03:00
|
|
|
if (!state.hasPin) {
|
|
|
|
if (state.bioEnroll != null) {
|
|
|
|
return MessagePage(
|
2023-03-02 14:45:55 +03:00
|
|
|
title: Text(l10n.s_webauthn),
|
2022-05-20 12:11:20 +03:00
|
|
|
graphic: noFingerprints,
|
2023-03-02 14:45:55 +03:00
|
|
|
header: l10n.s_no_fingerprints,
|
2023-02-28 21:05:46 +03:00
|
|
|
message: l10n.l_set_pin_fingerprints,
|
2022-12-08 18:40:00 +03:00
|
|
|
keyActionsBuilder: _buildActions,
|
2022-04-05 12:46:22 +03:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return MessagePage(
|
2023-03-02 14:45:55 +03:00
|
|
|
title: Text(l10n.s_webauthn),
|
2022-06-07 13:10:01 +03:00
|
|
|
graphic: manageAccounts,
|
2022-09-12 16:46:43 +03:00
|
|
|
header: state.credMgmt
|
2023-02-28 21:05:46 +03:00
|
|
|
? l10n.l_no_discoverable_accounts
|
|
|
|
: l10n.l_ready_to_use,
|
|
|
|
message: l10n.l_optionally_set_a_pin,
|
2022-12-08 18:40:00 +03:00
|
|
|
keyActionsBuilder: _buildActions,
|
2022-04-05 12:46:22 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 13:10:01 +03:00
|
|
|
if (!state.credMgmt && state.bioEnroll == null) {
|
|
|
|
return MessagePage(
|
2023-03-02 14:45:55 +03:00
|
|
|
title: Text(l10n.s_webauthn),
|
2022-06-07 13:10:01 +03:00
|
|
|
graphic: manageAccounts,
|
2023-02-28 21:05:46 +03:00
|
|
|
header: l10n.l_ready_to_use,
|
|
|
|
message: l10n.l_register_sk_on_websites,
|
2022-12-08 18:40:00 +03:00
|
|
|
keyActionsBuilder: _buildActions,
|
2022-06-07 13:10:01 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-03 12:05:37 +03:00
|
|
|
return AppPage(
|
2023-03-02 14:45:55 +03:00
|
|
|
title: Text(l10n.s_webauthn),
|
2022-12-08 18:40:00 +03:00
|
|
|
keyActionsBuilder: _buildActions,
|
2022-04-03 12:05:37 +03:00
|
|
|
child: Column(
|
|
|
|
children: [
|
2022-04-05 12:46:22 +03:00
|
|
|
_PinEntryForm(state, node),
|
2022-04-03 12:05:37 +03:00
|
|
|
],
|
|
|
|
),
|
2022-04-05 12:46:22 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-12 10:56:08 +03:00
|
|
|
Widget _buildActions(BuildContext context) =>
|
|
|
|
fidoBuildActions(context, node, state, -1);
|
2022-04-03 12:05:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
class _PinEntryForm extends ConsumerStatefulWidget {
|
2022-04-05 12:46:22 +03:00
|
|
|
final FidoState _state;
|
|
|
|
final DeviceNode _deviceNode;
|
2022-05-12 10:56:55 +03:00
|
|
|
const _PinEntryForm(this._state, this._deviceNode);
|
2022-04-03 12:05:37 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
ConsumerState<_PinEntryForm> createState() => _PinEntryFormState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _PinEntryFormState extends ConsumerState<_PinEntryForm> {
|
|
|
|
final _pinController = TextEditingController();
|
|
|
|
bool _blocked = false;
|
|
|
|
int? _retries;
|
2022-06-08 17:23:19 +03:00
|
|
|
bool _pinIsWrong = false;
|
|
|
|
bool _isObscure = true;
|
2022-04-03 12:05:37 +03:00
|
|
|
|
|
|
|
void _submit() async {
|
2022-06-08 17:23:19 +03:00
|
|
|
setState(() {
|
|
|
|
_pinIsWrong = false;
|
|
|
|
_isObscure = true;
|
|
|
|
});
|
2022-04-03 12:05:37 +03:00
|
|
|
final result = await ref
|
2022-04-05 12:46:22 +03:00
|
|
|
.read(fidoStateProvider(widget._deviceNode.path).notifier)
|
2022-04-03 12:05:37 +03:00
|
|
|
.unlock(_pinController.text);
|
|
|
|
result.whenOrNull(failed: (retries, authBlocked) {
|
|
|
|
setState(() {
|
|
|
|
_pinController.clear();
|
2022-06-08 17:23:19 +03:00
|
|
|
_pinIsWrong = true;
|
2022-04-03 12:05:37 +03:00
|
|
|
_retries = retries;
|
|
|
|
_blocked = authBlocked;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
String? _getErrorText() {
|
2023-02-28 21:05:46 +03:00
|
|
|
final l10n = AppLocalizations.of(context)!;
|
2022-04-03 12:05:37 +03:00
|
|
|
if (_retries == 0) {
|
2023-02-28 21:05:46 +03:00
|
|
|
return l10n.l_pin_blocked_reset;
|
2022-04-03 12:05:37 +03:00
|
|
|
}
|
|
|
|
if (_blocked) {
|
2023-02-28 21:05:46 +03:00
|
|
|
return l10n.l_pin_soft_locked;
|
2022-04-03 12:05:37 +03:00
|
|
|
}
|
|
|
|
if (_retries != null) {
|
2023-02-28 21:05:46 +03:00
|
|
|
return l10n.l_wrong_pin_attempts_remaining(_retries!);
|
2022-04-03 12:05:37 +03:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-02-28 21:05:46 +03:00
|
|
|
final l10n = AppLocalizations.of(context)!;
|
2022-04-05 12:46:22 +03:00
|
|
|
final noFingerprints = widget._state.bioEnroll == false;
|
2022-04-03 12:05:37 +03:00
|
|
|
return Padding(
|
2022-06-08 17:23:19 +03:00
|
|
|
padding: const EdgeInsets.only(left: 18.0, right: 18, top: 32),
|
2022-04-03 12:05:37 +03:00
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
2023-02-28 21:05:46 +03:00
|
|
|
Text(l10n.l_enter_fido2_pin),
|
2022-04-03 12:05:37 +03:00
|
|
|
Padding(
|
2022-06-08 17:23:19 +03:00
|
|
|
padding: const EdgeInsets.only(top: 16.0, bottom: 16.0),
|
2022-04-03 12:05:37 +03:00
|
|
|
child: TextField(
|
|
|
|
autofocus: true,
|
2022-06-08 17:23:19 +03:00
|
|
|
obscureText: _isObscure,
|
2022-04-03 12:05:37 +03:00
|
|
|
controller: _pinController,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
border: const OutlineInputBorder(),
|
2023-03-02 14:45:55 +03:00
|
|
|
labelText: l10n.s_pin,
|
2022-06-08 17:23:19 +03:00
|
|
|
helperText: '', // Prevents dialog resizing
|
|
|
|
errorText: _pinIsWrong ? _getErrorText() : null,
|
|
|
|
errorMaxLines: 3,
|
2022-06-09 12:23:34 +03:00
|
|
|
prefixIcon: const Icon(Icons.pin_outlined),
|
2022-06-08 17:23:19 +03:00
|
|
|
suffixIcon: IconButton(
|
|
|
|
icon: Icon(
|
|
|
|
_isObscure ? Icons.visibility : Icons.visibility_off,
|
2022-09-01 15:00:33 +03:00
|
|
|
color: IconTheme.of(context).color,
|
2022-06-08 17:23:19 +03:00
|
|
|
),
|
|
|
|
onPressed: () {
|
|
|
|
setState(() {
|
|
|
|
_isObscure = !_isObscure;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
),
|
2022-04-03 12:05:37 +03:00
|
|
|
),
|
2022-06-08 17:23:19 +03:00
|
|
|
onChanged: (value) {
|
|
|
|
setState(() {
|
|
|
|
_pinIsWrong = false;
|
|
|
|
});
|
|
|
|
}, // Update state on change
|
2022-04-03 12:05:37 +03:00
|
|
|
onSubmitted: (_) => _submit(),
|
|
|
|
),
|
|
|
|
),
|
2022-04-05 12:46:22 +03:00
|
|
|
ListTile(
|
|
|
|
leading:
|
|
|
|
noFingerprints ? const Icon(Icons.warning_amber_rounded) : null,
|
|
|
|
title: noFingerprints
|
2022-09-12 16:46:43 +03:00
|
|
|
? Text(
|
2023-02-28 21:05:46 +03:00
|
|
|
l10n.l_no_fps_added,
|
2022-04-05 12:46:22 +03:00
|
|
|
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),
|
2023-03-02 14:45:55 +03:00
|
|
|
label: Text(l10n.s_unlock),
|
2022-04-03 12:05:37 +03:00
|
|
|
onPressed:
|
|
|
|
_pinController.text.isNotEmpty && !_blocked ? _submit : null,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|