Esempio n. 1
0
        public override void Run()
        {
            try
            {
                if (App.Kp2a.GetDb().LastOpenedEntry == null)
                    return; //DB was locked

                Dictionary<string, string> entryFields = App.Kp2a.GetDb().LastOpenedEntry.OutputStrings.ToDictionary(pair => StrUtil.SafeXmlString(pair.Key), pair => pair.Value.ReadString());
                //mute warnings to avoid repeated display of the toasts
                TotpData totpData = _adapter.GetTotpData(entryFields, _context, true /*mute warnings*/);
                if (totpData.IsTotpEnry)
                {
                    //generate a new totp
                    Totp_Provider prov = new Totp_Provider(totpData.Duration, totpData.Length);
                    string totp = prov.Generate(Base32.Decode(totpData.TotpSeed));
                    //update entry and keyboard
                    UpdateEntryData(totp);
                    //broadcast new field value (update EntryActivity). this might result in another keyboard
                    //update, but that's inexpensive and relatively rare
                    BroadcastNewTotp(totp);
                    //restart timer
                    new Timer().Schedule(new UpdateTotpTimerTask(_context, _adapter), 1000 * prov.Timer);
                }
            }
            catch (Exception e)
            {
                Android.Util.Log.Debug(_totp, e.ToString());
            }
        }
 /// <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);
             }
         }
     }
 }