11using FlatSharp ;
22using Microsoft . AspNetCore . Mvc ;
33using Microsoft . AspNetCore . Mvc . Filters ;
4- using Microsoft . Extensions . Options ;
54using OneOf ;
65using OneOf . Types ;
7- using OpenShock . Common . Authentication . Services ;
86using OpenShock . Common . Constants ;
97using OpenShock . Common . Errors ;
10- using OpenShock . Common . OpenShockDb ;
118using OpenShock . Common . Problems ;
129using OpenShock . Common . Utils ;
1310using OpenShock . LiveControlGateway . LifetimeManager ;
1411using OpenShock . LiveControlGateway . Options ;
1512using OpenShock . LiveControlGateway . Websocket ;
1613using OpenShock . Serialization . Gateway ;
1714using System . Net . WebSockets ;
15+ using System . Security . Claims ;
16+ using OpenShock . Common . Authentication ;
17+ using OpenShock . Common . Extensions ;
1818using SemVersion = OpenShock . Common . Models . SemVersion ;
1919using Timer = System . Timers . Timer ;
2020
@@ -30,7 +30,8 @@ public abstract class HubControllerBase<TIn, TOut> : FlatbuffersWebsocketBaseCon
3030 /// <summary>
3131 /// The current hub
3232 /// </summary>
33- protected Device CurrentHub = null ! ;
33+ protected Guid CurrentHubId = Guid . Empty ;
34+ protected Guid CurrentHubOwnerId = Guid . Empty ;
3435
3536 /// <summary>
3637 /// Service provider
@@ -43,7 +44,12 @@ public abstract class HubControllerBase<TIn, TOut> : FlatbuffersWebsocketBaseCon
4344 /// Hub lifetime
4445 /// </summary>
4546 /// <exception cref="InvalidOperationException"></exception>
46- protected HubLifetime HubLifetime => _hubLifetime ?? throw new InvalidOperationException ( "Hub lifetime is null but was tried to access" ) ;
47+ protected HubLifetime HubLifetime
48+ {
49+ get => _hubLifetime ?? throw new InvalidOperationException ( "Hub lifetime is null but was tried to access" ) ;
50+ private set => _hubLifetime = value ;
51+ }
52+
4753 private readonly LcgOptions _options ;
4854
4955 private readonly HubLifetimeManager _hubLifetimeManager ;
@@ -53,7 +59,7 @@ public abstract class HubControllerBase<TIn, TOut> : FlatbuffersWebsocketBaseCon
5359 private string ? _userAgent ;
5460
5561 /// <inheritdoc cref="IHubController.Id" />
56- public override Guid Id => CurrentHub . Id ;
62+ public override Guid Id => CurrentHubId ;
5763
5864 /// <summary>
5965 /// Authentication context
@@ -62,9 +68,9 @@ public abstract class HubControllerBase<TIn, TOut> : FlatbuffersWebsocketBaseCon
6268 [ NonAction ]
6369 public void OnActionExecuting ( ActionExecutingContext context )
6470 {
65- CurrentHub = ControllerContext . HttpContext . RequestServices
66- . GetRequiredService < IClientAuthService < Device > > ( )
67- . CurrentClient ;
71+ var identity = User . GetOpenShockUserIdentity ( ) ;
72+ CurrentHubId = identity . GetClaimValueAsGuid ( OpenShockAuthClaims . HubId ) ;
73+ CurrentHubOwnerId = identity . GetClaimValueAsGuid ( ClaimTypes . NameIdentifier ) ;
6874 }
6975
7076 /// <summary>
@@ -147,7 +153,7 @@ protected override async Task<OneOf<Success, Error<OpenShockProblem>>> Connectio
147153 return new Error < OpenShockProblem > ( ExceptionError . Exception ) ;
148154 }
149155
150- _hubLifetime = hubLifetimeResult . AsT0 ;
156+ HubLifetime = hubLifetimeResult . AsT0 ;
151157
152158 return new Success ( ) ;
153159 }
@@ -212,18 +218,18 @@ protected async Task<bool> SelfOnline(ulong uptimeMs, ushort? latency = null, in
212218 var bootedAt = GetBootedAtFromUptimeMs ( uptimeMs ) ;
213219 if ( ! bootedAt . HasValue )
214220 {
215- Logger . LogDebug ( "Client attempted to abuse reported boot time, uptime indicated that hub [{HubId}] booted prior to 2024" , CurrentHub . Id ) ;
221+ Logger . LogDebug ( "Client attempted to abuse reported boot time, uptime indicated that hub [{HubId}] booted prior to 2024" , CurrentHubId ) ;
216222 return false ;
217223 }
218224
219- Logger . LogDebug ( "Received keep alive from hub [{HubId}]" , CurrentHub . Id ) ;
225+ Logger . LogDebug ( "Received keep alive from hub [{HubId}]" , CurrentHubId ) ;
220226
221227 // Reset the keep alive timeout
222228 _keepAliveTimeoutTimer . Interval = Duration . DeviceKeepAliveTimeout . TotalMilliseconds ;
223229
224- await HubLifetime . Online ( CurrentHub . Id , new SelfOnlineData ( )
230+ await HubLifetime . Online ( CurrentHubId , new SelfOnlineData ( )
225231 {
226- Owner = CurrentHub . OwnerId ,
232+ Owner = CurrentHubOwnerId ,
227233 Gateway = _options . Fqdn ,
228234 FirmwareVersion = _firmwareVersion ! ,
229235 ConnectedAt = _connected ,
0 commit comments