2020-05-09 22:16:16 +00:00
|
|
|
|
using System;
|
2021-02-18 05:44:54 +00:00
|
|
|
|
using System.Collections.Concurrent;
|
2020-05-09 22:16:16 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
using System.Text.Encodings.Web;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
using Newtonsoft.Json;
|
2020-06-14 13:13:29 +00:00
|
|
|
|
using Wabbajack.Common;
|
|
|
|
|
using Wabbajack.Common.Serialization.Json;
|
2020-05-09 22:16:16 +00:00
|
|
|
|
using Wabbajack.Server.DataLayer;
|
2020-06-14 13:13:29 +00:00
|
|
|
|
using Wabbajack.Server.DTOs;
|
2021-02-18 05:44:54 +00:00
|
|
|
|
using Wabbajack.Server.Services;
|
2020-05-09 22:16:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Wabbajack.BuildServer
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public class ApiKeyAuthenticationOptions : AuthenticationSchemeOptions
|
|
|
|
|
{
|
|
|
|
|
public const string DefaultScheme = "API Key";
|
|
|
|
|
public string Scheme => DefaultScheme;
|
|
|
|
|
public string AuthenticationType = DefaultScheme;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ApiKeyAuthenticationHandler : AuthenticationHandler<ApiKeyAuthenticationOptions>
|
|
|
|
|
{
|
|
|
|
|
private const string ProblemDetailsContentType = "application/problem+json";
|
|
|
|
|
private readonly SqlService _sql;
|
2021-04-11 22:04:12 +00:00
|
|
|
|
public const string ApiKeyHeaderName = "X-Api-Key";
|
2020-05-09 22:16:16 +00:00
|
|
|
|
|
2021-02-18 05:44:54 +00:00
|
|
|
|
private MetricsKeyCache _keyCache;
|
|
|
|
|
|
2020-05-09 22:16:16 +00:00
|
|
|
|
public ApiKeyAuthenticationHandler(
|
|
|
|
|
IOptionsMonitor<ApiKeyAuthenticationOptions> options,
|
|
|
|
|
ILoggerFactory logger,
|
|
|
|
|
UrlEncoder encoder,
|
|
|
|
|
ISystemClock clock,
|
2021-02-18 05:44:54 +00:00
|
|
|
|
MetricsKeyCache keyCache,
|
2020-05-09 22:16:16 +00:00
|
|
|
|
SqlService db) : base(options, logger, encoder, clock)
|
|
|
|
|
{
|
|
|
|
|
_sql = db;
|
2021-02-18 05:44:54 +00:00
|
|
|
|
_keyCache = keyCache;
|
2020-05-09 22:16:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
|
|
|
|
|
{
|
2020-06-14 13:13:29 +00:00
|
|
|
|
var metricsKey = Request.Headers[Consts.MetricsKeyHeader].FirstOrDefault();
|
2021-02-04 03:48:30 +00:00
|
|
|
|
// Never needed this, disabled for now
|
|
|
|
|
//await LogRequest(metricsKey);
|
2020-06-14 13:13:29 +00:00
|
|
|
|
if (metricsKey != default)
|
|
|
|
|
{
|
2021-03-04 12:50:40 +00:00
|
|
|
|
await _keyCache.AddKey(metricsKey);
|
2020-06-14 13:13:29 +00:00
|
|
|
|
if (await _sql.IsTarKey(metricsKey))
|
|
|
|
|
{
|
2021-02-17 05:46:05 +00:00
|
|
|
|
await _sql.IngestMetric(new Metric
|
|
|
|
|
{
|
|
|
|
|
Action = "TarKey",
|
|
|
|
|
Subject = "Auth",
|
|
|
|
|
MetricsKey = metricsKey,
|
|
|
|
|
Timestamp = DateTime.UtcNow
|
|
|
|
|
});
|
2020-06-14 13:13:29 +00:00
|
|
|
|
await Task.Delay(TimeSpan.FromSeconds(60));
|
|
|
|
|
throw new Exception("Error, lipsum timeout of the cross distant cloud.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-16 22:21:01 +00:00
|
|
|
|
var authorKey = Request.Headers[ApiKeyHeaderName].FirstOrDefault();
|
2021-02-17 05:46:05 +00:00
|
|
|
|
|
2021-04-11 22:04:12 +00:00
|
|
|
|
if (authorKey == null)
|
|
|
|
|
Request.Cookies.TryGetValue(ApiKeyHeaderName, out authorKey);
|
|
|
|
|
|
|
|
|
|
|
2020-06-16 22:21:01 +00:00
|
|
|
|
if (authorKey == null && metricsKey == null)
|
2020-05-09 22:16:16 +00:00
|
|
|
|
{
|
|
|
|
|
return AuthenticateResult.NoResult();
|
|
|
|
|
}
|
2021-02-17 05:46:05 +00:00
|
|
|
|
|
2020-05-09 22:16:16 +00:00
|
|
|
|
|
2020-06-16 22:21:01 +00:00
|
|
|
|
if (authorKey != null)
|
2020-05-09 22:16:16 +00:00
|
|
|
|
{
|
2020-06-16 22:21:01 +00:00
|
|
|
|
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);
|
2020-05-09 22:16:16 +00:00
|
|
|
|
|
2020-06-16 22:21:01 +00:00
|
|
|
|
return AuthenticateResult.Success(ticket);
|
|
|
|
|
}
|
2021-02-18 05:44:54 +00:00
|
|
|
|
|
2020-05-09 22:16:16 +00:00
|
|
|
|
|
2021-02-18 05:44:54 +00:00
|
|
|
|
if (!await _keyCache.IsValidKey(metricsKey))
|
2020-05-09 22:16:16 +00:00
|
|
|
|
{
|
2020-06-16 22:21:01 +00:00
|
|
|
|
return AuthenticateResult.Fail("Invalid Metrics Key");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-02-04 03:48:30 +00:00
|
|
|
|
var claims = new List<Claim> {new(ClaimTypes.Role, "User")};
|
2020-05-09 22:16:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-14 13:13:29 +00:00
|
|
|
|
[JsonName("RequestLog")]
|
|
|
|
|
public class RequestLog
|
|
|
|
|
{
|
|
|
|
|
public string Path { get; set; }
|
|
|
|
|
public string Query { get; set; }
|
|
|
|
|
public Dictionary<string, string[]> Headers { get; set; }
|
|
|
|
|
}
|
|
|
|
|
private async Task LogRequest(string metricsKey)
|
|
|
|
|
{
|
|
|
|
|
var action = new RequestLog {
|
|
|
|
|
Path = Request.Path,
|
|
|
|
|
Query = Request.QueryString.Value,
|
|
|
|
|
Headers = Request.Headers.GroupBy(s => s.Key)
|
|
|
|
|
.ToDictionary(s => s.Key, s => s.SelectMany(v => v.Value).ToArray())
|
|
|
|
|
};
|
2020-06-16 22:21:01 +00:00
|
|
|
|
var ip = Request.Headers["CF-Connecting-IP"].FirstOrDefault() ??
|
|
|
|
|
Request.Headers["X-Forwarded-For"].FirstOrDefault() ??
|
|
|
|
|
Request.HttpContext.Connection.RemoteIpAddress.ToString();
|
|
|
|
|
await _sql.IngestAccess(ip, action.ToJson());
|
2020-06-14 13:13:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-09 22:16:16 +00:00
|
|
|
|
protected override async Task HandleChallengeAsync(AuthenticationProperties properties)
|
|
|
|
|
{
|
|
|
|
|
Response.StatusCode = 401;
|
|
|
|
|
Response.ContentType = ProblemDetailsContentType;
|
|
|
|
|
await Response.WriteAsync("Unauthorized");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override async Task HandleForbiddenAsync(AuthenticationProperties properties)
|
|
|
|
|
{
|
|
|
|
|
Response.StatusCode = 403;
|
|
|
|
|
Response.ContentType = ProblemDetailsContentType;
|
|
|
|
|
await Response.WriteAsync("forbidden");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class ApiKeyAuthorizationHandlerExtensions
|
|
|
|
|
{
|
|
|
|
|
public static AuthenticationBuilder AddApiKeySupport(this AuthenticationBuilder authenticationBuilder, Action<ApiKeyAuthenticationOptions> options)
|
|
|
|
|
{
|
|
|
|
|
return authenticationBuilder.AddScheme<ApiKeyAuthenticationOptions, ApiKeyAuthenticationHandler>(ApiKeyAuthenticationOptions.DefaultScheme, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|