-
-
Notifications
You must be signed in to change notification settings - Fork 563
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1261 from PiranhaCMS/features/simplify-authentica…
…tion Added core support for authentication. Fixes #1259
- Loading branch information
Showing
10 changed files
with
171 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright (c) .NET Foundation and Contributors | ||
* | ||
* This software may be modified and distributed under the terms | ||
* of the MIT license. See the LICENSE file for details. | ||
* | ||
* https://github.com/piranhacms/piranha.core | ||
* | ||
*/ | ||
|
||
using System; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Piranha; | ||
using Piranha.AspNetCore.Security; | ||
|
||
/// <summary> | ||
/// Security extensions for simplifying authorization in | ||
/// the client application. | ||
/// </summary> | ||
public static class AspNetCoreSecurityExtensions | ||
{ | ||
/// <summary> | ||
/// Adds authorization with the given application policies to the aplication. | ||
/// </summary> | ||
/// <param name="builder">The service builder</param> | ||
/// <param name="options">The security options</param> | ||
/// <returns>The service builder</returns> | ||
public static PiranhaServiceBuilder UseSecurity(this PiranhaServiceBuilder builder, Action<SecurityBuilder> options) | ||
{ | ||
// Add authentication | ||
builder.Services.AddAuthorization(o => | ||
{ | ||
// Invoke the security options | ||
var securityBuilder = new SecurityBuilder(o, builder); | ||
options?.Invoke(securityBuilder); | ||
}); | ||
return builder; | ||
} | ||
|
||
/// <summary> | ||
/// Adds the security middleware to the pipeline. | ||
/// </summary> | ||
/// <param name="builder">The current application builder</param> | ||
/// <returns>The update builder</returns> | ||
public static IApplicationBuilder UseSecurityMiddleware(this IApplicationBuilder builder) | ||
{ | ||
return builder.UseMiddleware<Piranha.AspNetCore.Security.SecurityMiddleware>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright (c) .NET Foundation and Contributors | ||
* | ||
* This software may be modified and distributed under the terms | ||
* of the MIT license. See the LICENSE file for details. | ||
* | ||
* https://github.com/piranhacms/piranha.core | ||
* | ||
*/ | ||
|
||
using Microsoft.AspNetCore.Authorization; | ||
|
||
namespace Piranha.AspNetCore.Security | ||
{ | ||
/// <summary> | ||
/// The security builder is used for creating application | ||
/// policies that can be selected from the manager | ||
/// interface. | ||
/// </summary> | ||
public class SecurityBuilder | ||
{ | ||
/// <summary> | ||
/// The policy builder. | ||
/// </summary> | ||
private readonly AuthorizationOptions _options; | ||
private readonly PiranhaServiceBuilder _builder; | ||
|
||
/// <summary> | ||
/// Default constructor. | ||
/// </summary> | ||
/// <param name="options">The authorization options</param> | ||
/// <param name="builder">The service builder</param> | ||
public SecurityBuilder(AuthorizationOptions options, PiranhaServiceBuilder builder) | ||
{ | ||
_options = options; | ||
_builder = builder; | ||
} | ||
|
||
/// <summary> | ||
/// Uses the specified permission in the application. | ||
/// </summary> | ||
/// <param name="name">The name</param> | ||
/// <param name="title">The optional title. If omitted the name will be used as title</param> | ||
/// <returns>The builder</returns> | ||
public SecurityBuilder UsePermission(string name, string title = null) | ||
{ | ||
// Add a policy with the specified name | ||
_options.AddPolicy(name, policy => | ||
{ | ||
// Require a claim with the same name as the policy | ||
policy.RequireClaim(name, name); | ||
|
||
// Add the specified policy to the manager | ||
App.Permissions["App"].Add(new Piranha.Security.PermissionItem | ||
{ | ||
Title = title != null ? title : name, | ||
Name = name | ||
}); | ||
}); | ||
|
||
return this; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright (c) .NET Foundation and Contributors | ||
* | ||
* This software may be modified and distributed under the terms | ||
* of the MIT license. See the LICENSE file for details. | ||
* | ||
* https://github.com/piranhacms/piranha.core | ||
* | ||
*/ | ||
|
||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Piranha.AspNetCore.Services; | ||
|
||
namespace Piranha.AspNetCore.Security | ||
{ | ||
public class SecurityMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
private readonly PiranhaRouteConfig _config; | ||
|
||
public SecurityMiddleware(RequestDelegate next, PiranhaRouteConfig config) | ||
{ | ||
_next = next; | ||
_config = config; | ||
} | ||
|
||
public async Task InvokeAsync(HttpContext ctx, IApplicationService service) | ||
{ | ||
await _next(ctx); | ||
|
||
if (ctx.Response.StatusCode == 401) | ||
{ | ||
ctx.Response.Redirect($"{ _config.LoginUrl }?returnUrl={ service.Request.Url }"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters