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>
<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>
</Project>

4
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)

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;
namespace Volo.Abp.IdentityModel
@ -71,6 +72,16 @@ namespace Volo.Abp.IdentityModel
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()
{
@ -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();
}
}
}

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.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Threading;
namespace Volo.Abp.IdentityModel
{
@ -18,10 +19,13 @@ namespace Volo.Abp.IdentityModel
{
public ILogger<IdentityModelAuthenticationService> Logger { get; set; }
protected IdentityClientOptions ClientOptions { get; }
protected ICancellationTokenProvider CancellationTokenProvider { get; }
public IdentityModelAuthenticationService(
IOptions<IdentityClientOptions> options)
IOptions<IdentityClientOptions> options,
ICancellationTokenProvider cancellationTokenProvider)
{
CancellationTokenProvider = cancellationTokenProvider;
ClientOptions = options.Value;
Logger = NullLogger<IdentityModelAuthenticationService>.Instance;
}
@ -87,31 +91,57 @@ namespace Volo.Abp.IdentityModel
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
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);
}
}
}
}

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 Volo.Abp.AutoMapper;
using Volo.Abp.Caching;
using Volo.Abp.Domain;
using Volo.Abp.Identity;
using Volo.Abp.IdentityServer.Clients;
using Volo.Abp.Modularity;

Loading…
Cancel
Save