Skip to content

Commit

Permalink
feature/custom policies (#13)
Browse files Browse the repository at this point in the history
* added the possibility to register custom authorization policies

* removed commented code

* introduced ApiGroup Constants

* added examples in code comment for Policy options

* updated notes.txt

Co-authored-by: Thomas Duft <[email protected]>
  • Loading branch information
thomasduft and Thomas Duft authored Dec 15, 2021
1 parent 7d7df60 commit 1a0e720
Show file tree
Hide file tree
Showing 16 changed files with 198 additions and 124 deletions.
22 changes: 14 additions & 8 deletions notes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ TODO:
- https://github.com/openiddict/openiddict-samples/tree/dev/samples/Contruum/Contruum.Server


- migrate to .net Program.cs file
- refactor DB-migrations in sample server


- load options.Permissions from database instead of configuring it


- client refactoring
- split identity, openiddict => create libs
- better Client deployment
- introduce deploy.js
- check server stylings not removed


- organize APIs a bit more by feature
- i.e. identity -> AccountController
- AccountController
Expand Down Expand Up @@ -36,14 +50,6 @@ TODO:
- PolicyClient


- load options.Permissions from database instead of configuring it


- better Client deployment
- introduce deploy.js
- check server stylings not removed


- better ux for applications.permissions
- sth. like the approach of OrchardCore ID UI

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using tomware.OpenIddict.UI.Suite.Api;

namespace tomware.OpenIddict.UI.Identity.Api
{
[Route("accounts")]
[ApiExplorerSettings(GroupName = "openiddict-ui-identity")]
public class AccountController : ApiControllerBase
public class AccountController : IdentityApiController
{
private readonly IAccountApiService _service;

Expand Down Expand Up @@ -41,7 +39,7 @@ public async Task<IActionResult> Register([FromBody] RegisterUserViewModel model

[HttpPost("changepassword")]
[ProducesResponseType(typeof(IdentityResult), StatusCodes.Status200OK)]
public async Task<IActionResult> ChangePassword([FromBody]ChangePasswordViewModel model)
public async Task<IActionResult> ChangePassword([FromBody] ChangePasswordViewModel model)
{
if (model == null) return BadRequest();
if (ModelState.IsValid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using tomware.OpenIddict.UI.Suite.Api;

namespace tomware.OpenIddict.UI.Identity.Api
{
[Route("claimtypes")]
[ApiExplorerSettings(GroupName = "openiddict-ui-identity")]
public class ClaimTypeController : ApiControllerBase
public class ClaimTypeController : IdentityApiController
{
private readonly IClaimTypeApiService _service;

Expand Down
12 changes: 12 additions & 0 deletions src/identity/OpenIddict.UI.Identity.Api/Common/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace tomware.OpenIddict.UI.Identity.Api
{
public static class Policies
{
public const string OPENIDDICT_UI_IDENTITY_API_POLICY = "OpenIddictUiIdentityApiPolicy";
}

public static class ApiGroups
{
public const string OPENIDDICT_UI_IDENTITY_GROUP = "openiddict-ui-identity";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using OpenIddict.Validation.AspNetCore;
using tomware.OpenIddict.UI.Suite.Api;

namespace tomware.OpenIddict.UI.Identity.Api
{
[ApiExplorerSettings(GroupName = ApiGroups.OPENIDDICT_UI_IDENTITY_GROUP)]
[Authorize(
Policies.OPENIDDICT_UI_IDENTITY_API_POLICY,
AuthenticationSchemes = OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme
)]
public class IdentityApiController : ApiControllerBase
{
}
}
16 changes: 7 additions & 9 deletions src/identity/OpenIddict.UI.Identity.Api/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;

using tomware.OpenIddict.UI.Suite.Core;
using Microsoft.AspNetCore.Authorization;
using tomware.OpenIddict.UI.Suite.Api;

namespace tomware.OpenIddict.UI.Identity.Api
Expand All @@ -23,6 +22,8 @@ public static OpenIddictBuilder AddUIIdentityApis<TApplicationUser>(

builder.Services.AddApiServices<TApplicationUser>();

builder.Services.AddAuthorizationServices(options.Policy);

return builder;
}

Expand All @@ -48,22 +49,19 @@ this IServiceCollection services
services.AddTransient<IRoleService, RoleService>();
services.AddTransient<IClaimTypeApiService, ClaimTypeApiService>();

services.AddAuthorizationServices();

return services;
}

private static IServiceCollection AddAuthorizationServices(
this IServiceCollection services
this IServiceCollection services,
Action<AuthorizationPolicyBuilder> policy
)
{
services.AddAuthorization(options =>
{
options.AddPolicy(
Policies.ADMIN_POLICY,
policy => policy
.RequireAuthenticatedUser()
.RequireRole(Roles.ADMINISTRATOR_ROLE)
Policies.OPENIDDICT_UI_IDENTITY_API_POLICY,
policy
);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
using System;
using Microsoft.AspNetCore.Authorization;
using tomware.OpenIddict.UI.Suite.Core;

namespace tomware.OpenIddict.UI.Identity.Api
{
public class OpenIddictUIIdentityApiOptions
Expand All @@ -6,5 +10,20 @@ public class OpenIddictUIIdentityApiOptions
/// Registers a conventional route prefix for the API controllers. Defaults to "api/".
/// </summary>
public string RoutePrefix { get; set; } = "api/";

/// <summary>
/// Allows to register custom authorization policies for accessing OpenIddict-UI Identity API's.
/// <example>Defaults to:
/// <code>
/// policy
/// .RequireAuthenticatedUser()
/// .RequireRole(Roles.ADMINISTRATOR_ROLE);
/// </code>
/// </example>
/// </summary>
public Action<AuthorizationPolicyBuilder> Policy { get; set; } = policy =>
policy
.RequireAuthenticatedUser()
.RequireRole(Roles.ADMINISTRATOR_ROLE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using tomware.OpenIddict.UI.Suite.Api;

namespace tomware.OpenIddict.UI.Identity.Api
{
[Route("roles")]
[ApiExplorerSettings(GroupName = "openiddict-ui-identity")]
public class RoleController : ApiControllerBase
public class RoleController : IdentityApiController
{
private readonly IRoleService _service;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using tomware.OpenIddict.UI.Suite.Api;

namespace tomware.OpenIddict.UI.Api
{
[Route("application")]
[ApiExplorerSettings(GroupName = "openiddict-ui-api")]
public class ApplicationController : ApiControllerBase
public class ApplicationController : OpenIddictApiController
{
private readonly IApplicationApiService _service;

Expand Down
12 changes: 12 additions & 0 deletions src/openiddict/OpenIddict.UI.Api/Common/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace tomware.OpenIddict.UI.Api
{
public static class Policies
{
public const string OPENIDDICT_UI_API_POLICY = "OpenIddictUiApiPolicy";
}

public static class ApiGroups
{
public const string OPENIDDICT_UI_GROUP = "openiddict-ui-api";
}
}
16 changes: 16 additions & 0 deletions src/openiddict/OpenIddict.UI.Api/Common/OpenIddictApiController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using OpenIddict.Validation.AspNetCore;
using tomware.OpenIddict.UI.Suite.Api;

namespace tomware.OpenIddict.UI.Api
{
[ApiExplorerSettings(GroupName = ApiGroups.OPENIDDICT_UI_GROUP)]
[Authorize(
Policies.OPENIDDICT_UI_API_POLICY,
AuthenticationSchemes = OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme
)]
public class OpenIddictApiController : ApiControllerBase
{
}
}
Loading

0 comments on commit 1a0e720

Please sign in to comment.