示例#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());
     }
 }
示例#2
0
            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);
            }
        public TotpData GetTotpData(IDictionary <string, string> entryFields, Context ctx, bool muteWarnings)
        {
            TotpData res = new TotpData();

            byte[] pbSecret = (GetOtpSecret(entryFields, "TimeOtp-") ?? MemUtil.EmptyByteArray);
            if (pbSecret == null)
            {
                return(res);
            }

            string strPeriod;
            uint   uPeriod = 0;

            if (entryFields.TryGetValue("TimeOtp-Period", out strPeriod))
            {
                uint.TryParse(strPeriod, out uPeriod);
            }

            res.IsTotpEntry = true;

            if (uPeriod == 0)
            {
                uPeriod = 30U;
            }

            string strLength;
            uint   uLength = 0;

            if (entryFields.TryGetValue("TimeOtp-Length", out strLength))
            {
                uint.TryParse(strLength, out uLength);
            }


            if (uLength == 0)
            {
                uLength = 6;
            }

            string strAlg;

            entryFields.TryGetValue("TimeOtp-Algorithm", out strAlg);


            res.TotpSecret = pbSecret;
            res.Length     = uLength.ToString();
            res.Duration   = uPeriod.ToString();

            return(res);
        }
示例#4
0
            public TotpData GetData()
            {
                TotpData res = new TotpData();
                string   data;

                if (!_entryFields.TryGetValue("otp", out data))
                {
                    return(res);
                }
                NameValueCollection parameters = ParseQueryString(data);

                if (parameters[KeyParameter] == null)
                {
                    return(res);
                }
                res.TotpSeed = parameters[KeyParameter];


                res.Duration = GetIntOrDefault(parameters, StepParameter, 30).ToString();
                res.Length   = GetIntOrDefault(parameters, SizeParameter, 6).ToString();

                res.IsTotpEnry = true;
                return(res);
            }