mirror of https://github.com/abpframework/abp.git
11 changed files with 135 additions and 124 deletions
@ -0,0 +1,23 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.AspNetCore.Http; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
using Volo.Abp.Security.Claims; |
||||
|
using Volo.Abp.Users; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Security.Claims; |
||||
|
|
||||
|
public class AbpDynamicClaimsMiddleware : IMiddleware, ITransientDependency |
||||
|
{ |
||||
|
public async Task InvokeAsync(HttpContext context, RequestDelegate next) |
||||
|
{ |
||||
|
var currentUser = context.RequestServices.GetRequiredService<ICurrentUser>(); |
||||
|
if (currentUser.IsAuthenticated) |
||||
|
{ |
||||
|
var abpClaimsPrincipalFactory = context.RequestServices.GetRequiredService<IAbpClaimsPrincipalFactory>(); |
||||
|
await abpClaimsPrincipalFactory.CreateDynamicAsync(context.User); |
||||
|
} |
||||
|
|
||||
|
await next(context); |
||||
|
} |
||||
|
} |
||||
@ -1,31 +0,0 @@ |
|||||
using System.Collections.Generic; |
|
||||
using Volo.Abp.Security.Claims; |
|
||||
|
|
||||
namespace Volo.Abp.AspNetCore.Security.Claims; |
|
||||
|
|
||||
public class AbpDynamicClaimsOptions |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// List of the claims that will be dynamically added/overriden to the current principal.
|
|
||||
/// Default values are:
|
|
||||
/// - <see cref="AbpClaimTypes.UserName"/>
|
|
||||
/// - <see cref="AbpClaimTypes.Role"/>
|
|
||||
/// - <see cref="AbpClaimTypes.Email"/>
|
|
||||
/// - <see cref="AbpClaimTypes.EmailVerified"/>
|
|
||||
/// - <see cref="AbpClaimTypes.PhoneNumber"/>
|
|
||||
/// - <see cref="AbpClaimTypes.PhoneNumberVerified"/>
|
|
||||
/// </summary>
|
|
||||
public List<string> DynamicClaims { get; } = new(); |
|
||||
|
|
||||
public string RemoteRefreshUrl { get; set; } = "/api/account/refresh-dynamic-claims"; |
|
||||
|
|
||||
public AbpDynamicClaimsOptions() |
|
||||
{ |
|
||||
DynamicClaims.Add(AbpClaimTypes.UserName); |
|
||||
DynamicClaims.Add(AbpClaimTypes.Role); |
|
||||
DynamicClaims.Add(AbpClaimTypes.Email); |
|
||||
DynamicClaims.Add(AbpClaimTypes.EmailVerified); |
|
||||
DynamicClaims.Add(AbpClaimTypes.PhoneNumber); |
|
||||
DynamicClaims.Add(AbpClaimTypes.PhoneNumberVerified); |
|
||||
} |
|
||||
} |
|
||||
@ -1,57 +0,0 @@ |
|||||
using System; |
|
||||
using System.Security.Claims; |
|
||||
using System.Threading.Tasks; |
|
||||
using Microsoft.Extensions.Caching.Distributed; |
|
||||
using Volo.Abp.DependencyInjection; |
|
||||
|
|
||||
namespace Volo.Abp.AspNetCore.Security.Claims; |
|
||||
|
|
||||
/* This will be used by DynamicClaimsMiddleware to get dynamic claims |
|
||||
* and override the claims in the current principle. |
|
||||
*/ |
|
||||
public class DynamicClaimService : IDynamicClaimService, ITransientDependency |
|
||||
{ |
|
||||
public Task<Claim[]> GetClaimsAsync() |
|
||||
{ |
|
||||
/* TODO: |
|
||||
* - If current user is not authenticated, return empty array. |
|
||||
* - Try to get from distributed cache for the current user, if available |
|
||||
* - If not available, call IDynamicClaimRefreshService.RefreshAsync() |
|
||||
* Then check from distributed cache again. If still not found, ignore it. |
|
||||
*/ |
|
||||
|
|
||||
throw new NotImplementedException(); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
public interface IDynamicClaimRefreshService |
|
||||
{ |
|
||||
Task RefreshAsync(); |
|
||||
} |
|
||||
|
|
||||
[Dependency(TryRegister = true)] |
|
||||
public class NullDynamicClaimRefreshService : IDynamicClaimRefreshService, ISingletonDependency |
|
||||
{ |
|
||||
public Task RefreshAsync() |
|
||||
{ |
|
||||
/* TODO: Set distributed cache with an empty claim list |
|
||||
*/ |
|
||||
throw new NotImplementedException(); |
|
||||
|
|
||||
/* TODO: |
|
||||
* IDynamicClaimRefreshService will have more implementations: |
|
||||
* - AccountDynamicClaimRefreshService is the real implementation |
|
||||
* that constructs claims using UserManager. |
|
||||
* It will be effective in a monolith application, where the account |
|
||||
* module is installed in the application |
|
||||
* - RemoteDynamicClaimRefreshService is used in a system (like a microservice solution) |
|
||||
* where the account module is located remotely. In that case, we make a REST call |
|
||||
* to the account service. |
|
||||
* RemoteDynamicClaimRefreshService should be registered only if |
|
||||
* NullDynamicClaimRefreshService is used. I mean AccountDynamicClaimRefreshService |
|
||||
* (in-process implementation) should work by default if available. |
|
||||
* RemoteDynamicClaimRefreshService uses AbpDynamicClaimsOptions.RemoteRefreshUrl (and issuer) |
|
||||
*/ |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
@ -1,9 +0,0 @@ |
|||||
using System.Security.Claims; |
|
||||
using System.Threading.Tasks; |
|
||||
|
|
||||
namespace Volo.Abp.AspNetCore.Security.Claims; |
|
||||
|
|
||||
public interface IDynamicClaimService |
|
||||
{ |
|
||||
Task<Claim[]> GetClaimsAsync(); |
|
||||
} |
|
||||
Loading…
Reference in new issue