-
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.
- Loading branch information
Showing
7 changed files
with
110 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System.Net; | ||
|
||
namespace AiTestimonials.Authorization; | ||
|
||
public class ApiKeyMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
private readonly IApiKeyValidation _apiKeyValidation; | ||
|
||
private readonly List<string> _utilityEndpoints = | ||
[ | ||
"/health/ready", | ||
"/health/live" | ||
]; | ||
|
||
public ApiKeyMiddleware(RequestDelegate next, IApiKeyValidation apiKeyValidation) | ||
{ | ||
_next = next; | ||
_apiKeyValidation = apiKeyValidation; | ||
} | ||
public async Task InvokeAsync(HttpContext context) | ||
{ | ||
if (_utilityEndpoints.Contains(context.Request.Path)) | ||
{ | ||
await _next(context); | ||
return; | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(context.Request.Headers[AuthConstants.ApiKeyHeaderName])) | ||
{ | ||
context.Response.StatusCode = (int)HttpStatusCode.BadRequest; | ||
return; | ||
} | ||
string? userApiKey = context.Request.Headers[AuthConstants.ApiKeyHeaderName]; | ||
if (!_apiKeyValidation.IsValidApiKey(userApiKey!)) | ||
{ | ||
context.Response.StatusCode = (int)HttpStatusCode.Unauthorized; | ||
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace AiTestimonials.Authorization; | ||
|
||
internal sealed class ApiKeyValidation(IConfiguration configuration) : IApiKeyValidation | ||
{ | ||
public bool IsValidApiKey(string userApiKey) | ||
{ | ||
if (string.IsNullOrWhiteSpace(userApiKey)) | ||
{ | ||
return false; | ||
} | ||
|
||
var apiKey = configuration.GetValue<string>(AuthConstants.ApiKeyName); | ||
return apiKey != null && apiKey == userApiKey; | ||
} | ||
} |
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,7 @@ | ||
namespace AiTestimonials.Authorization; | ||
|
||
public static class AuthConstants | ||
{ | ||
public const string ApiKeyHeaderName = "x-api-key"; | ||
public const string ApiKeyName = "API_KEY"; | ||
} |
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 AiTestimonials.Authorization; | ||
|
||
public interface IApiKeyValidation | ||
{ | ||
bool IsValidApiKey(string userApiKey); | ||
} |
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 |
---|---|---|
|
@@ -11,5 +11,6 @@ | |
"PostgresDatabase": "", | ||
"PostgresUser": "", | ||
"PostgresHost": "" | ||
} | ||
}, | ||
"API_KEY": "developer" | ||
} |