Skip to content

Commit 94f4ee0

Browse files
committed
Remove ClientAuthService
1 parent 50113f6 commit 94f4ee0

File tree

6 files changed

+11
-30
lines changed

6 files changed

+11
-30
lines changed

Common/Authentication/AuthenticationHandlers/ApiTokenAuthentication.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ namespace OpenShock.Common.Authentication.AuthenticationHandlers;
1515

1616
public sealed class ApiTokenAuthentication : AuthenticationHandler<AuthenticationSchemeOptions>
1717
{
18-
private readonly IClientAuthService<User> _authService;
1918
private readonly IUserReferenceService _userReferenceService;
2019
private readonly IBatchUpdateService _batchUpdateService;
2120
private readonly OpenShockContext _db;
@@ -25,14 +24,12 @@ public ApiTokenAuthentication(
2524
IOptionsMonitor<AuthenticationSchemeOptions> options,
2625
ILoggerFactory logger,
2726
UrlEncoder encoder,
28-
IClientAuthService<User> clientAuth,
2927
IUserReferenceService userReferenceService,
3028
OpenShockContext db,
3129
IBatchUpdateService batchUpdateService
3230
)
3331
: base(options, logger, encoder)
3432
{
35-
_authService = clientAuth;
3633
_userReferenceService = userReferenceService;
3734
_db = db;
3835
_batchUpdateService = batchUpdateService;
@@ -58,7 +55,7 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
5855
}
5956

6057
_batchUpdateService.UpdateApiTokenLastUsed(tokenDto.Id);
61-
_authService.CurrentClient = tokenDto.User;
58+
Context.Items["User"] = tokenDto.User;
6259
_userReferenceService.AuthReference = tokenDto;
6360

6461
var claims = new List<Claim>(3 + tokenDto.Permissions.Count)

Common/Authentication/AuthenticationHandlers/UserSessionAuthentication.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ namespace OpenShock.Common.Authentication.AuthenticationHandlers;
1616

1717
public sealed class UserSessionAuthentication : AuthenticationHandler<AuthenticationSchemeOptions>
1818
{
19-
private readonly IClientAuthService<User> _authService;
2019
private readonly IUserReferenceService _userReferenceService;
2120
private readonly IBatchUpdateService _batchUpdateService;
2221
private readonly OpenShockContext _db;
@@ -27,15 +26,13 @@ public UserSessionAuthentication(
2726
IOptionsMonitor<AuthenticationSchemeOptions> options,
2827
ILoggerFactory logger,
2928
UrlEncoder encoder,
30-
IClientAuthService<User> clientAuth,
3129
IUserReferenceService userReferenceService,
3230
OpenShockContext db,
3331
ISessionService sessionService,
3432
IBatchUpdateService batchUpdateService
3533
)
3634
: base(options, logger, encoder)
3735
{
38-
_authService = clientAuth;
3936
_userReferenceService = userReferenceService;
4037
_db = db;
4138
_sessionService = sessionService;
@@ -60,29 +57,29 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
6057

6158
_batchUpdateService.UpdateSessionLastUsed(sessionToken, DateTimeOffset.UtcNow);
6259

63-
var retrievedUser = await _db.Users.Include(u => u.UserDeactivation).FirstOrDefaultAsync(user => user.Id == session.UserId);
64-
if (retrievedUser == null) return Fail(AuthResultError.SessionInvalid);
65-
if (retrievedUser.ActivatedAt is null)
60+
var user = await _db.Users.Include(u => u.UserDeactivation).FirstOrDefaultAsync(user => user.Id == session.UserId);
61+
if (user == null) return Fail(AuthResultError.SessionInvalid);
62+
if (user.ActivatedAt is null)
6663
{
6764
await _sessionService.DeleteSessionAsync(session);
6865
return Fail(AuthResultError.AccountNotActivated);
6966
}
70-
if (retrievedUser.UserDeactivation is not null)
67+
if (user.UserDeactivation is not null)
7168
{
7269
await _sessionService.DeleteSessionAsync(session);
7370
return Fail(AuthResultError.AccountDeactivated);
7471
}
7572

76-
_authService.CurrentClient = retrievedUser;
73+
Context.Items["User"] = user;
7774
_userReferenceService.AuthReference = session;
7875

79-
var claims = new List<Claim>(2 + retrievedUser.Roles.Count)
76+
var claims = new List<Claim>(2 + user.Roles.Count)
8077
{
8178
new(ClaimTypes.AuthenticationMethod, OpenShockAuthSchemes.UserSessionCookie),
82-
new(ClaimTypes.NameIdentifier, retrievedUser.Id.ToString()),
79+
new(ClaimTypes.NameIdentifier, user.Id.ToString()),
8380
};
8481

85-
foreach (var roletype in retrievedUser.Roles)
82+
foreach (var roletype in user.Roles)
8683
{
8784
claims.Add(new Claim(ClaimTypes.Role, roletype.ToString()));
8885
}

Common/Authentication/ControllerBase/AuthenticatedSessionControllerBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class AuthenticatedSessionControllerBase : OpenShockControllerBase, IActi
1313
[NonAction]
1414
public void OnActionExecuting(ActionExecutingContext context)
1515
{
16-
CurrentUser = ControllerContext.HttpContext.RequestServices.GetRequiredService<IClientAuthService<User>>().CurrentClient;
16+
CurrentUser = HttpContext.Items["User"] as User ?? throw new Exception("User not found");
1717
}
1818

1919
[NonAction]

Common/Authentication/Services/ClientAuthService.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.

Common/OpenShockServiceHelper.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ public static IServiceCollection AddOpenShockServices(this IServiceCollection se
108108
};
109109
});
110110

111-
services.AddScoped<IClientAuthService<User>, ClientAuthService<User>>();
112111
services.AddScoped<IUserReferenceService, UserReferenceService>();
113112

114113
var authBuilder = services

LiveControlGateway/Controllers/LiveControlController.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,7 @@ public async Task UpdatePermissions(OpenShockContext db)
218218
[NonAction]
219219
public void OnActionExecuting(ActionExecutingContext context)
220220
{
221-
_currentUser = ControllerContext.HttpContext.RequestServices.GetRequiredService<IClientAuthService<User>>()
222-
.CurrentClient;
221+
_currentUser = HttpContext.Items["User"] as User ?? throw new Exception("User not found");
223222
}
224223

225224
/// <summary>

0 commit comments

Comments
 (0)