-
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.
Added Auth extension method to check for authorization in the pipeline.
- Loading branch information
1 parent
0b5bbce
commit ea5ce98
Showing
11 changed files
with
434 additions
and
29 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 |
---|---|---|
@@ -1,38 +1,38 @@ | ||
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<!-- | ||
<!-- | ||
Push the target package to nuget | ||
--> | ||
<Target Name="PushPackage" AfterTargets="Pack" | ||
Condition="'$(PushAfterPack)'=='true' AND '$(IsPackable)'=='true'"> | ||
<Exec Command="dotnet nuget push $(SolutionDir)output/$(TargetName).$(PackageVersion).nupkg $(PackageSourceParam) $(PackageApiKeyParam)"></Exec> | ||
</Target> | ||
<Target Name="PushPackage" AfterTargets="Pack" | ||
Condition="'$(PushAfterPack)'=='true' AND '$(IsPackable)'=='true'"> | ||
<Exec Command="dotnet nuget push $(SolutionDir)output/$(TargetName).$(PackageVersion).nupkg $(PackageSourceParam) $(PackageApiKeyParam)"></Exec> | ||
</Target> | ||
|
||
<!-- | ||
<!-- | ||
Set the package version and source before Pack | ||
https://github.com/NuGet/NuGet.Client/blob/4.3.0.4202/src/NuGet.Core/NuGet.Build.Tasks.Pack.Library/Pack.targets | ||
Must run before target "GenerateNuspec" ("Pack") for the version to be applied as expected | ||
--> | ||
<Target Name="SetPackageVersion" BeforeTargets="GenerateNuspec" | ||
Condition="'$(IsPackable)'=='true'"> | ||
<PropertyGroup> | ||
<VersionPrefix>$(MajorVersion).$(MinorVersion).$(PatchVersion)</VersionPrefix> | ||
<PackageVersion Condition="'$(VersionSuffix)'==''">$(VersionPrefix)</PackageVersion> | ||
<PackageVersion Condition="'$(VersionSuffix)'!=''">$(VersionPrefix)-$(VersionSuffix)</PackageVersion> | ||
</PropertyGroup> | ||
</Target> | ||
<Target Name="SetPackageVersion" BeforeTargets="GenerateNuspec" | ||
Condition="'$(IsPackable)'=='true'"> | ||
<PropertyGroup> | ||
<VersionPrefix>$(MajorVersion).$(MinorVersion).$(PatchVersion)</VersionPrefix> | ||
<PackageVersion Condition="'$(VersionSuffix)'==''">$(VersionPrefix)</PackageVersion> | ||
<PackageVersion Condition="'$(VersionSuffix)'!=''">$(VersionPrefix)-$(VersionSuffix)</PackageVersion> | ||
</PropertyGroup> | ||
</Target> | ||
|
||
<Target Name="SetPackageSource" BeforeTargets="GenerateNuspec" | ||
Condition="'$(PushAfterPack)'=='true'"> | ||
<PropertyGroup> | ||
<PackageSourceParam Condition="'$(PackageSource)'!=''">--source $(PackageSource)</PackageSourceParam> | ||
</PropertyGroup> | ||
</Target> | ||
<Target Name="SetPackageSource" BeforeTargets="GenerateNuspec" | ||
Condition="'$(PushAfterPack)'=='true'"> | ||
<PropertyGroup> | ||
<PackageSourceParam Condition="'$(PackageSource)'!=''">--source $(PackageSource)</PackageSourceParam> | ||
</PropertyGroup> | ||
</Target> | ||
|
||
<Target Name="SetPackageApiKey" BeforeTargets="GenerateNuspec" | ||
Condition="'$(PushAfterPack)'=='true'"> | ||
<PropertyGroup> | ||
<PackageApiKeyParam Condition="'$(PackageApiKey)'!=''">--api-key $(PackageApiKey)</PackageApiKeyParam> | ||
</PropertyGroup> | ||
</Target> | ||
<Target Name="SetPackageApiKey" BeforeTargets="GenerateNuspec" | ||
Condition="'$(PushAfterPack)'=='true'"> | ||
<PropertyGroup> | ||
<PackageApiKeyParam Condition="'$(PackageApiKey)'!=''">--api-key $(PackageApiKey)</PackageApiKeyParam> | ||
</PropertyGroup> | ||
</Target> | ||
|
||
</Project> |
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<IsPackable>true</IsPackable> | ||
|
||
<Authors>Stillpoint Software, Inc.</Authors> | ||
<PackageReadmeFile>README.md</PackageReadmeFile> | ||
<PackageTags>pipeline;auth</PackageTags> | ||
<PackageIcon>icon.png</PackageIcon> | ||
<PackageProjectUrl>https://github.com/Stillpoint-Software/Hyperbee.Pipeline/</PackageProjectUrl> | ||
<PackageReleaseNotes>https://github.com/Stillpoint-Software/hyperbee.pipeline/releases/latest</PackageReleaseNotes> | ||
<TargetFrameworks>net8.0</TargetFrameworks> | ||
<PackageLicenseFile>LICENSE</PackageLicenseFile> | ||
<Copyright>Stillpoint Software, Inc.</Copyright> | ||
<Title>Hyperbee Pipline Auth</Title> | ||
<Description>Auth for Hyperbee.Pipelines async pipelines</Description> | ||
<RepositoryUrl>https://github.com/Stillpoint-Software/Hyperbee.Pipeline</RepositoryUrl> | ||
<RepositoryType>git</RepositoryType> | ||
<PackageReleaseNotes>https://github.com/Stillpoint-Software/hyperbee.pipeline/releases/latest</PackageReleaseNotes> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<FrameworkReference Include="Microsoft.AspNetCore.App" /> | ||
<None Include="..\..\assets\icon.png" Pack="true" Visible="false" PackagePath="/" /> | ||
<None Include="..\..\LICENSE"> | ||
<Pack>True</Pack> | ||
<PackagePath>\</PackagePath> | ||
</None> | ||
<None Include="README.md"> | ||
<Pack>True</Pack> | ||
<PackagePath>\</PackagePath> | ||
</None> | ||
<PackageReference Include="Microsoft.AspNetCore.Http.Connections.Common" Version="8.0.4" /> | ||
<ProjectReference Include="..\Hyperbee.Pipeline\Hyperbee.Pipeline.csproj" /> | ||
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="8.0.0"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
</Project> |
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,23 @@ | ||
## LICENSE | ||
|
||
MIT License | ||
|
||
Copyright (c) 2024 Stillpoint Software, Inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,68 @@ | ||
using System.Security.Claims; | ||
using Hyperbee.Pipeline.Context; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Hyperbee.Pipeline.Auth; | ||
|
||
public static class PipelineAuthExtensions | ||
{ | ||
private const string ClaimsPrincipalKey = nameof( ClaimsPrincipalKey ); | ||
|
||
public static IPipelineBuilder<TInput, TNext> PipeIfClaim<TInput, TOutput, TNext>( | ||
this IPipelineBuilder<TInput, TOutput> builder, | ||
Claim claim, | ||
Func<IPipelineStartBuilder<TOutput, TOutput>, IPipelineBuilder<TOutput, TNext>> childBuilder ) | ||
{ | ||
return (IPipelineBuilder<TInput, TNext>) builder | ||
.PipeIf( ( context, arg ) => | ||
{ | ||
var claimsPrincipal = context.GetClaimsPrincipal(); | ||
|
||
return claimsPrincipal.HasClaim( x => x.Type == claim.Type && x.Value == claim.Value ); | ||
} | ||
, childBuilder ); | ||
|
||
} | ||
|
||
public static IPipelineBuilder<TInput, TOutput> WithAuth<TInput, TOutput>( | ||
this IPipelineStartBuilder<TInput, TOutput> builder, | ||
Func<IPipelineContext, TOutput, ClaimsPrincipal, bool> validateClaims ) | ||
{ | ||
return builder | ||
.WithAuth() | ||
.Call( ( context, argument ) => | ||
{ | ||
var claimsPrincipal = context.GetClaimsPrincipal(); | ||
|
||
if ( !validateClaims( context, argument, claimsPrincipal ) ) | ||
context.CancelAfter(); | ||
|
||
} ); | ||
} | ||
|
||
public static IPipelineBuilder<TInput, TOutput> WithAuth<TInput, TOutput>( | ||
this IPipelineStartBuilder<TInput, TOutput> builder ) | ||
{ | ||
return builder.HookAsync( async ( context, argument, next ) => | ||
{ | ||
context.GetClaimsPrincipal(); | ||
|
||
return await next( context, argument ); | ||
|
||
} ); | ||
} | ||
|
||
public static ClaimsPrincipal GetClaimsPrincipal( this IPipelineContext context ) | ||
{ | ||
if ( context.Items.TryGetValue<ClaimsPrincipal>( ClaimsPrincipalKey, out var claimsPrincipal ) ) | ||
return claimsPrincipal; | ||
|
||
var claimsPrincipalAccessor = context.ServiceProvider.GetService<IClaimsPrincipalAccessor>(); | ||
claimsPrincipal = claimsPrincipalAccessor.ClaimsPrincipal; | ||
|
||
context.Items.SetValue( ClaimsPrincipalKey, claimsPrincipal ); | ||
return claimsPrincipal; | ||
} | ||
|
||
|
||
} |
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,48 @@ | ||
# Hyperbee.Pipeline.Claims | ||
|
||
The `Hyperbee.Pipeline.Auth` library is a set of extentsions to `Hyperbee.Pipeline` that adds support for authorization within the pipeline. | ||
|
||
|
||
## Examples | ||
|
||
```csharp | ||
// Will return the claim if available | ||
var command = PipelineFactory | ||
.Start<string>() | ||
.PipeIfClaim( Claim ) | ||
.Build(); | ||
|
||
``` | ||
|
||
```csharp | ||
// WithAuth takes a function to validate the claim. | ||
var command = PipelineFactory | ||
.Start<string>() | ||
.WithAuth( ValidateClaim ) | ||
.Build(); | ||
|
||
private async Task<bool> ValidateClaim( IPipelineContext context, string roleValue, ClaimsPrincipal claimsPrincipal ) | ||
{ | ||
return claimsPrincipal.HasClaim( x => x.Value == roleValue ); | ||
} | ||
``` | ||
|
||
## Dependacy Injection | ||
|
||
```csharp | ||
// Add httpContextAccessor if using web api | ||
services.AddHttpContextAccessor(); | ||
|
||
// Add with the pipelines | ||
services.AddClaimPrincipalAccessor(); | ||
``` | ||
|
||
Or create your own claims principal use for the pipelines: | ||
|
||
```csharp | ||
services.AddPipeline( (factoryServices, rootProvider) => | ||
{ | ||
factoryServices.AddClaimPrincipalAccessor( IClaimsPrincipal claimsPrincipal ) | ||
} ); | ||
``` | ||
|
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,47 @@ | ||
using System.Security.Claims; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Hyperbee.Pipeline.Auth; | ||
|
||
public static class ServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddClaimPrincipalAccessor<T>( | ||
this IServiceCollection services, | ||
T claimsPrincipalAccessor | ||
) where T : IClaimsPrincipalAccessor | ||
{ | ||
return services.AddTransient<IClaimsPrincipalAccessor>( _ => claimsPrincipalAccessor ); | ||
|
||
} | ||
|
||
//For web API | ||
public static IServiceCollection AddClaimPrincipalAccessor( | ||
this IServiceCollection services | ||
) | ||
{ | ||
return services.AddTransient<IClaimsPrincipalAccessor, HttpClaimsAccessor>(); | ||
} | ||
} | ||
|
||
public interface IClaimsPrincipalAccessor | ||
{ | ||
public ClaimsPrincipal ClaimsPrincipal { get; } | ||
} | ||
|
||
public class HttpClaimsAccessor : IClaimsPrincipalAccessor | ||
{ | ||
private readonly IHttpContextAccessor _httpContextAccessor; | ||
public HttpClaimsAccessor( IHttpContextAccessor httpContextAccessor ) | ||
{ | ||
_httpContextAccessor = httpContextAccessor; | ||
} | ||
|
||
public ClaimsPrincipal ClaimsPrincipal | ||
{ | ||
get { return _httpContextAccessor.HttpContext.User; } | ||
} | ||
} | ||
|
||
|
||
|
32 changes: 32 additions & 0 deletions
32
test/Hyperbee.Pipeline.Auth.Tests/Hyperbee.Pipeline.Auth.Tests.csproj
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,32 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.2"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" /> | ||
<PackageReference Include="MSTest.TestAdapter" Version="3.3.1" /> | ||
<PackageReference Include="MSTest.TestFramework" Version="3.3.1" /> | ||
<PackageReference Include="NSubstitute" Version="5.1.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Hyperbee.Pipeline.Auth\Hyperbee.Pipeline.Auth.csproj" /> | ||
<ProjectReference Include="..\..\src\Hyperbee.Pipeline\Hyperbee.Pipeline.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.