Exemplo n.º 1
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));
                    OutputField.AppendText(string.Format("Url: {0}", response.Url));
                }
                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);
            }
        }
Exemplo n.º 2
0
        /// <inheritdoc/>
        public async Task <bool> VerifyAsync(string otp, CancellationToken ct)
        {
            var client = new YubicoDotNetClient.YubicoClient(this.Configuration.UserName, this.Configuration.Password);

            try
            {
                var response = await client.VerifyAsync(otp)
                               .ConfigureAwait(false);

                if (response?.Status == YubicoResponseStatus.Ok)
                {
                    return(true);
                }
            }
            catch (Exception ex)
            {
                this.Logger.Error(ex, "Yubico Exception");
            }

            return(false);
        }
        public YubicoResponse(string response, string url)
        {
            var    reader = new StringReader(response);
            string line;

            var responseMap = new SortedDictionary <string, string>();

            ResponseMap = responseMap;

            while ((line = reader.ReadLine()) != null)
            {
                var unhandled = false;
                var parts     = line.Split(new[] { '=' }, 2);

                switch (parts[0])
                {
                case "h":
                    H = parts[1];
                    break;

                case "t":
                    T = parts[1];
                    break;

                case "status":
                    var statusCode = parts[1];
                    if (statusCode.Equals("EMPTY", StringComparison.OrdinalIgnoreCase))
                    {
                        Status = YubicoResponseStatus.Empty;
                    }
                    else if (statusCode.Equals("OK", StringComparison.OrdinalIgnoreCase))
                    {
                        Status = YubicoResponseStatus.Ok;
                    }
                    else if (statusCode.Equals("BAD_OTP", StringComparison.OrdinalIgnoreCase))
                    {
                        Status = YubicoResponseStatus.BadOtp;
                    }
                    else if (statusCode.Equals("REPLAYED_OTP", StringComparison.OrdinalIgnoreCase))
                    {
                        Status = YubicoResponseStatus.ReplayedOtp;
                    }
                    else if (statusCode.Equals("BAD_SIGNATURE", StringComparison.OrdinalIgnoreCase))
                    {
                        Status = YubicoResponseStatus.BadSignature;
                    }
                    else if (statusCode.Equals("MISSING_PARAMETER", StringComparison.OrdinalIgnoreCase))
                    {
                        Status = YubicoResponseStatus.MissingParameter;
                    }
                    else if (statusCode.Equals("NO_SUCH_CLIENT", StringComparison.OrdinalIgnoreCase))
                    {
                        Status = YubicoResponseStatus.NoSuchClient;
                    }
                    else if (statusCode.Equals("OPERATION_NOT_ALLOWED", StringComparison.OrdinalIgnoreCase))
                    {
                        Status = YubicoResponseStatus.OperationNotAllowed;
                    }
                    else if (statusCode.Equals("BACKEND_ERROR", StringComparison.OrdinalIgnoreCase))
                    {
                        Status = YubicoResponseStatus.BackendError;
                    }
                    else if (statusCode.Equals("NOT_ENOUGH_ANSWERS", StringComparison.OrdinalIgnoreCase))
                    {
                        Status = YubicoResponseStatus.NotEnoughAnswers;
                    }
                    else if (statusCode.Equals("REPLAYED_REQUEST", StringComparison.OrdinalIgnoreCase))
                    {
                        Status = YubicoResponseStatus.ReplayedRequest;
                    }
                    else
                    {
                        throw new ArgumentException("Response doesn't look like a validation response.");
                    }
                    break;

                case "timestamp":
                    Timestamp = int.Parse(parts[1]);
                    break;

                case "sessioncounter":
                    SessionCounter = int.Parse(parts[1]);
                    break;

                case "sessionuse":
                    UseCounter = int.Parse(parts[1]);
                    break;

                case "sl":
                    Sync = parts[1];
                    break;

                case "otp":
                    Otp = parts[1];
                    break;

                case "nonce":
                    Nonce = parts[1];
                    break;

                default:
                    unhandled = true;
                    break;
                }
                if (!unhandled)
                {
                    responseMap.Add(parts[0], parts[1]);
                }
            }
            if (Status == YubicoResponseStatus.Empty)
            {
                throw new ArgumentException("Response doesn't look like a validation response.");
            }

            if (Otp != null && Otp.Length > 32 && YubicoClient.IsOtpValidFormat(Otp))
            {
                PublicId = Otp.Substring(0, Otp.Length - 32);
            }

            Url = url;
        }