-
Notifications
You must be signed in to change notification settings - Fork 446
/
Copy pathTokenProvider.cs
151 lines (134 loc) · 4.98 KB
/
TokenProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using WebApiClientCore.Extensions.OAuths.Exceptions;
namespace WebApiClientCore.Extensions.OAuths.TokenProviders
{
/// <summary>
/// 表示Token提供者抽象类
/// </summary>
public abstract class TokenProvider : ITokenProvider
{
private class TokenItem
{
public AsyncRoot AsyncRoot { get; } = new AsyncRoot();
public TokenResult? TokenResult { get; set; }
}
/// <summary>
/// token缓存
/// </summary>
private static readonly ConcurrentDictionary<string, Lazy<TokenItem?>> keyTokens
= new ConcurrentDictionary<string, Lazy<TokenItem?>>();
/// <summary>
/// 服务提供者
/// </summary>
private readonly IServiceProvider services;
/// <summary>
/// 获取或设置别名
/// </summary>
public string Name { get; set; } = Options.DefaultName;
/// <summary>
/// Token提供者抽象类
/// </summary>
/// <param name="services"></param>
public TokenProvider(IServiceProvider services)
{
this.services = services;
}
/// <summary>
/// 获取选项值
/// Options名称为本类型的Name属性
/// </summary>
/// <typeparam name="TOptions"></typeparam>
/// <returns></returns>
public TOptions GetOptionsValue<TOptions>()
{
return this.services.GetRequiredService<IOptionsMonitor<TOptions>>().Get(this.Name);
}
/// <summary>
/// 强制清除token以支持下次获取到新的token
/// </summary>
public void ClearToken()
{
ClearToken(string.Empty);
}
/// <summary>
/// 强制清除token以支持下次获取到新的token
/// </summary>
/// <param name="key">应用标识</param>
public void ClearToken(string key)
{
keyTokens.TryRemove(key, out _);
}
/// <summary>
/// 获取token信息
/// </summary>
/// <returns></returns>
public async Task<TokenResult> GetTokenAsync()
{
return await GetTokenAsync(string.Empty);
}
/// <summary>
/// 获取token信息
/// </summary>
/// <param name="key">应用标识</param>
public async Task<TokenResult> GetTokenAsync(string key)
{
var tokenItem = keyTokens.GetOrAdd(key, _ => new Lazy<TokenItem?>(() => new TokenItem())).Value!;
using (await tokenItem.AsyncRoot.LockAsync().ConfigureAwait(false))
{
if (tokenItem.TokenResult == null)
{
using var scope = services.CreateScope();
tokenItem.TokenResult = await RequestTokenAsync(scope.ServiceProvider, key).ConfigureAwait(false);
}
else if (tokenItem.TokenResult.IsExpired())
{
using var scope = services.CreateScope();
tokenItem.TokenResult = tokenItem.TokenResult.CanRefresh()
? await RefreshTokenAsync(scope.ServiceProvider, tokenItem.TokenResult.Refresh_token!).ConfigureAwait(false)
: await RequestTokenAsync(scope.ServiceProvider, key).ConfigureAwait(false);
}
if (tokenItem.TokenResult == null)
{
throw new TokenNullException();
}
return tokenItem.TokenResult.EnsureSuccess();
}
}
/// <summary>
/// 请求获取token
/// </summary>
/// <param name="serviceProvider">服务提供者</param>
/// <returns></returns>
protected abstract Task<TokenResult?> RequestTokenAsync(IServiceProvider serviceProvider);
/// <summary>
/// 请求获取token
/// </summary>
/// <param name="serviceProvider">服务提供者</param>
/// <param name="key">应用标识</param>
protected virtual Task<TokenResult?> RequestTokenAsync(IServiceProvider serviceProvider, string? key)
{
if (string.IsNullOrEmpty(key))
return RequestTokenAsync(serviceProvider);
throw new NotImplementedException();
}
/// <summary>
/// 刷新token
/// </summary>
/// <param name="serviceProvider">服务提供者</param>
/// <param name="refresh_token">刷新token</param>
/// <returns></returns>
protected abstract Task<TokenResult?> RefreshTokenAsync(IServiceProvider serviceProvider, string refresh_token);
/// <summary>
/// 转换为string
/// </summary>
/// <returns></returns>
public override string ToString()
{
return this.Name;
}
}
}