Skip to content

Commit

Permalink
Adds the C# Auth Policy bits to protect the API to only users who hav…
Browse files Browse the repository at this point in the history
…e the permission
  • Loading branch information
warrenbuckley committed Nov 28, 2024
1 parent 9135d10 commit 16e9cd7
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 3 deletions.
37 changes: 37 additions & 0 deletions ExaminePeek/Auth/HasUmbracoPermissionHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Microsoft.AspNetCore.Authorization;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Security.Authorization;
using Umbraco.Cms.Core.Services;
using Umbraco.Extensions;

namespace ExaminePeek.Auth
{
public class HasUmbracoPermissionHandler : AuthorizationHandler<HasUmbracoPermissionRequirement>
{
private readonly IAuthorizationHelper _authorizationHelper;
private readonly IUserService _userService;

public HasUmbracoPermissionHandler(IAuthorizationHelper authorizationHelper, IUserService userService)
{
_authorizationHelper = authorizationHelper;
_userService = userService;
}

protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, HasUmbracoPermissionRequirement requirement)
{
var umbracoUser = _authorizationHelper.GetUmbracoUser(context.User);
umbracoUser.
var permissions = umbracoUser.GetPermissions(Constants.System.RootString, _userService);
var hasPermission = permissions.Contains(requirement.Permission);

if (hasPermission)
{
context.Succeed(requirement);
return Task.CompletedTask;
}

context.Fail();
return Task.CompletedTask;
}
}
}
10 changes: 10 additions & 0 deletions ExaminePeek/Auth/HasUmbracoPermissionRequirement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Microsoft.AspNetCore.Authorization;

namespace ExaminePeek.Auth
{
public class HasUmbracoPermissionRequirement : IAuthorizationRequirement
{
public HasUmbracoPermissionRequirement(string permission) => Permission = permission;
public string Permission { get; }
}
}
17 changes: 16 additions & 1 deletion ExaminePeek/Composers/ExaminePeekComposer.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
using Microsoft.Extensions.DependencyInjection;
using ExaminePeek.Auth;
using ExaminePeek.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using OpenIddict.Validation.AspNetCore;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Reflection;
using Umbraco.Cms.Api.Management.OpenApi;
using Umbraco.Cms.Api.Management.Security.Authorization.UserGroup;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.DependencyInjection;

Expand Down Expand Up @@ -40,6 +45,16 @@ public void Compose(IUmbracoBuilder builder)
opt.IncludeXmlComments(xmlPath);
}
});

builder.Services.AddSingleton<IAuthorizationHandler, HasUmbracoPermissionHandler>();
builder.Services.Configure<AuthorizationOptions>(opt =>
{
opt.AddPolicy("HasExaminePeekPermission", policyBuilder =>
{
policyBuilder.AuthenticationSchemes.Add(OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme);
policyBuilder.RequireUmbracoPermission("ExaminePeek.Enabled");
});
});
}

// PR: https://github.com/umbraco/Umbraco-CMS/pull/15699
Expand Down
3 changes: 1 addition & 2 deletions ExaminePeek/Controllers/ExaminePeekControllerBase.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Api.Common.Attributes;
using Umbraco.Cms.Web.Common.Authorization;
using Umbraco.Cms.Web.Common.Routing;

namespace ExaminePeek.Controllers
{
[ApiController]
[BackOfficeRoute("examinepeek/api/v{version:apiVersion}")]
[Authorize(Policy = AuthorizationPolicies.SectionAccessContent)]
[Authorize(Policy = "HasExaminePeekPermission")]
[MapToApi("ExaminePeek")]
public class ExaminePeekControllerBase : ControllerBase
{
Expand Down
13 changes: 13 additions & 0 deletions ExaminePeek/Extensions/PolicyBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using ExaminePeek.Auth;
using Microsoft.AspNetCore.Authorization;

namespace ExaminePeek.Extensions
{
public static class PolicyBuilderExtensions
{
public static void RequireUmbracoPermission(this AuthorizationPolicyBuilder builder, string permission)
{
builder.Requirements.Add(new HasUmbracoPermissionRequirement(permission));
}
}
}

0 comments on commit 16e9cd7

Please sign in to comment.