Skip to content

Commit 249f79c

Browse files
committed
Enabled allowed user config without client secret
1 parent 676402a commit 249f79c

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

src/Website/AllowListAuthorizationHandler.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Net.Http;
45
using System.Net.Http.Headers;
56
using System.Security.Claims;
67
using System.Threading.Tasks;
78
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
89
using Microsoft.AspNetCore.Authorization;
10+
using Microsoft.Extensions.DependencyInjection;
911
using Microsoft.Extensions.Options;
1012
using Microsoft.Graph;
1113
using Microsoft.Identity.Web;
@@ -18,16 +20,16 @@ public class AllowListAuthorizationHandler : AuthorizationHandler<AllowListRequi
1820
private const string AllowedGroupClaimName = "ExplorePackages.AllowedGroup";
1921
private const string HttpContextKeyForJwt = "JwtSecurityTokenUsedToCallWebAPI";
2022

21-
private readonly GraphServiceClient _graphServiceClient;
23+
private readonly IServiceProvider _serviceProvider;
2224
private readonly bool _restrictUsers;
2325
private readonly Dictionary<string, HashSet<string>> _allowedUsers;
2426
private readonly Dictionary<string, HashSet<string>> _allowedGroups;
2527

2628
public AllowListAuthorizationHandler(
27-
GraphServiceClient graphServiceClient,
29+
IServiceProvider serviceProvider,
2830
IOptions<ExplorePackagesWebsiteSettings> options)
2931
{
30-
_graphServiceClient = graphServiceClient;
32+
_serviceProvider = serviceProvider;
3133
_restrictUsers = options.Value.RestrictUsers;
3234
_allowedUsers = TenantToObjectIds(options.Value.AllowedUsers);
3335
_allowedGroups = TenantToObjectIds(options.Value.AllowedGroups);
@@ -111,7 +113,8 @@ public async Task AddAllowedGroupClaimsAsync(TokenValidatedContext context)
111113
// Source: https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/blob/ef20861535add11f5d37e25228379c8dfc5d1796/5-WebApp-AuthZ/5-2-Groups/Services/MicrosoftGraph-Rest/GraphHelper.cs
112114
context.HttpContext.Items[HttpContextKeyForJwt] = context.SecurityToken;
113115

114-
var memberGroups = await _graphServiceClient
116+
var memberGroups = await _serviceProvider
117+
.GetRequiredService<GraphServiceClient>()
115118
.Me
116119
.CheckMemberGroups(objectIds)
117120
.Request()

src/Website/Startup.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using System.Text.Json.Serialization;
34
using Knapcode.ExplorePackages.Website.Logic;
45
using Knapcode.ExplorePackages.Worker;
@@ -52,7 +53,7 @@ public void ConfigureServices(IServiceCollection services)
5253
options.PayloadSerializerOptions.Converters.Add(new JsonStringEnumConverter());
5354
});
5455

55-
services
56+
var microsoftIdentityBuilder = services
5657
.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
5758
.AddMicrosoftIdentityWebApp(options =>
5859
{
@@ -71,10 +72,18 @@ await context
7172
options.ExpireTimeSpan = TimeSpan.FromHours(1);
7273
options.SlidingExpiration = false;
7374
options.AccessDeniedPath = "/Home/AccessDenied";
74-
})
75-
.EnableTokenAcquisitionToCallDownstreamApi()
76-
.AddInMemoryTokenCaches()
77-
.AddMicrosoftGraph();
75+
});
76+
77+
var initialSettings = Configuration
78+
.GetSection(ExplorePackagesSettings.DefaultSectionName)
79+
.Get<ExplorePackagesWebsiteSettings>();
80+
if (initialSettings.AllowedGroups.Any())
81+
{
82+
microsoftIdentityBuilder
83+
.EnableTokenAcquisitionToCallDownstreamApi()
84+
.AddInMemoryTokenCaches()
85+
.AddMicrosoftGraph();
86+
}
7887

7988
services
8089
.AddAuthorization(options =>

0 commit comments

Comments
 (0)