Browse Source

feat: Fixed the PAR request failed to be repaired

pull/1566/head
colin 3 weeks ago
parent
commit
e34fb189c9
  1. 4
      aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.AspNetCore.Session/LINGYUN/Abp/OpenIddict/AspNetCore/Session/AbpOpenIddictAspNetCoreSessionModule.cs
  2. 5
      aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.AspNetCore.Session/LINGYUN/Abp/OpenIddict/AspNetCore/Session/AbpOpenIddictAspNetCoreSessionOptions.cs
  3. 15
      aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.AspNetCore.Session/LINGYUN/Abp/OpenIddict/AspNetCore/Session/ProcessSignInIdentitySession.cs
  4. 5
      aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.AspNetCore.Session/LINGYUN/Abp/OpenIddict/AspNetCore/Session/RevocationIdentitySession.cs
  5. 26
      aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.AspNetCore.Session/LINGYUN/Abp/OpenIddict/AspNetCore/Session/ServerValidationTokenCheckIdentitySession.cs
  6. 8
      aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.AspNetCore.Session/LINGYUN/Abp/OpenIddict/AspNetCore/Session/ValidationTokenCheckIdentitySession.cs

4
aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.AspNetCore.Session/LINGYUN/Abp/OpenIddict/AspNetCore/Session/AbpOpenIddictAspNetCoreSessionModule.cs

@ -37,6 +37,10 @@ public class AbpOpenIddictAspNetCoreSessionModule : AbpModule
Configure<AbpOpenIddictAspNetCoreSessionOptions>(options =>
{
options.PersistentSessionGrantTypes.Add(GrantTypes.Password);
options.ValidationSessionEndpointTypes.Add(OpenIddictServerEndpointType.Token);
options.ValidationSessionEndpointTypes.Add(OpenIddictServerEndpointType.UserInfo);
options.ValidationSessionEndpointTypes.Add(OpenIddictServerEndpointType.Introspection);
options.ValidationSessionEndpointTypes.Add(OpenIddictServerEndpointType.Revocation);
});
context.Services.Add(ValidationTokenCheckIdentitySession.Descriptor.ServiceDescriptor);

5
aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.AspNetCore.Session/LINGYUN/Abp/OpenIddict/AspNetCore/Session/AbpOpenIddictAspNetCoreSessionOptions.cs

@ -1,11 +1,14 @@
using System.Collections.Generic;
using OpenIddict.Server;
using System.Collections.Generic;
namespace LINGYUN.Abp.OpenIddict.AspNetCore.Session;
public class AbpOpenIddictAspNetCoreSessionOptions
{
public List<string> PersistentSessionGrantTypes { get; set; }
public List<OpenIddictServerEndpointType> ValidationSessionEndpointTypes { get; set; }
public AbpOpenIddictAspNetCoreSessionOptions()
{
PersistentSessionGrantTypes = new List<string>();
ValidationSessionEndpointTypes = new List<OpenIddictServerEndpointType>();
}
}

15
aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.AspNetCore.Session/LINGYUN/Abp/OpenIddict/AspNetCore/Session/ProcessSignInIdentitySession.cs

@ -1,7 +1,10 @@
using LINGYUN.Abp.Identity.Session;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using OpenIddict.Server;
using System;
using System.Security.Principal;
using System.Threading.Tasks;
namespace LINGYUN.Abp.OpenIddict.AspNetCore.Session;
@ -10,6 +13,8 @@ namespace LINGYUN.Abp.OpenIddict.AspNetCore.Session;
/// </summary>
public class ProcessSignInIdentitySession : IOpenIddictServerHandler<OpenIddictServerEvents.ProcessSignInContext>
{
public ILogger<ProcessSignInIdentitySession> Logger { protected get; set; }
protected IIdentitySessionManager IdentitySessionManager { get; }
protected AbpOpenIddictAspNetCoreSessionOptions AbpOpenIddictAspNetCoreSessionOptions { get; }
@ -27,6 +32,8 @@ public class ProcessSignInIdentitySession : IOpenIddictServerHandler<OpenIddictS
{
IdentitySessionManager = identitySessionManager;
AbpOpenIddictAspNetCoreSessionOptions = abpOpenIddictAspNetCoreSessionOptions.Value;
Logger = NullLogger<ProcessSignInIdentitySession>.Instance;
}
public async virtual ValueTask HandleAsync(OpenIddictServerEvents.ProcessSignInContext context)
@ -35,7 +42,15 @@ public class ProcessSignInIdentitySession : IOpenIddictServerHandler<OpenIddictS
AbpOpenIddictAspNetCoreSessionOptions.PersistentSessionGrantTypes.Contains(context.Request.GrantType) &&
context.Principal != null)
{
Logger.LogInformation("Saving session for grant type: {grantType}", context.Request.GrantType);
await IdentitySessionManager.SaveSessionAsync(context.Principal, context.CancellationToken);
Logger.LogInformation("Session saved successfully: {sessionId}", context.Principal.FindSessionId());
}
else
{
Logger.LogDebug("Skipping session save for grant type: {grantType}", context.Request.GrantType);
}
}
}

5
aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.AspNetCore.Session/LINGYUN/Abp/OpenIddict/AspNetCore/Session/RevocationIdentitySession.cs

@ -1,4 +1,6 @@
using LINGYUN.Abp.Identity.Session;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using OpenIddict.Server;
using System;
using System.Security.Principal;
@ -11,6 +13,7 @@ namespace LINGYUN.Abp.OpenIddict.AspNetCore.Session;
/// </summary>
public class RevocationIdentitySession : IOpenIddictServerHandler<OpenIddictServerEvents.HandleRevocationRequestContext>
{
public ILogger<RevocationIdentitySession> Logger { protected get; set; }
protected ICurrentTenant CurrentTenant { get; }
protected IIdentitySessionManager IdentitySessionManager { get; }
@ -28,6 +31,8 @@ public class RevocationIdentitySession : IOpenIddictServerHandler<OpenIddictServ
{
CurrentTenant = currentTenant;
IdentitySessionManager = identitySessionManager;
Logger = NullLogger<RevocationIdentitySession>.Instance;
}
public async virtual ValueTask HandleAsync(OpenIddictServerEvents.HandleRevocationRequestContext context)

26
aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.AspNetCore.Session/LINGYUN/Abp/OpenIddict/AspNetCore/Session/ServerValidationTokenCheckIdentitySession.cs

@ -1,5 +1,7 @@
using LINGYUN.Abp.Identity.Session;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using OpenIddict.Server;
using System.Security.Principal;
using System.Threading.Tasks;
@ -9,8 +11,10 @@ using static OpenIddict.Abstractions.OpenIddictConstants;
namespace LINGYUN.Abp.OpenIddict.AspNetCore.Session;
public class ServerValidationTokenCheckIdentitySession : IOpenIddictServerHandler<OpenIddictServerEvents.ValidateTokenContext>
{
public ILogger<ServerValidationTokenCheckIdentitySession> Logger { protected get; set; }
protected ICurrentTenant CurrentTenant { get; }
protected IIdentitySessionChecker IdentitySessionChecker { get; }
protected AbpOpenIddictAspNetCoreSessionOptions AbpOpenIddictAspNetCoreSessionOptions { get; }
public static OpenIddictServerHandlerDescriptor Descriptor { get; } =
OpenIddictServerHandlerDescriptor.CreateBuilder<OpenIddictServerEvents.ValidateTokenContext>()
@ -20,20 +24,38 @@ public class ServerValidationTokenCheckIdentitySession : IOpenIddictServerHandle
public ServerValidationTokenCheckIdentitySession(
ICurrentTenant currentTenant,
IIdentitySessionChecker identitySessionChecker)
IIdentitySessionChecker identitySessionChecker,
IOptions<AbpOpenIddictAspNetCoreSessionOptions> abpOpenIddictAspNetCoreSessionOptions)
{
CurrentTenant = currentTenant;
IdentitySessionChecker = identitySessionChecker;
AbpOpenIddictAspNetCoreSessionOptions = abpOpenIddictAspNetCoreSessionOptions.Value;
Logger = NullLogger<ServerValidationTokenCheckIdentitySession>.Instance;
}
public async virtual ValueTask HandleAsync(OpenIddictServerEvents.ValidateTokenContext context)
{
Logger.LogInformation("Server Validate Token: {endpointType} - {requestUri}", context.EndpointType, context.RequestUri);
if (!AbpOpenIddictAspNetCoreSessionOptions.ValidationSessionEndpointTypes.Contains(context.EndpointType))
{
Logger.LogDebug("Endpoint '{endpointType}' is not in validation whitelist, skipping session validation.", context.EndpointType);
return;
}
if (context.Principal == null || context.Principal.Identity?.IsAuthenticated == false)
{
Logger.LogWarning("Principal is null or not authenticated for endpoint '{endpointType}', skipping session validation.", context.EndpointType);
return;
}
var tenantId = context.Principal?.FindTenantId();
using (CurrentTenant.Change(tenantId))
{
if (!await IdentitySessionChecker.ValidateSessionAsync(context.Principal!))
{
context.Logger.LogWarning("The token is no longer valid because the user's session expired.");
Logger.LogWarning("The token is no longer valid because the user's session expired.");
// Errors.InvalidToken ---> 401
// Errors.ExpiredToken ---> 400
context.Reject(Errors.InvalidToken, "The user session has expired.");

8
aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.AspNetCore.Session/LINGYUN/Abp/OpenIddict/AspNetCore/Session/ValidationTokenCheckIdentitySession.cs

@ -1,5 +1,6 @@
using LINGYUN.Abp.Identity.Session;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using OpenIddict.Validation;
using System.Security.Principal;
using System.Threading.Tasks;
@ -9,6 +10,7 @@ using static OpenIddict.Abstractions.OpenIddictConstants;
namespace LINGYUN.Abp.OpenIddict.AspNetCore.Session;
public class ValidationTokenCheckIdentitySession : IOpenIddictValidationHandler<OpenIddictValidationEvents.ValidateTokenContext>
{
public ILogger<ValidationTokenCheckIdentitySession> Logger { protected get; set; }
protected ICurrentTenant CurrentTenant { get; }
protected IIdentitySessionChecker IdentitySessionChecker { get; }
@ -25,16 +27,20 @@ public class ValidationTokenCheckIdentitySession : IOpenIddictValidationHandler<
{
CurrentTenant = currentTenant;
IdentitySessionChecker = identitySessionChecker;
Logger = NullLogger<ValidationTokenCheckIdentitySession>.Instance;
}
public async virtual ValueTask HandleAsync(OpenIddictValidationEvents.ValidateTokenContext context)
{
Logger.LogInformation("Validate Token: {endpointType} - {requestUri}", context.EndpointType, context.RequestUri);
var tenantId = context.Principal?.FindTenantId();
using (CurrentTenant.Change(tenantId))
{
if (!await IdentitySessionChecker.ValidateSessionAsync(context.Principal!))
{
context.Logger.LogWarning("The token is no longer valid because the user's session expired.");
Logger.LogWarning("The token is no longer valid because the user's session expired.");
// Errors.InvalidToken ---> 401
// Errors.ExpiredToken ---> 400
context.Reject(Errors.InvalidToken, "The user session has expired.");

Loading…
Cancel
Save