diff --git a/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Grants/IPersistentGrantRepository.cs b/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Grants/IPersistentGrantRepository.cs new file mode 100644 index 0000000000..709fe00d62 --- /dev/null +++ b/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Grants/IPersistentGrantRepository.cs @@ -0,0 +1,17 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using Volo.Abp.Domain.Repositories; + +namespace Volo.Abp.IdentityServer.Grants +{ + public interface IPersistentGrantRepository : IRepository + { + Task FindByKeyAsync(string key); + + Task> GetListBySubjectIdAsync(string key); + + Task DeleteAsync(string subjectId, string clientId); + + Task DeleteAsync(string subjectId, string clientId, string type); + } +} \ No newline at end of file diff --git a/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Grants/PersistedGrantStore.cs b/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Grants/PersistedGrantStore.cs new file mode 100644 index 0000000000..409353167e --- /dev/null +++ b/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/Grants/PersistedGrantStore.cs @@ -0,0 +1,70 @@ +using System.Collections.Generic; +using System.Linq; +using System.Linq.Dynamic.Core; +using System.Threading.Tasks; +using IdentityServer4.Stores; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Domain.Repositories; +using Volo.Abp.ObjectMapping; + +namespace Volo.Abp.IdentityServer.Grants +{ + public class PersistedGrantStore : IPersistedGrantStore, ITransientDependency + { + private readonly IPersistentGrantRepository _persistentGrantRepository; + private readonly IObjectMapper _objectMapper; + + public PersistedGrantStore(IPersistentGrantRepository persistentGrantRepository, IObjectMapper objectMapper) + { + _persistentGrantRepository = persistentGrantRepository; + _objectMapper = objectMapper; + } + + public virtual async Task StoreAsync(IdentityServer4.Models.PersistedGrant grant) + { + var entity = _objectMapper.Map(grant); + var existing = _persistentGrantRepository.FindByKeyAsync(grant.Key); + if (existing == null) + { + await _persistentGrantRepository.InsertAsync(entity); + } + else + { + await _persistentGrantRepository.UpdateAsync(entity); + } + } + + public virtual async Task GetAsync(string key) + { + var persistedGrant = await _persistentGrantRepository.FindByKeyAsync(key); + return _objectMapper.Map(persistedGrant); + } + + public virtual async Task> GetAllAsync(string subjectId) + { + var persistedGrants = await _persistentGrantRepository.GetListBySubjectIdAsync(subjectId); + return persistedGrants.Select(x => _objectMapper.Map(x)); + } + + public virtual async Task RemoveAsync(string key) + { + var persistedGrant = await _persistentGrantRepository.FindByKeyAsync(key); + if (persistedGrant == null) + { + return; + } + + await _persistentGrantRepository.DeleteAsync(persistedGrant); + } + + public virtual async Task RemoveAllAsync(string subjectId, string clientId) + { + await _persistentGrantRepository.DeleteAsync(subjectId, clientId); + } + + public virtual async Task RemoveAllAsync(string subjectId, string clientId, string type) + { + await _persistentGrantRepository.DeleteAsync(subjectId, clientId, type); + } + } +} diff --git a/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/PersistedGrantRepository.cs b/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/PersistedGrantRepository.cs new file mode 100644 index 0000000000..0b1011992c --- /dev/null +++ b/src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/PersistedGrantRepository.cs @@ -0,0 +1,50 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Volo.Abp.Domain.Repositories.EntityFrameworkCore; +using Volo.Abp.EntityFrameworkCore; +using Volo.Abp.IdentityServer.EntityFrameworkCore; +using Volo.Abp.IdentityServer.Grants; + +namespace Volo.Abp.IdentityServer +{ + public class PersistentGrantRepository : EfCoreRepository, IPersistentGrantRepository + { + public PersistentGrantRepository(IDbContextProvider dbContextProvider) : base(dbContextProvider) + { + + } + + public Task FindByKeyAsync(string key) + { + return DbSet.FirstOrDefaultAsync(x => x.Key == key); + } + + public Task> GetListBySubjectIdAsync(string subjectId) + { + return DbSet.Where(x => x.SubjectId == subjectId).ToListAsync(); + } + + public Task DeleteAsync(string subjectId, string clientId) + { + DbSet.RemoveRange( + DbSet.Where(x => x.SubjectId == subjectId && x.ClientId == clientId) + ); + + return Task.FromResult(0); + } + + public Task DeleteAsync(string subjectId, string clientId, string type) + { + DbSet.RemoveRange( + DbSet.Where(x => + x.SubjectId == subjectId && + x.ClientId == clientId && + x.Type == type) + ); + + return Task.FromResult(0); + } + } +}