/// <summary>
        /// Takes specified header name by index:
        /// </summary>
        /// <param name="headerIndex"></param>
        /// <returns></returns>
        public string GetHeaderNameByIndex(int headerIndex)
        {
            _wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.ClassName("board-header")));

            string header = Headers.ElementAt(headerIndex).Text;

            return(header);
        }
Пример #2
0
        public JToken AsJObject()
        {
            JArray joTable = new JArray();

            foreach (IEnumerable <string> record in Records)
            {
                JObject joRecord = new JObject();
                for (int h = 0; h < Headers.Count(); h++)
                {
                    joRecord.Add(Headers.ElementAt(h), record.ElementAt(h));
                }
                joTable.Add(joRecord);
            }

            return(joTable);
        }
        private static bool TryRetrieveToken(HttpRequestMessage request, out string token)
        {
            token = null;
            IEnumerable <string> Headers;

            if (!request.Headers.TryGetValues("Authorization", out Headers) || Headers.Count() > 1)
            {
                return(false);
            }


            // revisa si el token tiene la palabra bearer, como es usado en la convencion y si no pues simpemente lo iguala al token en si
            var bearerToken = Headers.ElementAt(0);

            token = bearerToken.StartsWith("Bearer ") ? bearerToken.Substring(7) : bearerToken;
            return(true);
        }
Пример #4
0
        public async Task Invoke(HttpContext httpContext)
        {
            StringValues Headers;

            if (!httpContext.Request.Headers.TryGetValue("Authorization", out Headers) || Headers.Count() > 1)
            {
                httpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                throw new NotImplementedException("Authorization Key Empty");
            }

            var authHeader = Headers.ElementAt(0);

            if (authHeader.StartsWith(Constants.Auth.Basic) && authHeader != null)
            {
                var encodedCredentials = authHeader.Substring(6);

                if (!string.IsNullOrWhiteSpace(encodedCredentials))
                {
                    // Get the encoded username and password
                    var encodedUsernamePassword = authHeader.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries)[1]?.Trim();
                    // Decode from Base64 to string
                    var decodedUsernamePassword = Encoding.UTF8.GetString(Convert.FromBase64String(encodedUsernamePassword));
                    // Split username and password
                    var username = decodedUsernamePassword.Split(':', 2)[0];
                    var password = decodedUsernamePassword.Split(':', 2)[1];
                    // Check if login is correct
                    if (IsAuthorized(username, password))
                    {
                        await _next.Invoke(httpContext);

                        return;
                    }
                }
            }
            else if (authHeader.StartsWith(Constants.Auth.Jwt) && authHeader != null)
            {
                var token = authHeader.Substring(7);

                if (!string.IsNullOrWhiteSpace(token))
                {
                    var tokenValidationParameters = new TokenValidationParameters
                    {
                        // Validar la firma del emisor
                        ValidateIssuerSigningKey = true,
                        IssuerSigningKey         = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["SECRET_KEY"])),

                        // Validar el emisor JWT (iss)
                        ValidateIssuer = true,
                        ValidIssuer    = _configuration["ISSUER"],

                        // Validar la audiencia JWT (aud)
                        ValidateAudience = false,
                        ValidAudience    = _configuration["AUDIENCE"],

                        // Validar la caducidad del token
                        ValidateLifetime = true,
                    };
                    try
                    {
                        SecurityToken           validatedToken;
                        JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler();
                        handler.ValidateToken(token, tokenValidationParameters, out validatedToken);
                    }
                    catch (SecurityTokenExpiredException)
                    {
                        throw new SecurityTokenExpiredException();
                    }
                    catch (SecurityTokenInvalidIssuerException)
                    {
                        throw new SecurityTokenInvalidIssuerException();
                    };

                    await _next.Invoke(httpContext);

                    return;
                }
            }

            httpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
            throw new NotImplementedException("Request Headers Invalid");
        }