Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitHub Actions CI #37

Merged
merged 3 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: CI build

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:
runs-on: ubuntu-latest
name: CI Build
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0 # avoid shallow clone so nbgv can do its work.

# Use GitVersion
- name: Install GitVersion
uses: gittools/actions/gitversion/[email protected]
with:
versionSpec: '6.x'

- name: Determine Version
id: gitversion
uses: gittools/actions/gitversion/[email protected]

- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 9.0.x

- name: Restore Dependencies
run: dotnet restore src/Ktos.AspNetCore.Authentication.ApiKeyHeader.csproj

- name: Build
run: dotnet build src/Ktos.AspNetCore.Authentication.ApiKeyHeader.csproj --configuration Release --no-restore /p:InformationalVersion=${{ steps.gitversion.outputs.fullSemVer }} /p:Version=${{ steps.gitversion.outputs.semVer }}

- name: Test
run: dotnet test test/Ktos.AspNetCore.Authentication.ApiKeyHeader.Tests.csproj --configuration Release --verbosity normal --collect:"XPlat Code Coverage" --results-directory ./coverage

- name: Code Coverage Report
uses: irongut/[email protected]
with:
filename: coverage/**/coverage.cobertura.xml
badge: true
format: markdown
hide_branch_rate: false
hide_complexity: false
indicators: true
output: both
thresholds: '60 80'

- name: Add Coverage PR Comment
uses: marocchino/sticky-pull-request-comment@v2
if: github.event_name == 'pull_request'
with:
recreate: true
path: code-coverage-results.md
44 changes: 0 additions & 44 deletions azure-pipelines-ci.yml

This file was deleted.

60 changes: 0 additions & 60 deletions azure-pipelines-tag.yml

This file was deleted.

45 changes: 33 additions & 12 deletions src/ApiKeyHeaderAuthenticationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
/// <summary>
/// Handles ApiKeyHeader authentication scheme
/// </summary>
public class ApiKeyHeaderAuthenticationHandler : AuthenticationHandler<ApiKeyHeaderAuthenticationOptions>
public class ApiKeyHeaderAuthenticationHandler
: AuthenticationHandler<ApiKeyHeaderAuthenticationOptions>
{
/// <summary>
/// Initializes a new instance of ApiKeyHeaderAuthenticationHandler
Expand All @@ -59,25 +60,36 @@
/// <param name="logger"></param>
/// <param name="encoder"></param>
/// <param name="clock"></param>
public ApiKeyHeaderAuthenticationHandler(IOptionsMonitor<ApiKeyHeaderAuthenticationOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock)
{
}
public ApiKeyHeaderAuthenticationHandler(
IOptionsMonitor<ApiKeyHeaderAuthenticationOptions> options,
ILoggerFactory logger,
UrlEncoder encoder,
ISystemClock clock

Check warning on line 67 in src/ApiKeyHeaderAuthenticationHandler.cs

View workflow job for this annotation

GitHub Actions / CI Build

'ISystemClock' is obsolete: 'Use TimeProvider instead.'

Check warning on line 67 in src/ApiKeyHeaderAuthenticationHandler.cs

View workflow job for this annotation

GitHub Actions / CI Build

'ISystemClock' is obsolete: 'Use TimeProvider instead.'
)
: base(options, logger, encoder, clock) { }

Check warning on line 69 in src/ApiKeyHeaderAuthenticationHandler.cs

View workflow job for this annotation

GitHub Actions / CI Build

'AuthenticationHandler<ApiKeyHeaderAuthenticationOptions>.AuthenticationHandler(IOptionsMonitor<ApiKeyHeaderAuthenticationOptions>, ILoggerFactory, UrlEncoder, ISystemClock)' is obsolete: 'ISystemClock is obsolete, use TimeProvider on AuthenticationSchemeOptions instead.'

Check warning on line 69 in src/ApiKeyHeaderAuthenticationHandler.cs

View workflow job for this annotation

GitHub Actions / CI Build

'AuthenticationHandler<ApiKeyHeaderAuthenticationOptions>.AuthenticationHandler(IOptionsMonitor<ApiKeyHeaderAuthenticationOptions>, ILoggerFactory, UrlEncoder, ISystemClock)' is obsolete: 'ISystemClock is obsolete, use TimeProvider on AuthenticationSchemeOptions instead.'

/// <summary>
/// Handles authentication by checking if there is proper api key set in HTTP header
/// </summary>
/// <returns>Returns Claim with name if authentication was successful or NoResult of not</returns>
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
{
var registeredHandler = Context.RequestServices.GetService(typeof(IApiKeyCustomAuthenticator));
var registeredHandler2 = Context.RequestServices.GetService(typeof(IApiKeyCustomAuthenticationTicketHandler));
var registeredHandler = Context.RequestServices.GetService(
typeof(IApiKeyCustomAuthenticator)
);
var registeredHandler2 = Context.RequestServices.GetService(
typeof(IApiKeyCustomAuthenticationTicketHandler)
);

var headerKey = Context.Request.Headers[Options.Header].FirstOrDefault();
if (headerKey == null)
{
return AuthenticateResult.NoResult();
}
else if (Options.CustomAuthenticationHandler != null && !Options.UseRegisteredAuthenticationHandler)
else if (
Options.CustomAuthenticationHandler != null
&& !Options.UseRegisteredAuthenticationHandler
)
{
var (result, claimName) = Options.CustomAuthenticationHandler(headerKey);

Expand All @@ -92,7 +104,9 @@
}
else if (registeredHandler != null && Options.UseRegisteredAuthenticationHandler)
{
var (result, claimName) = (registeredHandler as IApiKeyCustomAuthenticator).CustomAuthenticationHandler(headerKey);
var (result, claimName) = (
registeredHandler as IApiKeyCustomAuthenticator
).CustomAuthenticationHandler(headerKey);

if (result)
{
Expand All @@ -105,7 +119,9 @@
}
else if (registeredHandler2 != null && Options.UseRegisteredAuthenticationHandler)
{
return (registeredHandler2 as IApiKeyCustomAuthenticationTicketHandler).CustomAuthenticationHandler(headerKey);
return (
registeredHandler2 as IApiKeyCustomAuthenticationTicketHandler
).CustomAuthenticationHandler(headerKey);
}
else if (headerKey == Options.ApiKey && !Options.UseRegisteredAuthenticationHandler)
{
Expand All @@ -117,14 +133,19 @@
}
}

private AuthenticationTicket CreateAuthenticationTicket(string claimName = ApiKeyHeaderAuthenticationDefaults.AuthenticationClaimName)
private AuthenticationTicket CreateAuthenticationTicket(
string claimName = ApiKeyHeaderAuthenticationDefaults.AuthenticationClaimName
)
{
var claims = new[] { new Claim(ClaimTypes.Name, claimName) };
var identity = new ClaimsIdentity(claims, Scheme.Name);
var principal = new ClaimsPrincipal(identity);
var at = new AuthenticationTicket(principal, ApiKeyHeaderAuthenticationDefaults.AuthenticationScheme);
var at = new AuthenticationTicket(
principal,
ApiKeyHeaderAuthenticationDefaults.AuthenticationScheme
);
//Context.User.AddIdentity(new ClaimsIdentity(ApiKeyHeaderAuthenticationDefaults.AuthenticationScheme));
return at;
}
}
}
}
4 changes: 2 additions & 2 deletions src/Ktos.AspNetCore.Authentication.ApiKeyHeader.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<PackageId>Ktos.AspNetCore.Authentication.ApiKeyHeader</PackageId>
<Authors>Marcin Badurowicz</Authors>
<NoWarn>$(NoWarn);CS1998</NoWarn>
<Description>Api Key in HTTP Header Authentication Scheme for ASP.NET Core</Description>
<PackageVersion>7.0.0</PackageVersion>
<PackageVersion>9.0.0</PackageVersion>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>api;apikey;aspnetcore;authentication;security</PackageTags>
<PackageProjectUrl>https://github.com/ktos/Ktos.AspNetCore.Authentication.ApiKeyHeader</PackageProjectUrl>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<NoWarn>$(NoWarn);CS1591</NoWarn>
<IsPackable>false</IsPackable>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
Expand Down
Loading