1
1
using Microsoft . AspNetCore . Authentication ;
2
+ using Microsoft . Extensions . DependencyModel ;
2
3
using Microsoft . Extensions . Logging ;
3
4
using Newtonsoft . Json ;
4
5
using System ;
6
+ using System . Linq ;
7
+ using System . Reflection ;
5
8
using System . Threading ;
6
9
using System . Threading . Tasks ;
7
10
8
11
namespace Aguacongas . AspNetCore . Authentication
9
12
{
10
- public class DynamicManager : DynamicManager < ProviderDefinition >
11
- {
12
- /// <summary>
13
- /// Initializes a new instance of the <see cref="DynamicManager"/> class.
14
- /// </summary>
15
- /// <param name="schemeProvider">The scheme provider.</param>
16
- /// <param name="wrapperFactory">The wrapper factory.</param>
17
- /// <param name="store">The store.</param>
18
- /// <param name="logger">The logger.</param>
19
- public DynamicManager ( IAuthenticationSchemeProvider schemeProvider ,
20
- OptionsMonitorCacheWrapperFactory wrapperFactory ,
21
- IDynamicProviderStore < ProviderDefinition > store ,
22
- ILogger < DynamicManager < ProviderDefinition > > logger )
23
- : base ( schemeProvider , wrapperFactory , store , logger )
24
- {
25
- }
26
- }
27
-
28
13
public class DynamicManager < TDefinition >
29
14
where TDefinition : ProviderDefinition , new ( )
30
15
{
31
- private static readonly JsonSerializerSettings _jsonSerializerSettings = new JsonSerializerSettings
32
- {
33
- NullValueHandling = NullValueHandling . Ignore ,
34
- ReferenceLoopHandling = ReferenceLoopHandling . Ignore ,
35
- Formatting = Formatting . None ,
36
- DefaultValueHandling = DefaultValueHandling . Ignore ,
37
- ContractResolver = new ContractResolver ( )
38
- } ;
39
-
40
16
private readonly IDynamicProviderStore < TDefinition > _store ;
41
17
private readonly ILogger < DynamicManager < TDefinition > > _logger ;
42
18
private readonly IAuthenticationSchemeProvider _schemeProvider ;
43
19
private readonly OptionsMonitorCacheWrapperFactory _wrapperFactory ;
44
20
21
+ /// <summary>
22
+ /// Gets or sets the json serializer settings.
23
+ /// </summary>
24
+ /// <value>
25
+ /// The json serializer settings.
26
+ /// </value>
27
+ public static JsonSerializerSettings JsonSerializerSettings { get ; } = new JsonSerializerSettings
28
+ {
29
+ NullValueHandling = NullValueHandling . Ignore ,
30
+ ReferenceLoopHandling = ReferenceLoopHandling . Ignore ,
31
+ Formatting = Formatting . None ,
32
+ DefaultValueHandling = DefaultValueHandling . Ignore ,
33
+ ContractResolver = new ContractResolver ( )
34
+ } ;
35
+
36
+ /// <summary>
37
+ /// Gets or sets the function to serialize the provider definition.
38
+ /// </summary>
39
+ /// <value>
40
+ /// The serialize function.
41
+ /// </value>
45
42
public Func < AuthenticationSchemeOptions , Type , string > Serialize { get ; set ; } = SerializeOptions ;
46
43
44
+ public Func < string , Type , AuthenticationSchemeOptions > Deserialize { get ; set ; } = DeserializeOptions ;
45
+
47
46
/// <summary>
48
47
/// Initializes a new instance of the <see cref="DynamicManager{TDefinition}"/> class.
49
48
/// </summary>
@@ -55,10 +54,10 @@ public DynamicManager(IAuthenticationSchemeProvider schemeProvider,
55
54
OptionsMonitorCacheWrapperFactory wrapperFactory ,
56
55
IDynamicProviderStore < TDefinition > store , ILogger < DynamicManager < TDefinition > > logger )
57
56
{
58
- _schemeProvider = schemeProvider ;
59
- _wrapperFactory = wrapperFactory ;
60
- _store = store ;
61
- _logger = logger ;
57
+ _schemeProvider = schemeProvider ?? throw new ArgumentNullException ( nameof ( schemeProvider ) ) ;
58
+ _wrapperFactory = wrapperFactory ?? throw new ArgumentNullException ( nameof ( wrapperFactory ) ) ;
59
+ _store = store ?? throw new ArgumentNullException ( nameof ( store ) ) ;
60
+ _logger = logger ?? throw new ArgumentNullException ( nameof ( logger ) ) ;
62
61
}
63
62
64
63
/// <summary>
@@ -75,7 +74,7 @@ public DynamicManager(IAuthenticationSchemeProvider schemeProvider,
75
74
where THandler : AuthenticationHandler < TOptions >
76
75
where TOptions : AuthenticationSchemeOptions , new ( )
77
76
{
78
- return AddAsync ( scheme , displayName , typeof ( THandler ) , ( AuthenticationSchemeOptions ) options , cancellationToken ) ;
77
+ return AddAsync ( scheme , displayName , typeof ( THandler ) , options , cancellationToken ) ;
79
78
}
80
79
81
80
/// <summary>
@@ -123,6 +122,22 @@ await _store.AddAsync(new TDefinition
123
122
_logger . LogInformation ( "Scheme {scheme} added with name {displayName} for {handlerType} with options {options}" , scheme , displayName , handlerType , serializerOptions ) ;
124
123
}
125
124
125
+ /// <summary>
126
+ /// Updates the scheme asynchronously.
127
+ /// </summary>
128
+ /// <typeparam name="THandler">The type of the handler.</typeparam>
129
+ /// <typeparam name="TOptions">The type of the options.</typeparam>
130
+ /// <param name="scheme">The scheme.</param>
131
+ /// <param name="displayName">The display name.</param>
132
+ /// <param name="options">The options.</param>
133
+ /// <param name="cancellationToken">The cancellation token.</param>
134
+ /// <returns></returns>
135
+ public Task UpdateAsync < THandler , TOptions > ( string scheme , string displayName , TOptions options , CancellationToken cancellationToken = default ( CancellationToken ) )
136
+ where THandler : AuthenticationHandler < TOptions >
137
+ where TOptions : AuthenticationSchemeOptions , new ( )
138
+ {
139
+ return UpdateAsync ( scheme , displayName , typeof ( THandler ) , options , cancellationToken ) ;
140
+ }
126
141
/// <summary>
127
142
/// Updates the scheme asynchronously.
128
143
/// </summary>
@@ -139,7 +154,7 @@ await _store.AddAsync(new TDefinition
139
154
public async Task UpdateAsync ( string scheme , string displayName , Type handlerType , AuthenticationSchemeOptions options , CancellationToken cancellationToken = default ( CancellationToken ) )
140
155
{
141
156
var genericTypeArguments = GetGenericTypeArguments ( handlerType ) ;
142
- if ( handlerType . GetInterface ( nameof ( IAuthenticationHandler ) ) == null || handlerType . GenericTypeArguments . Length == 0 )
157
+ if ( handlerType . GetInterface ( nameof ( IAuthenticationHandler ) ) == null || genericTypeArguments . Length == 0 )
143
158
{
144
159
throw new ArgumentException ( $ "Parameter { nameof ( handlerType ) } should be a { nameof ( AuthenticationHandler < AuthenticationSchemeOptions > ) } ") ;
145
160
}
@@ -171,6 +186,8 @@ await _store.AddAsync(new TDefinition
171
186
172
187
_schemeProvider . AddScheme ( new AuthenticationScheme ( scheme , displayName , handlerType ) ) ;
173
188
optionsMonitorCache . TryAdd ( scheme , options ) ;
189
+
190
+ _logger . LogInformation ( "Scheme {scheme} updated with name {displayName} for {handlerType} with options {options}" , scheme , displayName , handlerType , serializerOptions ) ;
174
191
}
175
192
176
193
/// <summary>
@@ -191,6 +208,8 @@ await _store.AddAsync(new TDefinition
191
208
await _store . RemoveAsync ( definition , cancellationToken ) ;
192
209
_schemeProvider . RemoveScheme ( scheme ) ;
193
210
optionsMonitorCache . TryRemove ( scheme ) ;
211
+
212
+ _logger . LogInformation ( "Scheme {scheme} removed for handler type {handlerType}" , scheme , handlerType ) ;
194
213
}
195
214
}
196
215
@@ -199,22 +218,34 @@ await _store.AddAsync(new TDefinition
199
218
/// </summary>
200
219
public void Load ( )
201
220
{
202
- foreach ( var definition in _store . ProviderDefinitions )
221
+ var platform = Environment . OSVersion . Platform . ToString ( ) ;
222
+ var runtimeAssemblyNames = DependencyContext . Default . GetRuntimeAssemblyNames ( platform ) ;
223
+
224
+ foreach ( var definition in _store . ProviderDefinitions )
203
225
{
204
226
var scheme = definition . Id ;
205
- var handlerType = Type . GetType ( definition . HandlerTypeName ) ;
206
- var optionsType = handlerType . GenericTypeArguments [ 0 ] ;
227
+ var handlerType = runtimeAssemblyNames
228
+ . Select ( Assembly . Load )
229
+ . SelectMany ( a => a . ExportedTypes )
230
+ . First ( t => t . FullName == definition . HandlerTypeName ) ;
231
+
232
+ var optionsType = GetGenericTypeArguments ( handlerType ) [ 0 ] ;
207
233
var optionsMonitorCache = _wrapperFactory . Get ( optionsType ) ;
208
- var options = JsonConvert . DeserializeObject ( definition . SerializedOptions , optionsType ) as AuthenticationSchemeOptions ;
234
+ var options = Deserialize ( definition . SerializedOptions , optionsType ) ;
209
235
210
236
_schemeProvider . AddScheme ( new AuthenticationScheme ( scheme , definition . DisplayName , handlerType ) ) ;
211
237
optionsMonitorCache . TryAdd ( scheme , options ) ;
212
238
}
213
239
}
214
240
215
- protected static string SerializeOptions ( AuthenticationSchemeOptions options , Type optionsType )
241
+ private static string SerializeOptions ( AuthenticationSchemeOptions options , Type optionsType )
242
+ {
243
+ return JsonConvert . SerializeObject ( options , optionsType , JsonSerializerSettings ) ;
244
+ }
245
+
246
+ private static AuthenticationSchemeOptions DeserializeOptions ( string value , Type optionsType )
216
247
{
217
- return JsonConvert . SerializeObject ( options , optionsType , _jsonSerializerSettings ) ;
248
+ return JsonConvert . DeserializeObject ( value , optionsType ) as AuthenticationSchemeOptions ;
218
249
}
219
250
220
251
private Type [ ] GetGenericTypeArguments ( Type type )
@@ -231,13 +262,5 @@ private Type[] GetGenericTypeArguments(Type type)
231
262
232
263
return GetGenericTypeArguments ( type . BaseType ) ;
233
264
}
234
-
235
- class OptionsMonitorCacheWrapper
236
- {
237
- public OptionsMonitorCacheWrapper ( Type optionsType )
238
- {
239
-
240
- }
241
- }
242
265
}
243
266
}
0 commit comments