-
Notifications
You must be signed in to change notification settings - Fork 11
/
IdentityBuilderExtensions.cs
225 lines (198 loc) · 10.6 KB
/
IdentityBuilderExtensions.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using Passwordless;
using Passwordless.AspNetCore;
using Passwordless.AspNetCore.Services;
using Passwordless.AspNetCore.Services.Implementations;
// Trick to make it show up where it's more likely to be useful
namespace Microsoft.Extensions.DependencyInjection;
/// <summary>
/// Default extensions to <see cref="IServiceCollection"/> and <see cref="IdentityBuilder"/> for <see cref="PasswordlessApiEndpointRouteBuilderExtensions.MapPasswordless(IEndpointRouteBuilder)"/>.
/// </summary>
public static class IdentityBuilderExtensions
{
[RequiresUnreferencedCode("This method is incompatible with assembly trimming.")]
[RequiresDynamicCode("This method is incompatible with native AOT compilation.")]
private static IServiceCollection AddPasswordlessIdentity(
this IServiceCollection services,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
Type userType,
Action<OptionsBuilder<PasswordlessAspNetCoreOptions>> configureOptions,
string? defaultScheme)
{
// Options
var optionsBuilder = services.AddOptions<PasswordlessAspNetCoreOptions>();
configureOptions(optionsBuilder);
// Default scheme
if (!string.IsNullOrEmpty(defaultScheme))
{
optionsBuilder.Configure(o => o.SignInScheme = defaultScheme);
}
// Services
services.TryAddScoped(
typeof(IPasswordlessService<PasswordlessRegisterRequest>),
typeof(PasswordlessService<>).MakeGenericType(userType)
);
services.TryAddScoped<ICustomizeRegisterOptions, NoopCustomizeRegisterOptions>();
return services;
}
/// <summary>
/// Adds the services to support <see cref="PasswordlessApiEndpointRouteBuilderExtensions.MapPasswordless(IEndpointRouteBuilder)" />
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" />.</param>
/// <param name="configure">Configures the <see cref="PasswordlessAspNetCoreOptions" />.</param>
/// <returns>The <see cref="IServiceCollection" />.</returns>
[RequiresUnreferencedCode("This method is incompatible with assembly trimming.")]
[RequiresDynamicCode("This method is incompatible with native AOT compilation.")]
public static IServiceCollection AddPasswordless<TUser>(
this IServiceCollection services,
Action<PasswordlessAspNetCoreOptions> configure)
where TUser : class, new()
{
// Don't set up options here because we can't use the provided delegate as it's for a different type
services.AddPasswordlessSdk(_ => { });
// Derive core options from ASP.NET Core options
services.AddOptions<PasswordlessOptions>()
.Configure<IOptions<PasswordlessAspNetCoreOptions>>((options, aspNetCoreOptionsAccessor) =>
{
var aspNetCoreOptions = aspNetCoreOptionsAccessor.Value;
options.ApiUrl = aspNetCoreOptions.ApiUrl;
options.ApiSecret = aspNetCoreOptions.ApiSecret;
options.ApiKey = aspNetCoreOptions.ApiKey;
});
return services.AddPasswordlessIdentity(typeof(TUser), o => o.Configure(configure), null);
}
/// <summary>
/// Adds the services to support <see cref="PasswordlessApiEndpointRouteBuilderExtensions.MapPasswordless(IEndpointRouteBuilder)" />
/// </summary>
[RequiresUnreferencedCode("This method is incompatible with assembly trimming.")]
[RequiresDynamicCode("This method is incompatible with native AOT compilation.")]
public static IServiceCollection AddPasswordless<TUser>(
this IServiceCollection services,
IConfiguration configuration)
where TUser : class, new()
{
services.AddPasswordlessSdk(configuration);
return services.AddPasswordlessIdentity(typeof(TUser), o => o.Bind(configuration), null);
}
/// <summary>
/// Adds the services to support <see cref="PasswordlessApiEndpointRouteBuilderExtensions.MapPasswordless(IEndpointRouteBuilder)" />
/// </summary>
[RequiresUnreferencedCode("This method is incompatible with assembly trimming.")]
[RequiresDynamicCode("This method is incompatible with native AOT compilation.")]
public static IServiceCollection AddPasswordless<TUser>(
this IServiceCollection services,
string configurationSection)
where TUser : class, new()
{
services.AddPasswordlessSdk(configurationSection);
return services.AddPasswordlessIdentity(typeof(TUser), o => o.BindConfiguration(configurationSection), null);
}
/// <summary>
/// Adds the services to support <see cref="PasswordlessApiEndpointRouteBuilderExtensions.MapPasswordless(IEndpointRouteBuilder)" />
/// </summary>
/// <param name="identity">The current <see cref="IdentityBuilder" /> instance.</param>
/// <param name="configure">Configures the <see cref="PasswordlessAspNetCoreOptions" />.</param>
/// <returns>The <see cref="IdentityBuilder" />.</returns>
[RequiresUnreferencedCode("This method is incompatible with assembly trimming.")]
[RequiresDynamicCode("This method is incompatible with native AOT compilation.")]
public static IdentityBuilder AddPasswordless(
this IdentityBuilder identity,
Action<PasswordlessAspNetCoreOptions> configure)
{
// Don't set up options here because we can't use the provided delegate as it's for a different type
identity.Services.AddPasswordlessSdk(_ => { });
// Derive core options from ASP.NET Core options
identity.Services.AddOptions<PasswordlessOptions>()
.Configure<IOptions<PasswordlessAspNetCoreOptions>>((options, aspNetCoreOptionsAccessor) =>
{
var aspNetCoreOptions = aspNetCoreOptionsAccessor.Value;
options.ApiUrl = aspNetCoreOptions.ApiUrl;
options.ApiSecret = aspNetCoreOptions.ApiSecret;
options.ApiKey = aspNetCoreOptions.ApiKey;
});
identity.Services.AddPasswordlessIdentity(
identity.UserType,
o => o.Configure(configure),
IdentityConstants.ApplicationScheme
);
return identity;
}
/// <summary>
/// Adds the services to support <see cref="PasswordlessApiEndpointRouteBuilderExtensions.MapPasswordless(IEndpointRouteBuilder)" />
/// </summary>
/// <param name="identity">The current <see cref="IdentityBuilder" /> instance.</param>
/// <param name="configuration">The <see cref="IConfiguration" /> to use to bind to <see cref="PasswordlessAspNetCoreOptions" />. Generally it's own section.</param>
/// <returns>The <see cref="IdentityBuilder" />.</returns>
[RequiresUnreferencedCode("This method is incompatible with assembly trimming.")]
[RequiresDynamicCode("This method is incompatible with native AOT compilation.")]
public static IdentityBuilder AddPasswordless(this IdentityBuilder identity, IConfiguration configuration)
{
identity.Services.AddPasswordlessSdk(configuration);
identity.Services.AddPasswordlessIdentity(
identity.UserType,
o => o.Bind(configuration),
IdentityConstants.ApplicationScheme
);
return identity;
}
/// <summary>
/// Adds the services to support <see cref="PasswordlessApiEndpointRouteBuilderExtensions.MapPasswordless(IEndpointRouteBuilder)" />
/// </summary>
/// <param name="identity">The current <see cref="IdentityBuilder" /> instance.</param>
/// <param name="configuration">The <see cref="IConfiguration" /> to use to bind to <see cref="PasswordlessAspNetCoreOptions" />. Generally it's own section.</param>
/// <returns>The <see cref="IdentityBuilder" />.</returns>
[RequiresUnreferencedCode("This method is incompatible with assembly trimming.")]
[RequiresDynamicCode("This method is incompatible with native AOT compilation.")]
public static IdentityBuilder AddPasswordless<TUserType>(this IdentityBuilder identity, IConfiguration configuration)
{
identity.Services.AddPasswordlessSdk(configuration);
identity.Services.AddPasswordlessIdentity(
typeof(TUserType),
o => o.Bind(configuration),
IdentityConstants.ApplicationScheme
);
return identity;
}
/// <summary>
/// Adds the services to support <see cref="PasswordlessApiEndpointRouteBuilderExtensions.MapPasswordless(IEndpointRouteBuilder)" />
/// </summary>
/// <param name="identity">The current <see cref="IdentityBuilder" /> instance.</param>
/// <param name="configurationSection">The configuration path to use to bind to <see cref="PasswordlessAspNetCoreOptions" />.</param>
/// <returns>The <see cref="IServiceCollection" />.</returns>
[RequiresUnreferencedCode("This method is incompatible with assembly trimming.")]
[RequiresDynamicCode("This method is incompatible with native AOT compilation.")]
public static IdentityBuilder AddPasswordless(this IdentityBuilder identity, string configurationSection)
{
identity.Services.AddPasswordlessSdk(configurationSection);
identity.Services.AddPasswordlessIdentity(
identity.UserType,
o => o.BindConfiguration(configurationSection),
IdentityConstants.ApplicationScheme
);
return identity;
}
/// <summary>
/// Adds the services to support <see cref="PasswordlessApiEndpointRouteBuilderExtensions.MapPasswordless(IEndpointRouteBuilder)" />
/// </summary>
/// <param name="identity">The current <see cref="IdentityBuilder" /> instance.</param>
/// <param name="configurationSection">The configuration path to use to bind to <see cref="PasswordlessAspNetCoreOptions" />.</param>
/// <returns>The <see cref="IServiceCollection" />.</returns>
[RequiresUnreferencedCode("This method is incompatible with assembly trimming.")]
[RequiresDynamicCode("This method is incompatible with native AOT compilation.")]
public static IdentityBuilder AddPasswordless<TUserType>(this IdentityBuilder identity, string configurationSection)
{
identity.Services.AddPasswordlessSdk(configurationSection);
identity.Services.AddPasswordlessIdentity(
typeof(TUserType),
o => o.BindConfiguration(configurationSection),
IdentityConstants.ApplicationScheme
);
return identity;
}
}