Browse Source

Use new IDS4 API for IdentityModelAuthenticationService.

pull/1190/head
Halil ibrahim Kalkan 7 years ago
parent
commit
843ccb3db2
  1. 2
      framework/src/Volo.Abp.IdentityModel/Volo.Abp.IdentityModel.csproj
  2. 4
      framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/AbpIdentityModelModule.cs
  3. 17
      framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityClientConfiguration.cs
  4. 72
      framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelAuthenticationService.cs
  5. 1
      modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs

2
framework/src/Volo.Abp.IdentityModel/Volo.Abp.IdentityModel.csproj

@ -15,7 +15,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="IdentityModel" Version="3.10.9" /> <PackageReference Include="IdentityModel" Version="3.10.9" />
<ProjectReference Include="..\Volo.Abp.Core\Volo.Abp.Core.csproj" /> <ProjectReference Include="..\Volo.Abp.Threading\Volo.Abp.Threading.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

4
framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/AbpIdentityModelModule.cs

@ -1,8 +1,12 @@
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
using Volo.Abp.Threading;
namespace Volo.Abp.IdentityModel namespace Volo.Abp.IdentityModel
{ {
[DependsOn(
typeof(AbpThreadingModule)
)]
public class AbpIdentityModelModule : AbpModule public class AbpIdentityModelModule : AbpModule
{ {
public override void ConfigureServices(ServiceConfigurationContext context) public override void ConfigureServices(ServiceConfigurationContext context)

17
framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityClientConfiguration.cs

@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using IdentityModel; using IdentityModel;
namespace Volo.Abp.IdentityModel namespace Volo.Abp.IdentityModel
@ -71,6 +72,16 @@ namespace Volo.Abp.IdentityModel
set => this[nameof(Scope)] = value; set => this[nameof(Scope)] = value;
} }
/// <summary>
/// RequireHttps.
/// Default: true.
/// </summary>
public bool RequireHttps
{
get => this.GetOrDefault(nameof(RequireHttps))?.To<bool>() ?? true;
set => this[nameof(RequireHttps)] = value.ToString().ToLowerInvariant();
}
public IdentityClientConfiguration() public IdentityClientConfiguration()
{ {
@ -83,7 +94,8 @@ namespace Volo.Abp.IdentityModel
string clientSecret, string clientSecret,
string grantType = OidcConstants.GrantTypes.ClientCredentials, string grantType = OidcConstants.GrantTypes.ClientCredentials,
string userName = null, string userName = null,
string userPassword = null) string userPassword = null,
bool requireHttps = true)
{ {
this[nameof(Authority)] = authority; this[nameof(Authority)] = authority;
this[nameof(Scope)] = scope; this[nameof(Scope)] = scope;
@ -92,6 +104,7 @@ namespace Volo.Abp.IdentityModel
this[nameof(GrantType)] = grantType; this[nameof(GrantType)] = grantType;
this[nameof(UserName)] = userName; this[nameof(UserName)] = userName;
this[nameof(UserPassword)] = userPassword; this[nameof(UserPassword)] = userPassword;
this[nameof(RequireHttps)] = requireHttps.ToString().ToLowerInvariant();
} }
} }
} }

72
framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelAuthenticationService.cs

@ -10,6 +10,7 @@ using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
using Volo.Abp.Threading;
namespace Volo.Abp.IdentityModel namespace Volo.Abp.IdentityModel
{ {
@ -18,10 +19,13 @@ namespace Volo.Abp.IdentityModel
{ {
public ILogger<IdentityModelAuthenticationService> Logger { get; set; } public ILogger<IdentityModelAuthenticationService> Logger { get; set; }
protected IdentityClientOptions ClientOptions { get; } protected IdentityClientOptions ClientOptions { get; }
protected ICancellationTokenProvider CancellationTokenProvider { get; }
public IdentityModelAuthenticationService( public IdentityModelAuthenticationService(
IOptions<IdentityClientOptions> options) IOptions<IdentityClientOptions> options,
ICancellationTokenProvider cancellationTokenProvider)
{ {
CancellationTokenProvider = cancellationTokenProvider;
ClientOptions = options.Value; ClientOptions = options.Value;
Logger = NullLogger<IdentityModelAuthenticationService>.Instance; Logger = NullLogger<IdentityModelAuthenticationService>.Instance;
} }
@ -87,31 +91,57 @@ namespace Volo.Abp.IdentityModel
ClientOptions.IdentityClients.Default; ClientOptions.IdentityClients.Default;
} }
protected virtual async Task<DiscoveryResponse> GetDiscoveryResponse(IdentityClientConfiguration configuration) protected virtual async Task<DiscoveryResponse> GetDiscoveryResponse(
IdentityClientConfiguration configuration)
{ {
return await DiscoveryClient.GetAsync(configuration.Authority); using (var httpClient = new HttpClient())
{
return await httpClient.GetDiscoveryDocumentAsync(new DiscoveryDocumentRequest
{
Address = configuration.Authority,
Policy =
{
RequireHttps = configuration.RequireHttps
}
});
}
} }
protected virtual async Task<TokenResponse> GetTokenResponse(DiscoveryResponse discoveryResponse, IdentityClientConfiguration configuration) protected virtual async Task<TokenResponse> GetTokenResponse(
DiscoveryResponse discoveryResponse,
IdentityClientConfiguration configuration)
{ {
//TODO: Pass cancellation token using (var httpClient = new HttpClient())
var tokenClient = new TokenClient(discoveryResponse.TokenEndpoint, configuration.ClientId, configuration.ClientSecret);
switch (configuration.GrantType)
{ {
case OidcConstants.GrantTypes.ClientCredentials: switch (configuration.GrantType)
return await tokenClient.RequestClientCredentialsAsync( {
configuration.Scope case OidcConstants.GrantTypes.ClientCredentials:
); return await httpClient.RequestClientCredentialsTokenAsync(
case OidcConstants.GrantTypes.Password: new ClientCredentialsTokenRequest
return await tokenClient.RequestResourceOwnerPasswordAsync( {
configuration.UserName, Address = discoveryResponse.TokenEndpoint,
configuration.UserPassword, Scope = configuration.Scope,
configuration.Scope ClientId = configuration.ClientId,
); ClientSecret = configuration.ClientSecret
default: },
throw new AbpException("Grant type was not implemented: " + configuration.GrantType); CancellationTokenProvider.Token
);
case OidcConstants.GrantTypes.Password:
return await httpClient.RequestPasswordTokenAsync(
new PasswordTokenRequest
{
Address = discoveryResponse.TokenEndpoint,
Scope = configuration.Scope,
ClientId = configuration.ClientId,
ClientSecret = configuration.ClientSecret,
UserName = configuration.UserName,
Password = configuration.UserPassword
},
CancellationTokenProvider.Token
);
default:
throw new AbpException("Grant type was not implemented: " + configuration.GrantType);
}
} }
} }
} }

1
modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs

@ -3,7 +3,6 @@ using IdentityServer4.Stores;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.AutoMapper; using Volo.Abp.AutoMapper;
using Volo.Abp.Caching; using Volo.Abp.Caching;
using Volo.Abp.Domain;
using Volo.Abp.Identity; using Volo.Abp.Identity;
using Volo.Abp.IdentityServer.Clients; using Volo.Abp.IdentityServer.Clients;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;

Loading…
Cancel
Save