Revize 45f1655e
Přidáno uživatelem Tomáš Orlovský před téměř 4 roky(ů)
src/Core/Application/Leuze.Core.Application/Authentication/AuthenticatedUserProvider.cs | ||
---|---|---|
1 |
using Microsoft.AspNetCore.Components.Authorization; |
|
2 |
using System; |
|
3 |
using System.Collections.Generic; |
|
4 |
using System.Linq; |
|
5 |
using System.Security.Claims; |
|
6 |
using System.Text; |
|
7 |
using System.Threading.Tasks; |
|
8 |
|
|
9 |
namespace Leuze.Core.Application.Authentication |
|
10 |
{ |
|
11 |
public class AuthenticatedUserProvider |
|
12 |
{ |
|
13 |
private readonly AuthenticationStateProvider _authenticationStateProvider; |
|
14 |
|
|
15 |
public AuthenticatedUserProvider(AuthenticationStateProvider authenticationStateProvider) |
|
16 |
=> (_authenticationStateProvider) = (authenticationStateProvider); |
|
17 |
|
|
18 |
public Guid? UserId |
|
19 |
{ |
|
20 |
get |
|
21 |
{ |
|
22 |
var state = _authenticationStateProvider.GetAuthenticationStateAsync().Result; |
|
23 |
var isOk = Guid.TryParse(state.User!.FindFirst(ClaimTypes.NameIdentifier)!.Value, out var id); |
|
24 |
return isOk ? id : null; |
|
25 |
} |
|
26 |
private set { } |
|
27 |
} |
|
28 |
|
|
29 |
public Guid RequiredUserId |
|
30 |
{ |
|
31 |
get |
|
32 |
{ |
|
33 |
var state = _authenticationStateProvider.GetAuthenticationStateAsync().Result; |
|
34 |
var isOk = Guid.TryParse(state.User!.FindFirst(ClaimTypes.NameIdentifier)!.Value, out var id); |
|
35 |
if (!isOk) throw new UnauthorizedAccessException(); |
|
36 |
return id; |
|
37 |
} |
|
38 |
private set { } |
|
39 |
} |
|
40 |
|
|
41 |
} |
|
42 |
} |
src/Core/Application/Leuze.Core.Application/CQRS/Users/Queries/GetUsersList.cs | ||
---|---|---|
7 | 7 |
using System.Security.Claims; |
8 | 8 |
using System.Threading; |
9 | 9 |
using System.Threading.Tasks; |
10 |
using Leuze.Core.Application.Authentication; |
|
10 | 11 |
|
11 | 12 |
namespace Leuze.Core.Application.CQRS.Users.Queries |
12 | 13 |
{ |
... | ... | |
31 | 32 |
public class Handler : IBaseRequestHandler<Query, Response> |
32 | 33 |
{ |
33 | 34 |
private readonly IDomainUserRepository _domainUserRepository; |
34 |
private readonly AuthenticationStateProvider _authenticationStateProvider;
|
|
35 |
private readonly AuthenticatedUserProvider _authenticationStateProvider;
|
|
35 | 36 |
|
36 | 37 |
/// <summary> |
37 | 38 |
/// |
38 | 39 |
/// </summary> |
39 | 40 |
/// <param name="domainUserRepository"></param> |
40 | 41 |
/// <param name="authenticationStateProvider"></param> |
41 |
public Handler(IDomainUserRepository domainUserRepository, AuthenticationStateProvider authenticationStateProvider)
|
|
42 |
public Handler(IDomainUserRepository domainUserRepository, AuthenticatedUserProvider authenticationStateProvider)
|
|
42 | 43 |
=> (_domainUserRepository, _authenticationStateProvider) = (domainUserRepository, authenticationStateProvider); |
43 | 44 |
|
44 | 45 |
/// <summary> |
... | ... | |
50 | 51 |
public async Task<RequestResponse<Response>> Handle(Query request, CancellationToken cancellationToken) |
51 | 52 |
{ |
52 | 53 |
var list = await _domainUserRepository.GetListAsync(); |
53 |
var state = await _authenticationStateProvider.GetAuthenticationStateAsync(); |
|
54 |
var id = Guid.Parse(state.User!.FindFirst(ClaimTypes.NameIdentifier)!.Value);
|
|
54 |
//var state = await _authenticationStateProvider.GetAuthenticationStateAsync();
|
|
55 |
var id = _authenticationStateProvider.RequiredUserId;
|
|
55 | 56 |
if (request.ExcludeMyself) list = list.Where(o => o.Id != id).ToList(); |
56 | 57 |
return RequestResponse<Response>.CreateSuccessResponse(new(list)); |
57 | 58 |
} |
src/Core/Application/Leuze.Core.Application/Configuration/Extensions/CustomAuthenticationExtensions.cs | ||
---|---|---|
11 | 11 |
using System.Linq; |
12 | 12 |
using System.Security.Claims; |
13 | 13 |
using System.Threading.Tasks; |
14 |
using Leuze.Core.Application.Authentication; |
|
14 | 15 |
|
15 | 16 |
namespace Leuze.Core.Application.Configuration.Extensions |
16 | 17 |
{ |
... | ... | |
26 | 27 |
/// <param name="configuration"></param> |
27 | 28 |
public static void AddAzureAdAuthentication(this IServiceCollection services, IConfiguration configuration) |
28 | 29 |
{ |
30 |
services.AddTransient(typeof(AuthenticatedUserProvider)); |
|
29 | 31 |
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme) |
30 | 32 |
.AddMicrosoftIdentityWebApp(options => |
31 | 33 |
{ |
src/Core/Infrastructure/Leuze.Core.Infrastructure.Persistence/Seeds/ApplicationSeed.cs | ||
---|---|---|
6 | 6 |
using System.Linq; |
7 | 7 |
using System.Text; |
8 | 8 |
using System.Threading.Tasks; |
9 |
using Microsoft.EntityFrameworkCore; |
|
9 | 10 |
|
10 | 11 |
namespace Leuze.Core.Infrastructure.Persistence.Seeds |
11 | 12 |
{ |
... | ... | |
21 | 22 |
/// <param name="context"></param> |
22 | 23 |
/// <param name="userManager"></param> |
23 | 24 |
/// <param name="roleManager"></param> |
24 |
public static void Run(LeuzeDbContext context, UserManager<ApplicationUser> userManager, RoleManager<ApplicationRole> roleManager)
|
|
25 |
public static void Run(DbContext context, UserManager<ApplicationUser> userManager, RoleManager<ApplicationRole> roleManager) |
|
25 | 26 |
{ |
26 | 27 |
var permissions = context.Set<ApplicationPermission>().ToList(); |
27 | 28 |
var role1 = CreateRole("Admin", false, roleManager, permissions); |
... | ... | |
32 | 33 |
CreateUser("Testovací MA", "ma@test.cz", role3, context, userManager); |
33 | 34 |
} |
34 | 35 |
|
35 |
private static void CreateUser(string name, string email, ApplicationRole role, LeuzeDbContext context, UserManager<ApplicationUser> userManager)
|
|
36 |
private static void CreateUser(string name, string email, ApplicationRole role, DbContext context, UserManager<ApplicationUser> userManager) |
|
36 | 37 |
{ |
37 | 38 |
ApplicationUser user = new(email, email, false); |
38 | 39 |
userManager.CreateAsync(user).Wait(); |
tests/Core/Leuze.Tests.Core/Leuze.Tests.Core.csproj | ||
---|---|---|
9 | 9 |
<ItemGroup> |
10 | 10 |
<PackageReference Include="FluentAssertions" Version="5.10.3" /> |
11 | 11 |
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" /> |
12 |
<PackageReference Include="Moq" Version="4.16.1" /> |
|
12 | 13 |
<PackageReference Include="xunit" Version="2.4.1" /> |
13 | 14 |
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"> |
14 | 15 |
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> |
tests/Core/Leuze.Tests.Core/LoginTests.cs | ||
---|---|---|
5 | 5 |
using Leuze.Core.Application.Identity; |
6 | 6 |
using Leuze.Tests.Configuration; |
7 | 7 |
using MediatR; |
8 |
using Microsoft.AspNetCore.Http; |
|
9 |
using Microsoft.AspNetCore.Identity; |
|
8 | 10 |
using Microsoft.Extensions.DependencyInjection; |
11 |
using Moq; |
|
9 | 12 |
using Xunit; |
10 | 13 |
|
11 | 14 |
namespace Leuze.Tests.Core |
... | ... | |
28 | 31 |
//DatabaseFixture.UserManager.AddPasswordAsync(user1, "kappa123").Wait(); |
29 | 32 |
} |
30 | 33 |
|
31 |
[Fact] |
|
32 |
public async Task SimpleLoginTest() |
|
33 |
{ |
|
34 |
await _semaphore.WaitAsync(); |
|
34 |
//[Fact]
|
|
35 |
//public async Task SimpleLoginTest()
|
|
36 |
//{
|
|
37 |
// await _semaphore.WaitAsync();
|
|
35 | 38 |
|
36 |
IMediator mediator = DatabaseFixture.Services.GetRequiredService<IMediator>(); |
|
39 |
// IMediator mediator = DatabaseFixture.Services.GetRequiredService<IMediator>();
|
|
37 | 40 |
|
38 |
var response = await mediator.Send(new VerifyLocalUser.Command(USER1.mail, USER1.pass)); |
|
39 |
// response.IsSuccess.Should().BeTrue(); |
|
41 |
// var response = await mediator.Send(new VerifyLocalUser.Command(USER1.mail, USER1.pass));
|
|
42 |
// // response.IsSuccess.Should().BeTrue();
|
|
40 | 43 |
|
41 | 44 |
|
42 |
//HttpContext context = DatabaseFixture.Services.GetRequiredService<IHttpContextAccessor>().HttpContext; |
|
45 |
// //HttpContext context = DatabaseFixture.Services.GetRequiredService<IHttpContextAccessor>().HttpContext;
|
|
43 | 46 |
|
44 |
//Assert.True(context.User.Identity.IsAuthenticated); |
|
45 |
//context.User.Identity.IsAuthenticated.Should().Be(true); |
|
47 |
// //Assert.True(context.User.Identity.IsAuthenticated);
|
|
48 |
// //context.User.Identity.IsAuthenticated.Should().Be(true);
|
|
46 | 49 |
|
50 |
// _semaphore.Release(); |
|
51 |
//} |
|
52 |
|
|
53 |
[Fact] |
|
54 |
public async Task SimpleLoginTest() |
|
55 |
{ |
|
56 |
var signInManagerMock = new Mock<SignInManager<ApplicationUser>>( |
|
57 |
DatabaseFixture.UserManager, |
|
58 |
Mock.Of<IHttpContextAccessor>(), |
|
59 |
Mock.Of<IUserClaimsPrincipalFactory<ApplicationUser>>(), |
|
60 |
null, null, null, null); |
|
61 |
var user = await DatabaseFixture.UserManager.FindByEmailAsync("user@test.com"); |
|
62 |
signInManagerMock.Setup(e => e.SignInAsync(It.IsAny<ApplicationUser>(), It.IsAny<bool>(), null)).Returns(Task.CompletedTask); |
|
63 |
signInManagerMock.Setup(e => e.CheckPasswordSignInAsync(user, "Kappa123.", It.IsAny<bool>())).Returns(Task.FromResult(SignInResult.Success)); |
|
64 |
signInManagerMock.Setup(e => e.CheckPasswordSignInAsync(user, It.IsNotIn("Kappa123."), It.IsAny<bool>())).Returns(Task.FromResult(SignInResult.Failed)); |
|
65 |
signInManagerMock.Setup(e => e.CheckPasswordSignInAsync(It.IsNotIn(user), It.IsAny<string>(), It.IsAny<bool>())).Returns(Task.FromResult(SignInResult.Failed)); |
|
66 |
|
|
67 |
await _semaphore.WaitAsync(); |
|
68 |
var handler = new VerifyLocalUser.Handler(signInManagerMock.Object, DatabaseFixture.UserManager); |
|
69 |
var result = await handler.Handle(new VerifyLocalUser.Command("user@test.com", "Kappa123."), default); |
|
70 |
result.IsSuccess.Should().BeTrue(); |
|
71 |
|
|
47 | 72 |
_semaphore.Release(); |
48 | 73 |
} |
49 | 74 |
|
tests/Core/Leuze.Tests.Core/RoleTests.cs | ||
---|---|---|
1 | 1 |
using System; |
2 | 2 |
using System.Collections.Generic; |
3 | 3 |
using System.Linq; |
4 |
using System.Security.Claims; |
|
4 | 5 |
using System.Text; |
6 |
using FluentAssertions; |
|
5 | 7 |
using System.Threading.Tasks; |
8 |
using Leuze.Core.Application.Authentication; |
|
9 |
using Leuze.Core.Application.CQRS.Users.Queries; |
|
10 |
using Leuze.Core.Infrastructure.Persistence.Seeds; |
|
6 | 11 |
using Leuze.Tests.Configuration; |
12 |
using Microsoft.AspNetCore.Components.Authorization; |
|
7 | 13 |
using Microsoft.Extensions.DependencyInjection; |
14 |
using Moq; |
|
8 | 15 |
using Xunit; |
9 | 16 |
|
10 | 17 |
namespace Leuze.Tests.Core |
... | ... | |
17 | 24 |
var services = new ServiceCollection(); |
18 | 25 |
|
19 | 26 |
Initialize(services); |
27 |
|
|
28 |
ApplicationSeed.Run(DatabaseFixture.Storage, DatabaseFixture.UserManager, DatabaseFixture.RoleManager); |
|
20 | 29 |
} |
21 | 30 |
|
22 | 31 |
[Fact] |
23 | 32 |
public async Task AdminRoleTest() |
24 | 33 |
{ |
25 |
//TODO: implementation |
|
34 |
await _semaphore.WaitAsync(); |
|
35 |
var user = await DatabaseFixture.UserManager.FindByEmailAsync("admin@test.cz"); |
|
36 |
(user != null).Should().BeTrue(); |
|
37 |
|
|
38 |
var AuthenticationStateProviderMock = new Mock<AuthenticatedUserProvider>(); |
|
39 |
AuthenticationStateProviderMock.Setup(e => e.RequiredUserId).Returns(user.Id); |
|
40 |
|
|
41 |
var handler = new GetUsersList.Handler(DatabaseFixture.UserManager) |
|
42 |
|
|
43 |
_semaphore.Release(); |
|
26 | 44 |
} |
27 | 45 |
|
28 | 46 |
[Fact] |
Také k dispozici: Unified diff
Changed the code so it is easier to test and rewrited some tests