From 843ccb3db23ab91f44bd58a56b3cf20ea1ac1b5f Mon Sep 17 00:00:00 2001 From: Halil ibrahim Kalkan Date: Thu, 30 May 2019 14:00:46 +0300 Subject: [PATCH] Use new IDS4 API for IdentityModelAuthenticationService. --- .../Volo.Abp.IdentityModel.csproj | 2 +- .../IdentityModel/AbpIdentityModelModule.cs | 4 ++ .../IdentityClientConfiguration.cs | 17 ++++- .../IdentityModelAuthenticationService.cs | 72 +++++++++++++------ .../AbpIdentityServerDomainModule.cs | 1 - 5 files changed, 71 insertions(+), 25 deletions(-) diff --git a/framework/src/Volo.Abp.IdentityModel/Volo.Abp.IdentityModel.csproj b/framework/src/Volo.Abp.IdentityModel/Volo.Abp.IdentityModel.csproj index fcfff3fcb5..e608961fff 100644 --- a/framework/src/Volo.Abp.IdentityModel/Volo.Abp.IdentityModel.csproj +++ b/framework/src/Volo.Abp.IdentityModel/Volo.Abp.IdentityModel.csproj @@ -15,7 +15,7 @@ - + \ No newline at end of file diff --git a/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/AbpIdentityModelModule.cs b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/AbpIdentityModelModule.cs index cadeb794a7..b8578ca95d 100644 --- a/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/AbpIdentityModelModule.cs +++ b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/AbpIdentityModelModule.cs @@ -1,8 +1,12 @@ using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Modularity; +using Volo.Abp.Threading; namespace Volo.Abp.IdentityModel { + [DependsOn( + typeof(AbpThreadingModule) + )] public class AbpIdentityModelModule : AbpModule { public override void ConfigureServices(ServiceConfigurationContext context) diff --git a/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityClientConfiguration.cs b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityClientConfiguration.cs index 35ca881f7f..e2e6e45e49 100644 --- a/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityClientConfiguration.cs +++ b/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; namespace Volo.Abp.IdentityModel @@ -71,6 +72,16 @@ namespace Volo.Abp.IdentityModel set => this[nameof(Scope)] = value; } + /// + /// RequireHttps. + /// Default: true. + /// + public bool RequireHttps + { + get => this.GetOrDefault(nameof(RequireHttps))?.To() ?? true; + set => this[nameof(RequireHttps)] = value.ToString().ToLowerInvariant(); + } + public IdentityClientConfiguration() { @@ -83,7 +94,8 @@ namespace Volo.Abp.IdentityModel string clientSecret, string grantType = OidcConstants.GrantTypes.ClientCredentials, string userName = null, - string userPassword = null) + string userPassword = null, + bool requireHttps = true) { this[nameof(Authority)] = authority; this[nameof(Scope)] = scope; @@ -92,6 +104,7 @@ namespace Volo.Abp.IdentityModel this[nameof(GrantType)] = grantType; this[nameof(UserName)] = userName; this[nameof(UserPassword)] = userPassword; + this[nameof(RequireHttps)] = requireHttps.ToString().ToLowerInvariant(); } } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelAuthenticationService.cs b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelAuthenticationService.cs index 4f316ec357..032bd15457 100644 --- a/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelAuthenticationService.cs +++ b/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.Threading.Tasks; using Volo.Abp.DependencyInjection; +using Volo.Abp.Threading; namespace Volo.Abp.IdentityModel { @@ -18,10 +19,13 @@ namespace Volo.Abp.IdentityModel { public ILogger Logger { get; set; } protected IdentityClientOptions ClientOptions { get; } + protected ICancellationTokenProvider CancellationTokenProvider { get; } public IdentityModelAuthenticationService( - IOptions options) + IOptions options, + ICancellationTokenProvider cancellationTokenProvider) { + CancellationTokenProvider = cancellationTokenProvider; ClientOptions = options.Value; Logger = NullLogger.Instance; } @@ -87,31 +91,57 @@ namespace Volo.Abp.IdentityModel ClientOptions.IdentityClients.Default; } - protected virtual async Task GetDiscoveryResponse(IdentityClientConfiguration configuration) + protected virtual async Task 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 GetTokenResponse(DiscoveryResponse discoveryResponse, IdentityClientConfiguration configuration) + protected virtual async Task GetTokenResponse( + DiscoveryResponse discoveryResponse, + IdentityClientConfiguration configuration) { - //TODO: Pass cancellation token - - var tokenClient = new TokenClient(discoveryResponse.TokenEndpoint, configuration.ClientId, configuration.ClientSecret); - - switch (configuration.GrantType) + using (var httpClient = new HttpClient()) { - case OidcConstants.GrantTypes.ClientCredentials: - return await tokenClient.RequestClientCredentialsAsync( - configuration.Scope - ); - case OidcConstants.GrantTypes.Password: - return await tokenClient.RequestResourceOwnerPasswordAsync( - configuration.UserName, - configuration.UserPassword, - configuration.Scope - ); - default: - throw new AbpException("Grant type was not implemented: " + configuration.GrantType); + switch (configuration.GrantType) + { + case OidcConstants.GrantTypes.ClientCredentials: + return await httpClient.RequestClientCredentialsTokenAsync( + new ClientCredentialsTokenRequest + { + Address = discoveryResponse.TokenEndpoint, + Scope = configuration.Scope, + ClientId = configuration.ClientId, + ClientSecret = configuration.ClientSecret + }, + 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); + } } } } diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs index dc06ce130d..7ddc619cfa 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs @@ -3,7 +3,6 @@ using IdentityServer4.Stores; using Microsoft.Extensions.DependencyInjection; using Volo.Abp.AutoMapper; using Volo.Abp.Caching; -using Volo.Abp.Domain; using Volo.Abp.Identity; using Volo.Abp.IdentityServer.Clients; using Volo.Abp.Modularity;