mirror of https://github.com/abpframework/abp.git
committed by
GitHub
7 changed files with 288 additions and 3 deletions
@ -0,0 +1,23 @@ |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.IdentityServer.IdentityResources; |
|||
|
|||
namespace Volo.Abp.IdentityServer.ApiResources |
|||
{ |
|||
public class ApiResources |
|||
{ |
|||
public Task<IdentityResource[]> Resources { get; set; } |
|||
|
|||
public Task<ApiResource[]> IdentityResources { get; set; } |
|||
|
|||
public ApiResources() |
|||
{ |
|||
|
|||
} |
|||
|
|||
public ApiResources(Task<IdentityResource[]> resources, Task<ApiResource[]> identityResources) |
|||
{ |
|||
Resources = resources; |
|||
IdentityResources = identityResources; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,18 @@ |
|||
using System.Collections.Generic; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Domain.Repositories; |
|||
using ApiResource = Volo.Abp.IdentityServer.ApiResources.ApiResource; |
|||
|
|||
namespace Volo.Abp.IdentityServer.IdentityResources |
|||
{ |
|||
public interface IIdentityResourceRepository : IRepository<IdentityResource> |
|||
{ |
|||
Task<List<IdentityResource>> FindIdentityResourcesByScopeAsync(string[] scopeNames); |
|||
|
|||
Task<List<ApiResource>> FindApiResourcesByScopeAsync(string[] scopeNames); |
|||
|
|||
Task<ApiResource> FindApiResourceAsync(string name); |
|||
|
|||
Task<ApiResources.ApiResources> GetAllResourcesAsync(); |
|||
} |
|||
} |
|||
@ -0,0 +1,50 @@ |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using IdentityServer4.Models; |
|||
using IdentityServer4.Stores; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.ObjectMapping; |
|||
|
|||
namespace Volo.Abp.IdentityServer.IdentityResources |
|||
{ |
|||
public class IdentityResourceStore : IResourceStore, ITransientDependency |
|||
{ |
|||
private readonly IIdentityResourceRepository _identityResourceRepository; |
|||
private readonly IObjectMapper _objectMapper; |
|||
|
|||
public IdentityResourceStore(IIdentityResourceRepository identityResourceRepository, IObjectMapper objectMapper) |
|||
{ |
|||
_identityResourceRepository = identityResourceRepository; |
|||
_objectMapper = objectMapper; |
|||
} |
|||
|
|||
public virtual async Task<IEnumerable<IdentityServer4.Models.IdentityResource>> FindIdentityResourcesByScopeAsync(IEnumerable<string> scopeNames) |
|||
{ |
|||
var results = await _identityResourceRepository.FindIdentityResourcesByScopeAsync(scopeNames.ToArray()); |
|||
return _objectMapper.Map<List<IdentityResource>, List<IdentityServer4.Models.IdentityResource>>(results); |
|||
} |
|||
|
|||
public virtual async Task<IEnumerable<ApiResource>> FindApiResourcesByScopeAsync(IEnumerable<string> scopeNames) |
|||
{ |
|||
var results = await _identityResourceRepository.FindApiResourcesByScopeAsync(scopeNames.ToArray()); |
|||
return results?.Select(x => _objectMapper.Map<ApiResources.ApiResource, ApiResource>(x)); |
|||
} |
|||
|
|||
public virtual async Task<ApiResource> FindApiResourceAsync(string name) |
|||
{ |
|||
var result = await _identityResourceRepository.FindApiResourceAsync(name); |
|||
return _objectMapper.Map<ApiResources.ApiResource, ApiResource>(result); |
|||
} |
|||
|
|||
public virtual async Task<Resources> GetAllResourcesAsync() |
|||
{ |
|||
var result = await _identityResourceRepository.GetAllResourcesAsync(); |
|||
return new Resources |
|||
( |
|||
result.Resources.Result.Select(y => _objectMapper.Map<IdentityResource, IdentityServer4.Models.IdentityResource>(y)), |
|||
result.IdentityResources.Result.Select(x => _objectMapper.Map<ApiResources.ApiResource, ApiResource>(x)) |
|||
); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,76 @@ |
|||
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.IdentityResources; |
|||
using ApiResource = Volo.Abp.IdentityServer.ApiResources.ApiResource; |
|||
using IdentityResource = Volo.Abp.IdentityServer.IdentityResources.IdentityResource; |
|||
|
|||
namespace Volo.Abp.IdentityServer |
|||
{ |
|||
public class IdentityResourceRepository : EfCoreRepository<IdentityServerDbContext, IdentityResource>, IIdentityResourceRepository |
|||
{ |
|||
public IdentityResourceRepository(IDbContextProvider<IdentityServerDbContext> dbContextProvider) : base(dbContextProvider) |
|||
{ |
|||
|
|||
} |
|||
|
|||
public Task<List<IdentityResource>> FindIdentityResourcesByScopeAsync(string[] scopeNames) |
|||
{ |
|||
var query = from identityResource in DbSet.Include(x => x.UserClaims) |
|||
where scopeNames.Contains(identityResource.Name) |
|||
select identityResource; |
|||
|
|||
return query.ToListAsync(); |
|||
} |
|||
|
|||
public Task<List<ApiResource>> FindApiResourcesByScopeAsync(string[] scopeNames) |
|||
{ |
|||
var names = scopeNames.ToArray(); |
|||
|
|||
var query = from api in DbContext.ApiResources |
|||
where api.Scopes.Any(x => names.Contains(x.Name)) |
|||
select api; |
|||
|
|||
var apis = query |
|||
.Include(x => x.Secrets) |
|||
.Include(x => x.Scopes) |
|||
.ThenInclude(s => s.UserClaims) |
|||
.Include(x => x.UserClaims); |
|||
|
|||
return apis.ToListAsync(); |
|||
} |
|||
|
|||
public Task<ApiResource> FindApiResourceAsync(string name) |
|||
{ |
|||
var query = from apiResource in DbContext.ApiResources |
|||
where apiResource.Name == name |
|||
select apiResource; |
|||
|
|||
var apis = query |
|||
.Include(x => x.Secrets) |
|||
.Include(x => x.Scopes) |
|||
.ThenInclude(s => s.UserClaims) |
|||
.Include(x => x.UserClaims); |
|||
|
|||
return apis.FirstOrDefaultAsync(); |
|||
} |
|||
|
|||
public Task<ApiResources.ApiResources> GetAllResourcesAsync() |
|||
{ |
|||
var identity = DbContext.IdentityResources |
|||
.Include(x => x.UserClaims); |
|||
|
|||
var apis = DbContext.ApiResources |
|||
.Include(x => x.Secrets) |
|||
.Include(x => x.Scopes) |
|||
.ThenInclude(s => s.UserClaims) |
|||
.Include(x => x.UserClaims); |
|||
|
|||
return Task.FromResult(new ApiResources.ApiResources(identity.ToArrayAsync(), apis.ToArrayAsync())); |
|||
} |
|||
} |
|||
} |
|||
@ -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); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue