Skip to content
This repository has been archived by the owner on Dec 20, 2018. It is now read-only.

Add ProviderInstance for use in 2FA TokenProvider #983

Merged
merged 1 commit into from
Sep 29, 2016
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
5 changes: 5 additions & 0 deletions src/Microsoft.AspNetCore.Identity/TokenProviderDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,10 @@ public TokenProviderDescriptor(Type type)
/// The type that will be used for this token provider.
/// </summary>
public Type ProviderType { get; }

/// <summary>
/// If specified, the instance to be used for the token provider.
/// </summary>
public object ProviderInstance { get; set; }
}
}
5 changes: 4 additions & 1 deletion src/Microsoft.AspNetCore.Identity/UserManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ public UserManager(IUserStore<TUser> store,
_context = services.GetService<IHttpContextAccessor>()?.HttpContext;
foreach (var providerName in Options.Tokens.ProviderMap.Keys)
{
var provider = services.GetRequiredService(Options.Tokens.ProviderMap[providerName].ProviderType) as IUserTwoFactorTokenProvider<TUser>;
var description = Options.Tokens.ProviderMap[providerName];

var provider = (description.ProviderInstance ?? services.GetRequiredService(description.ProviderType))
as IUserTwoFactorTokenProvider<TUser>;
if (provider != null)
{
RegisterTokenProvider(providerName, provider);
Expand Down
46 changes: 46 additions & 0 deletions test/Microsoft.AspNetCore.Identity.Test/UserManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,52 @@ public async Task ClaimMethodsFailWhenStoreNotImplemented()
await Assert.ThrowsAsync<NotSupportedException>(async () => await manager.GetClaimsAsync(null));
}

private class ATokenProvider : IUserTwoFactorTokenProvider<TestUser>
{
public Task<bool> CanGenerateTwoFactorTokenAsync(UserManager<TestUser> manager, TestUser user)
{
throw new NotImplementedException();
}

public Task<string> GenerateAsync(string purpose, UserManager<TestUser> manager, TestUser user)
{
throw new NotImplementedException();
}

public Task<bool> ValidateAsync(string purpose, string token, UserManager<TestUser> manager, TestUser user)
{
throw new NotImplementedException();
}
}

[Fact]
public void UserManagerWillUseTokenProviderInstance()
{
var services = new ServiceCollection();
var provider = new ATokenProvider();
services.AddLogging()
.AddIdentity<TestUser, TestRole>(o => o.Tokens.ProviderMap.Add("A", new TokenProviderDescriptor(typeof(ATokenProvider))
{
ProviderInstance = provider
})).AddUserStore<NoopUserStore>();
var manager = services.BuildServiceProvider().GetService<UserManager<TestUser>>();
Assert.ThrowsAsync<NotImplementedException>(() => manager.GenerateUserTokenAsync(new TestUser(), "A", "purpose"));
}

[Fact]
public void UserManagerWillUseTokenProviderInstanceOverDefaults()
{
var services = new ServiceCollection();
var provider = new ATokenProvider();
services.AddLogging()
.AddIdentity<TestUser, TestRole>(o => o.Tokens.ProviderMap.Add(TokenOptions.DefaultProvider, new TokenProviderDescriptor(typeof(ATokenProvider))
{
ProviderInstance = provider
})).AddUserStore<NoopUserStore>().AddDefaultTokenProviders();
var manager = services.BuildServiceProvider().GetService<UserManager<TestUser>>();
Assert.ThrowsAsync<NotImplementedException>(() => manager.GenerateUserTokenAsync(new TestUser(), TokenOptions.DefaultProvider, "purpose"));
}

[Fact]
public async Task TwoFactorStoreMethodsFailWhenStoreNotImplemented()
{
Expand Down