Browse Source

Made console client working for identity server.

pull/194/head
Halil İbrahim Kalkan 9 years ago
parent
commit
de8de3c92b
  1. 14
      src/AbpDesk/AbpDesk.Web.Mvc/Controllers/IdentityServerTestController.cs
  2. 7
      src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs
  3. 9
      src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/ApiResources/ApiResource.cs
  4. 26
      src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/ResourceStore.cs
  5. 17
      src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/IdentityResourceRepository.cs
  6. 24
      test/Abp.IdentityServer.EntityFrameworkCore.Tests/Volo/Abp/IdentityServer/AbpIdentityServerTestDataBuilder.cs
  7. 1
      test/Abp.IdentityServer.EntityFrameworkCore.Tests/Volo/Abp/IdentityServer/Clients/ClientStore_Tests.cs
  8. 3
      test/Abp.IdentityServer.EntityFrameworkCore.Tests/Volo/Abp/IdentityServer/Clients/IdentityResourceStore_Tests.cs

14
src/AbpDesk/AbpDesk.Web.Mvc/Controllers/IdentityServerTestController.cs

@ -1,6 +1,7 @@
using System.Linq;
using System.Threading.Tasks;
using IdentityServer4.Models;
using IdentityServer4.Stores;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.IdentityServer.ApiResources;
@ -15,11 +16,22 @@ namespace AbpDesk.Web.Mvc.Controllers
{
private readonly IClientRepository _clientRepository;
private readonly IApiResourceRepository _apiResourceRepository;
private readonly IResourceStore _resourceStore;
public IdentityServerTestController(IClientRepository clientRepository, IApiResourceRepository apiResourceRepository)
public IdentityServerTestController(
IClientRepository clientRepository,
IApiResourceRepository apiResourceRepository,
IResourceStore resourceStore)
{
_clientRepository = clientRepository;
_apiResourceRepository = apiResourceRepository;
_resourceStore = resourceStore;
}
[HttpGet]
public async Task<IActionResult> Get()
{
return Content("Resources: " + (await _resourceStore.FindApiResourceAsync("api1")).Name);
}
[HttpGet]

7
src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs

@ -2,6 +2,7 @@
using Volo.Abp.AutoMapper;
using Volo.Abp.Identity;
using Volo.Abp.IdentityServer.Clients;
using Volo.Abp.IdentityServer.Temp;
using Volo.Abp.Modularity;
using Volo.Abp.Security;
@ -30,8 +31,10 @@ namespace Volo.Abp.IdentityServer
{
var identityServerBuilder = services.AddIdentityServer();
//TODO: Remove in-memory stores once EF Core stores are fully completed.
identityServerBuilder.AddDeveloperSigningCredential()
identityServerBuilder
.AddDeveloperSigningCredential()
//.AddInMemoryApiResources(IdentityServerConfig.GetApiResources())
//.AddInMemoryClients(IdentityServerConfig.GetClients())
.AddAbpIdentityServer();
services.ExecutePreConfiguredActions(identityServerBuilder);

9
src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/ApiResources/ApiResource.cs

@ -40,6 +40,15 @@ namespace Volo.Abp.IdentityServer.ApiResources
Secrets = new List<ApiSecret>();
Scopes = new List<ApiScope>();
UserClaims = new List<ApiResourceClaim>();
//TODO: Use IGuidGenerator!
//TODO: Add a good ctor to ApiScope
Scopes.Add(new ApiScope(Guid.NewGuid())
{
Name = name,
DisplayName = displayName,
Description = description
});
}
}
}

26
src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/IdentityResources/IdentityResourceStore.cs → src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/ResourceStore.cs

@ -4,16 +4,18 @@ using System.Threading.Tasks;
using IdentityServer4.Models;
using IdentityServer4.Stores;
using Volo.Abp.DependencyInjection;
using Volo.Abp.IdentityServer.IdentityResources;
using Volo.Abp.ObjectMapping;
using IdentityResource = Volo.Abp.IdentityServer.IdentityResources.IdentityResource;
namespace Volo.Abp.IdentityServer.IdentityResources
namespace Volo.Abp.IdentityServer
{
public class IdentityResourceStore : IResourceStore, ITransientDependency
public class ResourceStore : IResourceStore, ITransientDependency
{
private readonly IIdentityResourceRepository _identityResourceRepository;
private readonly IObjectMapper _objectMapper;
public IdentityResourceStore(IIdentityResourceRepository identityResourceRepository, IObjectMapper objectMapper)
public ResourceStore(IIdentityResourceRepository identityResourceRepository, IObjectMapper objectMapper)
{
_identityResourceRepository = identityResourceRepository;
_objectMapper = objectMapper;
@ -21,29 +23,29 @@ namespace Volo.Abp.IdentityServer.IdentityResources
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);
var resource = await _identityResourceRepository.FindIdentityResourcesByScopeAsync(scopeNames.ToArray());
return _objectMapper.Map<List<IdentityResource>, List<IdentityServer4.Models.IdentityResource>>(resource);
}
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));
var resources = await _identityResourceRepository.FindApiResourcesByScopeAsync(scopeNames.ToArray());
return resources?.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);
var resource = await _identityResourceRepository.FindApiResourceAsync(name);
return _objectMapper.Map<ApiResources.ApiResource, ApiResource>(resource);
}
public virtual async Task<Resources> GetAllResourcesAsync()
{
var result = await _identityResourceRepository.GetAllResourcesAsync();
var resources = await _identityResourceRepository.GetAllResourcesAsync();
return new Resources(
_objectMapper.Map<IdentityResource[], IdentityServer4.Models.IdentityResource[]>(result.IdentityResources),
_objectMapper.Map<ApiResources.ApiResource[], ApiResource[]>(result.ApiResources)
_objectMapper.Map<IdentityResource[], IdentityServer4.Models.IdentityResource[]>(resources.IdentityResources),
_objectMapper.Map<ApiResources.ApiResource[], ApiResource[]>(resources.ApiResources)
);
}
}

17
src/Volo.Abp.IdentityServer.EntityFrameworkCore/Volo/Abp/IdentityServer/IdentityResourceRepository.cs

@ -11,9 +11,12 @@ using IdentityResource = Volo.Abp.IdentityServer.IdentityResources.IdentityResou
namespace Volo.Abp.IdentityServer
{
//TODO: This is not true implementation! This repository works for 2 different aggregate root!
public class IdentityResourceRepository : EfCoreRepository<IdentityServerDbContext, IdentityResource>, IIdentityResourceRepository
{
public IdentityResourceRepository(IDbContextProvider<IdentityServerDbContext> dbContextProvider) : base(dbContextProvider)
public IdentityResourceRepository(IDbContextProvider<IdentityServerDbContext> dbContextProvider)
: base(dbContextProvider)
{
}
@ -35,9 +38,9 @@ namespace Volo.Abp.IdentityServer
query = query
.Include(x => x.Secrets)
.Include(x => x.UserClaims)
.Include(x => x.Scopes)
.ThenInclude(s => s.UserClaims)
.Include(x => x.UserClaims);
.ThenInclude(s => s.UserClaims);
return await query.ToListAsync();
}
@ -50,9 +53,9 @@ namespace Volo.Abp.IdentityServer
query = query
.Include(x => x.Secrets)
.Include(x => x.UserClaims)
.Include(x => x.Scopes)
.ThenInclude(s => s.UserClaims)
.Include(x => x.UserClaims);
.ThenInclude(s => s.UserClaims);
return await query.FirstOrDefaultAsync();
}
@ -64,9 +67,9 @@ namespace Volo.Abp.IdentityServer
var apis = DbContext.ApiResources
.Include(x => x.Secrets)
.Include(x => x.UserClaims)
.Include(x => x.Scopes)
.ThenInclude(s => s.UserClaims)
.Include(x => x.UserClaims);
.ThenInclude(s => s.UserClaims);
return new ApiResources.ApiAndIdentityResources(
await identity.ToArrayAsync(),

24
test/Abp.IdentityServer.EntityFrameworkCore.Tests/Volo/Abp/IdentityServer/AbpIdentityServerTestDataBuilder.cs

@ -1,10 +1,15 @@
using System.Collections.Generic;
using IdentityServer4.Models;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Guids;
using Volo.Abp.IdentityServer.ApiResources;
using Volo.Abp.IdentityServer.Clients;
using Volo.Abp.IdentityServer.Grants;
using Volo.Abp.IdentityServer.IdentityResources;
using ApiResource = Volo.Abp.IdentityServer.ApiResources.ApiResource;
using Client = Volo.Abp.IdentityServer.Clients.Client;
using IdentityResource = Volo.Abp.IdentityServer.IdentityResources.IdentityResource;
using PersistedGrant = Volo.Abp.IdentityServer.Grants.PersistedGrant;
namespace Volo.Abp.IdentityServer
{
@ -40,19 +45,21 @@ namespace Volo.Abp.IdentityServer
private void AddClients()
{
var client42 = new Client(_guidGenerator.Create())
var client42 = new Client(_guidGenerator.Create(), "42")
{
ClientId = "42",
ProtocolType = "TestProtocol-42"
};
client42.AllowedCorsOrigins.Add(
new ClientCorsOrigin(_guidGenerator.Create())
{
Origin = "Origin1"
Origin = "Origin1",
ClientId = client42.Id
}
);
client42.AddAllowedScope(_guidGenerator, "api1");
_clientRepository.Insert(client42);
}
@ -97,24 +104,23 @@ namespace Volo.Abp.IdentityServer
private void AddApiResources()
{
_apiResourceRepository.Insert(new ApiResource(_guidGenerator.Create())
_apiResourceRepository.Insert(new ApiResource(_guidGenerator.Create(), "Test-ApiResource-Name-1")
{
Name = "Test-ApiResource-Name-1",
Enabled = true,
Description = "Test-ApiResource-Description-1",
DisplayName = "Test-ApiResource-DisplayName-1",
Secrets = new List<ApiSecret>
Secrets =
{
new ApiSecret(_guidGenerator.Create())
new ApiSecret(_guidGenerator.Create(), "secret".Sha256())
},
UserClaims = new List<ApiResourceClaim>
UserClaims =
{
new ApiResourceClaim(_guidGenerator.Create())
{
Type = "Test-ApiResource-Claim-Type-1"
}
},
Scopes = new List<ApiScope>
Scopes =
{
new ApiScope(_guidGenerator.Create())
{

1
test/Abp.IdentityServer.EntityFrameworkCore.Tests/Volo/Abp/IdentityServer/Clients/ClientStore_Tests.cs

@ -33,6 +33,7 @@ namespace Volo.Abp.IdentityServer.Clients
client.ClientId.ShouldBe("42");
client.ProtocolType.ShouldBe("TestProtocol-42");
client.AllowedCorsOrigins.ShouldContain("Origin1");
client.AllowedScopes.ShouldContain("api1");
}
}
}

3
test/Abp.IdentityServer.EntityFrameworkCore.Tests/Volo/Abp/IdentityServer/Clients/IdentityResourceStore_Tests.cs

@ -54,8 +54,7 @@ namespace Volo.Abp.IdentityServer.Clients
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");
apiResources[0].Scopes.GroupBy(x => x.Name).Count().ShouldBe(2);
}
[Fact]

Loading…
Cancel
Save