-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServiceCollectionExtensions.cs
107 lines (99 loc) · 4.83 KB
/
ServiceCollectionExtensions.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
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;
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)
{
services.AddHttpClient(Consts.HTTP_CLIENT_FACTORY_NAME);
return services
.AddSingleton<DataStore>()
.AddSingleton<ISerializationProvider, SystemTextJsonSerializer>()
.AddSingleton<IApiCommunicationHttpClientFactory, ApiCommunicationHttpClientFactory>()
.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)
{
services.AddHttpClient(Consts.HTTP_CLIENT_FACTORY_NAME);
return services
.AddSingleton<DataStore>()
.AddSingleton<ISerializationProvider, SystemTextJsonSerializer>()
.AddSingleton<IApiCommunicationHttpClientFactory, ApiCommunicationHttpClientFactory>()
.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);
});
}
}
}