public TotpData GetTotpData(IDictionary <string, string> entryFields, Context ctx, bool muteWarnings)
        {
            TotpData res = new TotpData();
            string   data;

            if (!entryFields.TryGetValue("otp", out data))
            {
                return(res);
            }

            string otpUriStart = "otpauth://totp/";

            if (!data.StartsWith(otpUriStart))
            {
                return(res);
            }


            try
            {
                Uri myUri       = new Uri(data);
                var parsedQuery = HttpUtility.ParseQueryString(myUri.Query);
                res.TotpSeed = parsedQuery.Get("secret");
                res.Length   = parsedQuery.Get("digits");
                res.Duration = parsedQuery.Get("period");
            }
            catch (Exception)
            {
                return(res);
            }

            res.IsTotpEnry = true;
            return(res);
        }
Beispiel #2
0
        public ITotpPluginAdapter TryGetAdapter(PwEntryOutput entry)
        {
            if (entry == null)
            {
                return(null);
            }

            try
            {
                foreach (ITotpPluginAdapter adapter in _pluginAdapters)
                {
                    TotpData totpData = adapter.GetTotpData(
                        App.Kp2a.LastOpenedEntry.OutputStrings.ToDictionary(pair => StrUtil.SafeXmlString(pair.Key),
                                                                            pair => pair.Value.ReadString()), LocaleManager.LocalizedAppContext, false);
                    if (totpData.IsTotpEntry)
                    {
                        return(adapter);
                    }
                }
            }
            catch (Exception e)
            {
                Kp2aLog.LogUnexpectedError(e);
            }


            return(null);
        }
Beispiel #3
0
        /// <summary>
        /// Instanciates a new TOTP_Generator.
        /// </summary>
        /// <param name="initDuration">Duration of generation of each totp, in seconds.</param>
        /// <param name="initLength">Length of the generated totp.</param>
        /// <param name="initEncoder">The output encoder.</param>

        /*public TOTPProvider(int initDuration, int initLength, Func<byte[], int, string> initEncoder)
         * {
         *  this.Duration = initDuration;
         *  this.Length = initLength;
         *  this.encoder = initEncoder;
         *  this.TimeCorrection = TimeSpan.Zero;
         * }*/

        /// <summary>
        /// Instanciates a new TOTP_Generator.
        /// </summary>
        /// <param name="initSettings">Saved Settings.</param>
        public TOTPProvider(TotpData data)
        {
            this.duration = Convert.ToInt16(data.Duration);

            if (data.Encoder == TotpData.EncoderSteam)
            {
                this.length  = 5;
                this.encoder = TOTPEncoder.steam;
            }
            else
            {
                this.length  = Convert.ToInt16(data.Length);
                this.encoder = TOTPEncoder.rfc6238;
            }

            if (data.TimeCorrectionUrl != null)
            {
                {
                    this.TimeCorrection      = TimeSpan.Zero;
                    this.timeCorrectionError = false;
                }
            }
            else
            {
                this.TimeCorrection = TimeSpan.Zero;
            }

            this.HashAlgorithm = data.HashAlgorithm;
        }
        //END TWO FACTOR AUTHENTICATION



        //TOTP CONFIGURATION/SETUP
        public async Task <IActionResult> SetUpTotp(TotpData model)
        {
            User user = await userManager.GetUserAsync(HttpContext.User);

            if (user == null)
            {
                user = await userManager.FindByIdAsync(HttpContext.Session.GetString("Id"));
            }
            if (user.TotpConfigured == false)
            {
                HttpContext.Session.SetString("Id", user.Id);
                string randomKey            = RandomString(25);
                var    totpSetupGenerator   = new TotpSetupGenerator();
                var    totpSetup            = totpSetupGenerator.Generate("SportsAdministrationApp", user.Name, randomKey, 300, 300);
                string qrCodeImageUrl       = totpSetup.QrCodeImage;
                string manualEntrySetupCode = totpSetup.ManualSetupKey;

                user.QrCodeUrl      = qrCodeImageUrl;
                user.TotpSetupCode  = manualEntrySetupCode;
                user.randomKey      = randomKey;
                user.TotpConfigured = true;
            }
            //to pass data into View
            TotpData dta = new TotpData
            {
                TotpSetupCode = user.TotpSetupCode,
                QrCodeUrl     = user.QrCodeUrl
            };

            model.TotpSetupCode = user.TotpSetupCode;
            model.QrCodeUrl     = user.QrCodeUrl;
            await userManager.UpdateAsync(user);

            return(View(dta));
        }
        public TotpData GetTotpData(IDictionary <string, string> entryFields, Context ctx, bool muteWarnings)
        {
            TotpData res = new TotpData();
            string   data;

            if (!entryFields.TryGetValue("otp", out data))
            {
                return(res);
            }

            string otpUriStart = "otpauth://totp/";

            if (!data.StartsWith(otpUriStart))
            {
                return(res);
            }


            try
            {
                Uri myUri       = new Uri(data);
                var parsedQuery = HttpUtility.ParseQueryString(myUri.Query);
                res.TotpSeed = parsedQuery.Get("secret");
                res.Length   = parsedQuery.Get("digits");
                res.Duration = parsedQuery.Get("period");
                res.Encoder  = parsedQuery.Get("encoder");
                string algo = parsedQuery.Get("algorithm");
                if (algo == "SHA512")
                {
                    res.HashAlgorithm = TotpData.HashSha512;
                }
                if (algo == "SHA256")
                {
                    res.HashAlgorithm = TotpData.HashSha256;
                }


                //set defaults according to https://github.com/google/google-authenticator/wiki/Key-Uri-Format
                if (res.Length == null)
                {
                    res.Length = "6";
                }
                if (res.Duration == null)
                {
                    res.Duration = "30";
                }
                if (res.Encoder == null)
                {
                    res.Encoder = TotpData.EncoderRfc6238;
                }
            }
            catch (Exception e)
            {
                return(res);
            }

            res.IsTotpEntry = true;
            return(res);
        }
 public void OnOpenEntry()
 {
     foreach (ITotpPluginAdapter adapter in _pluginAdapters)
     {
         TotpData totpData = adapter.GetTotpData(App.Kp2a.GetDb().LastOpenedEntry.OutputStrings.ToDictionary(pair => StrUtil.SafeXmlString(pair.Key), pair => pair.Value.ReadString()), Application.Context, false);
         if (totpData.IsTotpEnry)
         {
             new UpdateTotpTimerTask(Application.Context, adapter).Run();
         }
     }
 }
Beispiel #7
0
        public ITotpPluginAdapter TryGetAdapter(PwEntryOutput entry)
        {
            if (entry == null)
            {
                return(null);
            }
            foreach (ITotpPluginAdapter adapter in _pluginAdapters)
            {
                TotpData totpData = adapter.GetTotpData(App.Kp2a.LastOpenedEntry.OutputStrings.ToDictionary(pair => StrUtil.SafeXmlString(pair.Key), pair => pair.Value.ReadString()), Application.Context, false);
                if (totpData.IsTotpEnry)
                {
                    return(adapter);
                }
            }

            return(null);
        }
Beispiel #8
0
        public TotpData TryGetTotpData(PwEntryOutput entry)
        {
            if (entry == null)
            {
                return(null);
            }
            foreach (ITotpPluginAdapter adapter in _pluginAdapters)
            {
                TotpData totpData = adapter.GetTotpData(entry.OutputStrings.ToDictionary(pair => StrUtil.SafeXmlString(pair.Key), pair => pair.Value.ReadString()), LocaleManager.LocalizedAppContext, false);
                if (totpData.IsTotpEntry)
                {
                    return(totpData);
                }
            }

            return(null);
        }
Beispiel #9
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);
                res.Length = GetIntOrDefault(parameters, SizeParameter, 6);

                res.IsTotpEnry = true;
                return res;
            }
            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 = Convert.ToInt16(Settings[0]);
                        res.Length = Convert.ToInt16(Settings[1]);
                        if (ValidUrl)
                        {
                            NoTimeCorrection = true;
                            /*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.IsTotpEnry = 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;
            }