<commit_before>import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../../app/message.dart';
import '../../app/models.dart';
import '../../app/views/app_page.dart';
import '../../app/views/graphics.dart';
import '../../app/views/message_page.dart';
import '../../theme.dart';
import '../models.dart';
import '../state.dart';
import 'pin_dialog.dart';
import 'reset_dialog.dart';

class FidoLockedPage extends ConsumerWidget {
  final DeviceNode node;
  final FidoState state;

  const FidoLockedPage(this.node, this.state, {super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    if (!state.hasPin) {
      if (state.bioEnroll != null) {
        return MessagePage(
          title: const Text('WebAuthn'),
          graphic: noFingerprints,
          header: 'No fingerprints',
          message: 'Set a PIN to register fingerprints',
          actions: _buildActions(context),
        );
      } else {
        return MessagePage(
          title: const Text('WebAuthn'),
          graphic: manageAccounts,
          header: state.credMgmt ? 'No discoverable accounts' : 'Ready to use',
          message:
              'Optionally set a PIN to protect access to your YubiKey\nRegister as a Security Key on websites',
          actions: _buildActions(context),
        );
      }
    }

    if (!state.credMgmt && state.bioEnroll == null) {
      return MessagePage(
        title: const Text('WebAuthn'),
        graphic: manageAccounts,
        header: 'Ready to use',
        message: 'Register as a Security Key on websites',
        actions: _buildActions(context),
      );
    }

    return AppPage(
      title: const Text('WebAuthn'),
      actions: _buildActions(context),
      child: Column(
        children: [
          _PinEntryForm(state, node),
        ],
      ),
    );
  }

  List<Widget> _buildActions(BuildContext context) => [
        if (!state.hasPin)
          OutlinedButton.icon(
            style: state.bioEnroll != null
                ? AppTheme.primaryOutlinedButtonStyle(context)
                : null,
            label: const Text('Set PIN'),
            icon: const Icon(Icons.pin),
            onPressed: () {
              showDialog(
                context: context,
                builder: (context) => FidoPinDialog(node.path, state),
              );
            },
          ),
        OutlinedButton.icon(
          label: const Text('Options'),
          icon: const Icon(Icons.tune),
          onPressed: () {
            showBottomMenu(context, [
              if (state.hasPin)
                MenuAction(
                  text: 'Change PIN',
                  icon: const Icon(Icons.pin),
                  action: (context) {
                    showDialog(
                      context: context,
                      builder: (context) => FidoPinDialog(node.path, state),
                    );
                  },
                ),
              MenuAction(
                text: 'Reset FIDO',
                icon: const Icon(Icons.delete),
                action: (context) {
                  showDialog(
                    context: context,
                    builder: (context) => ResetDialog(node),
                  );
                },
              ),
            ]);
          },
        ),
      ];
}

class _PinEntryForm extends ConsumerStatefulWidget {
  final FidoState _state;
  final DeviceNode _deviceNode;
  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) {
      return 'PIN is blocked. Factory reset the FIDO application.';
    }
    if (_blocked) {
      return 'PIN temporarily blocked, remove and reinsert your YubiKey.';
    }
    if (_retries != null) {
      return 'Wrong PIN. $_retries attempts remaining.';
    }
    return null;<commit_msg>N attempts -> N attempt(s).<commit_after>import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../../app/message.dart';
import '../../app/models.dart';
import '../../app/views/app_page.dart';
import '../../app/views/graphics.dart';
import '../../app/views/message_page.dart';
import '../../theme.dart';
import '../models.dart';
import '../state.dart';
import 'pin_dialog.dart';
import 'reset_dialog.dart';

class FidoLockedPage extends ConsumerWidget {
  final DeviceNode node;
  final FidoState state;

  const FidoLockedPage(this.node, this.state, {super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    if (!state.hasPin) {
      if (state.bioEnroll != null) {
        return MessagePage(
          title: const Text('WebAuthn'),
          graphic: noFingerprints,
          header: 'No fingerprints',
          message: 'Set a PIN to register fingerprints',
          actions: _buildActions(context),
        );
      } else {
        return MessagePage(
          title: const Text('WebAuthn'),
          graphic: manageAccounts,
          header: state.credMgmt ? 'No discoverable accounts' : 'Ready to use',
          message:
              'Optionally set a PIN to protect access to your YubiKey\nRegister as a Security Key on websites',
          actions: _buildActions(context),
        );
      }
    }

    if (!state.credMgmt && state.bioEnroll == null) {
      return MessagePage(
        title: const Text('WebAuthn'),
        graphic: manageAccounts,
        header: 'Ready to use',
        message: 'Register as a Security Key on websites',
        actions: _buildActions(context),
      );
    }

    return AppPage(
      title: const Text('WebAuthn'),
      actions: _buildActions(context),
      child: Column(
        children: [
          _PinEntryForm(state, node),
        ],
      ),
    );
  }

  List<Widget> _buildActions(BuildContext context) => [
        if (!state.hasPin)
          OutlinedButton.icon(
            style: state.bioEnroll != null
                ? AppTheme.primaryOutlinedButtonStyle(context)
                : null,
            label: const Text('Set PIN'),
            icon: const Icon(Icons.pin),
            onPressed: () {
              showDialog(
                context: context,
                builder: (context) => FidoPinDialog(node.path, state),
              );
            },
          ),
        OutlinedButton.icon(
          label: const Text('Options'),
          icon: const Icon(Icons.tune),
          onPressed: () {
            showBottomMenu(context, [
              if (state.hasPin)
                MenuAction(
                  text: 'Change PIN',
                  icon: const Icon(Icons.pin),
                  action: (context) {
                    showDialog(
                      context: context,
                      builder: (context) => FidoPinDialog(node.path, state),
                    );
                  },
                ),
              MenuAction(
                text: 'Reset FIDO',
                icon: const Icon(Icons.delete),
                action: (context) {
                  showDialog(
                    context: context,
                    builder: (context) => ResetDialog(node),
                  );
                },
              ),
            ]);
          },
        ),
      ];
}

class _PinEntryForm extends ConsumerStatefulWidget {
  final FidoState _state;
  final DeviceNode _deviceNode;
  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) {
      return 'PIN is blocked. Factory reset the FIDO application.';
    }
    if (_blocked) {
      return 'PIN temporarily blocked, remove and reinsert your YubiKey.';
    }
    if (_retries != null) {
      return 'Wrong PIN. $_retries attempt(s) remaining.';
    }
    return null;
{'fraction_non_alphanumeric': 0.11560880829015543, 'fraction_numerical': 0.0002158894645941278, 'mean_word_length': 1.8853939582684522, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '904027', 'n_tokens_mistral': 2639, 'n_tokens_neox': 2569, 'n_words': 610}