Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.ASP.NET Identity SDK: Options pattern is not reading updated values #125

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 86 additions & 31 deletions src/Passwordless.AspNetCore/IdentityBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,6 @@ namespace Microsoft.Extensions.DependencyInjection;
/// </summary>
public static class IdentityBuilderExtensions
{
/// <summary>
/// Adds the services to support <see cref="PasswordlessApiEndpointRouteBuilderExtensions.MapPasswordless(IEndpointRouteBuilder)" />
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" />.</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="IServiceCollection" />.</returns>
public static IServiceCollection AddPasswordless<TUser>(this IServiceCollection services, IConfiguration configuration)
where TUser : class, new()
{
return services.AddPasswordlessCore(typeof(TUser), configuration.Bind, defaultScheme: null);
}

/// <summary>
/// Adds the services to support <see cref="PasswordlessApiEndpointRouteBuilderExtensions.MapPasswordless(IEndpointRouteBuilder)" />
/// </summary>
Expand All @@ -42,19 +30,6 @@ public static IServiceCollection AddPasswordless<TUser>(this IServiceCollection
return services.AddPasswordlessCore(typeof(TUser), configure, defaultScheme: null);
}


/// <summary>
/// Adds the services to support <see cref="PasswordlessApiEndpointRouteBuilderExtensions.MapPasswordless(IEndpointRouteBuilder)" />
/// </summary>
/// <param name="builder">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>
public static IdentityBuilder AddPasswordless(this IdentityBuilder builder, IConfiguration configuration)
{
builder.Services.AddPasswordlessCore(builder.UserType, configuration.Bind, IdentityConstants.ApplicationScheme);
return builder;
}

/// <summary>
/// Adds the services to support <see cref="PasswordlessApiEndpointRouteBuilderExtensions.MapPasswordless(IEndpointRouteBuilder)" />
/// </summary>
Expand Down Expand Up @@ -84,12 +59,6 @@ private static IServiceCollection AddPasswordlessCore(this IServiceCollection se
// Add the SDK services but don't configure it there since ASP.NET Core options are a superset of their options.
services.AddPasswordlessSdk(_ => { });

services.TryAddScoped(
typeof(IPasswordlessService<PasswordlessRegisterRequest>),
typeof(PasswordlessService<>).MakeGenericType(userType));

services.TryAddScoped<ICustomizeRegisterOptions, NoopCustomizeRegisterOptions>();

// Override SDK options to come from ASP.NET Core options
services.AddOptions<PasswordlessOptions>()
.Configure<IOptions<PasswordlessAspNetCoreOptions>>((options, aspNetCoreOptionsAccessor) =>
Expand All @@ -102,4 +71,90 @@ private static IServiceCollection AddPasswordlessCore(this IServiceCollection se

return services;
}

/// <summary>
/// Adds the services to support <see cref="PasswordlessApiEndpointRouteBuilderExtensions.MapPasswordless(IEndpointRouteBuilder)" />
/// </summary>
/// <param name="builder">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>
public static IServiceCollection AddPasswordless(this IdentityBuilder builder, IConfiguration configuration)
{
return builder.AddPasswordless(configuration, builder.UserType, IdentityConstants.ApplicationScheme);
}

/// <summary>
/// Adds the services to support <see cref="PasswordlessApiEndpointRouteBuilderExtensions.MapPasswordless(IEndpointRouteBuilder)" />
/// </summary>
/// <param name="builder">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>
public static IServiceCollection AddPasswordless<TUserType>(this IdentityBuilder builder, IConfiguration configuration)
{
return builder.AddPasswordless(configuration, typeof(TUserType), null);
}

private static IServiceCollection AddPasswordless(this IdentityBuilder builder, IConfiguration configuration, Type? userType, string? defaultScheme)
{
var optionsBuilder = builder.Services
.AddOptions<PasswordlessAspNetCoreOptions>()
.Bind(configuration);

builder.Services.AddPasswordlessSdk(configuration);

return builder.Services.AddShared(userType ?? builder.UserType, optionsBuilder, IdentityConstants.ApplicationScheme);
}

/// <summary>
/// Adds the services to support <see cref="PasswordlessApiEndpointRouteBuilderExtensions.MapPasswordless(IEndpointRouteBuilder)" />
/// </summary>
/// <param name="builder">The current <see cref="IdentityBuilder" /> instance.</param>
/// <param name="path">The configuration path to use to bind to <see cref="PasswordlessAspNetCoreOptions" />.</param>
/// <returns>The <see cref="IServiceCollection" />.</returns>
public static IServiceCollection AddPasswordless(this IdentityBuilder builder, string path)
{
return builder.AddPasswordless(path, builder.UserType, IdentityConstants.ApplicationScheme);
}

/// <summary>
/// Adds the services to support <see cref="PasswordlessApiEndpointRouteBuilderExtensions.MapPasswordless(IEndpointRouteBuilder)" />
/// </summary>
/// <param name="builder">The current <see cref="IdentityBuilder" /> instance.</param>
/// <param name="path">The configuration path to use to bind to <see cref="PasswordlessAspNetCoreOptions" />.</param>
/// <returns>The <see cref="IServiceCollection" />.</returns>
public static IServiceCollection AddPasswordless<TUserType>(this IdentityBuilder builder, string path)
{
return builder.AddPasswordless(path, typeof(TUserType), null);
}

private static IServiceCollection AddPasswordless(this IdentityBuilder builder, string path, Type userType, string? defaultScheme)
{
var optionsBuilder = builder.Services
.AddOptions<PasswordlessAspNetCoreOptions>()
.BindConfiguration(path);

builder.Services.AddPasswordlessSdk(path);

return builder.Services.AddShared(userType ?? builder.UserType, optionsBuilder, IdentityConstants.ApplicationScheme);
}

private static IServiceCollection AddShared(this IServiceCollection services,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
Type userType,
OptionsBuilder<PasswordlessAspNetCoreOptions> optionsBuilder,
string? defaultScheme)
{
if (!string.IsNullOrEmpty(defaultScheme))
{
optionsBuilder.Configure(o => o.SignInScheme = defaultScheme);
}

services.TryAddScoped(
typeof(IPasswordlessService<PasswordlessRegisterRequest>),
typeof(PasswordlessService<>).MakeGenericType(userType));

services.TryAddScoped<ICustomizeRegisterOptions, NoopCustomizeRegisterOptions>();

return services;
}
}
3 changes: 1 addition & 2 deletions src/Passwordless/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Dynamic;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using Passwordless;
Expand Down Expand Up @@ -66,7 +65,7 @@ public static IServiceCollection AddPasswordlessSdk(
private static void RegisterDependencies(this IServiceCollection services)
{
services.AddHttpClient<IPasswordlessClient, PasswordlessClient>((http, sp) =>
new PasswordlessClient(http, sp.GetRequiredService<IOptions<PasswordlessOptions>>().Value)
new PasswordlessClient(http, sp.GetRequiredService<IOptionsSnapshot<PasswordlessOptions>>().Value)
);

// TODO: Get rid of this service, all consumers should use the interface
Expand Down
Loading