Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Package segregation #48

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Balea.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FunctionalTests", "test\Fun
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Balea", "src\Balea\Balea.csproj", "{32784D58-1B36-4083-ABF4-4EDB02761110}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Balea.AspNetCore", "src\Balea.AspNetCore\Balea.AspNetCore.csproj", "{B6AE3CFE-F70D-420A-B71C-85D4D4A48B09}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Balea.Configuration.Store", "src\Balea.Configuration.Store\Balea.Configuration.Store.csproj", "{4856F4E7-C091-4C65-8C86-9EC9869D15BE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Balea.EntityFrameworkCore.Store", "src\Balea.EntityFrameworkCore.Store\Balea.EntityFrameworkCore.Store.csproj", "{2A1CD101-2868-49ED-8BD4-8B0DDD793F76}"
Expand Down Expand Up @@ -69,6 +71,10 @@ Global
{9624712C-B17A-438B-9653-83F1BC35F38C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9624712C-B17A-438B-9653-83F1BC35F38C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9624712C-B17A-438B-9653-83F1BC35F38C}.Release|Any CPU.Build.0 = Release|Any CPU
{B6AE3CFE-F70D-420A-B71C-85D4D4A48B09}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B6AE3CFE-F70D-420A-B71C-85D4D4A48B09}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B6AE3CFE-F70D-420A-B71C-85D4D4A48B09}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B6AE3CFE-F70D-420A-B71C-85D4D4A48B09}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -82,6 +88,7 @@ Global
{4BC1F93E-EF93-4AD2-BCD7-632E2A782D80} = {58F16D81-1049-4D95-83E8-D773BEE31A5B}
{FAB1588F-DEE0-4A06-94E2-51FC0B02C897} = {58F16D81-1049-4D95-83E8-D773BEE31A5B}
{9624712C-B17A-438B-9653-83F1BC35F38C} = {58F16D81-1049-4D95-83E8-D773BEE31A5B}
{B6AE3CFE-F70D-420A-B71C-85D4D4A48B09} = {902B3EDC-5C6F-4058-8A05-DA80D4B03F97}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {943847A1-D097-4149-BEDC-B8541BE7D287}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Balea.AspNetCore\Balea.AspNetCore.csproj" />
<ProjectReference Include="..\..\src\Balea.Api.Store\Balea.Api.Store.csproj" />
<ProjectReference Include="..\..\src\Balea.Configuration.Store\Balea.Configuration.Store.csproj" />
</ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions sample/ContosoUniversity.Configuration.Store/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public void ConfigureServices(IServiceCollection services)
services
.AddBalea(options =>
{
options.UnauthorizedFallback = AuthorizationFallbackAction.RedirectToAction("Account", "AccessDenied");
options.DefaultClaimTypeMap = new DefaultClaimTypeMap
options.WebHost.Events.UnauthorizedFallback = AuthorizationFallbackAction.RedirectToAction("Account", "AccessDenied");
options.Common.ClaimTypeMap = new ClaimTypeMap
{
PermissionClaimType = "permissions"
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Balea.AspNetCore\Balea.AspNetCore.csproj" />
<ProjectReference Include="..\..\src\Balea.EntityFrameworkCore.Store\Balea.EntityFrameworkCore.Store.csproj" />
</ItemGroup>

Expand Down
6 changes: 3 additions & 3 deletions sample/ContosoUniversity.EntityFrameworkCore.Store/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ public void ConfigureServices(IServiceCollection services)
services
.AddBalea(options =>
{
options.DefaultClaimTypeMap = new DefaultClaimTypeMap
options.Common.ClaimTypeMap = new ClaimTypeMap
{
RoleClaimType = JwtClaimTypes.Role,
NameClaimType = JwtClaimTypes.Name,
};

options.DefaultClaimTypeMap.AllowedSubjectClaimTypes.Clear();
options.DefaultClaimTypeMap.AllowedSubjectClaimTypes.Add(JwtClaimTypes.Subject);
options.Common.ClaimTypeMap.AllowedSubjectClaimTypes.Clear();
options.Common.ClaimTypeMap.AllowedSubjectClaimTypes.Add(JwtClaimTypes.Subject);
})
.AddEntityFrameworkCoreStore(options =>
{
Expand Down
2 changes: 1 addition & 1 deletion src/Balea.Api.Store/ApiRuntimeAuthorizationServerStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private async Task<HttpClientStorePolicyResponse> GetPolicy(string name)
private string GetMappings(ClaimsPrincipal user)
{
return string.Concat(user
.GetClaimValues(_baleaOptions.DefaultClaimTypeMap.RoleClaimType)
.GetClaimValues(_baleaOptions.ClaimTypeMap.RoleClaimType)
.Select(role => $"&roles={role}"));
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Balea.Api.Store/Balea.Api.Store.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" />
</ItemGroup>

Expand Down
6 changes: 3 additions & 3 deletions src/Balea.Api.Store/Extensions/IDistributedCacheExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static class DistributedCacheExtensions

public static async Task<T> Get<T>(this IDistributedCache cache, string key, CancellationToken token = default)
{
var json = await cache.GetStringAsync(key, token);
var json = await cache.GetStringAsync(key);

if (string.IsNullOrEmpty(json))
{
Expand Down Expand Up @@ -99,12 +99,12 @@ public static Task Insert<T>(this IDistributedCache cache, string key, T data, T

public static Task Remove(this IDistributedCache cache, string key, CancellationToken token = default)
{
return cache.RemoveAsync(key, token);
return cache.RemoveAsync(key);
}

public static Task Refresh(this IDistributedCache cache, string key, CancellationToken token = default)
{
return cache.RefreshAsync(key, token);
return cache.RefreshAsync(key);
}
}
}
21 changes: 21 additions & 0 deletions src/Balea.AspNetCore/Abstractions/BaleaBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Balea.Authorization.Abac.Context;
using Microsoft.Extensions.DependencyInjection;

namespace Balea.Abstractions
{
internal sealed class BaleaBuilder : IBaleaBuilder
{
public BaleaBuilder(IServiceCollection services)
{
Services = services;
}

public IServiceCollection Services { get; }

public IBaleaBuilder AddPropertyBag<TPropertyBag>() where TPropertyBag: class, IPropertyBag
{
Services.AddScoped<IPropertyBag, TPropertyBag>();
return this;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,64 +1,64 @@
using Balea.Abstractions;
using Balea.Authorization.Abac.Context;
using Balea.Authorization.Abac.Grammars;
using Balea.Diagnostics;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
namespace Balea.Authorization.Abac
{
internal class AbacAuthorizationHandler : AuthorizationHandler<AbacRequirement>
{
private readonly AbacAuthorizationContextFactory _abacAuthorizationContextFactory;
private readonly IRuntimeAuthorizationServerStore _runtimeAuthorizationServerStore;
private readonly ILogger<AbacAuthorizationHandler> _logger;
public AbacAuthorizationHandler(
AbacAuthorizationContextFactory abacAuthorizationContextFactory,
IRuntimeAuthorizationServerStore runtimeAuthorizationServerStore,
ILogger<AbacAuthorizationHandler> logger)
{
Ensure.Argument.NotNull(abacAuthorizationContextFactory, nameof(abacAuthorizationContextFactory));
Ensure.Argument.NotNull(runtimeAuthorizationServerStore, nameof(runtimeAuthorizationServerStore));
Ensure.Argument.NotNull(logger, nameof(logger));
_abacAuthorizationContextFactory = abacAuthorizationContextFactory;
_runtimeAuthorizationServerStore = runtimeAuthorizationServerStore;
_logger = logger;
}
protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, AbacRequirement requirement)
{
if (context.User.Identity.IsAuthenticated)
{
try
{
var policy = await _runtimeAuthorizationServerStore
.GetPolicyAsync(requirement.Name);
if (policy is object)
{
Log.AbacAuthorizationHandlerIsEvaluatingPolicy(_logger, policy.Name, policy.Content);
var abacContext = await _abacAuthorizationContextFactory.Create(context);
var abacPolicy = AbacAuthorizationPolicy.CreateFromGrammar(policy.Content, WellKnownGrammars.Bal);
if (abacPolicy.IsSatisfied(abacContext))
{
Log.AbacAuthorizationHandlerEvaluationSuccesss(_logger,policy.Name);
context.Succeed(requirement);
return;
}
}
}
catch (Exception ex)
{
Log.AbacAuthorizationHandlerThrow(_logger, ex);
}
}
context.Fail();
}
}
}
using Balea.Abstractions;
using Balea.Authorization.Abac.Context;
using Balea.Authorization.Abac.Grammars;
using Balea.Diagnostics;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;

namespace Balea.Authorization.Abac
{
internal class AbacAuthorizationHandler : AuthorizationHandler<AbacRequirement>
{
private readonly AbacAuthorizationContextFactory _abacAuthorizationContextFactory;
private readonly IRuntimeAuthorizationServerStore _runtimeAuthorizationServerStore;
private readonly ILogger<AbacAuthorizationHandler> _logger;

public AbacAuthorizationHandler(
AbacAuthorizationContextFactory abacAuthorizationContextFactory,
IRuntimeAuthorizationServerStore runtimeAuthorizationServerStore,
ILogger<AbacAuthorizationHandler> logger)
{
Ensure.Argument.NotNull(abacAuthorizationContextFactory, nameof(abacAuthorizationContextFactory));
Ensure.Argument.NotNull(runtimeAuthorizationServerStore, nameof(runtimeAuthorizationServerStore));
Ensure.Argument.NotNull(logger, nameof(logger));
_abacAuthorizationContextFactory = abacAuthorizationContextFactory;
_runtimeAuthorizationServerStore = runtimeAuthorizationServerStore;
_logger = logger;
}

protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, AbacRequirement requirement)
{
if (context.User.Identity.IsAuthenticated)
{
try
{
var policy = await _runtimeAuthorizationServerStore
.GetPolicyAsync(requirement.Name);

if (policy is object)
{
Log.AbacAuthorizationHandlerIsEvaluatingPolicy(_logger, policy.Name, policy.Content);

var abacContext = await _abacAuthorizationContextFactory.Create(context);
var abacPolicy = AbacAuthorizationPolicy.CreateFromGrammar(policy.Content, WellKnownGrammars.Bal);

if (abacPolicy.IsSatisfied(abacContext))
{
Log.AbacAuthorizationHandlerEvaluationSuccesss(_logger,policy.Name);
context.Succeed(requirement);
return;
}
}
}
catch (Exception ex)
{
Log.AbacAuthorizationHandlerThrow(_logger, ex);
}
}

context.Fail();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
using Microsoft.AspNetCore.Authorization;
namespace Balea.Authorization.Abac
{
//
// Summary:
// Specifies that the class or method that this attribute is applied to requires
// the specified authorization using Balea DSL.
public class AbacAuthorizeAttribute
: AuthorizeAttribute
{
/// <summary>
/// Initialize a new instance.
/// </summary>
/// <param name="policy">The ABAC policy registered on Balea to be used.</param>
public AbacAuthorizeAttribute(string policy) :
base(new AbacPrefix(policy).ToString())
{ }
}
}
using Microsoft.AspNetCore.Authorization;

namespace Balea.Authorization.Abac
{
//
// Summary:
// Specifies that the class or method that this attribute is applied to requires
// the specified authorization using Balea DSL.
public class AbacAuthorizeAttribute
: AuthorizeAttribute
{
/// <summary>
/// Initialize a new instance.
/// </summary>
/// <param name="policy">The ABAC policy registered on Balea to be used.</param>
public AbacAuthorizeAttribute(string policy) :
base(new AbacPrefix(policy).ToString())
{ }
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
using System;
namespace Balea.Authorization.Abac
{
/// <summary>
/// Decorate action parameter to be allow be used on auhtorization policies using the
/// <see cref="ParameterPropertyBag"/> property bag.
/// </summary>
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
public class AbacParameterAttribute
: Attribute
{
/// <summary>
/// Modify the name of the parameter to be used on ABAC policies.
/// </summary>
public string Name { get; set; }
}
}
using System;

namespace Balea.Authorization.Abac
{
/// <summary>
/// Decorate action parameter to be allow be used on auhtorization policies using the
/// <see cref="ParameterPropertyBag"/> property bag.
/// </summary>
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
public class AbacParameterAttribute
: Attribute
{
/// <summary>
/// Modify the name of the parameter to be used on ABAC policies.
/// </summary>
public string Name { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
namespace Balea.Authorization.Abac
{
internal class AbacPrefix
{
private const string Prefix = "abac__";
public AbacPrefix(string policy)
{
Policy = policy.Replace(Prefix, string.Empty);
}
public string Policy { get; }
public override string ToString()
{
return $"{Prefix}{Policy}";
}
}
}
namespace Balea.Authorization.Abac
{
internal class AbacPrefix
{
private const string Prefix = "abac__";

public AbacPrefix(string policy)
{
Policy = policy.Replace(Prefix, string.Empty);
}

public string Policy { get; }

public override string ToString()
{
return $"{Prefix}{Policy}";
}
}
}
Loading