コード例 #1
0
        public static OtpAuthData uriToOtpAuthData(Uri uri)
        {
            if (uri.Scheme == "otpauth")
            {
                OtpAuthData data = new OtpAuthData();
                data.Type = (OtpType)Enum.Parse(typeof(OtpType), uri.Host, true);

                string query = uri.Query;
                if (query.StartsWith("?"))
                {
                    query = query.TrimStart('?');
                }

                NameValueCollection parameters = ParseQueryString(query);
                if (parameters[uriSecretKey] != null)
                {
                    if (data.Type == OtpType.Totp && parameters[KeeOtp1EncoderParameter] != null)
                    {
                        data.Type = (OtpType)Enum.Parse(typeof(OtpType), parameters[KeeOtp1EncoderParameter], true);
                    }

                    data.Encoding = OtpSecretEncoding.Base32;

                    string secret = correctPlainSecret(parameters[uriSecretKey], data.Encoding);

                    // Validate secret (catch)
                    OtpAuthUtils.validatePlainSecret(secret, data.Encoding);

                    data.SetPlainSecret(secret);

                    if (parameters[uriAlgorithmKey] != null)
                    {
                        data.Algorithm = (OtpHashMode)Enum.Parse(typeof(OtpHashMode), parameters[uriAlgorithmKey], true);
                    }

                    if (data.Type == OtpType.Totp)
                    {
                        data.Period = GetIntOrDefault(parameters, uriPeriodKey, 30);
                    }
                    else if (data.Type == OtpType.Hotp)
                    {
                        data.Counter = GetIntOrDefault(parameters, uriCounterKey, 0);
                    }

                    data.Digits = GetIntOrDefault(parameters, uriDigitsKey, 6);

                    return(data);
                }
                else
                {
                    throw new InvalidUriFormat("The Uri does not contain a secret. A secret is required!");
                }
            }
            else
            {
                throw new InvalidUriFormat("Given Uri does not start with 'otpauth://'!");
            }
        }
コード例 #2
0
        private OtpAuthData readData(bool skipType = false)
        {
            if (OtpAuthUtils.checkUriString(textBoxKey.Text))
            {
                OtpAuthData data = OtpAuthUtils.uriToOtpAuthData(new Uri(textBoxKey.Text));
                if (this.data != null)
                {
                    data.loadedFields = this.data.loadedFields;
                }
                return(data);
            }
            else
            {
                OtpAuthData data = new OtpAuthData();

                if (this.data != null)
                {
                    data = (OtpAuthData)this.data.Clone();
                }

                string secret = textBoxKey.Text.Replace(" ", string.Empty).Replace("-", string.Empty);
                if (string.IsNullOrEmpty(this.textBoxKey.Text))
                {
                    throw new InvalidOtpConfiguration(KeeOtp2Statics.InvalidOtpConfigurationMissingSecret);
                }

                if (checkBoxCustomSettings.Checked)
                {
                    if (this.radioButtonBase32.Checked)
                    {
                        data.Encoding = OtpSecretEncoding.Base32;
                    }
                    else if (this.radioButtonBase64.Checked)
                    {
                        data.Encoding = OtpSecretEncoding.Base64;
                    }
                    else if (this.radioButtonHex.Checked)
                    {
                        data.Encoding = OtpSecretEncoding.Hex;
                    }
                    else if (this.radioButtonUtf8.Checked)
                    {
                        data.Encoding = OtpSecretEncoding.UTF8;
                    }
                }

                // Secret validation, will throw an error if invalid
                secret = OtpAuthUtils.correctPlainSecret(secret, data.Encoding);
                OtpAuthUtils.validatePlainSecret(secret, data.Encoding);

                if (checkBoxCustomSettings.Checked)
                {
                    if (!skipType)
                    {
                        data.Type = comboBoxTypeIndexValue[comboBoxType.SelectedIndex];
                    }

                    if (data.Type == OtpType.Totp || data.Type == OtpType.Steam)
                    {
                        int period = 30;
                        if (int.TryParse(this.textBoxPeriodCounter.Text, out period))
                        {
                            if (period <= 0)
                            {
                                throw new InvalidOtpConfiguration(KeeOtp2Statics.InvalidOtpConfigurationInvalidInteger);
                            }
                        }
                        else
                        {
                            throw new InvalidOtpConfiguration(KeeOtp2Statics.InvalidOtpConfigurationInvalidInteger);
                        }
                        data.Period = period;
                    }
                    else if (data.Type == OtpType.Hotp)
                    {
                        int counter = 0;
                        if (int.TryParse(this.textBoxPeriodCounter.Text, out counter))
                        {
                            if (counter < 0)
                            {
                                throw new InvalidOtpConfiguration(KeeOtp2Statics.InvalidOtpConfigurationInvalidInteger);
                            }
                        }
                        else
                        {
                            throw new InvalidOtpConfiguration(KeeOtp2Statics.InvalidOtpConfigurationInvalidInteger);
                        }
                        data.Counter = counter;
                    }

                    data.Digits = comboBoxLengthIndexValue[comboBoxLength.SelectedIndex];

                    if (this.radioButtonSha1.Checked)
                    {
                        data.Algorithm = OtpHashMode.Sha1;
                    }
                    else if (this.radioButtonSha256.Checked)
                    {
                        data.Algorithm = OtpHashMode.Sha256;
                    }
                    else if (this.radioButtonSha512.Checked)
                    {
                        data.Algorithm = OtpHashMode.Sha512;
                    }

                    data.KeeOtp1Mode = checkboxOldKeeOtp.Checked;
                }

                data.SetPlainSecret(secret);

                return(data);
            }
        }