Skip to content

Commit

Permalink
修改所有TokenProvider实现ITokenProvider<>;
Browse files Browse the repository at this point in the history
修改所有TokenProvider注册为ITokenProvider<>;
增加ICustomTokenProvider和CustomTokenClient;
增加添加自定义TokenClient的服务注册扩展;
修改OAuthTokenAttribute为非抽象类;
  • Loading branch information
xljiulang committed Sep 12, 2020
1 parent 1fdc682 commit 68a7dc6
Show file tree
Hide file tree
Showing 33 changed files with 385 additions and 621 deletions.
24 changes: 12 additions & 12 deletions App/Clients/IUserApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,51 +13,51 @@ namespace App.Clients
/// 用户操作接口
/// </summary>
[LoggingFilter]
//[ClientCredentialsToken]
[OAuthToken]
[HttpHost("http://localhost:6000/")]
public interface IUserApi : IHttpApi
{
[HttpGet("api/users/{account}")]
ITask<HttpResponseMessage> GetAsync([Required]string account);
ITask<HttpResponseMessage> GetAsync([Required] string account);

[HttpGet("api/users/{account}")]
ITask<string> GetAsStringAsync([Required]string account, CancellationToken token = default);
ITask<string> GetAsStringAsync([Required] string account, CancellationToken token = default);


[HttpGet("api/users/{account}")]
[JsonReturn]
ITask<string> GetExpectJsonAsync([Required]string account, CancellationToken token = default);
ITask<string> GetExpectJsonAsync([Required] string account, CancellationToken token = default);


[HttpGet("api/users/{account}")]
[XmlReturn]
ITask<string> GetExpectXmlAsync([Required]string account, CancellationToken token = default);
ITask<string> GetExpectXmlAsync([Required] string account, CancellationToken token = default);



[HttpGet("api/users/{account}")]
ITask<byte[]> GetAsByteArrayAsync([Required]string account, CancellationToken token = default);
ITask<byte[]> GetAsByteArrayAsync([Required] string account, CancellationToken token = default);

[HttpGet("api/users/{account}")]
ITask<Stream> GetAsStreamAsync([Required]string account, CancellationToken token = default);
ITask<Stream> GetAsStreamAsync([Required] string account, CancellationToken token = default);

[HttpGet("api/users/{account}")]
ITask<User> GetAsModelAsync([Required]string account, CancellationToken token = default);
ITask<User> GetAsModelAsync([Required] string account, CancellationToken token = default);


[HttpPost("api/users/body")]
Task<User> PostByJsonAsync([Required, JsonContent]User user, CancellationToken token = default);
Task<User> PostByJsonAsync([Required, JsonContent] User user, CancellationToken token = default);

[HttpPost("api/users/body")]
Task<User> PostByXmlAsync([Required, XmlContent]User user, CancellationToken token = default);
Task<User> PostByXmlAsync([Required, XmlContent] User user, CancellationToken token = default);



[HttpPost("api/users/form")]
Task<User> PostByFormAsync([Required, FormContent]User user, CancellationToken token = default);
Task<User> PostByFormAsync([Required, FormContent] User user, CancellationToken token = default);

[HttpPost("api/users/formdata")]
Task<User> PostByFormDataAsync([Required, FormDataContent]User user, FormDataFile file, CancellationToken token = default);
Task<User> PostByFormDataAsync([Required, FormDataContent] User user, FormDataFile file, CancellationToken token = default);



Expand Down
17 changes: 2 additions & 15 deletions App/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,15 @@ public void ConfigureServices(IServiceCollection services)
{
o.UseLogging = Environment.IsDevelopment();
o.HttpHost = new Uri("http://localhost:5000/");
}).AddClientCredentialsTokenHandler();
});

// 注册与配置clientId模式的token提者选项
services.AddClientCredentialsTokenProvider<IUserApi>(o =>
{
o.Endpoint = new Uri("http://localhost:5000/api/tokens");
o.Credentials.Client_id = "clientId";
o.Credentials.Client_secret = "xxyyzz";
});

// 注册与配置password模式的token提者选项
services.AddPasswordCredentialsTokenProvider(x =>
{
x.AddOptions<IUserApi>().Configure(o =>
{
o.Endpoint = new Uri("http://localhost:5000/api/tokens");
o.Credentials.Client_id = "clientId";
o.Credentials.Client_secret = "xxyyzz";
o.Credentials.Username = "username";
o.Credentials.Password = "password";
});
});
});

// userApi客户端后台服务
services.AddScoped<UserService>().AddHostedService<UserHostedService>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
using Microsoft.Extensions.DependencyInjection;
using WebApiClientCore.Exceptions;
using WebApiClientCore.Extensions.OAuths;

namespace WebApiClientCore.Attributes
{
/// <summary>
/// 表示client_credentials授权方式的token应用特性
/// 表示由client模式token提供者提供的token应用特性
/// 需要注册services.AddClientCredentialsTokenProvider
/// </summary>
public class ClientCredentialsTokenAttribute : OAuthTokenAttribute
{
/// <summary>
/// 获取token提供者
/// </summary>
/// <param name="context">上下文</param>
/// <param name="context"></param>
/// <returns></returns>
protected override ITokenProvider GetTokenProvider(ApiRequestContext context)
{
var providerType = typeof(IClientCredentialsTokenProvider<>).MakeGenericType(context.ApiAction.InterfaceType);
return (ITokenProvider)context.HttpContext.ServiceProvider.GetRequiredService(providerType);
var provider = base.GetTokenProvider(context);
if (provider.ProviderType != ProviderType.ClientCredentials)
{
throw new ApiInvalidConfigException($"未注册{nameof(TokenProviderExtensions.AddClientCredentialsTokenProvider)}");
}
return provider;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.Extensions.DependencyInjection;
using WebApiClientCore.Exceptions;
using WebApiClientCore.Extensions.OAuths;

namespace WebApiClientCore.Attributes
{
/// <summary>
/// 表示由自定义token提供者提供的token应用特性
/// 需要注册services.AddCustomTokenProvider
/// </summary>
public class CustomTokenAttribute : OAuthTokenAttribute
{
/// <summary>
/// 获取token提供者
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
protected override ITokenProvider GetTokenProvider(ApiRequestContext context)
{
var provider = base.GetTokenProvider(context);
if (provider.ProviderType != ProviderType.Custom)
{
throw new ApiInvalidConfigException($"未注册{nameof(TokenProviderExtensions.AddCustomTokenProvider)}");
}
return provider;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System.Net;
using Microsoft.Extensions.DependencyInjection;
using System.Net;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using WebApiClientCore.Extensions.OAuths;

namespace WebApiClientCore.Attributes
{
/// <summary>
/// 表示OAuth授权token应用抽象特性
/// 表示token应用特性
/// </summary>
public abstract class OAuthTokenAttribute : ApiFilterAttribute
public class OAuthTokenAttribute : ApiFilterAttribute
{
/// <summary>
/// 请求之前
Expand Down Expand Up @@ -42,7 +43,11 @@ public sealed override Task OnResponseAsync(ApiResponseContext context)
/// </summary>
/// <param name="context">上下文</param>
/// <returns></returns>
protected abstract ITokenProvider GetTokenProvider(ApiRequestContext context);
protected virtual ITokenProvider GetTokenProvider(ApiRequestContext context)
{
var providerType = typeof(ITokenProvider<>).MakeGenericType(context.ApiAction.InterfaceType);
return (ITokenProvider)context.HttpContext.ServiceProvider.GetRequiredService(providerType);
}

/// <summary>
/// 应用token
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
using Microsoft.Extensions.DependencyInjection;
using WebApiClientCore.Exceptions;
using WebApiClientCore.Extensions.OAuths;

namespace WebApiClientCore.Attributes
{
/// <summary>
/// 表示password授权方式的token应用特性
/// 表示由password模式token提供者提供的token应用特性
/// 需要注册services.AddPasswordCredentialsTokenProvider
/// </summary>
public class PasswordCredentialsTokenAttribute : OAuthTokenAttribute
{
/// <summary>
/// 获取token提供者
/// </summary>
/// <param name="context">上下文</param>
/// <param name="context"></param>
/// <returns></returns>
protected override ITokenProvider GetTokenProvider(ApiRequestContext context)
{
var providerType = typeof(IPasswordCredentialsTokenProvider<>).MakeGenericType(context.ApiAction.InterfaceType);
return (ITokenProvider)context.HttpContext.ServiceProvider.GetRequiredService(providerType);
var provider = base.GetTokenProvider(context);
if (provider.ProviderType != ProviderType.PasswordClientCredentials)
{
throw new ApiInvalidConfigException($"未注册{nameof(TokenProviderExtensions.AddPasswordCredentialsTokenProvider)}");
}
return provider;
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
Expand All @@ -8,17 +9,17 @@
namespace WebApiClientCore.Extensions.OAuths.HttpMessageHandlers
{
/// <summary>
/// 表示OAuth授权token应用的http消息处理程序抽象
/// 表示token应用的http消息处理程序
/// </summary>
public abstract class OAuthTokenHandler : DelegatingHandler
public class OAuthTokenHandler : DelegatingHandler
{
/// <summary>
/// token提供者
/// </summary>
private readonly ITokenProvider tokenProvider;

/// <summary>
/// OAuth授权token应用的http消息处理程序抽象
/// token应用的http消息处理程序
/// </summary>
/// <param name="serviceProvider">服务提供者</param>
/// <param name="httpApiType">接口类型</param>
Expand All @@ -33,7 +34,11 @@ public OAuthTokenHandler(IServiceProvider serviceProvider, Type httpApiType)
/// <param name="serviceProvider">服务提供者</param>
/// <param name="httpApiType">接口类型</param>
/// <returns></returns>
protected abstract ITokenProvider GetTokenProvider(IServiceProvider serviceProvider, Type httpApiType);
protected virtual ITokenProvider GetTokenProvider(IServiceProvider serviceProvider, Type httpApiType)
{
var providerType = typeof(ITokenProvider<>).MakeGenericType(httpApiType);
return (ITokenProvider)serviceProvider.GetRequiredService(providerType);
}

/// <summary>
/// 检测响应是否未授权
Expand Down

This file was deleted.

This file was deleted.

5 changes: 5 additions & 0 deletions WebApiClientCore.Extensions.OAuths/ITokenProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ namespace WebApiClientCore.Extensions.OAuths
/// </summary>
public interface ITokenProvider
{
/// <summary>
/// 获取提供者类型
/// </summary>
ProviderType ProviderType { get; }

/// <summary>
/// 强制清除token以支持下次获取到新的token
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
namespace WebApiClientCore.Extensions.OAuths
{
/// <summary>
/// 定义Client身份信息token提供者的接口
/// 定义指定接口的token提供者的接口
/// </summary>
/// <typeparam name="THttpApi"></typeparam>
public interface IClientCredentialsTokenProvider<THttpApi> : ITokenProvider
public interface ITokenProvider<THttpApi> : ITokenProvider
{
}
}
}

This file was deleted.

Loading

0 comments on commit 68a7dc6

Please sign in to comment.