-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(auth): initial draft for static API keys * fix(auth): api key authentification The ApiKeys section of the appsettings.json is now loaded correctly * fix(static): we're in Hamburg now * feat(auth): allow api key for sending PMs & fix NRE --------- Co-authored-by: Maakinoh <info@maakinoh.de>
- Loading branch information
Showing
7 changed files
with
131 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/Eurofurence.App.Server.Web/Identity/ApiKeyAuthenticationDefaults.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace Eurofurence.App.Server.Web.Identity; | ||
|
||
public class ApiKeyAuthenticationDefaults | ||
{ | ||
|
||
/// <summary> | ||
/// The default authentication scheme. | ||
/// </summary> | ||
public const string AuthenticationScheme = "ApiKey"; | ||
|
||
/// <summary> | ||
/// Header name for the API keys. | ||
/// </summary> | ||
public const string HeaderName = "X-API-Key"; | ||
} |
47 changes: 47 additions & 0 deletions
47
src/Eurofurence.App.Server.Web/Identity/ApiKeyAuthenticationHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
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.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Eurofurence.App.Server.Web.Identity; | ||
|
||
public class ApiKeyAuthenticationHandler : AuthenticationHandler<ApiKeyAuthenticationOptions> | ||
{ | ||
public ApiKeyAuthenticationHandler(IOptionsMonitor<ApiKeyAuthenticationOptions> options, ILoggerFactory logger, UrlEncoder encoder) : base(options, logger, encoder) { } | ||
|
||
protected override Task<AuthenticateResult> HandleAuthenticateAsync() | ||
{ | ||
var requestApiKey = Context.Request.Headers[ApiKeyAuthenticationDefaults.HeaderName].FirstOrDefault(); | ||
|
||
if (Options.ApiKeys is null || Options.ApiKeys.Count == 0 || requestApiKey is null) return Task.FromResult(AuthenticateResult.Fail("Invalid X-API-Key.")); | ||
|
||
Logger.LogDebug("Attempting API key authentication…"); | ||
|
||
if (Options.ApiKeys.FirstOrDefault(apiKey => apiKey.Key == requestApiKey && DateTime.Now.CompareTo(apiKey.ValidUntil) <= 0) is { } apiKeyOptions) | ||
{ | ||
Logger.LogInformation($"Configured API key for {apiKeyOptions.PrincipalName} with roles {string.Join(',', apiKeyOptions.Roles)} valid until {apiKeyOptions.ValidUntil}."); | ||
|
||
var claims = new List<Claim> | ||
{ | ||
new Claim(ClaimTypes.Name, apiKeyOptions.PrincipalName) | ||
}; | ||
|
||
foreach (var role in apiKeyOptions.Roles) | ||
{ | ||
claims.Add(new Claim(ClaimTypes.Role, role)); | ||
} | ||
|
||
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)); | ||
} | ||
|
||
return Task.FromResult(AuthenticateResult.Fail("Invalid X-API-Key.")); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Eurofurence.App.Server.Web/Identity/ApiKeyAuthenticationOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.AspNetCore.Authentication; | ||
|
||
namespace Eurofurence.App.Server.Web.Identity; | ||
|
||
public class ApiKeyAuthenticationOptions : AuthenticationSchemeOptions | ||
{ | ||
public IList<ApiKeyOptions> ApiKeys { get; set; } | ||
public class ApiKeyOptions { | ||
public string Key { get; set;} | ||
public string PrincipalName { get; set;} | ||
public DateTime ValidUntil { get; set;} | ||
public IList<string> Roles { get; set;} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters