mirror of https://github.com/abpframework/abp.git
10 changed files with 192 additions and 21 deletions
@ -0,0 +1,18 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using JetBrains.Annotations; |
|||
|
|||
namespace Volo.Abp.Identity.AspNetCore |
|||
{ |
|||
public class ExternalLoginProviderDictionary : Dictionary<string, ExternalLoginProviderInfo> |
|||
{ |
|||
/// <summary>
|
|||
/// Adds or replaces a provider.
|
|||
/// </summary>
|
|||
public void Add<TProvider>([NotNull] string name) |
|||
where TProvider : IExternalLoginProvider |
|||
{ |
|||
this[name] = new ExternalLoginProviderInfo(name, typeof(TProvider)); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,25 @@ |
|||
using System; |
|||
using JetBrains.Annotations; |
|||
|
|||
namespace Volo.Abp.Identity.AspNetCore |
|||
{ |
|||
public class ExternalLoginProviderInfo |
|||
{ |
|||
public string Name { get; } |
|||
|
|||
public Type Type |
|||
{ |
|||
get => _type; |
|||
set => _type = Check.NotNull(value, nameof(value)); |
|||
} |
|||
private Type _type; |
|||
|
|||
public ExternalLoginProviderInfo( |
|||
[NotNull] string name, |
|||
[NotNull] Type type) |
|||
{ |
|||
Name = Check.NotNullOrWhiteSpace(name, nameof(name)); |
|||
Type = Check.AssignableTo<IExternalLoginProvider>(type, nameof(type)); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,30 @@ |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.Identity.AspNetCore |
|||
{ |
|||
public interface IExternalLoginProvider //TODO: A base class to simplift implementing this!
|
|||
{ |
|||
/// <summary>
|
|||
/// Used to try authenticate a user by this source.
|
|||
/// </summary>
|
|||
/// <param name="userName">User name or email address</param>
|
|||
/// <param name="plainPassword">Plain password of the user</param>
|
|||
/// <returns>True, indicates that this used has authenticated by this source</returns>
|
|||
Task<bool> TryAuthenticateAsync(string userName, string plainPassword); |
|||
|
|||
/// <summary>
|
|||
/// This method is called when a user is authenticated by this source but the user does not exists yet.
|
|||
/// So, the source should create the user and fill the properties.
|
|||
/// </summary>
|
|||
/// <param name="userName">User name</param>
|
|||
/// <returns>Newly created user</returns>
|
|||
Task<IdentityUser> CreateUserAsync(string userName); |
|||
|
|||
/// <summary>
|
|||
/// This method is called after an existing user is authenticated by this source.
|
|||
/// It can be used to update some properties of the user by the source.
|
|||
/// </summary>
|
|||
/// <param name="user">The user that can be updated</param>
|
|||
Task UpdateUserAsync(IdentityUser user); |
|||
} |
|||
} |
|||
@ -1,9 +1,24 @@ |
|||
using Volo.Abp.AspNetCore.TestBase; |
|||
using System.Net; |
|||
using System.Net.Http; |
|||
using System.Threading.Tasks; |
|||
using Shouldly; |
|||
using Volo.Abp.AspNetCore.TestBase; |
|||
|
|||
namespace Volo.Abp.Identity.AspNetCore |
|||
{ |
|||
public abstract class AbpIdentityAspNetCoreTestBase : AbpAspNetCoreIntegratedTestBase<AbpIdentityAspNetCoreTestStartup> |
|||
{ |
|||
protected virtual async Task<string> GetResponseAsStringAsync(string url, HttpStatusCode expectedStatusCode = HttpStatusCode.OK) |
|||
{ |
|||
var response = await GetResponseAsync(url, expectedStatusCode); |
|||
return await response.Content.ReadAsStringAsync(); |
|||
} |
|||
|
|||
protected virtual async Task<HttpResponseMessage> GetResponseAsync(string url, HttpStatusCode expectedStatusCode = HttpStatusCode.OK) |
|||
{ |
|||
var response = await Client.GetAsync(url); |
|||
response.StatusCode.ShouldBe(expectedStatusCode); |
|||
return response; |
|||
} |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,19 @@ |
|||
using System.Threading.Tasks; |
|||
using Shouldly; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.Identity.AspNetCore |
|||
{ |
|||
public class ExternalLoginProvider_Tests : AbpIdentityAspNetCoreTestBase |
|||
{ |
|||
[Fact] |
|||
public async Task Should_SignIn_With_ExternalLoginProvider() |
|||
{ |
|||
var result = await GetResponseAsStringAsync( |
|||
"api/signin-test/password?userName=ext_user&password=abc" |
|||
); |
|||
|
|||
result.ShouldBe("Succeeded"); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,34 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.Identity.AspNetCore |
|||
{ |
|||
public class FakeExternalLoginProvider : IExternalLoginProvider, ITransientDependency |
|||
{ |
|||
public const string Name = "Fake"; |
|||
|
|||
public Task<bool> TryAuthenticateAsync(string userName, string plainPassword) |
|||
{ |
|||
return Task.FromResult( |
|||
userName == "ext_user" && plainPassword == "abc" |
|||
); |
|||
} |
|||
|
|||
public Task<IdentityUser> CreateUserAsync(string userName) |
|||
{ |
|||
return Task.FromResult( |
|||
new IdentityUser( |
|||
Guid.NewGuid(), |
|||
userName, |
|||
"test@abp.io" |
|||
) |
|||
); |
|||
} |
|||
|
|||
public Task UpdateUserAsync(IdentityUser user) |
|||
{ |
|||
return Task.CompletedTask; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue