<commit_before>﻿using System;
using System.Windows.Forms;
using EnrollmentStation.Code;

namespace EnrollmentStation
{
    public partial class DlgResetPin : Form
    {
        private EnrolledYubikey _yubikey;

        public DlgResetPin(EnrolledYubikey key)
        {
            _yubikey = key;

            if (_yubikey == null)
                throw new ArgumentNullException("key");

            InitializeComponent();
        }

        public new DialogResult ShowDialog()
        {
            DlgPleaseInsertYubikey dialog = new DlgPleaseInsertYubikey(_yubikey);
            if (dialog.ShowDialog() != DialogResult.OK)
            {
                DialogResult = DialogResult.Cancel;
                return DialogResult;
            }

            return base.ShowDialog();
        }

        private void RefreshEligibilityForReset()
        {
            bool eligible = true;

            if (txtPinNew.Text.Length <= 0 || txtPinNew.Text.Length > 8)
                eligible = false;

            if (txtPinNew.Text != txtPinNewAgain.Text)
                eligible = false;

            cmdChange.Enabled = eligible;
        }

        private void DlgChangePin_Load(object sender, EventArgs e)
        {
            using (YubikeyNeoManager neo = new YubikeyNeoManager())
            {
                neo.RefreshDevice();

                int yubiSerial = neo.GetSerialNumber();
                lblSerialNumber.Text = yubiSerial.ToString();
            }
        }

        private void cmdChange_Click(object sender, EventArgs e)
        {
            using (YubikeyPivTool piv = new YubikeyPivTool())
            {
                bool authed = piv.Authenticate(_yubikey.ManagementKey);

                if (!authed)
                {
                    MessageBox.Show("Unable to authenticate.", "An error occurred.", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    return;
                }

                // Reset PIN
                piv.BlockPin();

                bool changed = piv.UnblockPin(_yubikey.PukKey, txtPinNew.Text);

                if (changed)
                {
                    MessageBox.Show("The PIN code has been reset.", "Success.", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    Close();
                }
                else
                    MessageBox.Show("An error occured while resetting the PIN code. Please try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void textField_Changed(object sender, EventArgs e)
        {
            RefreshEligibilityForReset();
        }
    }
}<commit_msg>Fix issue with Reset PIN not locking the Yubikey<commit_after>﻿using System;
using System.Windows.Forms;
using EnrollmentStation.Code;

namespace EnrollmentStation
{
    public partial class DlgResetPin : Form
    {
        private EnrolledYubikey _yubikey;

        public DlgResetPin(EnrolledYubikey key)
        {
            _yubikey = key;

            if (_yubikey == null)
                throw new ArgumentNullException("key");

            InitializeComponent();
        }

        public new DialogResult ShowDialog()
        {
            DlgPleaseInsertYubikey dialog = new DlgPleaseInsertYubikey(_yubikey);
            if (dialog.ShowDialog() != DialogResult.OK)
            {
                DialogResult = DialogResult.Cancel;
                return DialogResult;
            }

            return base.ShowDialog();
        }

        private void RefreshEligibilityForReset()
        {
            bool eligible = true;

            if (txtPinNew.Text.Length <= 0 || txtPinNew.Text.Length > 8)
                eligible = false;

            if (txtPinNew.Text != txtPinNewAgain.Text)
                eligible = false;

            cmdChange.Enabled = eligible;
        }

        private void DlgChangePin_Load(object sender, EventArgs e)
        {
            using (YubikeyNeoManager neo = new YubikeyNeoManager())
            {
                neo.RefreshDevice();

                int yubiSerial = neo.GetSerialNumber();
                lblSerialNumber.Text = yubiSerial.ToString();
            }
        }

        private void cmdChange_Click(object sender, EventArgs e)
        {
            using (YubikeyDetector.Instance.GetExclusiveLock())
            using (YubikeyPivTool piv = new YubikeyPivTool())
            {
                bool authed = piv.Authenticate(_yubikey.ManagementKey);

                if (!authed)
                {
                    MessageBox.Show("Unable to authenticate.", "An error occurred.", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    return;
                }

                // Reset PIN
                piv.BlockPin();

                bool changed = piv.UnblockPin(_yubikey.PukKey, txtPinNew.Text);

                if (changed)
                {
                    MessageBox.Show("The PIN code has been reset.", "Success.", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    Close();
                }
                else
                    MessageBox.Show("An error occured while resetting the PIN code. Please try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void textField_Changed(object sender, EventArgs e)
        {
            RefreshEligibilityForReset();
        }
    }
}
{'fraction_non_alphanumeric': 0.08078562164165277, 'fraction_numerical': 0.0007411524921252547, 'mean_word_length': 1.5402352941176471, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6415906', 'n_tokens_mistral': 1468, 'n_tokens_neox': 1364, 'n_words': 306}