mirror of https://github.com/abpframework/abp.git
19 changed files with 172 additions and 57 deletions
@ -1,18 +1,14 @@ |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Volo.Abp.IdentityModel; |
|||
using Volo.Abp.Modularity; |
|||
|
|||
namespace Volo.Abp.Http.Client.IdentityModel |
|||
{ |
|||
[DependsOn( |
|||
typeof(AbpHttpClientModule) |
|||
typeof(AbpHttpClientModule), |
|||
typeof(AbpIdentityModelModule) |
|||
)] |
|||
public class AbpHttpClientIdentityModelModule : AbpModule |
|||
{ |
|||
public override void ConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
var configuration = context.Services.GetConfiguration(); |
|||
|
|||
Configure<IdentityClientOptions>(configuration); |
|||
} |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,56 @@ |
|||
using System.Net.Http.Headers; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Authentication; |
|||
using Microsoft.AspNetCore.Http; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Http.Client.Authentication; |
|||
using Volo.Abp.IdentityModel; |
|||
|
|||
namespace Volo.Abp.Http.Client.IdentityModel |
|||
{ |
|||
[Dependency(ReplaceServices = true)] |
|||
public class IdentityModelRemoteServiceHttpClientAuthenticator : IRemoteServiceHttpClientAuthenticator, ITransientDependency |
|||
{ |
|||
public IHttpContextAccessor HttpContextAccessor { get; set; } |
|||
|
|||
protected IIdentityModelHttpClientAuthenticator IdentityModelHttpClientAuthenticator { get; } |
|||
|
|||
public IdentityModelRemoteServiceHttpClientAuthenticator(IIdentityModelHttpClientAuthenticator identityModelHttpClientAuthenticator) |
|||
{ |
|||
IdentityModelHttpClientAuthenticator = identityModelHttpClientAuthenticator; |
|||
} |
|||
|
|||
public async Task Authenticate(RemoteServiceHttpClientAuthenticateContext context) |
|||
{ |
|||
var accessToken = await GetAccessTokenFromHttpContextOrNullAsync(); |
|||
|
|||
if (accessToken != null) |
|||
{ |
|||
//TODO: "Bearer" should be configurable
|
|||
context.Client.DefaultRequestHeaders.Authorization |
|||
= new AuthenticationHeaderValue("Bearer", accessToken); |
|||
} |
|||
else |
|||
{ |
|||
await IdentityModelHttpClientAuthenticator.Authenticate( |
|||
new IdentityModelHttpClientAuthenticateContext( |
|||
context.Client, |
|||
context.RemoteService.GetIdentityClient() |
|||
) |
|||
); |
|||
} |
|||
} |
|||
|
|||
protected virtual async Task<string> GetAccessTokenFromHttpContextOrNullAsync() |
|||
{ |
|||
//TODO: What if the access_token in the current Http Request is not usable for this client?
|
|||
var httpContext = HttpContextAccessor?.HttpContext; |
|||
if (httpContext == null) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
return await httpContext.GetTokenAsync("access_token"); |
|||
} |
|||
} |
|||
} |
|||
@ -1,9 +0,0 @@ |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.Http.Client.Authentication |
|||
{ |
|||
public interface IHttpClientAuthenticator |
|||
{ |
|||
Task Authenticate(HttpClientAuthenticateContext context); |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.Http.Client.Authentication |
|||
{ |
|||
public interface IRemoteServiceHttpClientAuthenticator |
|||
{ |
|||
Task Authenticate(RemoteServiceHttpClientAuthenticateContext context); |
|||
} |
|||
} |
|||
@ -0,0 +1,21 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<Import Project="..\..\..\common.props" /> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>netstandard2.0</TargetFramework> |
|||
<AssemblyName>Volo.Abp.IdentityModel</AssemblyName> |
|||
<PackageId>Volo.Abp.IdentityModel</PackageId> |
|||
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback> |
|||
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> |
|||
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> |
|||
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> |
|||
<RootNamespace /> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<PackageReference Include="IdentityModel" Version="3.10.4" /> |
|||
<ProjectReference Include="..\Volo.Abp.Core\Volo.Abp.Core.csproj" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,15 @@ |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Volo.Abp.Modularity; |
|||
|
|||
namespace Volo.Abp.IdentityModel |
|||
{ |
|||
public class AbpIdentityModelModule : AbpModule |
|||
{ |
|||
public override void ConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
var configuration = context.Services.GetConfiguration(); |
|||
|
|||
Configure<IdentityClientOptions>(configuration); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.IdentityModel |
|||
{ |
|||
public interface IIdentityModelHttpClientAuthenticator |
|||
{ |
|||
Task Authenticate(IdentityModelHttpClientAuthenticateContext context); |
|||
} |
|||
} |
|||
@ -1,7 +1,7 @@ |
|||
using System.Collections.Generic; |
|||
using IdentityModel; |
|||
|
|||
namespace Volo.Abp.Http.Client.IdentityModel |
|||
namespace Volo.Abp.IdentityModel |
|||
{ |
|||
public class IdentityClientConfiguration : Dictionary<string, string> |
|||
{ |
|||
@ -1,6 +1,6 @@ |
|||
using System.Collections.Generic; |
|||
|
|||
namespace Volo.Abp.Http.Client.IdentityModel |
|||
namespace Volo.Abp.IdentityModel |
|||
{ |
|||
public class IdentityClientConfigurationDictionary : Dictionary<string, IdentityClientConfiguration> |
|||
{ |
|||
@ -1,4 +1,4 @@ |
|||
namespace Volo.Abp.Http.Client.IdentityModel |
|||
namespace Volo.Abp.IdentityModel |
|||
{ |
|||
public class IdentityClientOptions |
|||
{ |
|||
@ -0,0 +1,27 @@ |
|||
using System.Net.Http; |
|||
|
|||
namespace Volo.Abp.IdentityModel |
|||
{ |
|||
public class IdentityModelHttpClientAuthenticateContext |
|||
{ |
|||
public HttpClient Client { get; } |
|||
|
|||
/// <summary>
|
|||
/// The identity client name configured with the <see cref="IdentityClientOptions"/>.
|
|||
/// </summary>
|
|||
public string IdentityClientName { get; } |
|||
|
|||
/// <summary>
|
|||
///
|
|||
/// </summary>
|
|||
/// <param name="client"><see cref="HttpClient"/> object to be authorized</param>
|
|||
/// <param name="identityClientName">The identity client name configured with the <see cref="IdentityClientOptions"/>.</param>
|
|||
public IdentityModelHttpClientAuthenticateContext( |
|||
HttpClient client, |
|||
string identityClientName = null) |
|||
{ |
|||
Client = client; |
|||
IdentityClientName = identityClientName; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue