From 4ccc773e1b2edd0eeca6074ecbd4f38ecfd080da Mon Sep 17 00:00:00 2001 From: Jordan Dominion Date: Tue, 24 Sep 2024 18:14:29 -0400 Subject: [PATCH] Make extension method for authentication errors --- .../GraphQLServerClient.cs | 17 +---------- .../OperationResultExtensions.cs | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 16 deletions(-) create mode 100644 src/Tgstation.Server.Client.GraphQL/OperationResultExtensions.cs diff --git a/src/Tgstation.Server.Client.GraphQL/GraphQLServerClient.cs b/src/Tgstation.Server.Client.GraphQL/GraphQLServerClient.cs index 60004ce1fd..178c44e713 100644 --- a/src/Tgstation.Server.Client.GraphQL/GraphQLServerClient.cs +++ b/src/Tgstation.Server.Client.GraphQL/GraphQLServerClient.cs @@ -1,6 +1,5 @@ using System; using System.Diagnostics.CodeAnalysis; -using System.Linq; using System.Net.Http.Headers; using System.Threading; using System.Threading.Tasks; @@ -73,20 +72,6 @@ namespace Tgstation.Server.Client.GraphQL static void ThrowOtherCallerFailedAuthException() => throw new AuthenticationException("Another caller failed to authenticate!"); - /// - /// Checks if a given errored out with authentication errors. - /// - /// The . - /// if errored due to authentication issues, otherwise. - static bool IsAuthenticationError(IOperationResult operationResult) - => operationResult.Data == null - && operationResult.Errors.Any( - error => error.Extensions?.TryGetValue( - "code", - out object? codeExtension) == true - && codeExtension is string codeExtensionString - && codeExtensionString == "AUTH_NOT_AUTHENTICATED"); - /// /// Initializes a new instance of the class. /// @@ -243,7 +228,7 @@ namespace Tgstation.Server.Client.GraphQL var operationResult = await operationExecutor(graphQLClient); - if (IsAuthenticationError(operationResult)) + if (operationResult.IsAuthenticationError()) { currentAuthHeader = await Reauthenticate(currentAuthHeader, cancellationToken).ConfigureAwait(false); setAuthenticationHeader(currentAuthHeader); diff --git a/src/Tgstation.Server.Client.GraphQL/OperationResultExtensions.cs b/src/Tgstation.Server.Client.GraphQL/OperationResultExtensions.cs new file mode 100644 index 0000000000..0488db827d --- /dev/null +++ b/src/Tgstation.Server.Client.GraphQL/OperationResultExtensions.cs @@ -0,0 +1,30 @@ +using System; +using System.Linq; + +using StrawberryShake; + +namespace Tgstation.Server.Client.GraphQL +{ + /// + /// Extension methods for the interface. + /// + public static class OperationResultExtensions + { + /// + /// Checks if a given errored out with authentication errors. + /// + /// The . + /// if errored due to authentication issues, otherwise. + public static bool IsAuthenticationError(this IOperationResult operationResult) + { + ArgumentNullException.ThrowIfNull(operationResult); + + return operationResult.Errors.Any( + error => error.Extensions?.TryGetValue( + "code", + out object? codeExtension) == true + && codeExtension is string codeExtensionString + && codeExtensionString == "AUTH_NOT_AUTHENTICATED"); + } + } +}