예제 #1
0
        public async Task Invoke(HttpContext context)
        {
            string api_key = context.Request.Headers["api-key"];

            if (api_key != null)
            {
                string[] vals = api_key.Split(':');


                var key = ApiKeyManager.Find(vals[0]);

                if (key != null && key.secretKey == vals[1] && key.authorizedIP == context.Request.HttpContext.Connection.RemoteIpAddress.ToString())
                {
                    await _next.Invoke(context);
                }
                else
                {
                    context.Response.StatusCode = 401; //Unauthorized
                    return;
                }
            }
            else
            {
                // no authorization header
                context.Response.StatusCode = 401; //Unauthorized
                return;
            }
        }
예제 #2
0
        public static bool ValidateKey(string apiKey, string IPAddress)
        {
            var result = false;

            logger.Trace("Validating API Key. key={0}", apiKey);


            ApiKey key = ApiKeyManager.FindBySecretKey(apiKey);

            if (key != null)
            {
                logger.Debug("Key found: Key={0} IP={1} RequestIP={2}", key.secretKey, key.authorizedIP, IPAddress);
                if (IPAddress == key.authorizedIP)
                {
                    logger.Debug("IP authorized");
                    result = true;
                }
                else
                {
                    logger.Warn("IP address not authorized IP={0}", IPAddress);
                }
            }

            logger.Trace("End of validation API Key.");
            return(result);
        }
예제 #3
0
        public static List <string> getClaims(string apiKey)
        {
            ApiKey key = ApiKeyManager.FindBySecretKey(apiKey);

            if (key != null)
            {
                return(key.claims);
            }

            return(null);
        }
예제 #4
0
        public static ApiKey getKey(string apiKeyID)
        {
            ApiKey key = ApiKeyManager.Find(apiKeyID);

            if (key != null)
            {
                logger.Debug("Key found: Key={0} ", key.keyID);
                return(key);
            }

            return(null);
        }
        protected override Task <AuthenticateResult> HandleAuthenticateAsync()
        {
            if (!Request.Headers.ContainsKey("api-key"))
            {
                return(Task.FromResult(AuthenticateResult.Fail("Missing api-key Header")));
            }

            string api_key = Request.Headers["api-key"];

            if (api_key != null)
            {
                string[] vals = api_key.Split(':');


                var key = ApiKeyManager.Find(vals[0]);

                if (key != null && key.secretKey == vals[1] && key.authorizedIP == Request.HttpContext.Connection.RemoteIpAddress.ToString())
                {
                    const string Issuer = "https://fgv.br";
                    var          claims = new List <Claim>();

                    claims.Add(new Claim(ClaimTypes.Name, key.keyID, ClaimValueTypes.String, Issuer));

                    List <string> tclaims = HttpSecurity.getClaims(key.secretKey);

                    foreach (string claim in tclaims)
                    {
                        claims.Add(new Claim(claim, "true", ClaimValueTypes.Boolean));
                    }


                    var identity  = new ClaimsIdentity(claims, Scheme.Name);
                    var principal = new ClaimsPrincipal(identity);
                    var ticket    = new AuthenticationTicket(principal, Scheme.Name);


                    return(Task.FromResult(AuthenticateResult.Success(ticket)));
                }
                else
                {
                    _logger.LogDebug("Invalid api-key or IP address ip:" + Request.HttpContext.Connection.RemoteIpAddress.ToString() + " key:" + api_key);
                    // FAILED
                    return(Task.FromResult(AuthenticateResult.Fail("Invalid api-key or IP address")));
                }
            }
            else
            {
                // FAILED
                return(Task.FromResult(AuthenticateResult.Fail("Invalid api-key")));
            }
        }
예제 #6
0
        public static string getKeyID(string apiKey)
        {
            string result = "";

            ApiKey key = ApiKeyManager.FindBySecretKey(apiKey);

            if (key != null)
            {
                logger.Debug("Key found: Key={0} ", key.secretKey);
                result = key.keyID;
            }

            return(result);
        }