-
Notifications
You must be signed in to change notification settings - Fork 446
/
Copy pathTokenProviderFactory.cs
170 lines (152 loc) · 6.48 KB
/
TokenProviderFactory.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
namespace WebApiClientCore.Extensions.OAuths
{
/// <summary>
/// 表示默认的 token 提供者工厂
/// </summary>
sealed class TokenProviderFactory : ITokenProviderFactory
{
private readonly IServiceProvider serviceProvider;
private readonly TokenProviderFactoryOptions options;
private readonly ConcurrentDictionary<ServiceKey, ITokenProvider> tokenProviderCache = new();
/// <summary>
/// 默认的 token 提供者工厂
/// </summary>
/// <param name="serviceProvider"></param>
/// <param name="options"></param>
public TokenProviderFactory(IServiceProvider serviceProvider, IOptions<TokenProviderFactoryOptions> options)
{
this.serviceProvider = serviceProvider;
this.options = options.Value;
}
/// <summary>
/// 通过接口类型获取或创建其对应的 token 提供者
/// </summary>
/// <param name="httpApiType">接口类型</param>
/// <param name="typeMatchMode">类型匹配模式</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="InvalidOperationException"></exception>
public ITokenProvider Create(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type httpApiType,
TypeMatchMode typeMatchMode = TypeMatchMode.TypeOnly)
{
return this.Create(httpApiType, typeMatchMode, alias: string.Empty);
}
/// <summary>
/// 通过接口类型获取或创建其对应的 token 提供者
/// </summary>
/// <param name="httpApiType">接口类型</param>
/// <param name="typeMatchMode">类型匹配模式</param>
/// <param name="alias">TokenProvider的别名</param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public ITokenProvider Create(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type httpApiType,
TypeMatchMode typeMatchMode,
string alias)
{
if (httpApiType == null)
{
throw new ArgumentNullException(nameof(httpApiType));
}
if (alias == null)
{
throw new ArgumentNullException(nameof(alias));
}
var serviceKey = new ServiceKey(httpApiType, typeMatchMode, alias);
return this.tokenProviderCache.GetOrAdd(serviceKey, this.CreateTokenProvider);
}
/// <summary>
/// 创建其对应的 token 提供者
/// </summary>
/// <param name="serviceKey">缓存的键</param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
private ITokenProvider CreateTokenProvider(ServiceKey serviceKey)
{
var alias = serviceKey.Alias;
var httpApiType = serviceKey.HttpApiType;
if (this.options.TryGetValue(httpApiType, out var descriptor) && descriptor.ContainsAlias(alias))
{
var service = (ITokenProviderService)this.serviceProvider.GetRequiredService(descriptor.ServiceType);
service.SetProviderName(alias);
return service.TokenProvider;
}
if (serviceKey.TypeMatchMode == TypeMatchMode.TypeOrBaseTypes)
{
var tokenProvider = this.CreateTokenProviderFromBaseType(httpApiType, alias);
if (tokenProvider != null)
{
return tokenProvider;
}
}
var message = string.IsNullOrEmpty(alias)
? $"尚未注册 {httpApiType} 无别名的 token 提供者"
: $"尚未注册 {httpApiType} 别名为{alias}的 token 提供者";
throw new InvalidOperationException(message);
}
/// <summary>
/// 从基础接口创建TokenProvider
/// </summary>
/// <param name="httpApiType"></param>
/// <param name="alias">别名</param>
/// <exception cref="InvalidOperationException"></exception>
/// <returns></returns>
private ITokenProvider? CreateTokenProviderFromBaseType(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type httpApiType,
string alias)
{
foreach (var baseType in httpApiType.GetInterfaces())
{
if (this.options.TryGetValue(baseType, out var descriptor) && descriptor.ContainsAlias(alias))
{
var service = (ITokenProviderService)this.serviceProvider.GetRequiredService(descriptor.ServiceType);
service.SetProviderName(alias);
return service.TokenProvider;
}
}
return null;
}
/// <summary>
/// 服务缓存的键
/// </summary>
private sealed class ServiceKey : IEquatable<ServiceKey>
{
private int? hashCode;
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
public Type HttpApiType { get; }
public TypeMatchMode TypeMatchMode { get; }
public string Alias { get; }
public ServiceKey(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type httpApiType,
TypeMatchMode typeMatchMode,
string alias)
{
this.HttpApiType = httpApiType;
this.TypeMatchMode = typeMatchMode;
this.Alias = alias;
}
public bool Equals(ServiceKey? other)
{
return other != null &&
this.HttpApiType == other.HttpApiType &&
this.TypeMatchMode == other.TypeMatchMode &&
this.Alias == other.Alias;
}
public override bool Equals(object? obj)
{
return obj is ServiceKey other && this.Equals(other);
}
public override int GetHashCode()
{
this.hashCode ??= HashCode.Combine(this.HttpApiType, this.TypeMatchMode, this.Alias);
return this.hashCode.Value;
}
}
}
}