Skip to content

Commit

Permalink
Added support for custom claims binders
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesRandall committed May 12, 2019
1 parent cab6c9f commit 59f8a85
Show file tree
Hide file tree
Showing 17 changed files with 94 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ public interface IAuthorizationBuilder
/// <returns>The builder for use in a Fluent API</returns>
IAuthorizationBuilder TokenValidator<TTokenValidator>(string header=null) where TTokenValidator : ITokenValidator;

/// <summary>
/// Allows a custom claims binder to be registered. This can only occur globally and must take responsibility
/// for all claims mapping.
/// </summary>
/// <typeparam name="TCustomClaimsBinder">The type of the claims binder</typeparam>
/// <returns>The builder for use in a Fluent API</returns>
IAuthorizationBuilder CustomClaimsBinder<TCustomClaimsBinder>()
where TCustomClaimsBinder : ICommandClaimsBinder;

/// <summary>
/// Allows the default authorization mode for HTTP functions to be set. Defaults to Function Code if this method is not used.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>2.1.0</Version>
<Version>2.2.0</Version>
<Authors>James Randall</Authors>
<PackageLicenseUrl>https://raw.githubusercontent.com/JamesRandall/AzureFromTheTrenches.Commanding/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://commanding.azurefromthetrenches.com/</PackageProjectUrl>
<RepositoryUrl>https://github.com/JamesRandall/FunctionMonkey.git</RepositoryUrl>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
<FileVersion>2.1.0.0</FileVersion>
<AssemblyVersion>2.2.0.0</AssemblyVersion>
<FileVersion>2.2.0.0</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
7 changes: 7 additions & 0 deletions Source/FunctionMonkey.Abstractions/ICommandClaimsBinder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Security.Claims;
using System.Threading.Tasks;
using AzureFromTheTrenches.Commanding.Abstractions;

namespace FunctionMonkey.Abstractions
Expand All @@ -13,5 +14,11 @@ public interface ICommandClaimsBinder
/// Binds the claims in the principal onto properties of the command
/// </summary>
bool Bind(ClaimsPrincipal principal, ICommand command);

/// <summary>
/// Binds the claims in the principal onto properties of the command. If this returns null then
/// the non-async binder will be called.
/// </summary>
Task<bool> BindAsync(ClaimsPrincipal principal, ICommand command);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>2.1.0</Version>
<Version>2.2.0</Version>
<Company>James Randall</Company>
<Authors>James Randall</Authors>
<PackageLicenseUrl>https://raw.githubusercontent.com/JamesRandall/AzureFromTheTrenches.Commanding/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://functionmonkey.azurefromthetrenches.com/</PackageProjectUrl>
<RepositoryUrl>https://github.com/JamesRandall/FunctionMonkey.git</RepositoryUrl>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
<FileVersion>2.1.0.0</FileVersion>
<AssemblyVersion>2.2.0.0</AssemblyVersion>
<FileVersion>2.2.0.0</FileVersion>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>2.1.0</Version>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
<FileVersion>2.1.0.0</FileVersion>
<Version>2.2.0</Version>
<AssemblyVersion>2.2.0.0</AssemblyVersion>
<FileVersion>2.2.0.0</FileVersion>
<Authors>James Randall</Authors>
<Company>James Randall</Company>
<PackageLicenseUrl>https://raw.githubusercontent.com/JamesRandall/AzureFromTheTrenches.Commanding/master/LICENSE</PackageLicenseUrl>
Expand Down
6 changes: 3 additions & 3 deletions Source/FunctionMonkey.Compiler/FunctionMonkey.Compiler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<Version>2.1.1</Version>
<Version>2.2.0</Version>
<AssemblyName>FunctionMonkey.Compiler</AssemblyName>
<PackageId>FunctionMonkey.Compiler</PackageId>
</PropertyGroup>
Expand Down Expand Up @@ -385,8 +385,8 @@
<IntermediatePackDir>$(MSBuildProjectDirectory)/bin/$(Configuration)/publish/</IntermediatePackDir>
<PublishDir>$(IntermediatePackDir)$(TargetFramework)/</PublishDir>
<NuspecProperties>publishDir=$([MSBuild]::NormalizeDirectory($(IntermediatePackDir)))</NuspecProperties>
<AssemblyVersion>2.1.1.0</AssemblyVersion>
<FileVersion>2.1.1.0</FileVersion>
<AssemblyVersion>2.2.0.0</AssemblyVersion>
<FileVersion>2.2.1.0</FileVersion>
<PackageReleaseNotes></PackageReleaseNotes>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>FunctionMonkey.Compiler</id>
<version>2.1.1</version>
<version>2.2.0</version>
<authors>James Randall</authors>
<description>Generates Azure Functions from command registrations</description>
<licenseUrl>https://raw.githubusercontent.com/JamesRandall/AzureFromTheTrenches.Commanding/master/LICENSE</licenseUrl>
Expand Down
10 changes: 9 additions & 1 deletion Source/FunctionMonkey.Compiler/Templates/http.csharp.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,15 @@ namespace {{Namespace}}
{{#if ValidatesToken}}
var claimsBinder = (FunctionMonkey.Abstractions.ICommandClaimsBinder)
FunctionMonkey.Runtime.ServiceProvider.GetService(typeof(FunctionMonkey.Abstractions.ICommandClaimsBinder));
claimsBinder.Bind(principal, command);
var claimsBinderTask = claimsBinder.BindAsync(principal, command);
if (claimsBinderTask == null)
{
claimsBinder.Bind(principal, command);
}
else
{
await claimsBinderTask;
}
{{/if}}

{{#if HasHttpResponseHandler}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,15 @@ namespace {{Namespace}}
{{#if ValidatesToken}}
var claimsBinder = (FunctionMonkey.Abstractions.ICommandClaimsBinder)
FunctionMonkey.Runtime.ServiceProvider.GetService(typeof(FunctionMonkey.Abstractions.ICommandClaimsBinder));
claimsBinder.Bind(principal, command);
var claimsBinderTask = claimsBinder.BindAsync(principal, command);
if (claimsBinderTask == null)
{
claimsBinder.Bind(principal, command);
}
else
{
await claimsBinderTask;
}
{{/if}}

{{#if HasHttpResponseHandler}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>2.1.0</Version>
<Version>2.2.0</Version>
<Authors>James Randall</Authors>
<Company>James Randall</Company>
<PackageLicenseUrl>https://raw.githubusercontent.com/JamesRandall/AzureFromTheTrenches.Commanding/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://commanding.azurefromthetrenches.com/</PackageProjectUrl>
<RepositoryUrl>https://github.com/JamesRandall/FunctionMonkey.git</RepositoryUrl>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
<FileVersion>2.1.0.0</FileVersion>
<AssemblyVersion>2.2.0.0</AssemblyVersion>
<FileVersion>2.2.0.0</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>2.1.0</Version>
<Version>2.2.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions Source/FunctionMonkey.Testing/FunctionMonkey.Testing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<Version>2.1.2</Version>
<Version>2.2.0</Version>
<Authors>James Randall</Authors>
<Company>James Randall</Company>
<Copyright></Copyright>
<PackageLicenseUrl>https://raw.githubusercontent.com/JamesRandall/AzureFromTheTrenches.Commanding/master/LICENSE</PackageLicenseUrl>
<RepositoryUrl>https://github.com/JamesRandall/FunctionMonkey.git</RepositoryUrl>
<PackageProjectUrl>https://functionmonkey.azurefromthetrenches.com/</PackageProjectUrl>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<AssemblyVersion>2.1.2.0</AssemblyVersion>
<FileVersion>2.1.2.0</FileVersion>
<AssemblyVersion>2.2.0.0</AssemblyVersion>
<FileVersion>2.2.0.0</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Security.Claims;
using System.Threading.Tasks;
using AzureFromTheTrenches.Commanding.Abstractions;
using FunctionMonkey.Abstractions;

Expand All @@ -10,5 +11,10 @@ public bool Bind(ClaimsPrincipal principal, ICommand command)
{
return false;
}

public Task<bool> BindAsync(ClaimsPrincipal principal, ICommand command)
{
return null;
}
}
}
8 changes: 8 additions & 0 deletions Source/FunctionMonkey/Builders/AuthorizationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ internal class AuthorizationBuilder : IAuthorizationBuilder
public string AuthorizationHeader { get; set; }

public Type DefaultClaimsPrincipalAuthorizationType { get; set; }

public Type CustomClaimsBinderType { get; set; }

public AuthorizationTypeEnum AuthorizationDefaultValue { get; set; } = AuthorizationTypeEnum.Function;

Expand All @@ -24,6 +26,12 @@ public IAuthorizationBuilder TokenValidator<TTokenValidator>(string header=null)
return this;
}

public IAuthorizationBuilder CustomClaimsBinder<TCustomClaimsBinder>() where TCustomClaimsBinder : ICommandClaimsBinder
{
CustomClaimsBinderType = typeof(TCustomClaimsBinder);
return this;
}

public IAuthorizationBuilder AuthorizationDefault(AuthorizationTypeEnum authorizationDefault)
{
AuthorizationDefaultValue = authorizationDefault;
Expand Down
6 changes: 3 additions & 3 deletions Source/FunctionMonkey/FunctionMonkey.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>2.1.1</Version>
<Version>2.2.0</Version>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Company />
<Authors>James Randall</Authors>
<PackageLicenseUrl>https://raw.githubusercontent.com/JamesRandall/AzureFromTheTrenches.Commanding/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://functionmonkey.azurefromthetrenches.com/</PackageProjectUrl>
<RepositoryUrl>https://github.com/JamesRandall/FunctionMonkey.git</RepositoryUrl>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<AssemblyVersion>2.1.1.0</AssemblyVersion>
<FileVersion>2.1.1.0</FileVersion>
<AssemblyVersion>2.2.0.0</AssemblyVersion>
<FileVersion>2.2.0.0</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
12 changes: 12 additions & 0 deletions Source/FunctionMonkey/Infrastructure/CommandClaimsBinder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Threading.Tasks;
using AzureFromTheTrenches.Commanding.Abstractions;
using FunctionMonkey.Abstractions;

Expand All @@ -25,5 +26,16 @@ public bool Bind(ClaimsPrincipal principal, ICommand command)

return false;
}

public Task<bool> BindAsync(ClaimsPrincipal principal, ICommand command)
{
if (_mappers.TryGetValue(command.GetType(), out var binder))
{
binder(command, principal);
return Task.FromResult(true);
}

return Task.FromResult(false);
}
}
}
15 changes: 11 additions & 4 deletions Source/FunctionMonkey/RuntimeInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,17 @@ private void SetupAuthorization(FunctionHostBuilder builder, FunctionBuilder fun

// don't register the token validator type here - that gets passed down to the HTTP function
// definitions to allow for function overrides and so is registered as part of HTTP dependencies

ICommandClaimsBinder commandClaimsBinder = authorizationBuilder.ClaimsMappingBuilder.Build(
functionBuilder.GetHttpFunctionDefinitions().Select(x => x.CommandType).ToArray());
ServiceCollection.AddSingleton(commandClaimsBinder);
if (authorizationBuilder.CustomClaimsBinderType == null)
{
ICommandClaimsBinder commandClaimsBinder = authorizationBuilder.ClaimsMappingBuilder.Build(
functionBuilder.GetHttpFunctionDefinitions().Select(x => x.CommandType).ToArray());
ServiceCollection.AddSingleton(commandClaimsBinder);
}
else
{
ServiceCollection.AddTransient(typeof(ICommandClaimsBinder),
authorizationBuilder.CustomClaimsBinderType);
}
}

private IFunctionAppConfiguration LocateConfiguration(Assembly functionAppConfigurationAssembly)
Expand Down

0 comments on commit 59f8a85

Please sign in to comment.