|
|
|
@ -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); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|