-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45fc018
commit 88b01a0
Showing
11 changed files
with
84 additions
and
19 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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using GuildWarsPartySearch.Server.Options; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using Microsoft.Extensions.Options; | ||
using System.Core.Extensions; | ||
using System.Extensions; | ||
|
||
namespace GuildWarsPartySearch.Server.Filters | ||
{ | ||
public class ApiWhitelistFilter : IAsyncActionFilter | ||
{ | ||
private const string XApiKeyHeaderKey = "X-Api-Key"; | ||
|
||
private readonly ApiWhitelistOptions apiWhitelistOptions; | ||
private readonly ILogger<ApiWhitelistFilter> logger; | ||
|
||
public ApiWhitelistFilter( | ||
IOptions<ApiWhitelistOptions> options, | ||
ILogger<ApiWhitelistFilter> logger) | ||
{ | ||
this.apiWhitelistOptions = options.ThrowIfNull().Value.ThrowIfNull(); | ||
this.logger = logger.ThrowIfNull(); | ||
} | ||
|
||
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) | ||
{ | ||
var address = context.HttpContext.Connection.RemoteIpAddress?.ToString(); | ||
var scopedLogger = this.logger.CreateScopedLogger(nameof(this.OnActionExecutionAsync), address ?? string.Empty); | ||
scopedLogger.LogDebug($"Received request"); | ||
if (context.HttpContext.Request.Headers.TryGetValue(XApiKeyHeaderKey, out var xApiKeyvalues)) | ||
{ | ||
scopedLogger.LogDebug($"X-Api-Key {string.Join(',', xApiKeyvalues.Select(s => s))}"); | ||
} | ||
|
||
if (xApiKeyvalues.None(k => k == this.apiWhitelistOptions.Key)) | ||
{ | ||
context.Result = new ForbiddenResponseActionResult("Forbidden"); | ||
return; | ||
} | ||
|
||
await next(); | ||
} | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace GuildWarsPartySearch.Server.Options; | ||
|
||
public class ApiWhitelistOptions | ||
{ | ||
public string? Key { get; set; } | ||
} |