/// <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));
        }
Beispiel #3
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>
            /// 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);
            }
 public MFAProvider(Polymath polymath)
 {
     Duo    = new DuoProvider(polymath);
     Okta   = new OktaProvider(polymath);
     PingID = new PingIDProvider(polymath);
     TOTP   = new TOTPProvider(polymath);
 }
Beispiel #6
0
        private static void TestTOTP()
        {
            Console.WriteLine("======================== TOTP Test ========================");
            Console.WriteLine("Creating new user with data:");
            var user = new Models.FakeUser("John", "hunter2", "");

            Console.WriteLine("Username: "******"Password: "******"ezauth_totp_test");

            user.TOTPSecret = totp.SecretKey;

            Console.WriteLine("TOTPSecret: " + user.TOTPSecret);

            Console.WriteLine("\nPlease scan this code into your authenticator: \n");

            QRCodeGenerator qrGenerator = new QRCodeGenerator();
            QRCodeData      qrCodeData  = qrGenerator.CreateQrCode(totp.GetOTPURI(), QRCodeGenerator.ECCLevel.M);
            AsciiQRCode     qrCode      = new AsciiQRCode(qrCodeData);

            string qrCodeAsAsciiArt = qrCode.GetGraphic(1, "  ", "██");

            Console.WriteLine(qrCodeAsAsciiArt);

            Console.WriteLine("\nYou now may enter codes from your authenticator to test");
            Console.WriteLine("Enter 'q' to stop.");

            string input;

            Console.Write("> ");
            while ((input = Console.ReadLine()) != "q")
            {
                var res = totp.Validate(user.username, input);
                if (res == AuthResult.VALIDATED)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Valid code!");
                    Console.ForegroundColor = ConsoleColor.White;
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Invalid code");
                    Console.ForegroundColor = ConsoleColor.White;
                }
                Console.Write("> ");
            }
        }
        /// <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);
                }
            }
        }
        /// <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);
            }
        }