Skip to content

Commit

Permalink
Add old Admin
Browse files Browse the repository at this point in the history
  • Loading branch information
oveldman committed Mar 29, 2024
1 parent 70eda84 commit cceff2b
Show file tree
Hide file tree
Showing 50 changed files with 231 additions and 66 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public async Task<AuthenticationToken> LoginFromSessionAsync()

if (token.IsSuccess)
{
_authenticationStateProvider.Authenticate(token.AccessToken);
_authenticationStateProvider.Authenticate(token);
}

return token;
Expand Down Expand Up @@ -88,6 +88,6 @@ public async Task<AuthenticationToken> RefreshTokenAsync()
private async Task AuthenticateAsync(AuthenticationToken token)
{
await _localStorage.SetAsync(AuthenticationToken.Entry, token);
_authenticationStateProvider.Authenticate(token.AccessToken);
_authenticationStateProvider.Authenticate(token);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Components.Authorization;

namespace MadWorldNL.Clients.Admin.Application.Authentications;

public class GrpcHttpMessageHandler : DelegatingHandler
{
private readonly UserService _userService;
private readonly JwtAuthenticationStateProvider _authenticationStateProvider;

public GrpcHttpMessageHandler(AuthenticationStateProvider authenticationStateProvider, UserService userService)
{
_userService = userService;
_authenticationStateProvider = (JwtAuthenticationStateProvider)authenticationStateProvider;
}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var test = _userService.GetUser();

var token = _authenticationStateProvider.GetCurrentToken().AccessToken;
request.Headers.Add("Authorization", $"Bearer {token}");

return await base.SendAsync(request, cancellationToken);
}

protected override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken)
{
return SendAsync(request, cancellationToken).GetAwaiter().GetResult();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using MadWorldNL.Clients.Admin.Domain.Authentications;
using Microsoft.AspNetCore.Components.Authorization;

namespace MadWorldNL.Clients.Admin.Application.Authentications;
Expand All @@ -8,16 +9,24 @@ public class JwtAuthenticationStateProvider : AuthenticationStateProvider
{
private const string AuthenticationType = "jwt";

private AuthenticationToken _currentToken = new();
private AuthenticationState _currentUserState = GetAnonymous();

public override Task<AuthenticationState> GetAuthenticationStateAsync()
{
return Task.FromResult(_currentUserState);
}

public AuthenticationToken GetCurrentToken()
{
return _currentToken;
}

public void Authenticate(string jwtToken)
public void Authenticate(AuthenticationToken token)
{
var securityToken = new JwtSecurityToken(jwtToken);
_currentToken = token;

var securityToken = new JwtSecurityToken(token.AccessToken);
var identity = new ClaimsIdentity(securityToken.Claims, AuthenticationType, "name", "role");
var user = new ClaimsPrincipal(identity);
_currentUserState = new AuthenticationState(user);
Expand All @@ -26,6 +35,8 @@ public void Authenticate(string jwtToken)

public void Logout()
{
_currentToken = new AuthenticationToken();

_currentUserState = GetAnonymous();
NotifyAuthenticationStateChanged(Task.FromResult(_currentUserState));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System.Security.Claims;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Server.Circuits;

namespace MadWorldNL.Clients.Admin.Application.Authentications;

public class UserService
{
private ClaimsPrincipal currentUser = new(new ClaimsIdentity());

public ClaimsPrincipal GetUser()
{
return currentUser;
}

internal void SetUser(ClaimsPrincipal user)
{
if (currentUser != user)
{
currentUser = user;
}
}
}

internal sealed class UserCircuitHandler : CircuitHandler, IDisposable
{
private readonly AuthenticationStateProvider authenticationStateProvider;
private readonly UserService userService;

public UserCircuitHandler(
AuthenticationStateProvider authenticationStateProvider,
UserService userService)
{
this.authenticationStateProvider = authenticationStateProvider;
this.userService = userService;
}

public override Task OnCircuitOpenedAsync(Circuit circuit,
CancellationToken cancellationToken)
{
authenticationStateProvider.AuthenticationStateChanged +=
AuthenticationChanged;

return base.OnCircuitOpenedAsync(circuit, cancellationToken);
}

private void AuthenticationChanged(Task<AuthenticationState> task)
{
_ = UpdateAuthentication(task);

async Task UpdateAuthentication(Task<AuthenticationState> task)
{
try
{
var state = await task;
userService.SetUser(state.User);
}
catch
{
}
}
}

public override async Task OnConnectionUpAsync(Circuit circuit,
CancellationToken cancellationToken)
{
var state = await authenticationStateProvider.GetAuthenticationStateAsync();
userService.SetUser(state.User);
}

public void Dispose()
{
authenticationStateProvider.AuthenticationStateChanged -=
AuthenticationChanged;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Microsoft.AspNetCore.Components.Authorization;

namespace MadWorldNL.Clients.Admin.Application.Authentications;

public class UserServiceMiddleware
{
private readonly RequestDelegate next;

public UserServiceMiddleware(RequestDelegate next)
{
this.next = next ?? throw new ArgumentNullException(nameof(next));
}

public async Task InvokeAsync(HttpContext context, UserService service, AuthenticationStateProvider provider)
{
service.SetUser(context.User);
await next(context);
}
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="top-row ps-3 navbar navbar-dark">
<div class="container-fluid">
<a class="navbar-brand" href="">Clients.Admin</a>
<a class="navbar-brand" href="">Clients.Admin_Old</a>
</div>
</div>

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using MadWorldNL.Clients.Admin.Application.Authentications;
using MadWorldNL.Clients.Admin.Services.Authentications;
using Microsoft.AspNetCore.Components.Authorization;
using Server.Presentation.Grpc.Account.V1;
using Server.Presentation.Grpc.Authentication.V1;
using Server.Presentation.Grpc.UserManager.V1;

Expand All @@ -11,7 +10,7 @@ public static class WebApplicationBuilderExtensions
{
public static void AddGrpcClients(this WebApplicationBuilder builder)
{
builder.Services.AddScoped<GrpcHttpMessageHandler>();
builder.Services.AddTransient<GrpcHttpMessageHandler>();

builder.Services.AddGrpcClient<Authentication.AuthenticationClient>(o =>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System.Text;
using MadWorldNL.Clients.Admin.Application.Authentications;
using MadWorldNL.Clients.Admin.Components;
using MadWorldNL.Clients.Admin.Domain.Authorizations;
using MadWorldNL.Clients.Admin.Extensions;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Components.Server.Circuits;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.IdentityModel.Tokens;
using Radzen;

Expand Down Expand Up @@ -39,6 +42,10 @@
builder.AddGrpcClients();
builder.AddAdminServices();

builder.Services.AddScoped<UserService>();
builder.Services.TryAddEnumerable(
ServiceDescriptor.Scoped<CircuitHandler, UserCircuitHandler>());

builder.Services.AddRadzenComponents();

var app = builder.Build();
Expand All @@ -62,4 +69,6 @@
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();

app.UseMiddleware<UserServiceMiddleware>();

app.Run();
File renamed without changes.
File renamed without changes.
File renamed without changes
14 changes: 7 additions & 7 deletions sources/MadWorldNL.Identity.sln
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Server.Application", "Serve
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Server.Domain", "Server.Domain\Server.Domain.csproj", "{B8E44E4A-BA6D-43EE-BE23-23BC557BD32A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Clients.Admin", "Clients.Admin\Clients.Admin.csproj", "{DC343525-26E7-417E-9140-5DB722D44A62}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Server.Presentation.Grpc.IntegrationTests", "Server.Presentation.Grpc.IntegrationTests\Server.Presentation.Grpc.IntegrationTests.csproj", "{60C2824A-17C1-4019-8749-65AE0448E5B3}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Workflows", "Workflows", "{6612E52A-5F38-4585-8F9C-F0F027020BBD}"
Expand All @@ -42,6 +40,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Workflows", "Workflows", "{
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClientSdk.Grpc", "ClientSdk.Grpc\ClientSdk.Grpc.csproj", "{9963EC55-AADF-4940-AE11-EBCD9F372920}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Clients.Admin_Old", "Clients.Admin_Old\Clients.Admin_Old.csproj", "{18DCD2E1-CF25-4FAA-816A-3CFB316DD39F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -56,10 +56,10 @@ Global
{BC765338-FF40-4D9E-AC54-789E184B36F4} = {32514DC3-93DB-40F0-81CA-7525115445CA}
{EA259647-3CF2-42B4-9BD3-76D0E4262383} = {32514DC3-93DB-40F0-81CA-7525115445CA}
{B8E44E4A-BA6D-43EE-BE23-23BC557BD32A} = {32514DC3-93DB-40F0-81CA-7525115445CA}
{DC343525-26E7-417E-9140-5DB722D44A62} = {FD69CCDE-B746-4AAF-B04C-9628249B7AD1}
{60C2824A-17C1-4019-8749-65AE0448E5B3} = {0336D5D8-8B04-4701-804F-158356ED9475}
{6612E52A-5F38-4585-8F9C-F0F027020BBD} = {1648BD75-33AE-4918-AD6A-D12B0A3BC52F}
{9963EC55-AADF-4940-AE11-EBCD9F372920} = {4FEA52A1-D597-4D02-A23B-15E6E41589DF}
{18DCD2E1-CF25-4FAA-816A-3CFB316DD39F} = {FD69CCDE-B746-4AAF-B04C-9628249B7AD1}
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2B04007E-B2D9-4E8B-82D2-C553CD5F3DA2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
Expand All @@ -78,10 +78,6 @@ Global
{B8E44E4A-BA6D-43EE-BE23-23BC557BD32A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B8E44E4A-BA6D-43EE-BE23-23BC557BD32A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B8E44E4A-BA6D-43EE-BE23-23BC557BD32A}.Release|Any CPU.Build.0 = Release|Any CPU
{DC343525-26E7-417E-9140-5DB722D44A62}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DC343525-26E7-417E-9140-5DB722D44A62}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DC343525-26E7-417E-9140-5DB722D44A62}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DC343525-26E7-417E-9140-5DB722D44A62}.Release|Any CPU.Build.0 = Release|Any CPU
{60C2824A-17C1-4019-8749-65AE0448E5B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{60C2824A-17C1-4019-8749-65AE0448E5B3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{60C2824A-17C1-4019-8749-65AE0448E5B3}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -90,5 +86,9 @@ Global
{9963EC55-AADF-4940-AE11-EBCD9F372920}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9963EC55-AADF-4940-AE11-EBCD9F372920}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9963EC55-AADF-4940-AE11-EBCD9F372920}.Release|Any CPU.Build.0 = Release|Any CPU
{18DCD2E1-CF25-4FAA-816A-3CFB316DD39F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{18DCD2E1-CF25-4FAA-816A-3CFB316DD39F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{18DCD2E1-CF25-4FAA-816A-3CFB316DD39F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{18DCD2E1-CF25-4FAA-816A-3CFB316DD39F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
22 changes: 22 additions & 0 deletions sources/Server.Application/Users/GetUsersUserCase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using MadWorldNL.Server.Domain.Users;

namespace MadWorldNL.Server.Application.Users;

public class GetUsersUserCase
{
private readonly IUserRepository _userRepository;

public GetUsersUserCase(IUserRepository userRepository)
{
_userRepository = userRepository;
}
public IReadOnlyList<IIdentityUser> GetUsers(int page)
{
if (page < 0)
{
page = 0;
}

return _userRepository.GetUsers(page);
}
}
1 change: 1 addition & 0 deletions sources/Server.Domain/Users/IUserRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ public interface IUserRepository
Task AddRefreshToken(RefreshToken token);
Task<RefreshToken?> GetRefreshToken(string token);
Task<List<string>> GetRoles();
IReadOnlyList<IIdentityUser> GetUsers(int page);
}
11 changes: 11 additions & 0 deletions sources/Server.Infrastructure/Database/Users/UserRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,15 @@ public async Task<List<string>> GetRoles()
.Select(x => x.Name!)
.ToListAsync();
}

public IReadOnlyList<IIdentityUser> GetUsers(int page)
{
const int pageSize = 10;

return _context.Users
.AsNoTracking()
.Skip(page * pageSize)
.Take(pageSize)
.ToList();
}
}
Loading

0 comments on commit cceff2b

Please sign in to comment.