-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement api key authentication
- Loading branch information
1 parent
31721e0
commit f3e5d47
Showing
4 changed files
with
48 additions
and
0 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
35 changes: 35 additions & 0 deletions
35
src/NibiruConnector/Middlewares/ApiKeyAuthenticationMiddleware.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,35 @@ | ||
namespace NibiruConnector.Middlewares; | ||
|
||
public class ApiKeyAuthenticationMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
|
||
public ApiKeyAuthenticationMiddleware(RequestDelegate next) | ||
{ | ||
_next = next; | ||
} | ||
|
||
public async Task InvokeAsync(HttpContext context) | ||
{ | ||
if (string.IsNullOrEmpty(Configuration.ApiKey)) | ||
{ | ||
await _next(context); | ||
return; | ||
} | ||
|
||
context.Request.Headers.TryGetValue("ApiKey", out var apiKey); | ||
if (apiKey.Count != 1) | ||
{ | ||
context.Response.StatusCode = StatusCodes.Status401Unauthorized; | ||
return; | ||
} | ||
|
||
if (apiKey[0] != Configuration.ApiKey) | ||
{ | ||
context.Response.StatusCode = StatusCodes.Status401Unauthorized; | ||
return; | ||
} | ||
|
||
await _next(context); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -10,6 +10,8 @@ | |
|
||
var app = builder.Build(); | ||
|
||
app.UseApiKeyAuthentication(); | ||
|
||
app.MapControllers(); | ||
|
||
await app.RunAsync(); |