@ -4,6 +4,8 @@ using System.Net.Http.Headers;
using System.Threading.Tasks ;
using IdentityModel ;
using IdentityModel.Client ;
using Microsoft.Extensions.Logging ;
using Microsoft.Extensions.Logging.Abstractions ;
using Microsoft.Extensions.Options ;
using Volo.Abp.DependencyInjection ;
@ -12,12 +14,15 @@ namespace Volo.Abp.IdentityModel
[Dependency(ReplaceServices = true)]
public class IdentityModelHttpClientAuthenticator : IIdentityModelHttpClientAuthenticator , ITransientDependency
{
protected IdentityClientOptions ClientOptions { g et ; }
public ILogger < IdentityModelHttpClientAuthenticator > Logger { get ; s et ; }
protected IdentityClientOptions ClientOptions { get ; }
public IdentityModelHttpClientAuthenticator (
IOptions < IdentityClientOptions > options )
{
ClientOptions = options . Value ;
Logger = NullLogger < IdentityModelHttpClientAuthenticator > . Instance ;
}
public async Task Authenticate ( IdentityModelHttpClientAuthenticateContext context )
@ -34,22 +39,27 @@ namespace Volo.Abp.IdentityModel
protected virtual async Task < string > GetAccessTokenFromServerOrNullAsync ( IdentityModelHttpClientAuthenticateContext context )
{
//TODO: Better logging
var configuration = GetClientConfiguration ( context ) ;
if ( configuration = = null )
{
Logger . LogWarning ( $"Could not find {nameof(IdentityClientConfiguration)} for {context.IdentityClientName}. Either define a configuration for {context.IdentityClientName} or set a default configuration." ) ;
return null ;
}
var discoveryResponse = await GetDiscoveryResponse ( configuration ) ;
if ( discoveryResponse . IsError )
{
Logger . LogError ( $"Could not retrieve the OpenId Connect discovery document! ErrorType: {discoveryResponse.ErrorType}. Error: {discoveryResponse.Error}" ) ;
return null ;
}
var tokenResponse = await GetTokenResponse ( discoveryResponse , configuration ) ;
if ( tokenResponse . IsError )
{
Logger . LogError ( $"Could not get token from the OpenId Connect server! ErrorType: {tokenResponse.ErrorType}. Error: {tokenResponse.Error}. ErrorDescription: {tokenResponse.ErrorDescription}. HttpStatusCode: {tokenResponse.HttpStatusCode}" ) ;
return null ;
}