forked from Szops/gdzie-jest-szop
-
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.
45 authentication of backend data updating exception handling (Szops#50)
* Added API Key * Minor changes
- Loading branch information
Showing
6 changed files
with
104 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace SzopAPI.Attributes | ||
{ | ||
[AttributeUsage(validOn: AttributeTargets.Class)] | ||
public class ApiKeyAttribute : Attribute, IAsyncActionFilter | ||
{ | ||
private const string APIKEYNAME = "ApiKey"; | ||
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) | ||
{ | ||
if (!context.HttpContext.Request.Headers.TryGetValue(APIKEYNAME, out var extractedApiKey)) | ||
{ | ||
context.Result = new ContentResult() | ||
{ | ||
StatusCode = 401, | ||
Content = "Api Key was not provided" | ||
}; | ||
return; | ||
} | ||
|
||
var appSettings = context.HttpContext.RequestServices.GetRequiredService<IConfiguration>(); | ||
|
||
var apiKey = appSettings.GetValue<string>(APIKEYNAME); | ||
|
||
if (!apiKey.Equals(extractedApiKey)) | ||
{ | ||
context.Result = new ContentResult() | ||
{ | ||
StatusCode = 401, | ||
Content = "Api Key is not valid" | ||
}; | ||
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
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 |
---|---|---|
@@ -1 +1 @@ | ||
27.03.2022 16:24:38 | ||
03.04.2022 20:29:09 |
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 |
---|---|---|
|
@@ -5,5 +5,6 @@ | |
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
"AllowedHosts": "*", | ||
"ApiKey" : "szop" | ||
} |