From 548eca0aa4e0e755e63fafb40633bf59ad8d09e9 Mon Sep 17 00:00:00 2001 From: dnmh-admin <128390512+dnmh-admin@users.noreply.github.com> Date: Tue, 2 May 2023 16:11:31 +0200 Subject: [PATCH] Task/further refactorings (#7) * Renamed folders and updated readme * Updated release workflow to verbose logging * Fixed issue in solution file * Another fix --------- Co-authored-by: Pelle Stenild Coltau --- .github/workflows/release.yml | 2 +- Dnmh.Security.ApiKeyAuthentication.sln | 25 +++- README.md | 124 +++++++++++++++++- .../Controllers/TestApiKeyController.cs | 0 ...ecurity.ApiKeyAuthentication.Sample.csproj | 2 +- {sample => Samples}/Program.cs | 0 .../Properties/launchSettings.json | 0 .../Services/ApiKeyAuthenticationService.cs | 0 .../appsettings.Development.json | 0 {sample => Samples}/appsettings.json | 0 .../ApiKeyAuthenticationBuilder.cs | 0 .../ApiKeyAuthenticationEvents.cs | 0 .../ApiKeyAuthenticationExtensions.cs | 0 .../ApiKeyAuthenticationHandler.cs | 0 .../ApiKeyAuthenticationOptions.cs | 0 .../FailedAuthenticationException.cs | 0 .../IApiKeyAuthenticationService.cs | 0 .../ApiKeyAuthenticationOptionsValidator.cs | 0 .../Internal/ApiKeySchemeSwaggerOptions.cs | 0 .../PostConfigureSwaggerAuthorization.cs | 0 .../SimpleApiKeyAuthenticationService.cs | 0 .../Dnmh.Security.ApiKeyAuthentication.csproj | 2 +- .../ApiKeyAuthenticationHandlerTest.cs | 0 .../ApiKeyAuthenticationOptionsTest.cs | 0 .../AuthenticationHandlerExtensions.cs | 0 ....Security.ApiKeyAuthentication.Test.csproj | 2 +- {test => Tests}/MockHelpers.cs | 0 {test => Tests}/Usings.cs | 0 28 files changed, 146 insertions(+), 11 deletions(-) rename {sample => Samples}/Controllers/TestApiKeyController.cs (100%) rename {sample => Samples}/Dnmh.Security.ApiKeyAuthentication.Sample.csproj (83%) rename {sample => Samples}/Program.cs (100%) rename {sample => Samples}/Properties/launchSettings.json (100%) rename {sample => Samples}/Services/ApiKeyAuthenticationService.cs (100%) rename {sample => Samples}/appsettings.Development.json (100%) rename {sample => Samples}/appsettings.json (100%) rename {src => Source}/AuthenticationHandler/ApiKeyAuthenticationBuilder.cs (100%) rename {src => Source}/AuthenticationHandler/ApiKeyAuthenticationEvents.cs (100%) rename {src => Source}/AuthenticationHandler/ApiKeyAuthenticationExtensions.cs (100%) rename {src => Source}/AuthenticationHandler/ApiKeyAuthenticationHandler.cs (100%) rename {src => Source}/AuthenticationHandler/ApiKeyAuthenticationOptions.cs (100%) rename {src => Source}/AuthenticationHandler/FailedAuthenticationException.cs (100%) rename {src => Source}/AuthenticationHandler/IApiKeyAuthenticationService.cs (100%) rename {src => Source}/AuthenticationHandler/Internal/ApiKeyAuthenticationOptionsValidator.cs (100%) rename {src => Source}/AuthenticationHandler/Internal/ApiKeySchemeSwaggerOptions.cs (100%) rename {src => Source}/AuthenticationHandler/Internal/PostConfigureSwaggerAuthorization.cs (100%) rename {src => Source}/AuthenticationHandler/SimpleApiKeyAuthenticationService.cs (100%) rename {src => Source}/Dnmh.Security.ApiKeyAuthentication.csproj (100%) rename {test => Tests}/AuthenticationHandler/ApiKeyAuthenticationHandlerTest.cs (100%) rename {test => Tests}/AuthenticationHandler/ApiKeyAuthenticationOptionsTest.cs (100%) rename {test => Tests}/AuthenticationHandlerExtensions.cs (100%) rename {test => Tests}/Dnmh.Security.ApiKeyAuthentication.Test.csproj (92%) rename {test => Tests}/MockHelpers.cs (100%) rename {test => Tests}/Usings.cs (100%) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 1eaa9dd..8c1b7fb 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -25,7 +25,7 @@ jobs: - name: Test run: dotnet test --no-build --verbosity normal - name: Pack - run: dotnet pack + run: dotnet pack --verbosity detailed - name: Release run: dotnet nuget push ./src/bin/Release/Dnmh.Security.ApiKeyAuthentication.*.nupkg --api-key "${{ secrets.NUGET_API_KEY }}" --source https://api.nuget.org/v3/index.json diff --git a/Dnmh.Security.ApiKeyAuthentication.sln b/Dnmh.Security.ApiKeyAuthentication.sln index cf376d8..90ed0c8 100644 --- a/Dnmh.Security.ApiKeyAuthentication.sln +++ b/Dnmh.Security.ApiKeyAuthentication.sln @@ -3,17 +3,30 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.5.33502.453 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dnmh.Security.ApiKeyAuthentication", "src\Dnmh.Security.ApiKeyAuthentication.csproj", "{D351F140-C932-4E94-93BA-223EDD531E30}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dnmh.Security.ApiKeyAuthentication", "Source\Dnmh.Security.ApiKeyAuthentication.csproj", "{D351F140-C932-4E94-93BA-223EDD531E30}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dnmh.Security.ApiKeyAuthentication.Test", "test\Dnmh.Security.ApiKeyAuthentication.Test.csproj", "{DFEF603C-60C8-41B7-A3D5-0B470800A4C5}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dnmh.Security.ApiKeyAuthentication.Test", "Tests\Dnmh.Security.ApiKeyAuthentication.Test.csproj", "{DFEF603C-60C8-41B7-A3D5-0B470800A4C5}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dnmh.Security.ApiKeyAuthentication.Sample", "sample\Dnmh.Security.ApiKeyAuthentication.Sample.csproj", "{4D31192C-0339-43C1-B174-DC9B1E4535A0}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dnmh.Security.ApiKeyAuthentication.Sample", "Samples\Dnmh.Security.ApiKeyAuthentication.Sample.csproj", "{4D31192C-0339-43C1-B174-DC9B1E4535A0}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{F7255BAF-8084-453D-8BAF-950A2081361C}" +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Source", "Source", "{F7255BAF-8084-453D-8BAF-950A2081361C}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{1536AED4-4E33-4971-AB58-644266AC1CB3}" +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{1536AED4-4E33-4971-AB58-644266AC1CB3}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sample", "sample", "{81B9DF25-F242-43BE-90AF-FFF1C770E353}" +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{81B9DF25-F242-43BE-90AF-FFF1C770E353}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{6749E821-4761-4BC0-A4CB-8F8D2A0EDBB8}" + ProjectSection(SolutionItems) = preProject + .gitignore = .gitignore + LICENSE = LICENSE + README.md = README.md + EndProjectSection +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Workflows", "Workflows", "{D23C2BA8-A2A5-41DF-AE69-AE561682C67C}" + ProjectSection(SolutionItems) = preProject + .github\workflows\build.yml = .github\workflows\build.yml + .github\workflows\release.yml = .github\workflows\release.yml + EndProjectSection EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/README.md b/README.md index 864a6e4..0021c16 100644 --- a/README.md +++ b/README.md @@ -1 +1,123 @@ -# Dnmh.Security.ApiKeyAuthentication \ No newline at end of file +# Dnmh.Security.ApiKeyAuthentication + +*Note: The content of this readme has been entirely generated by [ChatGPT](https://chat.openai.com/).* + +## Overview + +`Dnmh.Security.ApiKeyAuthentication` is a .NET Core library that provides API key authentication for your web applications. + +## Installation + +You can install this library using NuGet. Simply run the following command: + +```bash +dotnet add package Dnmh.Security.ApiKeyAuthentication +``` + +## Usage + +To use this library, follow these steps: + +1. In your `Startup.cs` file, add the following code to the `ConfigureServices` method: + +```csharp +services.AddApiKeyAuthentication() + .AddSwaggerAuthorization("Description"); +``` + +2. In your controller or endpoint, add the `[Authorize(AuthenticationSchemes = "ApiKey")]` attribute to require an API key for that endpoint: + +```csharp +[HttpGet] +[Authorize(AuthenticationSchemes = "ApiKey")] +public IActionResult Get() +{ + // Your code here +} +``` + +That's it! Now your API endpoints will require an API key to access them, and Swagger UI will show an "Authorize" button that lets you enter your API key. + +## Example + +Here's an example of how to use this library in a .NET Core application: + +```csharp +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Dnmh.Security.ApiKeyAuthentication; + +namespace MyApp +{ + public class Startup + { + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + public void ConfigureServices(IServiceCollection services) + { + services.AddAuthentication(options => + { + options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; + options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; + }) + .AddJwtBearer(options => + { + // Your JWT configuration here + }); + + services.AddApiKeyAuthentication() + .AddSwaggerAuthorization("API Key Authorization"); + + services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0); + } + + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + app.UseAuthentication(); + app.UseRouting(); + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); + } + } + + [Route("api/[controller]")] + [ApiController] + public class MyController : ControllerBase + { + [HttpGet] + [Authorize(AuthenticationSchemes = "ApiKey")] + public IActionResult Get() + { + return Ok("Hello, world!"); + } + } +} + +public class ApiKeyAuthenticationService : IApiKeyAuthenticationService +{ + public bool ValidateApiKey(string apiKey) + { + return apiKey == "YOUR_API_KEY"; + } +} +``` + +In this example, the `AddApiKeyAuthentication` method is called with a generic type parameter `ApiKeyAuthenticationService` that implements the `IApiKeyAuthenticationService` interface with a `ValidateApiKey` method that checks if the provided API key matches the expected value. The `AddSwaggerAuthorization` method is called with a string parameter that specifies the description of the authorization method that will be shown in Swagger UI. The `[Authorize(AuthenticationSchemes = "ApiKey")]` attribute is added to the `Get` method of the `MyController` class to require an API key for that endpoint. + +## License + +This library is licensed under the [MIT License](LICENSE). \ No newline at end of file diff --git a/sample/Controllers/TestApiKeyController.cs b/Samples/Controllers/TestApiKeyController.cs similarity index 100% rename from sample/Controllers/TestApiKeyController.cs rename to Samples/Controllers/TestApiKeyController.cs diff --git a/sample/Dnmh.Security.ApiKeyAuthentication.Sample.csproj b/Samples/Dnmh.Security.ApiKeyAuthentication.Sample.csproj similarity index 83% rename from sample/Dnmh.Security.ApiKeyAuthentication.Sample.csproj rename to Samples/Dnmh.Security.ApiKeyAuthentication.Sample.csproj index 76ba45a..f94e4dc 100644 --- a/sample/Dnmh.Security.ApiKeyAuthentication.Sample.csproj +++ b/Samples/Dnmh.Security.ApiKeyAuthentication.Sample.csproj @@ -12,7 +12,7 @@ - + diff --git a/sample/Program.cs b/Samples/Program.cs similarity index 100% rename from sample/Program.cs rename to Samples/Program.cs diff --git a/sample/Properties/launchSettings.json b/Samples/Properties/launchSettings.json similarity index 100% rename from sample/Properties/launchSettings.json rename to Samples/Properties/launchSettings.json diff --git a/sample/Services/ApiKeyAuthenticationService.cs b/Samples/Services/ApiKeyAuthenticationService.cs similarity index 100% rename from sample/Services/ApiKeyAuthenticationService.cs rename to Samples/Services/ApiKeyAuthenticationService.cs diff --git a/sample/appsettings.Development.json b/Samples/appsettings.Development.json similarity index 100% rename from sample/appsettings.Development.json rename to Samples/appsettings.Development.json diff --git a/sample/appsettings.json b/Samples/appsettings.json similarity index 100% rename from sample/appsettings.json rename to Samples/appsettings.json diff --git a/src/AuthenticationHandler/ApiKeyAuthenticationBuilder.cs b/Source/AuthenticationHandler/ApiKeyAuthenticationBuilder.cs similarity index 100% rename from src/AuthenticationHandler/ApiKeyAuthenticationBuilder.cs rename to Source/AuthenticationHandler/ApiKeyAuthenticationBuilder.cs diff --git a/src/AuthenticationHandler/ApiKeyAuthenticationEvents.cs b/Source/AuthenticationHandler/ApiKeyAuthenticationEvents.cs similarity index 100% rename from src/AuthenticationHandler/ApiKeyAuthenticationEvents.cs rename to Source/AuthenticationHandler/ApiKeyAuthenticationEvents.cs diff --git a/src/AuthenticationHandler/ApiKeyAuthenticationExtensions.cs b/Source/AuthenticationHandler/ApiKeyAuthenticationExtensions.cs similarity index 100% rename from src/AuthenticationHandler/ApiKeyAuthenticationExtensions.cs rename to Source/AuthenticationHandler/ApiKeyAuthenticationExtensions.cs diff --git a/src/AuthenticationHandler/ApiKeyAuthenticationHandler.cs b/Source/AuthenticationHandler/ApiKeyAuthenticationHandler.cs similarity index 100% rename from src/AuthenticationHandler/ApiKeyAuthenticationHandler.cs rename to Source/AuthenticationHandler/ApiKeyAuthenticationHandler.cs diff --git a/src/AuthenticationHandler/ApiKeyAuthenticationOptions.cs b/Source/AuthenticationHandler/ApiKeyAuthenticationOptions.cs similarity index 100% rename from src/AuthenticationHandler/ApiKeyAuthenticationOptions.cs rename to Source/AuthenticationHandler/ApiKeyAuthenticationOptions.cs diff --git a/src/AuthenticationHandler/FailedAuthenticationException.cs b/Source/AuthenticationHandler/FailedAuthenticationException.cs similarity index 100% rename from src/AuthenticationHandler/FailedAuthenticationException.cs rename to Source/AuthenticationHandler/FailedAuthenticationException.cs diff --git a/src/AuthenticationHandler/IApiKeyAuthenticationService.cs b/Source/AuthenticationHandler/IApiKeyAuthenticationService.cs similarity index 100% rename from src/AuthenticationHandler/IApiKeyAuthenticationService.cs rename to Source/AuthenticationHandler/IApiKeyAuthenticationService.cs diff --git a/src/AuthenticationHandler/Internal/ApiKeyAuthenticationOptionsValidator.cs b/Source/AuthenticationHandler/Internal/ApiKeyAuthenticationOptionsValidator.cs similarity index 100% rename from src/AuthenticationHandler/Internal/ApiKeyAuthenticationOptionsValidator.cs rename to Source/AuthenticationHandler/Internal/ApiKeyAuthenticationOptionsValidator.cs diff --git a/src/AuthenticationHandler/Internal/ApiKeySchemeSwaggerOptions.cs b/Source/AuthenticationHandler/Internal/ApiKeySchemeSwaggerOptions.cs similarity index 100% rename from src/AuthenticationHandler/Internal/ApiKeySchemeSwaggerOptions.cs rename to Source/AuthenticationHandler/Internal/ApiKeySchemeSwaggerOptions.cs diff --git a/src/AuthenticationHandler/Internal/PostConfigureSwaggerAuthorization.cs b/Source/AuthenticationHandler/Internal/PostConfigureSwaggerAuthorization.cs similarity index 100% rename from src/AuthenticationHandler/Internal/PostConfigureSwaggerAuthorization.cs rename to Source/AuthenticationHandler/Internal/PostConfigureSwaggerAuthorization.cs diff --git a/src/AuthenticationHandler/SimpleApiKeyAuthenticationService.cs b/Source/AuthenticationHandler/SimpleApiKeyAuthenticationService.cs similarity index 100% rename from src/AuthenticationHandler/SimpleApiKeyAuthenticationService.cs rename to Source/AuthenticationHandler/SimpleApiKeyAuthenticationService.cs diff --git a/src/Dnmh.Security.ApiKeyAuthentication.csproj b/Source/Dnmh.Security.ApiKeyAuthentication.csproj similarity index 100% rename from src/Dnmh.Security.ApiKeyAuthentication.csproj rename to Source/Dnmh.Security.ApiKeyAuthentication.csproj index 85edee9..757ebcc 100644 --- a/src/Dnmh.Security.ApiKeyAuthentication.csproj +++ b/Source/Dnmh.Security.ApiKeyAuthentication.csproj @@ -10,9 +10,9 @@ True MIT https://github.com/DetNordjyskeMediehus/Dnmh.Security.ApiKeyAuthentication - README.md .Net ApiKey Authentication 1.0.0 + README.md diff --git a/test/AuthenticationHandler/ApiKeyAuthenticationHandlerTest.cs b/Tests/AuthenticationHandler/ApiKeyAuthenticationHandlerTest.cs similarity index 100% rename from test/AuthenticationHandler/ApiKeyAuthenticationHandlerTest.cs rename to Tests/AuthenticationHandler/ApiKeyAuthenticationHandlerTest.cs diff --git a/test/AuthenticationHandler/ApiKeyAuthenticationOptionsTest.cs b/Tests/AuthenticationHandler/ApiKeyAuthenticationOptionsTest.cs similarity index 100% rename from test/AuthenticationHandler/ApiKeyAuthenticationOptionsTest.cs rename to Tests/AuthenticationHandler/ApiKeyAuthenticationOptionsTest.cs diff --git a/test/AuthenticationHandlerExtensions.cs b/Tests/AuthenticationHandlerExtensions.cs similarity index 100% rename from test/AuthenticationHandlerExtensions.cs rename to Tests/AuthenticationHandlerExtensions.cs diff --git a/test/Dnmh.Security.ApiKeyAuthentication.Test.csproj b/Tests/Dnmh.Security.ApiKeyAuthentication.Test.csproj similarity index 92% rename from test/Dnmh.Security.ApiKeyAuthentication.Test.csproj rename to Tests/Dnmh.Security.ApiKeyAuthentication.Test.csproj index e712201..c8da469 100644 --- a/test/Dnmh.Security.ApiKeyAuthentication.Test.csproj +++ b/Tests/Dnmh.Security.ApiKeyAuthentication.Test.csproj @@ -24,7 +24,7 @@ - + diff --git a/test/MockHelpers.cs b/Tests/MockHelpers.cs similarity index 100% rename from test/MockHelpers.cs rename to Tests/MockHelpers.cs diff --git a/test/Usings.cs b/Tests/Usings.cs similarity index 100% rename from test/Usings.cs rename to Tests/Usings.cs