public TotpData GetTotpData(IDictionary <string, string> entryFields) { TotpData res = new TotpData(); if (SettingsCheck(entryFields) && SeedCheck(entryFields)) { bool ValidInterval; bool ValidLength; bool ValidUrl; if (SettingsValidate(entryFields, out ValidInterval, out ValidLength, out ValidUrl)) { bool NoTimeCorrection = false; string[] Settings = SettingsGet(entryFields); res.Duration = Settings[0]; res.Length = Settings[1]; if (res.Length == "S") { res.Encoder = TotpData.EncoderSteam; } if (ValidUrl) { NoTimeCorrection = true; res.TimeCorrectionUrl = Settings[2]; /*var CurrentTimeCorrection = TimeCorrections[Settings[2]]; * if (CurrentTimeCorrection != null) * { * TotpGenerator.TimeCorrection = CurrentTimeCorrection.TimeCorrection; * } * else * { * TotpGenerator.TimeCorrection = TimeSpan.Zero; * NoTimeCorrection = true; * }*/ } string InvalidCharacters; if (SeedValidate(entryFields, out InvalidCharacters)) { res.IsTotpEntry = true; res.TotpSeed = SeedGet(entryFields).ExtWithoutSpaces(); } else { ShowWarning("Bad seed!" + InvalidCharacters.ExtWithParenthesis().ExtWithSpaceBefore()); } if (NoTimeCorrection) { ShowWarning("Warning: TOTP Time correction not implemented!"); } } else { ShowWarning("Bad settings!"); } } else { //no totp entry } return(res); }
/// <summary> /// Copies the specified entry's generated TOTP to the clipboard using the KeePass's clipboard function. /// </summary> /// <param name="pe">Password Entry.</param> private void TOTPCopyToClipboard(PwEntry pe) { if (SettingsCheck(pe) && SeedCheck(pe)) { bool ValidInterval; bool ValidLength; bool ValidUrl; if (SettingsValidate(pe, out ValidInterval, out ValidLength, out ValidUrl)) { string[] Settings = SettingsGet(pe); TOTPProvider TOTPGenerator = new TOTPProvider(Settings, ref this.TimeCorrections); string InvalidCharacters; if (SeedValidate(pe, out InvalidCharacters)) { pe.Touch(false); string totp = TOTPGenerator.Generate(SeedGet(pe).ReadString().ExtWithoutSpaces()); ClipboardUtil.CopyAndMinimize(totp, true, m_host.MainWindow, pe, m_host.MainWindow.ActiveDatabase); m_host.MainWindow.StartClipboardCountdown(); } else { MessageService.ShowWarning(TrayTOTP_Plugin_Localization.strWarningBadSeed + InvalidCharacters.ExtWithParenthesis().ExtWithSpaceBefore()); } if (TOTPGenerator.TimeCorrectionError) { MessageService.ShowWarning(TrayTOTP_Plugin_Localization.strWarningBadUrl); } } else { MessageService.ShowWarning(TrayTOTP_Plugin_Localization.strWarningBadSet); } } else { MessageService.ShowWarning(TrayTOTP_Plugin_Localization.strWarningNotSet); } }
/// <summary> /// Auto-Type Function. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SprEngine_FilterCompile(object sender, SprEventArgs e) { if ((e.Context.Flags & SprCompileFlags.ExtActive) == SprCompileFlags.ExtActive) { if (e.Text.IndexOf(m_host.CustomConfig.GetString(setname_string_AutoType_FieldName, setdef_string_AutoType_FieldName).ExtWithBrackets(), StringComparison.InvariantCultureIgnoreCase) >= 0) { if (SettingsCheck(e.Context.Entry) && SeedCheck(e.Context.Entry)) { bool ValidInterval = false; bool ValidLength = false; bool ValidUrl = false; if (SettingsValidate(e.Context.Entry, out ValidInterval, out ValidLength, out ValidUrl)) { string[] Settings = SettingsGet(e.Context.Entry); TOTPProvider TOTPGenerator = new TOTPProvider(Settings, ref this.TimeCorrections); string InvalidCharacters; if (SeedValidate(e.Context.Entry, out InvalidCharacters)) { e.Context.Entry.Touch(false); string totp = TOTPGenerator.GenerateByByte(Base32.Decode(SeedGet(e.Context.Entry).ReadString().ExtWithoutSpaces())); e.Text = StrUtil.ReplaceCaseInsensitive(e.Text, m_host.CustomConfig.GetString(setname_string_AutoType_FieldName, setdef_string_AutoType_FieldName).ExtWithBrackets(), totp); } else { e.Text = string.Empty; MessageService.ShowWarning(TrayTOTP_Plugin_Localization.strWarningBadSeed + InvalidCharacters.ExtWithParenthesis().ExtWithSpaceBefore()); } if (TOTPGenerator.TimeCorrectionError) { MessageService.ShowWarning(TrayTOTP_Plugin_Localization.strWarningBadUrl); } } else { e.Text = string.Empty; MessageService.ShowWarning(TrayTOTP_Plugin_Localization.strWarningBadSet); } } else { e.Text = string.Empty; MessageService.ShowWarning(TrayTOTP_Plugin_Localization.strWarningNotSet); } } } }
/// <summary> /// Copies the specified entry's generated TOTP to the clipboard using the KeePass's clipboard function. /// </summary> /// <param name="pe">Password Entry.</param> private void TotpCopyToClipboard(PwEntry pe) { if (SettingsCheck(pe) && SeedCheck(pe)) { bool ValidInterval; bool ValidLength; bool ValidUrl; if (SettingsValidate(pe, out ValidInterval, out ValidLength, out ValidUrl)) { bool NoTimeCorrection = false; string[] Settings = SettingsGet(pe); var TotpGenerator = new Totp_Provider(Convert.ToInt16(Settings[0]), Convert.ToInt16(Settings[1])); if (ValidUrl) { var CurrentTimeCorrection = TimeCorrections[Settings[2]]; if (CurrentTimeCorrection != null) { TotpGenerator.TimeCorrection = CurrentTimeCorrection.TimeCorrection; } else { TotpGenerator.TimeCorrection = TimeSpan.Zero; NoTimeCorrection = true; } } string InvalidCharacters; if (SeedValidate(pe, out InvalidCharacters)) { pe.Touch(false); ClipboardUtil.CopyAndMinimize(TotpGenerator.Generate(Base32.Decode(SeedGet(pe).ReadString().ExtWithoutSpaces())), true, m_host.MainWindow, pe, m_host.MainWindow.ActiveDatabase); m_host.MainWindow.StartClipboardCountdown(); } else { MessageService.ShowWarning(TrayTotp_Plugin_Localization.strWarningBadSeed + InvalidCharacters.ExtWithParenthesis().ExtWithSpaceBefore()); } if (NoTimeCorrection) { MessageService.ShowWarning(TrayTotp_Plugin_Localization.strWarningBadUrl); } } else { MessageService.ShowWarning(TrayTotp_Plugin_Localization.strWarningBadSet); } } else { MessageService.ShowWarning(TrayTotp_Plugin_Localization.strWarningNotSet); } }
/// <summary> /// Auto-Type Function. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SprEngine_FilterCompile(object sender, SprEventArgs e) { if ((e.Context.Flags & SprCompileFlags.ExtActive) == SprCompileFlags.ExtActive) { if (e.Text.IndexOf(m_host.CustomConfig.GetString(setname_string_AutoType_FieldName, setdef_string_AutoType_FieldName).ExtWithBrackets(), StringComparison.InvariantCultureIgnoreCase) >= 0) { if (SettingsCheck(e.Context.Entry) && SeedCheck(e.Context.Entry)) { bool ValidInterval = false; bool ValidLength = false; bool ValidUrl = false; if (SettingsValidate(e.Context.Entry, out ValidInterval, out ValidLength, out ValidUrl)) { bool NoTimeCorrection = false; string[] Settings = SettingsGet(e.Context.Entry); var TotpGenerator = new Totp_Provider(Convert.ToInt16(Settings[0]), Convert.ToInt16(Settings[1])); if (ValidUrl) { var CurrentTimeCorrection = TimeCorrections[Settings[2]]; if (CurrentTimeCorrection != null) { TotpGenerator.TimeCorrection = CurrentTimeCorrection.TimeCorrection; } else { TotpGenerator.TimeCorrection = TimeSpan.Zero; NoTimeCorrection = true; } } string InvalidCharacters; if (SeedValidate(e.Context.Entry, out InvalidCharacters)) { e.Context.Entry.Touch(false); e.Text = StrUtil.ReplaceCaseInsensitive(e.Text, m_host.CustomConfig.GetString(setname_string_AutoType_FieldName, setdef_string_AutoType_FieldName).ExtWithBrackets(), TotpGenerator.Generate(Base32.Decode(SeedGet(e.Context.Entry).ReadString().ExtWithoutSpaces()))); } else { e.Text = string.Empty; MessageService.ShowWarning(TrayTotp_Plugin_Localization.strWarningBadSeed + InvalidCharacters.ExtWithParenthesis().ExtWithSpaceBefore()); } if (NoTimeCorrection) { MessageService.ShowWarning(TrayTotp_Plugin_Localization.strWarningBadUrl); } } else { e.Text = string.Empty; MessageService.ShowWarning(TrayTotp_Plugin_Localization.strWarningBadSet); } } else { e.Text = string.Empty; MessageService.ShowWarning(TrayTotp_Plugin_Localization.strWarningNotSet); } } } }