/// <summary>
        /// Tells KeePass what to display in the column.
        /// </summary>
        /// <param name="columnName"></param>
        /// <param name="pe"></param>
        /// <returns>String displayed in the columns.</returns>
        public override string GetCellData(string columnName, PwEntry pe)
        {
            if (columnName == null)
            {
                throw new ArgumentNullException("columnName");
            }

            if (pe == null)
            {
                throw new ArgumentNullException("pe");
            }

            if (_plugin.SettingsCheck(pe) && _plugin.SeedCheck(pe))
            {
                bool validInterval;
                bool validLength;
                bool validUrl;
                if (_plugin.SettingsValidate(pe, out validInterval, out validLength, out validUrl))
                {
                    string[] settings = _plugin.SettingsGet(pe);

                    TOTPProvider totpGenerator = new TOTPProvider(settings, ref _plugin.TimeCorrections);

                    if (_plugin.SeedValidate(pe))
                    {
                        return(totpGenerator.GenerateByByte(Base32.Decode(_plugin.SeedGet(pe).ReadString().ExtWithoutSpaces())) + (_pluginHost.CustomConfig.GetBool(KeeTrayTOTPExt.setname_bool_TOTPColumnTimer_Visible, true) ? totpGenerator.Timer.ToString().ExtWithParenthesis().ExtWithSpaceBefore() : string.Empty));
                    }
                    return(Localization.Strings.ErrorBadSeed);
                }
                return(Localization.Strings.ErrorBadSettings);
            }
            return(_plugin.SettingsCheck(pe) || _plugin.SeedCheck(pe) ? Localization.Strings.ErrorStorage : string.Empty);
        }
        private string GetInnerValueCode(PwEntry entry)
        {
            string[] settings      = _plugin.TOTPEntryValidator.SettingsGet(entry);
            var      totpGenerator = new TOTPProvider(settings, _plugin.TimeCorrections);

            return(totpGenerator.GenerateByByte(Base32.Decode(_plugin.TOTPEntryValidator.SeedGet(entry).ReadString().ExtWithoutSpaces())) + (_plugin.Settings.TOTPColumnTimerVisible ? totpGenerator.Timer.ToString().ExtWithParenthesis().ExtWithSpaceBefore() : string.Empty));
        }
            /// <summary>
            /// Tells KeePass what to display in the column.
            /// </summary>
            /// <param name="strColumnName"></param>
            /// <param name="pe"></param>
            /// <returns>String displayed in the columns.</returns>
            public override string GetCellData(string strColumnName, PwEntry pe)
            {
                if (strColumnName == null)
                {
                    throw new ArgumentNullException("strColumnName");
                }
                if (pe == null)
                {
                    throw new ArgumentNullException("pe");
                }
                if (plugin.SettingsCheck(pe) && plugin.SeedCheck(pe))
                {
                    bool ValidInterval;
                    bool ValidLength;
                    bool ValidUrl;
                    if (plugin.SettingsValidate(pe, out ValidInterval, out ValidLength, out ValidUrl))
                    {
                        string[] Settings = plugin.SettingsGet(pe);

                        TOTPProvider TOTPGenerator = new TOTPProvider(Settings, ref plugin.TimeCorrections);

                        if (plugin.SeedValidate(pe))
                        {
                            return(TOTPGenerator.GenerateByByte(
                                       Base32.Decode(plugin.SeedGet(pe).ReadString().ExtWithoutSpaces())) + (m_host.CustomConfig.GetBool(setname_bool_TOTPColumnTimer_Visible, true) ? TOTPGenerator.Timer.ToString().ExtWithParenthesis().ExtWithSpaceBefore() : string.Empty));
                        }
                        return(TrayTOTP_CustomColumn_Localization.strWarningBadSeed);
                    }
                    return(TrayTOTP_CustomColumn_Localization.strWarningBadSet);
                }
                return(plugin.SettingsCheck(pe) || plugin.SeedCheck(pe) ? TrayTOTP_CustomColumn_Localization.strWarningStorage : string.Empty);
            }
Beispiel #4
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.IsTotpEntry)
         {
             //generate a new totp
             TOTPProvider prov = new TOTPProvider(totpData);
             string       totp = prov.GenerateByByte(totpData.TotpSecret);
             //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>
        /// 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>
        /// 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 && e.Text.IndexOf(Settings.AutoTypeFieldName.ExtWithBrackets(), StringComparison.InvariantCultureIgnoreCase) >= 0)
            {
                if (TOTPEntryValidator.HasSeed(e.Context.Entry))
                {
                    if (TOTPEntryValidator.SettingsValidate(e.Context.Entry))
                    {
                        string[] settings = TOTPEntryValidator.SettingsGet(e.Context.Entry);

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

                        string invalidCharacters;

                        if (TOTPEntryValidator.SeedValidate(e.Context.Entry, out invalidCharacters))
                        {
                            e.Context.Entry.Touch(false);
                            string totp = totpGenerator.GenerateByByte(Base32.Decode(TOTPEntryValidator.SeedGet(e.Context.Entry).ReadString().ExtWithoutSpaces()));
                            e.Text = StrUtil.ReplaceCaseInsensitive(e.Text, Settings.AutoTypeFieldName.ExtWithBrackets(), totp);
                        }
                        else
                        {
                            e.Text = string.Empty;
                            MessageService.ShowWarning(Localization.Strings.ErrorBadSeed + invalidCharacters.ExtWithParenthesis().ExtWithSpaceBefore());
                        }
                        if (totpGenerator.TimeCorrectionError)
                        {
                            MessageService.ShowWarning(Localization.Strings.WarningBadURL);
                        }
                    }
                    else
                    {
                        e.Text = string.Empty;
                        MessageService.ShowWarning(Localization.Strings.ErrorBadSettings);
                    }
                }
                else
                {
                    e.Text = string.Empty;
                    MessageService.ShowWarning(Localization.Strings.ErrorNoSeed);
                }
            }
        }