Skip to content

Commit

Permalink
Merge pull request #990 from colinin/fix-transaction
Browse files Browse the repository at this point in the history
Fix transaction
  • Loading branch information
colinin committed Aug 16, 2024
2 parents 47aa6b5 + d242320 commit 4accea7
Show file tree
Hide file tree
Showing 15 changed files with 168 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public interface IIdentitySessionStore
/// <param name="userId">用户id</param>
/// <param name="clientId">客户端id</param>
/// <param name="ipAddresses">ip地址</param>
/// <param name="signedIn">登录时间</param>
/// <param name="lastAccessed">上次访问时间</param>
/// <param name="tenantId">租户id</param>
/// <param name="cancellationToken"></param>
/// <returns>创建完成的 <seealso cref="IdentitySession"/></returns>
Expand All @@ -29,6 +31,8 @@ Task<IdentitySession> CreateAsync(
Guid userId,
string clientId,
string ipAddresses,
DateTime signedIn,
DateTime? lastAccessed = null,
Guid? tenantId = null,
CancellationToken cancellationToken = default);
/// <summary>
Expand Down Expand Up @@ -124,7 +128,7 @@ Task RevokeAsync(
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task RevokeAllAsync(
Guid userId,
Guid userId,
Guid? exceptSessionId = null,
CancellationToken cancellationToken = default);
/// <summary>
Expand All @@ -136,7 +140,7 @@ Task RevokeAllAsync(
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task RevokeAllAsync(
Guid userId,
Guid userId,
string device,
Guid? exceptSessionId = null,
CancellationToken cancellationToken = default);
Expand All @@ -147,7 +151,7 @@ Task RevokeAllAsync(
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task RevokeAllAsync(
TimeSpan inactiveTimeSpan,
TimeSpan inactiveTimeSpan,
CancellationToken cancellationToken = default);
/// <summary>
/// 撤销指定的会话
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,64 +7,86 @@
using Volo.Abp.Auditing;
using Volo.Abp.Domain.Services;
using Volo.Abp.Identity;
using Volo.Abp.Timing;

namespace LINGYUN.Abp.Identity.Session;
public class IdentitySessionManager : DomainService, IIdentitySessionManager
{
protected IDeviceInfoProvider DeviceInfoProvider { get; }
protected IIdentitySessionCache IdentitySessionCache { get; }
protected IIdentitySessionStore IdentitySessionStore { get; }
protected IdentityDynamicClaimsPrincipalContributorCache IdentityDynamicClaimsPrincipalContributorCache { get; }

public IdentitySessionManager(
IDeviceInfoProvider deviceInfoProvider,
IIdentitySessionCache identitySessionCache,
IIdentitySessionStore identitySessionStore,
IdentityDynamicClaimsPrincipalContributorCache identityDynamicClaimsPrincipalContributorCache)
{
DeviceInfoProvider = deviceInfoProvider;
IdentitySessionCache = identitySessionCache;
IdentitySessionStore = identitySessionStore;
IdentityDynamicClaimsPrincipalContributorCache = identityDynamicClaimsPrincipalContributorCache;
}

[DisableAuditing]
public async virtual Task SaveSessionAsync(
ClaimsPrincipal claimsPrincipal,
ClaimsPrincipal claimsPrincipal,
CancellationToken cancellationToken = default)
{
{
if (claimsPrincipal != null)
{
var userId = claimsPrincipal.FindUserId();
var sessionId = claimsPrincipal.FindSessionId();
if (!userId.HasValue || sessionId.IsNullOrWhiteSpace())
{
return;
}
if (await IdentitySessionStore.ExistAsync(sessionId, cancellationToken))
var tenantId = claimsPrincipal.FindTenantId();

using (CurrentTenant.Change(tenantId))
{
return;
}
var deviceInfo = DeviceInfoProvider.DeviceInfo;
var sessionId = claimsPrincipal.FindSessionId();
if (!userId.HasValue || sessionId.IsNullOrWhiteSpace())
{
return;
}
if (await IdentitySessionStore.ExistAsync(sessionId, cancellationToken))
{
return;
}
var deviceInfo = DeviceInfoProvider.DeviceInfo;

var device = deviceInfo.Device ?? IdentitySessionDevices.OAuth;
var deviceDesc = deviceInfo.Description;
var clientIpAddress = deviceInfo.ClientIpAddress;
var device = deviceInfo.Device ?? IdentitySessionDevices.OAuth;
var deviceDesc = deviceInfo.Description;
var clientIpAddress = deviceInfo.ClientIpAddress;

var tenantId = claimsPrincipal.FindTenantId();
var clientId = claimsPrincipal.FindClientId();
var clientId = claimsPrincipal.FindClientId();

Logger.LogDebug($"Save user session for user: {userId}, session: {sessionId}");

Logger.LogDebug($"Save user session for user: {userId}, session: {sessionId}");
await IdentitySessionStore.CreateAsync(
sessionId,
device,
deviceDesc,
userId.Value,
clientId,
clientIpAddress,
Clock.Now,
Clock.Now,
tenantId,
cancellationToken);

await IdentitySessionStore.CreateAsync(
sessionId,
device,
deviceDesc,
userId.Value,
clientId,
clientIpAddress,
tenantId,
cancellationToken);
Logger.LogDebug($"Remove dynamic claims cache for user: {userId}");

Logger.LogDebug($"Remove dynamic claims cache for user: {userId}");
await IdentityDynamicClaimsPrincipalContributorCache.ClearAsync(userId.Value, tenantId);
await IdentityDynamicClaimsPrincipalContributorCache.ClearAsync(userId.Value, tenantId);

await IdentitySessionCache.RefreshAsync(sessionId,
new IdentitySessionCacheItem(
device,
deviceDesc,
userId.Value,
sessionId,
clientId,
clientIpAddress,
Clock.Now,
Clock.Now));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,20 @@
using Volo.Abp.DependencyInjection;
using Volo.Abp.Guids;
using Volo.Abp.Identity;
using Volo.Abp.Timing;
using Volo.Abp.Users;

namespace LINGYUN.Abp.Identity.Session;
public class IdentitySessionStore : IIdentitySessionStore, ITransientDependency
{
protected IClock Clock { get; }
protected ICurrentUser CurrentUser { get; }
protected IGuidGenerator GuidGenerator { get; }
protected IIdentitySessionRepository IdentitySessionRepository { get; }

public IdentitySessionStore(
IClock clock,
ICurrentUser currentUser,
IGuidGenerator guidGenerator,
IIdentitySessionRepository identitySessionRepository)
{
Clock = clock;
CurrentUser = currentUser;
GuidGenerator = guidGenerator;
IdentitySessionRepository = identitySessionRepository;
Expand All @@ -37,6 +33,8 @@ public async virtual Task<IdentitySession> CreateAsync(
Guid userId,
string clientId,
string ipAddresses,
DateTime signedIn,
DateTime? lastAccessed = null,
Guid? tenantId = null,
CancellationToken cancellationToken = default)
{
Expand All @@ -52,8 +50,8 @@ public async virtual Task<IdentitySession> CreateAsync(
tenantId,
clientId,
ipAddresses,
Clock.Now,
Clock.Now
signedIn,
lastAccessed
);

identitySession = await IdentitySessionRepository.InsertAsync(identitySession, cancellationToken: cancellationToken);
Expand Down Expand Up @@ -90,22 +88,22 @@ public async virtual Task<IdentitySession> GetAsync(
}

public async virtual Task<IdentitySession> FindAsync(
string sessionId,
string sessionId,
CancellationToken cancellationToken = default)
{
return await IdentitySessionRepository.FindAsync(sessionId, cancellationToken: cancellationToken);
}

public async virtual Task<IdentitySession> FindLastAsync(
Guid userId,
Guid userId,
string device,
CancellationToken cancellationToken = default)
{
return await IdentitySessionRepository.FindLastAsync(userId, device, cancellationToken: cancellationToken);
}

public async virtual Task<bool> ExistAsync(
string sessionId,
string sessionId,
CancellationToken cancellationToken = default)
{
return await IdentitySessionRepository.ExistAsync(sessionId, cancellationToken: cancellationToken);
Expand All @@ -126,16 +124,16 @@ public async virtual Task RevokeAsync(
}

public async virtual Task RevokeAllAsync(
Guid userId,
Guid userId,
Guid? exceptSessionId = null,
CancellationToken cancellationToken = default)
{
await IdentitySessionRepository.DeleteAllAsync(userId, exceptSessionId, cancellationToken: cancellationToken);
}

public async virtual Task RevokeAllAsync(
Guid userId,
string device,
Guid userId,
string device,
Guid? exceptSessionId = null,
CancellationToken cancellationToken = default)
{
Expand All @@ -150,8 +148,8 @@ public async virtual Task RevokeAllAsync(
}

public async virtual Task RevokeWithAsync(
Guid userId,
string device = null,
Guid userId,
string device = null,
Guid? exceptSessionId = null,
int maxCount = 0,
CancellationToken cancellationToken = default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ public class PortalGrantValidator : IExtensionGrantValidator
private readonly IHttpContextAccessor _httpContextAccessor;

public PortalGrantValidator(
ILogger<PortalGrantValidator> logger,
IOptions<IdentityServerOptions> options,
IEventService events,
IResourceOwnerPasswordValidator resourceOwnerValidator,
IdentitySecurityLogManager identitySecurityLogManager,
UserManager<IdentityUser> userManager,
ICurrentTenant currentTenant,
ILogger<PortalGrantValidator> logger,
IOptions<IdentityServerOptions> options,
IEventService events,
IResourceOwnerPasswordValidator resourceOwnerValidator,
IdentitySecurityLogManager identitySecurityLogManager,
UserManager<IdentityUser> userManager,
ICurrentTenant currentTenant,
IEnterpriseRepository enterpriseRepository,
IOptions<AbpAspNetCoreMultiTenancyOptions> multiTenancyOptions,
IHttpContextAccessor httpContextAccessor)
Expand Down Expand Up @@ -89,7 +89,7 @@ public async virtual Task ValidateAsync(ExtensionGrantValidationContext context)
Guid? tenantId = null;
using (_currentTenant.Change(null))
{
var enterprise = parameters.Get("EnterpriseId");
var enterprise = parameters.Get("enterpriseId") ?? parameters.Get("EnterpriseId");
if (enterprise.IsNullOrWhiteSpace() || !Guid.TryParse(enterprise, out var enterpriseId))
{
// TODO: configurabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Modularity;
using Volo.Abp.OpenIddict;
using static OpenIddict.Abstractions.OpenIddictConstants;

namespace LINGYUN.Abp.OpenIddict.AspNetCore.Session;

Expand Down Expand Up @@ -31,5 +32,10 @@ public override void ConfigureServices(ServiceConfigurationContext context)
options.SignInSessionEnabled = true;
options.SignOutSessionEnabled = true;
});

Configure<AbpOpenIddictAspNetCoreSessionOptions>(options =>
{
options.PersistentSessionGrantTypes.Add(GrantTypes.Password);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;

namespace LINGYUN.Abp.OpenIddict.AspNetCore.Session;
public class AbpOpenIddictAspNetCoreSessionOptions
{
public List<string> PersistentSessionGrantTypes { get; set; }
public AbpOpenIddictAspNetCoreSessionOptions()
{
PersistentSessionGrantTypes = new List<string>();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using LINGYUN.Abp.Identity.Session;
using OpenIddict.Abstractions;
using Microsoft.Extensions.Options;
using OpenIddict.Server;
using System.Threading.Tasks;

Expand All @@ -10,6 +10,7 @@ namespace LINGYUN.Abp.OpenIddict.AspNetCore.Session;
public class ProcessSignInIdentitySession : IOpenIddictServerHandler<OpenIddictServerEvents.ProcessSignInContext>
{
protected IIdentitySessionManager IdentitySessionManager { get; }
protected AbpOpenIddictAspNetCoreSessionOptions AbpOpenIddictAspNetCoreSessionOptions { get; }

public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<OpenIddictServerEvents.ProcessSignInContext>()
Expand All @@ -19,14 +20,18 @@ public class ProcessSignInIdentitySession : IOpenIddictServerHandler<OpenIddictS
.SetType(OpenIddictServerHandlerType.Custom)
.Build();

public ProcessSignInIdentitySession(IIdentitySessionManager identitySessionManager)
public ProcessSignInIdentitySession(
IIdentitySessionManager identitySessionManager,
IOptions<AbpOpenIddictAspNetCoreSessionOptions> abpOpenIddictAspNetCoreSessionOptions)
{
IdentitySessionManager = identitySessionManager;
AbpOpenIddictAspNetCoreSessionOptions = abpOpenIddictAspNetCoreSessionOptions.Value;
}

public async virtual ValueTask HandleAsync(OpenIddictServerEvents.ProcessSignInContext context)
{
if (context.Request.IsPasswordGrantType() && context.Principal != null)
if (AbpOpenIddictAspNetCoreSessionOptions.PersistentSessionGrantTypes.Contains(context.Request.GrantType) &&
context.Principal != null)
{
await IdentitySessionManager.SaveSessionAsync(context.Principal, context.CancellationToken);
}
Expand Down
Loading

0 comments on commit 4accea7

Please sign in to comment.