Skip to content

Commit da692d8

Browse files
authored
Merge pull request #13 from xaviersolau/main
Add DefaultAuthorizationHandlerProvider in order to enable use of role-based authorization.
2 parents a2a3809 + ea83ad7 commit da692d8

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

src/AzureFunctions.Extensions.OpenIDConnect.Tests/AuthorizationServiceShould.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ public async Task ApplyPolicyRequirements(string claimType, string claimValue, s
2424
// Arrange
2525
var policyName = "my policy";
2626
var localeClaim = new Claim(claimType, claimValue);
27-
28-
var collection = ServiceCollectionFixture.MinimalAzFunctionsServices();
27+
28+
var collection = ServiceCollectionFixture.MinimalAzFunctionsServices()
29+
.WithClaimsAuthorizationHandler(claimType, new[] { claimRequiredValue });
2930

3031
collection.AddOpenIDConnect(builder =>
3132
{

src/AzureFunctions.Extensions.OpenIDConnect.Tests/ServiceCollectionFixture.cs

+8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using Microsoft.AspNetCore.Authorization;
2+
using Microsoft.AspNetCore.Authorization.Infrastructure;
23
using Microsoft.Extensions.DependencyInjection;
34
using Microsoft.Extensions.Logging;
45
using Microsoft.Extensions.Options;
56
using NSubstitute;
7+
using System.Collections.Generic;
68

79
namespace AzureFunctions.Extensions.OpenIDConnect.Tests
810
{
@@ -24,5 +26,11 @@ public static ServiceCollection MinimalAzFunctionsServices(ServiceCollection col
2426

2527
return collection;
2628
}
29+
30+
public static ServiceCollection WithClaimsAuthorizationHandler(this ServiceCollection collection, string claimType, IEnumerable<string> allowedValues)
31+
{
32+
collection.AddSingleton<IAuthorizationHandler>(new ClaimsAuthorizationRequirement(claimType, allowedValues));
33+
return collection;
34+
}
2735
}
2836
}

src/AzureFunctions.Extensions.OpenIDConnect/Configuration/ServicesConfigurationExtensions.cs

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public static void AddOpenIDConnect(this IServiceCollection services, Action<Con
5050
services.AddSingleton<IFunctionFilter, AuthorizeFilter>();
5151
services.AddSingleton<IAuthorizationService, DefaultAuthorizationService>();
5252
services.AddSingleton<IAuthorizationPolicyProvider, DefaultAuthorizationPolicyProvider>();
53+
services.AddSingleton<IAuthorizationHandlerProvider, DefaultAuthorizationHandlerProvider>();
5354
}
5455
}
5556
}

0 commit comments

Comments
 (0)