Skip to content

Commit

Permalink
Task/further refactorings (#7)
Browse files Browse the repository at this point in the history
* Renamed folders and updated readme

* Updated release workflow to verbose logging

* Fixed issue in solution file

* Another fix

---------

Co-authored-by: Pelle Stenild Coltau <psc@dnmh.dk>
  • Loading branch information
dnmh-admin and dnmh-psc committed May 2, 2023
1 parent a8ed66e commit 548eca0
Show file tree
Hide file tree
Showing 28 changed files with 146 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

25 changes: 19 additions & 6 deletions Dnmh.Security.ApiKeyAuthentication.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
124 changes: 123 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,123 @@
# Dnmh.Security.ApiKeyAuthentication
# 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<ApiKeyAuthenticationService>()
.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<ApiKeyAuthenticationService>()
.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).
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\src\Dnmh.Security.ApiKeyAuthentication.csproj" />
<ProjectReference Include="..\Source\Dnmh.Security.ApiKeyAuthentication.csproj" />
</ItemGroup>

</Project>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/DetNordjyskeMediehus/Dnmh.Security.ApiKeyAuthentication</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Title>.Net ApiKey Authentication</Title>
<Version>1.0.0</Version>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

<ItemGroup>
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\src\Dnmh.Security.ApiKeyAuthentication.csproj" />
<ProjectReference Include="..\Source\Dnmh.Security.ApiKeyAuthentication.csproj" />
</ItemGroup>

</Project>
File renamed without changes.
File renamed without changes.

0 comments on commit 548eca0

Please sign in to comment.