mirror of
https://github.com/yawaflua/PL_JusticeBot.git
synced 2025-12-10 04:19:31 +02:00
add discord bot
change net6.0 net7.0
This commit is contained in:
8
Auth/ApiKeyTypes.cs
Normal file
8
Auth/ApiKeyTypes.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace DiscordApp.Auth
|
||||
{
|
||||
public enum ApiKeyTypes
|
||||
{
|
||||
Public,
|
||||
Private
|
||||
}
|
||||
}
|
||||
60
Auth/AuthanticationByBearerToken.cs
Normal file
60
Auth/AuthanticationByBearerToken.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using DiscordApp.Database;
|
||||
using DiscordApp.Database.Tables;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Collections.Specialized;
|
||||
using System.Security.Claims;
|
||||
using System.Text.Encodings.Web;
|
||||
|
||||
namespace DiscordApp.Auth
|
||||
{
|
||||
public class AuthanticationByBearerToken : AuthenticationHandler<AuthenticationSchemeOptions>
|
||||
{
|
||||
private AppDbContext dbContext;
|
||||
|
||||
public AuthanticationByBearerToken(
|
||||
IOptionsMonitor<AuthenticationSchemeOptions> options,
|
||||
ILoggerFactory logger,
|
||||
UrlEncoder encoder,
|
||||
ISystemClock clock,
|
||||
AppDbContext dbContext) : base(options, logger, encoder, clock)
|
||||
{
|
||||
this.dbContext = dbContext;
|
||||
}
|
||||
|
||||
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
|
||||
{
|
||||
if (!Request.Headers.TryGetValue("Authorization", out var apiKeyHeaderValues))
|
||||
{
|
||||
return AuthenticateResult.Fail("API Key was not provided.");
|
||||
}
|
||||
|
||||
#pragma warning disable CS8602 // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
|
||||
string providedApiKey = apiKeyHeaderValues
|
||||
.FirstOrDefault().Replace("'", "");
|
||||
if (CheckForInvalidCharacters(apiKeyHeaderValues)) return AuthenticateResult.Fail("Don`t use SQL injections, dog`s son");
|
||||
#pragma warning restore CS8602 // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
|
||||
|
||||
if (IsValidApiKey(providedApiKey))
|
||||
{
|
||||
var claims = new[] { new Claim("Bearer", providedApiKey) };
|
||||
var identity = new ClaimsIdentity(claims, Scheme.Name);
|
||||
var principal = new ClaimsPrincipal(identity);
|
||||
var ticket = new AuthenticationTicket(principal, Scheme.Name);
|
||||
|
||||
return AuthenticateResult.Success(ticket);
|
||||
}
|
||||
|
||||
return AuthenticateResult.Fail("Invalid API Key provided.");
|
||||
}
|
||||
|
||||
private bool IsValidApiKey(string apiKey)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
private bool CheckForInvalidCharacters(string value)
|
||||
{
|
||||
return value.IndexOfAny(";".ToCharArray()) != -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user