internal IYubicoResponse Validate(string token, int userId = -1)
        {
            var clientId  = ConfigurationManager.AppSettings["YubiKey.ClientId"];
            var secretKey = ConfigurationManager.AppSettings["YubiKey.SecretKey"];
            var client    = new YubicoClient(clientId, secretKey);
            var database  = ApplicationContext.Current.DatabaseContext.Database;

            try
            {
                var response = client.Verify(token);
                if (response.Status == YubicoResponseStatus.Ok)
                {
                    //check that this specific user has registered this YubiKey
                    if (userId == -1)
                    {
                        return(response);
                    }

                    var result = database.Fetch <TwoFactor>(string.Format("WHERE [userId] = {0} AND [key] = '{1}' AND [confirmed] = 1",
                                                                          userId, Constants.YubiKeyProviderName));

                    if (result.Any(x => x.Value == response.PublicId))
                    {
                        return(response);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error <YubiKeyService>("Could not validate the provided one-time-password", ex);
            }

            return(null);
        }
        public IYubicoResponse Verify(string onetimePassword)
        {
            var client = new YubicoClient(this.ClientId);

            client.SetApiKey(this.ApiKey);
            client.SetSync(this.SyncLevel);
            return(client.Verify(onetimePassword));
        }
Пример #3
0
        private void Submit(object sender, EventArgs e)
        {
            var otp      = OtpInput.Text;
            var clientId = ClientIdInput.Text;
            var apiKey   = ApiKeyInput.Text;
            var sync     = SyncInput.Text;
            var nonce    = NonceInput.Text;

            OutputField.Clear();

            var client = new YubicoClient(clientId);

            if (!string.IsNullOrEmpty(apiKey))
            {
                client.SetApiKey(apiKey);
            }
            if (!string.IsNullOrEmpty(sync))
            {
                client.SetSync(sync);
            }
            if (!string.IsNullOrEmpty(nonce))
            {
                client.SetNonce(nonce);
            }
            try
            {
                var sw       = Stopwatch.StartNew();
                var response = client.Verify(otp);
                sw.Stop();
                if (response != null)
                {
                    OutputField.AppendText(string.Format("response in: {0}{1}", sw.ElapsedMilliseconds, Environment.NewLine));
                    OutputField.AppendText(string.Format("Status: {0}{1}", response.Status, Environment.NewLine));
                    OutputField.AppendText(string.Format("Public ID: {0}{1}", response.PublicId, Environment.NewLine));
                    OutputField.AppendText(string.Format("Use/Session Counter: {0} {1}{2}", response.UseCounter, response.SessionCounter, Environment.NewLine));
                }
                else
                {
                    OutputField.Text = "Null result returned, error in call";
                }
            }
            catch (YubicoValidationFailure yvf)
            {
                OutputField.Text = string.Format("Failure in validation: {0}{1}", yvf.Message, Environment.NewLine);
            }
        }