Skip to content

Commit

Permalink
rename project
Browse files Browse the repository at this point in the history
  • Loading branch information
pwelter34 committed Mar 22, 2024
1 parent 0019547 commit a88509c
Show file tree
Hide file tree
Showing 36 changed files with 222 additions and 98 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ jobs:
- name: Run Test
run: dotnet test --no-build --configuration Release --collect:"XPlat Code Coverage" --settings coverlet.runsettings

- name: Report Coverage
if: success()
uses: coverallsapp/github-action@v2
with:
file: '${{github.workspace}}/test/*/TestResults/*/coverage.info'
format: lcov

- name: Create Packages
if: success() && github.event_name != 'pull_request'
run: dotnet pack --configuration Release --include-symbols --include-source --no-build --output "${{env.BUILD_PATH}}"
Expand Down
17 changes: 2 additions & 15 deletions AspNetCore.Extensions.sln → AspNetCore.SecurityKey.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNetCore.Extensions.SecurityKey", "src\AspNetCore.Extensions.SecurityKey\AspNetCore.Extensions.SecurityKey.csproj", "{B6EAF1BD-410D-44F2-B909-448E37BC0748}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNetCore.Extensions.Diagnostics", "src\AspNetCore.Extensions.Diagnostics\AspNetCore.Extensions.Diagnostics.csproj", "{D556675A-4D8F-4F7B-A82C-7DC8CC255FE3}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNetCore.SecurityKey", "src\AspNetCore.SecurityKey\AspNetCore.SecurityKey.csproj", "{B6EAF1BD-410D-44F2-B909-448E37BC0748}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{8F6AF28D-3302-4CD2-93F8-FEDFF40DA284}"
ProjectSection(SolutionItems) = preProject
test\Directory.Build.props = test\Directory.Build.props
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNetCore.Extensions.SecurityKey.Tests", "test\AspNetCore.Extensions.SecurityKey.Tests\AspNetCore.Extensions.SecurityKey.Tests.csproj", "{524DA054-BB33-46BC-B906-1781DA92816D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNetCore.Extensions.Diagnostics.Tests", "test\AspNetCore.Extensions.Diagnostics.Tests\AspNetCore.Extensions.Diagnostics.Tests.csproj", "{2CF093CB-B64C-4735-BC63-B37DA06B0247}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNetCore.SecurityKey.Tests", "test\AspNetCore.SecurityKey.Tests\AspNetCore.SecurityKey.Tests.csproj", "{524DA054-BB33-46BC-B906-1781DA92816D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Build", "Build", "{8FF17A69-FF3C-4F04-9F5A-1C58B0498D0D}"
ProjectSection(SolutionItems) = preProject
Expand Down Expand Up @@ -46,18 +42,10 @@ Global
{B6EAF1BD-410D-44F2-B909-448E37BC0748}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B6EAF1BD-410D-44F2-B909-448E37BC0748}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B6EAF1BD-410D-44F2-B909-448E37BC0748}.Release|Any CPU.Build.0 = Release|Any CPU
{D556675A-4D8F-4F7B-A82C-7DC8CC255FE3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D556675A-4D8F-4F7B-A82C-7DC8CC255FE3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D556675A-4D8F-4F7B-A82C-7DC8CC255FE3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D556675A-4D8F-4F7B-A82C-7DC8CC255FE3}.Release|Any CPU.Build.0 = Release|Any CPU
{524DA054-BB33-46BC-B906-1781DA92816D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{524DA054-BB33-46BC-B906-1781DA92816D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{524DA054-BB33-46BC-B906-1781DA92816D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{524DA054-BB33-46BC-B906-1781DA92816D}.Release|Any CPU.Build.0 = Release|Any CPU
{2CF093CB-B64C-4735-BC63-B37DA06B0247}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2CF093CB-B64C-4735-BC63-B37DA06B0247}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2CF093CB-B64C-4735-BC63-B37DA06B0247}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2CF093CB-B64C-4735-BC63-B37DA06B0247}.Release|Any CPU.Build.0 = Release|Any CPU
{24245841-8372-442D-8212-A357138445D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{24245841-8372-442D-8212-A357138445D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{24245841-8372-442D-8212-A357138445D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -80,7 +68,6 @@ Global
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{524DA054-BB33-46BC-B906-1781DA92816D} = {8F6AF28D-3302-4CD2-93F8-FEDFF40DA284}
{2CF093CB-B64C-4735-BC63-B37DA06B0247} = {8F6AF28D-3302-4CD2-93F8-FEDFF40DA284}
{24245841-8372-442D-8212-A357138445D9} = {8A309522-314D-4C40-9D0E-C42E88CB64E5}
{21060613-AFA4-4C8E-B0C9-2CE4368DA6F2} = {8A309522-314D-4C40-9D0E-C42E88CB64E5}
{53460F71-F58D-4BEF-9441-2CCA82FA9415} = {8A309522-314D-4C40-9D0E-C42E88CB64E5}
Expand Down
184 changes: 182 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,182 @@
# AspNetCore.Extensions
Extensions to AspNetCore
# Security API Keys for ASP.NET Core

API Key Authentication Implementation for ASP.NET Core

[![Build Project](https://github.com/loresoft/AspNetCore.SecurityKey/actions/workflows/dotnet.yml/badge.svg)](https://github.com/loresoft/AspNetCore.SecurityKey/actions/workflows/dotnet.yml)

[![Coverage Status](https://coveralls.io/repos/github/loresoft/AspNetCore.SecurityKey/badge.svg?branch=main)](https://coveralls.io/github/loresoft/AspNetCore.SecurityKey?branch=main)

[![AspNetCore.SecurityKey](https://img.shields.io/nuget/v/AspNetCore.SecurityKey.svg)](https://www.nuget.org/packages/AspNetCore.SecurityKey/)


## Passing API Key in a Request

- Request Headers
- Query Parameters
- Cookie

### Request Header

Example passing the security api key via a header

```
GET http://localhost:5009/users
Accept: application/json
X-API-KEY: 01HSGVBSF99SK6XMJQJYF0X3WQ
```

### Query Parameters


Example passing the security api key via a header

```
GET http://localhost:5009/users?X-API-KEY=01HSGVBSF99SK6XMJQJYF0X3WQ
Accept: application/json
```

## Security API Key Setup

### Set the Security API Key

Security API key in the appsetting.json

```json
{
"SecurityKey": "01HSGVBSF99SK6XMJQJYF0X3WQ"
}
```

Multiple keys supported via semicolon delimiter


```json
{
"SecurityKey": "01HSGVBGWXWDWTFGTJSYFXXDXQ;01HSGVBSF99SK6XMJQJYF0X3WQ"
}
```

### Register Services

```c#
var builder = WebApplication.CreateBuilder(args);

// add security api key scheme
builder.Services
.AddAuthentication()
.AddSecurityKey();

builder.Services.AddAuthorization();

// add security api key services
builder.Services.AddSecurityKey();

```

Configure Options

```c#
builder.Services.AddSecurityKey(options => {
options.ConfigurationName = "Authentication:ApiKey";
options.HeaderName = "x-api-key";
options.QueryName = "ApiKey";
options.KeyComparer = StringComparer.OrdinalIgnoreCase;
});
```

### Securty Endpoint

Secure Controller with `SecurityKeyAttribute`. Can be at class or method level

```c#
[ApiController]
[Route("[controller]")]
public class AddressController : ControllerBase
{
[SecurityKey]
[HttpGet(Name = "GetAddresses")]
public IEnumerable<Address> Get()
{
return AddressFaker.Instance.Generate(5);
}

}
```

Secure via middleware. All endpoints will require security API key

```c#
public static class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddAuthorization();
builder.Services.AddSecurityKey();

var app = builder.Build();

// required api key for all end points
app.UseSecurityKey();
app.UseAuthorization();

app.MapGet("/weather", () => WeatherFaker.Instance.Generate(5));

app.Run();
}
}
```

Secure Minimal API endpoint with filter, .NET 8+ only

```c#
public static class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddAuthorization();
builder.Services.AddSecurityKey();

var app = builder.Build();

app.UseAuthorization();

app.MapGet("/users", () => UserFaker.Instance.Generate(10))
.RequireSecurityKey();

app.Run();
}
}
```

Secure with Authentication Scheme

```c#
public static class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);

builder.Services
.AddAuthentication()
.AddSecurityKey();

builder.Services.AddAuthorization();
builder.Services.AddSecurityKey();

var app = builder.Build();

app.UseAuthentication();
app.UseAuthorization();

app.MapGet("/users", () => UserFaker.Instance.Generate(10))
.RequireAuthorization();

app.Run();
}
}
```
2 changes: 1 addition & 1 deletion samples/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<PropertyGroup>
<Description>Sample Project</Description>
<Description>Sample Project for AspNetCore.SecurityKey</Description>
<Copyright>Copyright © $([System.DateTime]::Now.ToString(yyyy)) LoreSoft</Copyright>
<Authors>LoreSoft</Authors>
<NeutralLanguage>en-US</NeutralLanguage>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using AspNetCore.Extensions.SecurityKey;
using AspNetCore.SecurityKey;

using Microsoft.AspNetCore.Mvc;

Expand Down
2 changes: 1 addition & 1 deletion samples/Sample.Controllers/Controllers/UserController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using AspNetCore.Extensions.SecurityKey;
using AspNetCore.SecurityKey;

using Microsoft.AspNetCore.Mvc;

Expand Down
2 changes: 1 addition & 1 deletion samples/Sample.Controllers/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using AspNetCore.Extensions.SecurityKey;
using AspNetCore.SecurityKey;

namespace Sample.Controllers;

Expand Down
3 changes: 1 addition & 2 deletions samples/Sample.Controllers/Sample.Controllers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\AspNetCore.Extensions.Diagnostics\AspNetCore.Extensions.Diagnostics.csproj" />
<ProjectReference Include="..\..\src\AspNetCore.Extensions.SecurityKey\AspNetCore.Extensions.SecurityKey.csproj" />
<ProjectReference Include="..\..\src\AspNetCore.SecurityKey\AspNetCore.SecurityKey.csproj" />
<ProjectReference Include="..\Sample.Shared\Sample.Shared.csproj" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion samples/Sample.Middleware/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using AspNetCore.Extensions.SecurityKey;
using AspNetCore.SecurityKey;

using Sample.Shared;

Expand Down
3 changes: 1 addition & 2 deletions samples/Sample.Middleware/Sample.Middleware.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\AspNetCore.Extensions.Diagnostics\AspNetCore.Extensions.Diagnostics.csproj" />
<ProjectReference Include="..\..\src\AspNetCore.Extensions.SecurityKey\AspNetCore.Extensions.SecurityKey.csproj" />
<ProjectReference Include="..\..\src\AspNetCore.SecurityKey\AspNetCore.SecurityKey.csproj" />
<ProjectReference Include="..\Sample.Shared\Sample.Shared.csproj" />
</ItemGroup>

Expand Down
6 changes: 1 addition & 5 deletions samples/Sample.MinimalApi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using AspNetCore.Extensions.Diagnostics;
using AspNetCore.Extensions.SecurityKey;
using AspNetCore.SecurityKey;

using Sample.Shared;

Expand Down Expand Up @@ -46,9 +45,6 @@ public static void Main(string[] args)
.WithOpenApi()
.RequireAuthorization();

app.MapConfigurationDebugger()
.RequireSecurityKey();

app.Run();
}
}
3 changes: 1 addition & 2 deletions samples/Sample.MinimalApi/Sample.MinimalApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\AspNetCore.Extensions.Diagnostics\AspNetCore.Extensions.Diagnostics.csproj" />
<ProjectReference Include="..\..\src\AspNetCore.Extensions.SecurityKey\AspNetCore.Extensions.SecurityKey.csproj" />
<ProjectReference Include="..\..\src\AspNetCore.SecurityKey\AspNetCore.SecurityKey.csproj" />
<ProjectReference Include="..\Sample.Shared\Sample.Shared.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.AspNetCore.Builder;

namespace AspNetCore.Extensions.SecurityKey;
namespace AspNetCore.SecurityKey;

public static class ApplicationBuilderExtensions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication;

namespace AspNetCore.Extensions.SecurityKey;
namespace AspNetCore.SecurityKey;

public static class AuthenticationBuilderExtensions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;

namespace AspNetCore.Extensions.SecurityKey;
namespace AspNetCore.SecurityKey;

public static class DependencyInjectionExtensions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;

namespace AspNetCore.Extensions.SecurityKey;
namespace AspNetCore.SecurityKey;

public static class EndpointFilterExtensions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.AspNetCore.Http;

namespace AspNetCore.Extensions.SecurityKey;
namespace AspNetCore.SecurityKey;

public interface ISecurityKeyExtractor
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AspNetCore.Extensions.SecurityKey;
namespace AspNetCore.SecurityKey;

public interface ISecurityKeyValidator
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.AspNetCore.Mvc;

namespace AspNetCore.Extensions.SecurityKey;
namespace AspNetCore.SecurityKey;

public class SecurityKeyAttribute : ServiceFilterAttribute
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AspNetCore.Extensions.SecurityKey;
namespace AspNetCore.SecurityKey;

public static class SecurityKeyAuthenticationDefaults
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace AspNetCore.Extensions.SecurityKey;
namespace AspNetCore.SecurityKey;

public class SecurityKeyAuthenticationHandler : AuthenticationHandler<SecurityKeyAuthenticationSchemeOptions>
{
Expand Down
Loading

0 comments on commit a88509c

Please sign in to comment.