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

Commit

Permalink
Add ProviderInstance for use in 2FA TokenProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoK committed Sep 28, 2016
1 parent b166c47 commit d18ae5c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
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

0 comments on commit d18ae5c

Please sign in to comment.