From 29120e34be283cd4c54e5eaad499ac7e7d9a95b4 Mon Sep 17 00:00:00 2001 From: Jordan Dominion Date: Sat, 26 Apr 2025 22:39:46 -0400 Subject: [PATCH] Requirements should always be specified when calling `CheckGraphQLAuthorized` --- .../GraphQL/AuthorizationHelper.cs | 4 +-- .../GraphQL/Types/ServerSwarm.cs | 28 ++++--------------- .../GraphQL/Types/SwarmNode.cs | 8 +----- 3 files changed, 8 insertions(+), 32 deletions(-) diff --git a/src/Tgstation.Server.Host/GraphQL/AuthorizationHelper.cs b/src/Tgstation.Server.Host/GraphQL/AuthorizationHelper.cs index b56d3ea49a..02756b1f9f 100644 --- a/src/Tgstation.Server.Host/GraphQL/AuthorizationHelper.cs +++ b/src/Tgstation.Server.Host/GraphQL/AuthorizationHelper.cs @@ -50,12 +50,12 @@ namespace Tgstation.Server.Host.GraphQL /// A representing the running operation. public static async ValueTask CheckGraphQLAuthorized( this Security.IAuthorizationService authorizationService, - IEnumerable? authorizationRequirements = null, + IEnumerable? authorizationRequirements, bool excludeUserSessionValidRequirement = false) { ArgumentNullException.ThrowIfNull(authorizationService); + ArgumentNullException.ThrowIfNull(authorizationRequirements); - authorizationRequirements ??= Enumerable.Empty(); if (!excludeUserSessionValidRequirement) authorizationRequirements = UserSessionValidRequirement.InstanceAsEnumerable.Concat(authorizationRequirements); diff --git a/src/Tgstation.Server.Host/GraphQL/Types/ServerSwarm.cs b/src/Tgstation.Server.Host/GraphQL/Types/ServerSwarm.cs index ea7b5e5c17..89cba488f1 100644 --- a/src/Tgstation.Server.Host/GraphQL/Types/ServerSwarm.cs +++ b/src/Tgstation.Server.Host/GraphQL/Types/ServerSwarm.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Threading.Tasks; using HotChocolate; using HotChocolate.Authorization; @@ -12,7 +11,6 @@ using Tgstation.Server.Api.Models; using Tgstation.Server.Host.Configuration; using Tgstation.Server.Host.GraphQL.Interfaces; using Tgstation.Server.Host.Properties; -using Tgstation.Server.Host.Security; using Tgstation.Server.Host.Swarm; namespace Tgstation.Server.Host.GraphQL.Types @@ -48,58 +46,42 @@ namespace Tgstation.Server.Host.GraphQL.Types /// /// Gets the connected server. /// - /// The to use. /// The containing the current . /// The to use. - /// A new for the local node if it is part of a swarm, otherwise. - public async ValueTask CurrentNode( - [Service] IAuthorizationService authorizationService, + /// The for the local node if it is part of a swarm, a otherwise. + public IServerNode CurrentNode( [Service] IOptionsSnapshot swarmConfigurationOptions, [Service] ISwarmService swarmService) { - ArgumentNullException.ThrowIfNull(authorizationService); ArgumentNullException.ThrowIfNull(swarmConfigurationOptions); ArgumentNullException.ThrowIfNull(swarmService); if (swarmConfigurationOptions.Value.PrivateKey == null) return new StandaloneNode(); - return ((IServerNode?)await SwarmNode.GetSwarmNode( + return ((IServerNode?)SwarmNode.GetSwarmNode( swarmConfigurationOptions.Value.Identifier!, - authorizationService, swarmService)) ?? new StandaloneNode(); } /// /// Gets all servers in the swarm. /// - /// The to use. /// The to use. /// A of s if the local node is part of a swarm, otherwise. [Authorize] - public async ValueTask?> Nodes( - [Service] IAuthorizationService authorizationService, + public List? Nodes( [Service] ISwarmService swarmService) { - ArgumentNullException.ThrowIfNull(authorizationService); ArgumentNullException.ThrowIfNull(swarmService); - - await authorizationService.CheckGraphQLAuthorized(); - return swarmService.GetSwarmServers()?.Select(x => new SwarmNode(x)).ToList(); } /// /// Gets the for the swarm. /// - /// The to use. /// A new . [Authorize] - public async ValueTask UpdateInformation( - [Service] IAuthorizationService authorizationService) - { - await authorizationService.CheckGraphQLAuthorized(); - return new(); - } + public UpdateInformation UpdateInformation() => new(); } } diff --git a/src/Tgstation.Server.Host/GraphQL/Types/SwarmNode.cs b/src/Tgstation.Server.Host/GraphQL/Types/SwarmNode.cs index 91045c6920..e0a2068908 100644 --- a/src/Tgstation.Server.Host/GraphQL/Types/SwarmNode.cs +++ b/src/Tgstation.Server.Host/GraphQL/Types/SwarmNode.cs @@ -1,6 +1,5 @@ using System; using System.Linq; -using System.Threading.Tasks; using HotChocolate; using HotChocolate.Authorization; @@ -12,7 +11,6 @@ using Tgstation.Server.Api.Models; using Tgstation.Server.Api.Models.Internal; using Tgstation.Server.Host.Configuration; using Tgstation.Server.Host.GraphQL.Interfaces; -using Tgstation.Server.Host.Security; using Tgstation.Server.Host.Swarm; namespace Tgstation.Server.Host.GraphQL.Types @@ -53,20 +51,16 @@ namespace Tgstation.Server.Host.GraphQL.Types /// Node resolver for s. /// /// The . - /// The to use. /// The to load from. /// A new with the matching if found, otherwise. [Authorize] - public static async ValueTask GetSwarmNode( + public static SwarmNode? GetSwarmNode( string identifier, - [Service] IAuthorizationService authorizationService, [Service] ISwarmService swarmService) { ArgumentNullException.ThrowIfNull(identifier); - ArgumentNullException.ThrowIfNull(authorizationService); ArgumentNullException.ThrowIfNull(swarmService); - await authorizationService.CheckGraphQLAuthorized(); var info = swarmService .GetSwarmServers() ?.FirstOrDefault(x => x.Identifier == identifier);