mirror of https://github.com/abpframework/abp.git
committed by
GitHub
11 changed files with 532 additions and 7 deletions
@ -0,0 +1,9 @@ |
|||
using Volo.Abp.Domain.Repositories; |
|||
|
|||
namespace Volo.Abp.IdentityServer.ApiResources |
|||
{ |
|||
public interface IApiResourceRepository : IRepository<ApiResource> |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -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<PersistedGrant> |
|||
{ |
|||
Task<PersistedGrant> FindByKeyAsync(string key); |
|||
|
|||
Task<List<PersistedGrant>> GetListBySubjectIdAsync(string key); |
|||
|
|||
Task DeleteAsync(string subjectId, string clientId); |
|||
|
|||
Task DeleteAsync(string subjectId, string clientId, string type); |
|||
} |
|||
} |
|||
@ -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<IdentityServer4.Models.PersistedGrant, PersistedGrant>(grant); |
|||
var existing = await _persistentGrantRepository.FindByKeyAsync(grant.Key); |
|||
if (existing == null) |
|||
{ |
|||
await _persistentGrantRepository.InsertAsync(entity); |
|||
} |
|||
else |
|||
{ |
|||
await _persistentGrantRepository.UpdateAsync(entity); |
|||
} |
|||
} |
|||
|
|||
public virtual async Task<IdentityServer4.Models.PersistedGrant> GetAsync(string key) |
|||
{ |
|||
var persistedGrant = await _persistentGrantRepository.FindByKeyAsync(key); |
|||
return _objectMapper.Map<PersistedGrant, IdentityServer4.Models.PersistedGrant>(persistedGrant); |
|||
} |
|||
|
|||
public virtual async Task<IEnumerable<IdentityServer4.Models.PersistedGrant>> GetAllAsync(string subjectId) |
|||
{ |
|||
var persistedGrants = await _persistentGrantRepository.GetListBySubjectIdAsync(subjectId); |
|||
return persistedGrants.Select(x => _objectMapper.Map<PersistedGrant, IdentityServer4.Models.PersistedGrant>(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); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,15 @@ |
|||
using Volo.Abp.Domain.Repositories.EntityFrameworkCore; |
|||
using Volo.Abp.EntityFrameworkCore; |
|||
using Volo.Abp.IdentityServer.ApiResources; |
|||
using Volo.Abp.IdentityServer.EntityFrameworkCore; |
|||
|
|||
namespace Volo.Abp.IdentityServer |
|||
{ |
|||
public class ApiResourceRepository : EfCoreRepository<IdentityServerDbContext, ApiResource>, IApiResourceRepository |
|||
{ |
|||
public ApiResourceRepository(IDbContextProvider<IdentityServerDbContext> dbContextProvider) : base(dbContextProvider) |
|||
{ |
|||
|
|||
} |
|||
} |
|||
} |
|||
@ -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<IdentityServerDbContext, PersistedGrant>, IPersistentGrantRepository |
|||
{ |
|||
public PersistentGrantRepository(IDbContextProvider<IdentityServerDbContext> dbContextProvider) : base(dbContextProvider) |
|||
{ |
|||
|
|||
} |
|||
|
|||
public Task<PersistedGrant> FindByKeyAsync(string key) |
|||
{ |
|||
return DbSet.FirstOrDefaultAsync(x => x.Key == key); |
|||
} |
|||
|
|||
public Task<List<PersistedGrant>> 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); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,92 @@ |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using IdentityServer4.Models; |
|||
using IdentityServer4.Stores; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Shouldly; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.IdentityServer.Clients |
|||
{ |
|||
public class IdentityResourceStore_Tests : AbpIdentityServerTestBase |
|||
{ |
|||
private readonly IResourceStore _resourceStore; |
|||
|
|||
public IdentityResourceStore_Tests() |
|||
{ |
|||
_resourceStore = ServiceProvider.GetRequiredService<IResourceStore>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task FindApiResourceAsync_Should_Return_Null_If_Not_Found() |
|||
{ |
|||
//Act
|
|||
var resource = await _resourceStore.FindApiResourceAsync("non-existing-name"); |
|||
|
|||
//Assert
|
|||
resource.ShouldBeNull(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task FindApiResourceAsync_Should_Return_If_Found() |
|||
{ |
|||
//Act
|
|||
var apiResource = await _resourceStore.FindApiResourceAsync("Test-ApiResource-Name-1"); |
|||
|
|||
//Assert
|
|||
apiResource.ShouldNotBe(null); |
|||
apiResource.Name.ShouldBe("Test-ApiResource-Name-1"); |
|||
apiResource.Description.ShouldBe("Test-ApiResource-Description-1"); |
|||
apiResource.DisplayName.ShouldBe("Test-ApiResource-DisplayName-1"); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task FindApiResourcesByScopeAsync_Should_Return_If_Found() |
|||
{ |
|||
//Act
|
|||
var apiResourcesByScope = await _resourceStore.FindApiResourcesByScopeAsync(new List<string> |
|||
{ |
|||
"Test-ApiResource-ApiScope-Name-1" |
|||
}); |
|||
|
|||
//Assert
|
|||
var apiResources = apiResourcesByScope as ApiResource[] ?? apiResourcesByScope.ToArray(); |
|||
apiResources.ShouldNotBe(null); |
|||
|
|||
apiResources[0].Scopes.GroupBy(x => x.Name).Count().ShouldBe(1); |
|||
apiResources[0].Scopes.GroupBy(x => x.Name).First().Key.ShouldBe("Test-ApiResource-ApiScope-Name-1"); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task FindIdentityResourcesByScopeAsync_Should_Return_For_Given_Scopes() |
|||
{ |
|||
//Act
|
|||
var identityResourcesByScope = await _resourceStore.FindIdentityResourcesByScopeAsync(new List<string> |
|||
{ |
|||
"Test-Identity-Resource-Name-1" |
|||
}); |
|||
|
|||
//Assert
|
|||
var resourcesByScope = identityResourcesByScope as IdentityResource[] ?? identityResourcesByScope.ToArray(); |
|||
resourcesByScope.Length.ShouldBe(1); |
|||
resourcesByScope.First().DisplayName.ShouldBe("Test-Identity-Resource-DisplayName-1"); |
|||
resourcesByScope.First().Description.ShouldBe("Test-Identity-Resource-Description-1"); |
|||
resourcesByScope.First().Required.ShouldBe(true); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task GetAllResourcesAsync_Should_Return() |
|||
{ |
|||
//Act
|
|||
var resources = await _resourceStore.GetAllResourcesAsync(); |
|||
|
|||
//Assert
|
|||
resources.ShouldNotBe(null); |
|||
resources.ApiResources.Count.ShouldBe(1); |
|||
resources.ApiResources.First().Name.ShouldBe("Test-ApiResource-Name-1"); |
|||
resources.IdentityResources.First().Name.ShouldBe("Test-Identity-Resource-Name-1"); |
|||
resources.IdentityResources.First().Required.ShouldBe(true); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,130 @@ |
|||
using System; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using IdentityServer4.Models; |
|||
using IdentityServer4.Stores; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Shouldly; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.IdentityServer.Clients |
|||
{ |
|||
public class PersistentGrantStore_Tests : AbpIdentityServerTestBase |
|||
{ |
|||
private readonly IPersistedGrantStore _persistedGrantStore; |
|||
|
|||
public PersistentGrantStore_Tests() |
|||
{ |
|||
_persistedGrantStore = ServiceProvider.GetRequiredService<IPersistedGrantStore>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task FindClientByIdAsync_Should_Return_Null_If_Not_Found() |
|||
{ |
|||
var persistentGrant = await _persistedGrantStore.GetAsync("not-existing-id"); |
|||
persistentGrant.ShouldBeNull(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task FindPersistentGrantByIdAsync_Should_Return_The_PersistentGrant_If_Found() |
|||
{ |
|||
//Act
|
|||
var client = await _persistedGrantStore.GetAsync("38"); |
|||
|
|||
//Assert
|
|||
client.ShouldNotBeNull(); |
|||
client.ClientId.ShouldBe("TestClientId-38"); |
|||
client.SubjectId.ShouldBe("TestSubject"); |
|||
client.Data.ShouldContain("TestData-38"); |
|||
client.Type.ShouldContain("TestType-38"); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task StoreAsync_Should_Store_PersistedGrant() |
|||
{ |
|||
//Act
|
|||
await _persistedGrantStore.StoreAsync(new PersistedGrant |
|||
{ |
|||
Key = "39", |
|||
ClientId = "TestClientId-39", |
|||
Type = "TestType-39", |
|||
SubjectId = "TestSubject", |
|||
Data = "TestData-39", |
|||
Expiration = new DateTime(2018, 1, 6, 21, 22, 23), |
|||
CreationTime = new DateTime(2018, 1, 5, 19, 20, 21) |
|||
}); |
|||
|
|||
//Assert
|
|||
var persistedGrant = await _persistedGrantStore.GetAsync("39"); |
|||
persistedGrant.Key.ShouldBe("39"); |
|||
persistedGrant.ClientId.ShouldBe("TestClientId-39"); |
|||
persistedGrant.Type.ShouldBe("TestType-39"); |
|||
persistedGrant.SubjectId.ShouldBe("TestSubject"); |
|||
persistedGrant.Data.ShouldBe("TestData-39"); |
|||
|
|||
persistedGrant.Expiration.HasValue.ShouldBe(true); |
|||
persistedGrant.Expiration.Value.Year.ShouldBe(2018); |
|||
persistedGrant.Expiration.Value.Month.ShouldBe(1); |
|||
persistedGrant.Expiration.Value.Day.ShouldBe(6); |
|||
persistedGrant.Expiration.Value.Hour.ShouldBe(21); |
|||
persistedGrant.Expiration.Value.Minute.ShouldBe(22); |
|||
persistedGrant.Expiration.Value.Second.ShouldBe(23); |
|||
|
|||
persistedGrant.CreationTime.Year.ShouldBe(2018); |
|||
persistedGrant.CreationTime.Month.ShouldBe(1); |
|||
persistedGrant.CreationTime.Day.ShouldBe(5); |
|||
persistedGrant.CreationTime.Hour.ShouldBe(19); |
|||
persistedGrant.CreationTime.Minute.ShouldBe(20); |
|||
persistedGrant.CreationTime.Second.ShouldBe(21); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task GetAllAsync_Should_Get_All_PersistedGrants_For_A_Given_SubjectId() |
|||
{ |
|||
//Act
|
|||
var persistentGrants = await _persistedGrantStore.GetAllAsync("TestSubject"); |
|||
|
|||
//Assert
|
|||
var persistedGrants = persistentGrants as PersistedGrant[] ?? persistentGrants.ToArray(); |
|||
persistedGrants.ShouldNotBe(null); |
|||
persistedGrants.Length.ShouldBe(2); |
|||
persistedGrants[0].SubjectId.ShouldBe("TestSubject"); |
|||
persistedGrants[1].SubjectId.ShouldBe("TestSubject"); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task RemoveAsync_Should_Remove_PeristedGrant() |
|||
{ |
|||
//Arrange
|
|||
await _persistedGrantStore.StoreAsync(new PersistedGrant |
|||
{ |
|||
Key = "#1P3R" |
|||
}); |
|||
|
|||
//Act
|
|||
await _persistedGrantStore.RemoveAsync("#1P3R"); |
|||
|
|||
//Assert
|
|||
var persistedGrant = await _persistedGrantStore.GetAsync("#1P3R"); |
|||
persistedGrant.ShouldBe(null); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task RemoveAllAsync_Should_RemoveAll_PeristedGrants_For_A_Given_Subject_And_ClientId() |
|||
{ |
|||
//Arrange
|
|||
var persistedGrantsWithTestSubjectX = await _persistedGrantStore.GetAllAsync("TestSubject-X"); |
|||
var persistedGrantsWithTestSubjectXBeforeLength = persistedGrantsWithTestSubjectX.ToArray().Length; |
|||
|
|||
//Act
|
|||
await _persistedGrantStore.RemoveAllAsync("TestSubject-X", "TestClientId-X"); |
|||
|
|||
//Assert
|
|||
persistedGrantsWithTestSubjectXBeforeLength.ShouldBe(2); |
|||
|
|||
var persistedGrants = (await _persistedGrantStore.GetAllAsync("TestClientId-37")).ToArray(); |
|||
persistedGrants.ShouldNotBe(null); |
|||
persistedGrants.Length.ShouldBe(0); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue