tgstation-server 6.12.3
The /tg/station 13 server suite
Loading...
Searching...
No Matches
LoginResultExtensions.cs
Go to the documentation of this file.
1using System;
2using System.Linq;
3
4using Microsoft.Extensions.Logging;
5using Microsoft.IdentityModel.JsonWebTokens;
6using StrawberryShake;
7
9{
14 {
22 public static JsonWebToken EnsureSuccess(this IOperationResult<ILoginResult> loginResult, ILogger logger)
23 {
24 ArgumentNullException.ThrowIfNull(loginResult);
25
26 try
27 {
28 loginResult.EnsureNoErrors();
29 }
30 catch (GraphQLClientException ex)
31 {
32 throw new AuthenticationException("Login attempt errored at the GraphQL level!", ex);
33 }
34
35 var data = loginResult.Data!.Login;
36 var errors = data.Errors;
37 if (errors != null)
38 {
39 foreach (var error in errors)
40 {
41 if (error is ILogin_Login_Errors_ErrorMessageError errorMessageError)
42 logger.LogError(
43 "Authentication error ({code}): {message}{additionalData}",
44 errorMessageError.ErrorCode?.ToString() ?? "No Code",
45 errorMessageError.Message,
46 errorMessageError.AdditionalData != null
47 ? $"{Environment.NewLine}{errorMessageError.AdditionalData}"
48 : String.Empty);
49 else
50 logger.LogError(
51 "Unknown authentication error: {error}",
52 error);
53 }
54 }
55
56 if (data.LoginResult == null)
57 {
58 if (errors != null)
59 {
60 var errorMessage = errors.OfType<ILogin_Login_Errors_ErrorMessageError>().FirstOrDefault();
61 if (errorMessage != null)
62 throw new AuthenticationException(errorMessage);
63
64 throw new AuthenticationException($"Null bearer field and {errors.Count} non-ErrorMessage errors:{(errors.Count > 0 ? $"{Environment.NewLine}\t- {String.Join($"{Environment.NewLine}\t- ", errors)}" : String.Empty)}");
65 }
66
67 throw new AuthenticationException($"Null bearer and error fields!");
68 }
69
70 return data.LoginResult.Bearer;
71 }
72 }
73}
Exception thrown when automatic IGraphQLServerClient authentication fails.
static JsonWebToken EnsureSuccess(this IOperationResult< ILoginResult > loginResult, ILogger logger)
Check a given loginResult for errors.