Browse Source

Check `SessionId` in the `TryRevokeOldAccessTokensAsync` method

pull/19479/head
maliming 2 years ago
parent
commit
5333e1f71f
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 1
      Directory.Packages.props
  2. 1
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo.Abp.AspNetCore.Components.WebAssembly.csproj
  3. 13
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/WebAssemblyAuthenticationStateProvider.cs
  4. 4
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentitySessionRepository.cs
  5. 10
      modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentitySessionRepository.cs
  6. 14
      modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentitySessionRepository.cs

1
Directory.Packages.props

@ -161,6 +161,7 @@
<PackageVersion Include="System.Text.Encoding.CodePages" Version="8.0.0" />
<PackageVersion Include="System.Text.Encodings.Web" Version="8.0.0" />
<PackageVersion Include="System.Text.Json" Version="8.0.0" />
<PackageVersion Include="System.IdentityModel.Tokens.Jwt" Version="7.5.1" />
<PackageVersion Include="TimeZoneConverter" Version="6.1.0" />
<PackageVersion Include="Unidecode.NET" Version="2.1.0" />
<PackageVersion Include="xunit" Version="2.6.1" />

1
framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo.Abp.AspNetCore.Components.WebAssembly.csproj

@ -27,6 +27,7 @@
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" />
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" />
<PackageReference Include="IdentityModel" />
</ItemGroup>

13
framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/WebAssemblyAuthenticationStateProvider.cs

@ -1,5 +1,6 @@
using System;
using System.Collections.Concurrent;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Net.Http;
using System.Security.Claims;
@ -13,6 +14,7 @@ using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Microsoft.JSInterop;
using Volo.Abp.Security.Claims;
namespace Volo.Abp.AspNetCore.Components.WebAssembly;
@ -138,6 +140,17 @@ public class WebAssemblyAuthenticationStateProvider<TRemoteAuthenticationState,
continue;
}
if (!accessToken.IsNullOrWhiteSpace() && !currentAccessToken.IsNullOrWhiteSpace())
{
var handler = new JwtSecurityTokenHandler();
var currentSessionId = handler.ReadJwtToken(currentAccessToken)?.Claims?.FirstOrDefault(x => x.Type == AbpClaimTypes.SessionId);
var sessionId = handler.ReadJwtToken(accessToken)?.Claims?.FirstOrDefault(x => x.Type == AbpClaimTypes.SessionId);
if (sessionId?.Value == currentSessionId?.Value)
{
continue;
}
}
var httpClient = HttpClientFactory.CreateClient(nameof(WebAssemblyAuthenticationStateProvider<TRemoteAuthenticationState, TAccount, TProviderOptions>));
var result = await httpClient.RevokeTokenAsync(new TokenRevocationRequest
{

4
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentitySessionRepository.cs

@ -12,6 +12,10 @@ public interface IIdentitySessionRepository : IBasicRepository<IdentitySession,
Task<IdentitySession> GetAsync(string sessionId, CancellationToken cancellationToken = default);
Task<bool> ExistAsync(Guid id, CancellationToken cancellationToken = default);
Task<bool> ExistAsync(string sessionId, CancellationToken cancellationToken = default);
Task<List<IdentitySession>> GetListAsync(
string sorting = null,
int maxResultCount = int.MaxValue,

10
modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentitySessionRepository.cs

@ -36,6 +36,16 @@ public class EfCoreIdentitySessionRepository : EfCoreRepository<IIdentityDbConte
return session;
}
public virtual async Task<bool> ExistAsync(Guid id, CancellationToken cancellationToken = default)
{
return await (await GetDbSetAsync()).AnyAsync(x => x.Id == id, GetCancellationToken(cancellationToken));
}
public virtual async Task<bool> ExistAsync(string sessionId, CancellationToken cancellationToken = default)
{
return await (await GetDbSetAsync()).AnyAsync(x => x.SessionId == sessionId, GetCancellationToken(cancellationToken));
}
public virtual async Task<List<IdentitySession>> GetListAsync(
string sorting = null,
int maxResultCount = int.MaxValue,

14
modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentitySessionRepository.cs

@ -39,6 +39,20 @@ public class MongoIdentitySessionRepository : MongoDbRepository<IAbpIdentityMong
return session;
}
public virtual async Task<bool> ExistAsync(Guid id, CancellationToken cancellationToken = default)
{
return await (await GetMongoQueryableAsync(GetCancellationToken(cancellationToken)))
.As<IMongoQueryable<IdentitySession>>()
.AnyAsync(x => x.Id == id, GetCancellationToken(cancellationToken));
}
public virtual async Task<bool> ExistAsync(string sessionId, CancellationToken cancellationToken = default)
{
return await (await GetMongoQueryableAsync(GetCancellationToken(cancellationToken)))
.As<IMongoQueryable<IdentitySession>>()
.AnyAsync(x => x.SessionId == sessionId, GetCancellationToken(cancellationToken));
}
public virtual async Task<List<IdentitySession>> GetListAsync(
string sorting = null,
int maxResultCount = int.MaxValue,

Loading…
Cancel
Save