mod: cleanup auth
Some checks failed
Deploy API / docker (17, 3.8.5) (push) Failing after 35s
Release Mod / Build (push) Successful in 30s

This commit is contained in:
Lee 2024-08-15 18:22:16 +01:00
parent d888ab1eb5
commit 7b560075ba
6 changed files with 44 additions and 79 deletions

@ -39,7 +39,5 @@ public class Main {
// Start the app // Start the app
SpringApplication.run(Main.class, args); SpringApplication.run(Main.class, args);
} }
} }

@ -2,42 +2,14 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace ScoreTracker.API namespace ScoreTracker.API
{ {
internal class AuthHelper internal class SigninResponse
{ {
public bool IsLoggedIn; public bool Success { get; set; }
public string FailReason = ""; public string Response { get; set; }
/// <summary>
/// Ensure the user is logged in
/// </summary>
/// <returns>the task</returns>
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<string, string>
{
{ "Authorization", $"Bearer {token}" }
});
},
reason =>
{
FailReason = reason; // Store the reason for failure
Request.HttpClient.DefaultRequestHeaders.Clear(); // Clear headers
}
);
}
} }
internal class Authentication internal class Authentication
@ -46,11 +18,42 @@ namespace ScoreTracker.API
private static string _authToken; private static string _authToken;
/// <summary> /// <summary>
/// Are we signed in? /// Validate the auth token and sign in if necessary
/// </summary> /// </summary>
public static bool IsSignedIn() public static async Task<SigninResponse> 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<string, string>
{
{ "Authorization", $"Bearer {token}" }
});
},
reason =>
{
response = reason;
Request.HttpClient.DefaultRequestHeaders.Clear(); // Clear headers
}
);
return new SigninResponse
{
Success = success,
Response = response
};
} }
/// <summary> /// <summary>

@ -35,11 +35,10 @@ namespace ScoreTracker.API
{ {
if (checkAuth) if (checkAuth)
{ {
var authHelper = new AuthHelper(); var signinResponse = await Authentication.ValidateAndSignIn();
await authHelper.EnsureLoggedIn(); if (!signinResponse.Success)
if (!authHelper.IsLoggedIn)
{ {
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); var jsonString = JsonConvert.SerializeObject(json, Formatting.None);

@ -40,8 +40,7 @@ namespace ScoreTracker
// Ensure the user is logged in // Ensure the user is logged in
Task.Factory.StartNew(async () => Task.Factory.StartNew(async () =>
{ {
var authHelper = new AuthHelper(); await Authentication.ValidateAndSignIn(); // Ensure the user is signed in
await authHelper.EnsureLoggedIn();
}); });
} }

@ -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")]

@ -5,7 +5,6 @@
<TargetFramework>net472</TargetFramework> <TargetFramework>net472</TargetFramework>
<OutputType>Library</OutputType> <OutputType>Library</OutputType>
<LangVersion>7.3</LangVersion> <LangVersion>7.3</LangVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<LocalRefsDir Condition="Exists('..\Refs')">..\Refs</LocalRefsDir> <LocalRefsDir Condition="Exists('..\Refs')">..\Refs</LocalRefsDir>
<BeatSaberDir>$(LocalRefsDir)</BeatSaberDir> <BeatSaberDir>$(LocalRefsDir)</BeatSaberDir>
<AppOutputBase>$(MSBuildProjectDirectory)\</AppOutputBase> <AppOutputBase>$(MSBuildProjectDirectory)\</AppOutputBase>
@ -17,6 +16,9 @@
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<Configurations>Release</Configurations> <Configurations>Release</Configurations>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DebugType>none</DebugType>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="BS_Utils, Version=1.12.0.0, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="BS_Utils, Version=1.12.0.0, Culture=neutral, processorArchitecture=MSIL">
<Private>False</Private> <Private>False</Private>