Esempio n. 1
0
        public async Task <Result> LogMetricAsync(string subject, string value)
        {
            var date       = DateTime.UtcNow;
            var metricsKey = Request.Headers[Consts.MetricsKeyHeader].FirstOrDefault();

            if (metricsKey != null)
            {
                await _keyCache.AddKey(metricsKey);
            }

            // Used in tests
            if (value == "Default" || value == "untitled" || subject == "failed_download" || Guid.TryParse(value, out _))
            {
                return new Result {
                           Timestamp = date
                }
            }
            ;

            await Log(date, subject, value, metricsKey);

            return(new Result {
                Timestamp = date
            });
        }
Esempio n. 2
0
        protected override async Task <AuthenticateResult> HandleAuthenticateAsync()
        {
            var metricsKey = Request.Headers[Consts.MetricsKeyHeader].FirstOrDefault();

            // Never needed this, disabled for now
            //await LogRequest(metricsKey);
            if (metricsKey != default)
            {
                await _keyCache.AddKey(metricsKey);

                if (await _sql.IsTarKey(metricsKey))
                {
                    await _sql.IngestMetric(new Metric
                    {
                        Action     = "TarKey",
                        Subject    = "Auth",
                        MetricsKey = metricsKey,
                        Timestamp  = DateTime.UtcNow
                    });

                    await Task.Delay(TimeSpan.FromSeconds(60));

                    throw new Exception("Error, lipsum timeout of the cross distant cloud.");
                }
            }

            var authorKey = Request.Headers[ApiKeyHeaderName].FirstOrDefault();

            if (authorKey == null && metricsKey == null)
            {
                return(AuthenticateResult.NoResult());
            }


            if (authorKey != null)
            {
                var owner = await _sql.LoginByApiKey(authorKey);

                if (owner == null)
                {
                    return(AuthenticateResult.Fail("Invalid author key"));
                }

                var claims = new List <Claim> {
                    new Claim(ClaimTypes.Name, owner)
                };

                claims.Add(new Claim(ClaimTypes.Role, "Author"));
                claims.Add(new Claim(ClaimTypes.Role, "User"));

                var identity   = new ClaimsIdentity(claims, Options.AuthenticationType);
                var identities = new List <ClaimsIdentity> {
                    identity
                };
                var principal = new ClaimsPrincipal(identities);
                var ticket    = new AuthenticationTicket(principal, Options.Scheme);

                return(AuthenticateResult.Success(ticket));
            }


            if (!await _keyCache.IsValidKey(metricsKey))
            {
                return(AuthenticateResult.Fail("Invalid Metrics Key"));
            }
            else
            {
                var claims = new List <Claim> {
                    new(ClaimTypes.Role, "User")
                };


                var identity   = new ClaimsIdentity(claims, Options.AuthenticationType);
                var identities = new List <ClaimsIdentity> {
                    identity
                };
                var principal = new ClaimsPrincipal(identities);
                var ticket    = new AuthenticationTicket(principal, Options.Scheme);

                return(AuthenticateResult.Success(ticket));
            }
        }