Beispiel #1
0
 public override void Run()
 {
     try
     {
         if (App.Kp2a.LastOpenedEntry == null)
         {
             return;                     //DB was locked
         }
         Dictionary <string, string> entryFields = App.Kp2a.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
             TOTPProvider prov = new TOTPProvider(totpData.Settings);
             string       totp = prov.Generate(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(TotpKey, 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))
                {
                    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>
        /// Copies the specified entry's generated TOTP to the clipboard using the KeePass's clipboard function.
        /// </summary>
        /// <param name="pe">Password Entry.</param>
        internal void TOTPCopyToClipboard(PwEntry pe)
        {
            if (TOTPEntryValidator.HasSeed(pe))
            {
                if (TOTPEntryValidator.SettingsValidate(pe))
                {
                    string[] settings = TOTPEntryValidator.SettingsGet(pe);

                    TOTPProvider totpGenerator = new TOTPProvider(settings, this.TimeCorrections);

                    string invalidCharacters;
                    if (TOTPEntryValidator.SeedValidate(pe, out invalidCharacters))
                    {
                        pe.Touch(false);

                        string totp = totpGenerator.Generate(TOTPEntryValidator.SeedGet(pe).ReadString().ExtWithoutSpaces());

                        ClipboardUtil.CopyAndMinimize(totp, true, PluginHost.MainWindow, pe, PluginHost.MainWindow.ActiveDatabase);
                        PluginHost.MainWindow.StartClipboardCountdown();
                    }
                    else
                    {
                        MessageService.ShowWarning(Localization.Strings.ErrorBadSeed + invalidCharacters.ExtWithParenthesis().ExtWithSpaceBefore());
                    }
                    if (totpGenerator.TimeCorrectionError)
                    {
                        MessageService.ShowWarning(Localization.Strings.WarningBadURL);
                    }
                }
                else
                {
                    MessageService.ShowWarning(Localization.Strings.ErrorBadSettings);
                }
            }
            else
            {
                MessageService.ShowWarning(Localization.Strings.ErrorNoSeed);
            }
        }