mod: cleanup auth
This commit is contained in:
parent
d888ab1eb5
commit
7b560075ba
@ -39,7 +39,5 @@ public class Main {
|
||||
|
||||
// Start the app
|
||||
SpringApplication.run(Main.class, args);
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -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 = "";
|
||||
|
||||
/// <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
|
||||
}
|
||||
);
|
||||
}
|
||||
public bool Success { get; set; }
|
||||
public string Response { get; set; }
|
||||
}
|
||||
|
||||
internal class Authentication
|
||||
@ -46,11 +18,42 @@ namespace ScoreTracker.API
|
||||
private static string _authToken;
|
||||
|
||||
/// <summary>
|
||||
/// Are we signed in?
|
||||
/// Validate the auth token and sign in if necessary
|
||||
/// </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>
|
||||
|
@ -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);
|
||||
|
@ -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
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -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>
|
||||
<OutputType>Library</OutputType>
|
||||
<LangVersion>7.3</LangVersion>
|
||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||
<LocalRefsDir Condition="Exists('..\Refs')">..\Refs</LocalRefsDir>
|
||||
<BeatSaberDir>$(LocalRefsDir)</BeatSaberDir>
|
||||
<AppOutputBase>$(MSBuildProjectDirectory)\</AppOutputBase>
|
||||
@ -17,6 +16,9 @@
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<Configurations>Release</Configurations>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
|
||||
<DebugType>none</DebugType>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="BS_Utils, Version=1.12.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<Private>False</Private>
|
||||
|
Reference in New Issue
Block a user