From 7b560075ba69c87d2eedb518859f3378cda53a19 Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 15 Aug 2024 18:22:16 +0100 Subject: [PATCH] mod: cleanup auth --- API/src/main/java/cc/fascinated/Main.java | 2 - Mod/API/Authentication.cs | 71 ++++++++++++----------- Mod/API/Request.cs | 7 +-- Mod/Plugin.cs | 3 +- Mod/Properties/AssemblyInfo.cs.d | 36 ------------ Mod/ScoreTracker.csproj | 4 +- 6 files changed, 44 insertions(+), 79 deletions(-) delete mode 100644 Mod/Properties/AssemblyInfo.cs.d diff --git a/API/src/main/java/cc/fascinated/Main.java b/API/src/main/java/cc/fascinated/Main.java index 4ceeedf..a9a2977 100644 --- a/API/src/main/java/cc/fascinated/Main.java +++ b/API/src/main/java/cc/fascinated/Main.java @@ -39,7 +39,5 @@ public class Main { // Start the app SpringApplication.run(Main.class, args); - - } } \ No newline at end of file diff --git a/Mod/API/Authentication.cs b/Mod/API/Authentication.cs index b30c7e6..9bfbc8b 100644 --- a/Mod/API/Authentication.cs +++ b/Mod/API/Authentication.cs @@ -2,42 +2,14 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Net.Http; using System.Threading.Tasks; namespace ScoreTracker.API { - internal class AuthHelper + internal class SigninResponse { - public bool IsLoggedIn; - public string FailReason = ""; - - /// - /// Ensure the user is logged in - /// - /// the task - public async Task EnsureLoggedIn() - { - if (Authentication.IsSignedIn() && await Authentication.ValidateAuthToken()) - { - return; // Already logged in with a valid token - } - - await Authentication.LoginUser( - token => { - IsLoggedIn = true; - Request.PersistHeaders(new Dictionary - { - { "Authorization", $"Bearer {token}" } - }); - }, - reason => - { - FailReason = reason; // Store the reason for failure - Request.HttpClient.DefaultRequestHeaders.Clear(); // Clear headers - } - ); - } + public bool Success { get; set; } + public string Response { get; set; } } internal class Authentication @@ -46,11 +18,42 @@ namespace ScoreTracker.API private static string _authToken; /// - /// Are we signed in? + /// Validate the auth token and sign in if necessary /// - public static bool IsSignedIn() + public static async Task ValidateAndSignIn() { - return _signedIn; + if (_signedIn && await ValidateAuthToken()) + { + return new SigninResponse + { + Success = true, + Response = null + }; // Already signed in + } + + bool success = false; + string response = null; + + await LoginUser( + token => { + success = true; + Request.PersistHeaders(new Dictionary + { + { "Authorization", $"Bearer {token}" } + }); + }, + reason => + { + response = reason; + Request.HttpClient.DefaultRequestHeaders.Clear(); // Clear headers + } + ); + + return new SigninResponse + { + Success = success, + Response = response + }; } /// diff --git a/Mod/API/Request.cs b/Mod/API/Request.cs index a078555..ab1247d 100644 --- a/Mod/API/Request.cs +++ b/Mod/API/Request.cs @@ -35,11 +35,10 @@ namespace ScoreTracker.API { if (checkAuth) { - var authHelper = new AuthHelper(); - await authHelper.EnsureLoggedIn(); - if (!authHelper.IsLoggedIn) + var signinResponse = await Authentication.ValidateAndSignIn(); + if (!signinResponse.Success) { - throw new Exception($"Failed to log in: {authHelper.FailReason}"); + throw new Exception($"Failed to log in: {signinResponse.Response}"); } } var jsonString = JsonConvert.SerializeObject(json, Formatting.None); diff --git a/Mod/Plugin.cs b/Mod/Plugin.cs index 4925ba4..bbe65a8 100644 --- a/Mod/Plugin.cs +++ b/Mod/Plugin.cs @@ -40,8 +40,7 @@ namespace ScoreTracker // Ensure the user is logged in Task.Factory.StartNew(async () => { - var authHelper = new AuthHelper(); - await authHelper.EnsureLoggedIn(); + await Authentication.ValidateAndSignIn(); // Ensure the user is signed in }); } diff --git a/Mod/Properties/AssemblyInfo.cs.d b/Mod/Properties/AssemblyInfo.cs.d deleted file mode 100644 index 02520ea..0000000 --- a/Mod/Properties/AssemblyInfo.cs.d +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("ScoreTracker")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("ScoreTracker")] -[assembly: AssemblyCopyright("Copyright © Fascinated 2024")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("8d4a32fe-ab2a-4d8e-a53b-6a1d4f1d7bb9")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.0.1")] -[assembly: AssemblyFileVersion("0.0.1")] diff --git a/Mod/ScoreTracker.csproj b/Mod/ScoreTracker.csproj index 76ead5d..dde27b4 100644 --- a/Mod/ScoreTracker.csproj +++ b/Mod/ScoreTracker.csproj @@ -5,7 +5,6 @@ net472 Library 7.3 - true ..\Refs $(LocalRefsDir) $(MSBuildProjectDirectory)\ @@ -17,6 +16,9 @@ prompt Release + + none + False