Requirements should always be specified when calling CheckGraphQLAuthorized

This commit is contained in:
Jordan Dominion
2025-04-26 22:39:46 -04:00
parent b51692140a
commit 29120e34be
3 changed files with 8 additions and 32 deletions
@@ -50,12 +50,12 @@ namespace Tgstation.Server.Host.GraphQL
/// <returns>A <see cref="ValueTask"/> representing the running operation.</returns>
public static async ValueTask CheckGraphQLAuthorized(
this Security.IAuthorizationService authorizationService,
IEnumerable<IAuthorizationRequirement>? authorizationRequirements = null,
IEnumerable<IAuthorizationRequirement>? authorizationRequirements,
bool excludeUserSessionValidRequirement = false)
{
ArgumentNullException.ThrowIfNull(authorizationService);
ArgumentNullException.ThrowIfNull(authorizationRequirements);
authorizationRequirements ??= Enumerable.Empty<IAuthorizationRequirement>();
if (!excludeUserSessionValidRequirement)
authorizationRequirements = UserSessionValidRequirement.InstanceAsEnumerable.Concat(authorizationRequirements);
@@ -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
/// <summary>
/// Gets the connected <see cref="SwarmNode"/> server.
/// </summary>
/// <param name="authorizationService">The <see cref="IAuthorizationService"/> to use.</param>
/// <param name="swarmConfigurationOptions">The <see cref="IOptionsSnapshot{TOptions}"/> containing the current <see cref="SwarmConfiguration"/>.</param>
/// <param name="swarmService">The <see cref="ISwarmService"/> to use.</param>
/// <returns>A new <see cref="SwarmNode"/> for the local node if it is part of a swarm, <see langword="null"/> otherwise.</returns>
public async ValueTask<IServerNode> CurrentNode(
[Service] IAuthorizationService authorizationService,
/// <returns>The <see cref="SwarmNode"/> for the local node if it is part of a swarm, a <see cref="StandaloneNode"/> otherwise.</returns>
public IServerNode CurrentNode(
[Service] IOptionsSnapshot<SwarmConfiguration> 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();
}
/// <summary>
/// Gets all <see cref="SwarmNode"/> servers in the swarm.
/// </summary>
/// <param name="authorizationService">The <see cref="IAuthorizationService"/> to use.</param>
/// <param name="swarmService">The <see cref="ISwarmService"/> to use.</param>
/// <returns>A <see cref="List{T}"/> of <see cref="SwarmNode"/>s if the local node is part of a swarm, <see langword="null"/> otherwise.</returns>
[Authorize]
public async ValueTask<List<SwarmNode>?> Nodes(
[Service] IAuthorizationService authorizationService,
public List<SwarmNode>? Nodes(
[Service] ISwarmService swarmService)
{
ArgumentNullException.ThrowIfNull(authorizationService);
ArgumentNullException.ThrowIfNull(swarmService);
await authorizationService.CheckGraphQLAuthorized();
return swarmService.GetSwarmServers()?.Select(x => new SwarmNode(x)).ToList();
}
/// <summary>
/// Gets the <see cref="Types.UpdateInformation"/> for the swarm.
/// </summary>
/// <param name="authorizationService">The <see cref="IAuthorizationService"/> to use.</param>
/// <returns>A new <see cref="Types.UpdateInformation"/>.</returns>
[Authorize]
public async ValueTask<UpdateInformation> UpdateInformation(
[Service] IAuthorizationService authorizationService)
{
await authorizationService.CheckGraphQLAuthorized();
return new();
}
public UpdateInformation UpdateInformation() => new();
}
}
@@ -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 <see cref="SwarmNode"/>s.
/// </summary>
/// <param name="identifier">The <see cref="Identifier"/>.</param>
/// <param name="authorizationService">The <see cref="IAuthorizationService"/> to use.</param>
/// <param name="swarmService">The <see cref="ISwarmService"/> to load from.</param>
/// <returns>A new <see cref="SwarmNode"/> with the matching <paramref name="identifier"/> if found, <see langword="null"/> otherwise.</returns>
[Authorize]
public static async ValueTask<SwarmNode?> 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);