mirror of
https://github.com/yawaflua/yaflay.ru.git
synced 2025-12-11 16:16:26 +02:00
hotfix redirect system
add auth to create posts and redirects
This commit is contained in:
73
Auth/ApiKeyAuthorization.cs
Normal file
73
Auth/ApiKeyAuthorization.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
using System.Security.Claims;
|
||||
using System.Text.Encodings.Web;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Options;
|
||||
using yaflay.ru.Database.Tables;
|
||||
using yaflay.ru.Models;
|
||||
using yaflay.ru.Models.Tables;
|
||||
|
||||
|
||||
namespace yaflay.ru.Auth;
|
||||
|
||||
public class ApiKeyAuthantication : AuthenticationHandler<AuthenticationSchemeOptions>
|
||||
{
|
||||
private AppDbContext ctx;
|
||||
private IMemoryCache cache;
|
||||
public ApiKeyAuthantication(
|
||||
IOptionsMonitor<AuthenticationSchemeOptions> options,
|
||||
ILoggerFactory logger,
|
||||
UrlEncoder encoder,
|
||||
ISystemClock clock,
|
||||
AppDbContext ctx,
|
||||
IMemoryCache cache
|
||||
) : base(options, logger, encoder, clock)
|
||||
{
|
||||
this.ctx = ctx;
|
||||
this.cache = cache;
|
||||
}
|
||||
|
||||
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
|
||||
{
|
||||
if (!Request.Headers.TryGetValue("Authorization", out var apiKeyHeaderValues))
|
||||
return AuthenticateResult.Fail("API Key was not provided.");
|
||||
|
||||
string? providedApiKey = apiKeyHeaderValues.FirstOrDefault()?.Replace("Bearer ", "");
|
||||
Console.WriteLine("APIKEY: " + providedApiKey);
|
||||
|
||||
if (FindApiKey(providedApiKey, out ApiKey? apiKey))
|
||||
{
|
||||
var claims = new[]
|
||||
{
|
||||
new Claim("Bearer", apiKey.Type.ToString())
|
||||
};
|
||||
|
||||
var identity = new ClaimsIdentity(claims, Scheme.Name);
|
||||
var principal = new ClaimsPrincipal(identity);
|
||||
var ticket = new AuthenticationTicket(principal, Scheme.Name);
|
||||
|
||||
return AuthenticateResult.Success(ticket);
|
||||
}
|
||||
Console.WriteLine("im there");
|
||||
return AuthenticateResult.Fail("Invalid API Key provided.");
|
||||
}
|
||||
|
||||
private bool FindApiKey(string? providedApiKey, out ApiKey? apiKey)
|
||||
{
|
||||
var fromCache = cache.Get<ApiKey>($"apiKey-{providedApiKey}");
|
||||
if (fromCache == null)
|
||||
{
|
||||
Console.WriteLine($"Im there: {fromCache}, {providedApiKey}");
|
||||
apiKey = ctx.ApiKeys.Find(providedApiKey);
|
||||
if (apiKey != null)
|
||||
{
|
||||
cache.Set($"apiKey-{providedApiKey}", (object)apiKey, DateTime.Now.AddMinutes(10));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
apiKey = fromCache;
|
||||
}
|
||||
return ctx.ApiKeys.Any(k => k.Key == providedApiKey);
|
||||
}
|
||||
}
|
||||
9
Auth/ApiKeyTypes.cs
Normal file
9
Auth/ApiKeyTypes.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace yaflay.ru.Auth
|
||||
{
|
||||
|
||||
public enum ApiKeyTypes
|
||||
{
|
||||
Public,
|
||||
Private
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user