Skip to content

Commit ef38dfd

Browse files
committed
增加ClientCredentialsTokenFilter
1 parent c2d51d0 commit ef38dfd

File tree

5 files changed

+42
-26
lines changed

5 files changed

+42
-26
lines changed

WebApiClient/AuthTokens/AuthTokenFilter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ async Task IApiActionFilter.OnBeginRequestAsync(ApiActionContext context)
4747
/// <summary>
4848
/// 初始化或刷新token
4949
/// </summary>
50+
/// <exception cref="HttpApiTokenException"></exception>
5051
/// <returns></returns>
5152
private async Task InitOrRefreshTokenAsync()
5253
{

Demo/TokenFilter.cs renamed to WebApiClient/AuthTokens/ClientCredentialsTokenFilter.cs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,40 @@
1-
using System;
2-
using System.Threading.Tasks;
3-
using WebApiClient.AuthTokens;
1+
using System.Threading.Tasks;
42

5-
namespace Demo
3+
namespace WebApiClient.AuthTokens
64
{
75
/// <summary>
86
/// 表示提供client_credentials方式的token过滤器
97
/// </summary>
10-
public class TokenFilter : AuthTokenFilter
8+
public class ClientCredentialsTokenFilter : AuthTokenFilter
119
{
1210
/// <summary>
13-
/// 获取提供Token获取的Url节点
11+
/// 获取或设置提供Token获取的Url节点
12+
/// 必填
1413
/// </summary>
15-
public string TokenEndpoint { get; private set; }
14+
public string TokenEndpoint { get; set; }
1615

1716
/// <summary>
18-
/// 获取client_id
17+
/// 获取或设置client_id
18+
/// 必填
1919
/// </summary>
20-
public string ClientId { get; private set; }
20+
public string ClientId { get; set; }
2121

2222
/// <summary>
23-
/// 获取client_secret
23+
/// 获取或设置client_secret
24+
/// 必填
2425
/// </summary>
25-
public string ClientSecret { get; private set; }
26+
public string ClientSecret { get; set; }
2627

2728
/// <summary>
28-
/// OAuth授权的token过滤器
29+
/// 获取或设置资源范围
2930
/// </summary>
30-
/// <param name="tokenEndPoint">提供Token获取的Url节点</param>
31-
/// <param name="client_id">客户端id</param>
32-
/// <param name="client_secret">客户端密码</param>
33-
public TokenFilter(string tokenEndPoint, string client_id, string client_secret)
34-
{
35-
this.TokenEndpoint = tokenEndPoint ?? throw new ArgumentNullException(nameof(tokenEndPoint));
36-
this.ClientId = client_id ?? throw new ArgumentNullException(nameof(client_id));
37-
this.ClientSecret = client_secret ?? throw new ArgumentNullException(nameof(client_secret));
38-
}
31+
public string Scope { get; set; }
32+
33+
/// <summary>
34+
/// 获取或设置额外字段,支持字典或模型
35+
/// </summary>
36+
public object Extra { get; set; }
37+
3938

4039
/// <summary>
4140
/// 请求获取token
@@ -45,7 +44,7 @@ public TokenFilter(string tokenEndPoint, string client_id, string client_secret)
4544
protected override async Task<TokenResult> RequestTokenResultAsync()
4645
{
4746
var tokenClient = new TokenClient(this.TokenEndpoint);
48-
return await tokenClient.RequestClientCredentialsAsync(this.ClientId, this.ClientSecret);
47+
return await tokenClient.RequestClientCredentialsAsync(this.ClientId, this.ClientSecret, this.Scope, this.Extra);
4948
}
5049

5150
/// <summary>

WebApiClient/AuthTokens/TokenClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public TimeSpan TimeOut
6767
/// <exception cref="ArgumentException"></exception>
6868
/// <exception cref="UriFormatException"></exception>
6969
public TokenClient(string tokenEndpoint)
70-
: this(new Uri(tokenEndpoint))
70+
: this(new Uri(tokenEndpoint ?? throw new ArgumentNullException(nameof(tokenEndpoint))))
7171
{
7272
}
7373

WebApiClient/AuthTokens/TokenResult.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Net.Http;
32
using WebApiClient.DataAnnotations;
43

54
namespace WebApiClient.AuthTokens
@@ -54,14 +53,14 @@ public class TokenResult
5453
/// <summary>
5554
/// 确保token成功
5655
/// </summary>
57-
/// <exception cref="HttpRequestException"></exception>
56+
/// <exception cref="HttpApiTokenException"></exception>
5857
public TokenResult EnsureSuccess()
5958
{
6059
if (this.IsSuccess() == true)
6160
{
6261
return this;
6362
}
64-
throw new HttpRequestException(this.Error);
63+
throw new HttpApiTokenException(this.Error);
6564
}
6665

6766
/// <summary>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace WebApiClient
2+
{
3+
/// <summary>
4+
/// 表示请求Token异常
5+
/// </summary>
6+
public class HttpApiTokenException : HttpApiException
7+
{
8+
/// <summary>
9+
/// 请求Token异常
10+
/// </summary>
11+
/// <param name="message">消息</param>
12+
public HttpApiTokenException(string message)
13+
: base(message)
14+
{
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)