-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceCollectionExtensions.cs
More file actions
134 lines (126 loc) · 5.4 KB
/
ServiceCollectionExtensions.cs
File metadata and controls
134 lines (126 loc) · 5.4 KB
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
using Forge.Security.Jwt.Client.Api;
using Forge.Security.Jwt.Client.Services;
using Forge.Security.Jwt.Shared.Client.Api;
using Forge.Security.Jwt.Shared.Client.Models;
using Forge.Security.Jwt.Shared.Client.Services;
using Forge.Security.Jwt.Shared.Serialization;
using Forge.Security.Jwt.Shared.Storage;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Net.Http;
namespace Forge.Security.Jwt.Client
{
/// <summary>ServiceCollection extension methods</summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// Registers the Forge Jwt Client side security services as scoped.
/// </summary>
/// <returns>IServiceCollection</returns>
public static IServiceCollection AddForgeJwtClientAuthenticationCore(this IServiceCollection services,
Action<JwtClientAuthenticationCoreOptions>
#if NETSTANDARD2_0
#else
?
#endif
configure = null,
HttpMessageHandler
#if NETSTANDARD2_0
#else
?
#endif
httpMessageHandler = null)
{
if (httpMessageHandler == null)
{
services.AddHttpClient(Consts.HTTP_CLIENT_FACTORY_NAME);
}
else
{
services.AddHttpClient(Consts.HTTP_CLIENT_FACTORY_NAME).ConfigurePrimaryHttpMessageHandler(_ => httpMessageHandler);
}
return services
.AddSingleton<DataStore>()
.AddSingleton<ISerializationProvider, SystemTextJsonSerializer>()
.AddSingleton<ITokenizedApiCommunicationService, TokenizedApiCommunicationService>()
.AddSingleton<IAdditionalData, AdditionalData>(serviceProvider =>
{
IOptions<JwtClientAuthenticationCoreOptions> options = serviceProvider.GetService<IOptions<JwtClientAuthenticationCoreOptions>>()
#if NETSTANDARD2_0
#else
!
#endif
;
AdditionalData logoutData = new AdditionalData();
logoutData.SecondaryKeys.AddRange(options.Value.SecondaryKeys);
return logoutData;
})
.AddScoped<IStorage<ParsedTokenData>, MemoryStorage<ParsedTokenData>>()
.AddScoped<JwtTokenAuthenticationStateProvider>()
.AddScoped<IJwtTokenAuthenticationStateProvider>(sp => sp.GetRequiredService<JwtTokenAuthenticationStateProvider>())
.AddScoped<AuthenticationStateProvider>(sp => sp.GetRequiredService<JwtTokenAuthenticationStateProvider>())
.AddScoped<IAuthenticationService, AuthenticationService>()
.AddScoped<IRefreshTokenService, JwtTokenRefreshHostedService>()
.Configure<JwtClientAuthenticationCoreOptions>(configureOptions =>
{
configure?.Invoke(configureOptions);
});
}
/// <summary>
/// Registers the Forge Jwt Client side security services as singletons.
/// </summary>
/// <returns>IServiceCollection</returns>
public static IServiceCollection AddForgeJwtClientAuthenticationCoreAsSingleton(
this IServiceCollection services,
Action<JwtClientAuthenticationCoreOptions>
#if NETSTANDARD2_0
#else
?
#endif
configure = null,
HttpMessageHandler
#if NETSTANDARD2_0
#else
?
#endif
httpMessageHandler = null)
{
if (httpMessageHandler == null)
{
services.AddHttpClient(Consts.HTTP_CLIENT_FACTORY_NAME);
}
else
{
services.AddHttpClient(Consts.HTTP_CLIENT_FACTORY_NAME).ConfigurePrimaryHttpMessageHandler(_ => httpMessageHandler);
}
return services
.AddSingleton<DataStore>()
.AddSingleton<ISerializationProvider, SystemTextJsonSerializer>()
.AddSingleton<ITokenizedApiCommunicationService, TokenizedApiCommunicationService>()
.AddSingleton<IStorage<ParsedTokenData>, MemoryStorage<ParsedTokenData>>()
.AddSingleton<JwtTokenAuthenticationStateProvider>()
.AddSingleton<IJwtTokenAuthenticationStateProvider>(sp => sp.GetRequiredService<JwtTokenAuthenticationStateProvider>())
.AddSingleton<AuthenticationStateProvider>(sp => sp.GetRequiredService<JwtTokenAuthenticationStateProvider>())
.AddSingleton<IAdditionalData, AdditionalData>(serviceProvider =>
{
IOptions<JwtClientAuthenticationCoreOptions> options = serviceProvider.GetService<IOptions<JwtClientAuthenticationCoreOptions>>()
#if NETSTANDARD2_0
#else
!
#endif
;
AdditionalData logoutData = new AdditionalData();
logoutData.SecondaryKeys.AddRange(options.Value.SecondaryKeys);
return logoutData;
})
.AddSingleton<IAuthenticationService, AuthenticationService>()
.AddSingleton<IRefreshTokenService, JwtTokenRefreshHostedService>()
.Configure<JwtClientAuthenticationCoreOptions>(configureOptions =>
{
configure?.Invoke(configureOptions);
});
}
}
}