diff --git a/ProjectLighthouse.Tests/LighthouseTest.cs b/ProjectLighthouse.Tests/LighthouseTest.cs index 176989dc..bf705a2c 100644 --- a/ProjectLighthouse.Tests/LighthouseTest.cs +++ b/ProjectLighthouse.Tests/LighthouseTest.cs @@ -22,13 +22,9 @@ namespace LBPUnion.ProjectLighthouse.Tests { } public async Task AuthenticateResponse(int number = 0) { - const char nullChar = (char)0x00; const string username = "unitTestUser"; - string nullString = ""; - for(int i = 0; i < 80; i++) nullString += nullChar; - - string stringContent = $"{nullString}{username}{number}{nullChar}"; + string stringContent = $"{LoginData.UsernamePrefix}{username}{number}{(char)0x00}"; HttpResponseMessage response = await this.Client.PostAsync("/LITTLEBIGPLANETPS3_XML/login", new StringContent(stringContent)); return response; diff --git a/ProjectLighthouse.Tests/Tests/MatchTests.cs b/ProjectLighthouse.Tests/Tests/MatchTests.cs index 138501cf..d14b6250 100644 --- a/ProjectLighthouse.Tests/Tests/MatchTests.cs +++ b/ProjectLighthouse.Tests/Tests/MatchTests.cs @@ -16,9 +16,9 @@ namespace LBPUnion.ProjectLighthouse.Tests { await semaphore.WaitAsync(); HttpResponseMessage result = await this.AuthenticatedUploadDataRequest("LITTLEBIGPLANETPS3_XML/match", Array.Empty(), loginResult.AuthTicket); - Assert.False(result.IsSuccessStatusCode); semaphore.Release(); + Assert.False(result.IsSuccessStatusCode); } [DatabaseFact] @@ -32,9 +32,9 @@ namespace LBPUnion.ProjectLighthouse.Tests { loginResult.AuthTicket ); - Assert.True(result.IsSuccessStatusCode); semaphore.Release(); + Assert.True(result.IsSuccessStatusCode); } public async Task GetPlayerCount() => Convert.ToInt32(await this.Client.GetStringAsync("LITTLEBIGPLANETPS3_XML/totalPlayerCount")); diff --git a/ProjectLighthouse/Controllers/LoginController.cs b/ProjectLighthouse/Controllers/LoginController.cs index edf494d0..60f8b741 100644 --- a/ProjectLighthouse/Controllers/LoginController.cs +++ b/ProjectLighthouse/Controllers/LoginController.cs @@ -20,14 +20,16 @@ namespace LBPUnion.ProjectLighthouse.Controllers { public async Task Login() { string body = await new StreamReader(this.Request.Body).ReadToEndAsync(); - LoginData loginData; + LoginData? loginData; try { loginData = LoginData.CreateFromString(body); } catch { - return this.BadRequest(); + loginData = null; } + if(loginData == null) return this.BadRequest(); + Token? token = await this.database.AuthenticateUser(loginData); if(token == null) return this.StatusCode(403, ""); diff --git a/ProjectLighthouse/Types/LoginData.cs b/ProjectLighthouse/Types/LoginData.cs index bcd10423..6a51d2b1 100644 --- a/ProjectLighthouse/Types/LoginData.cs +++ b/ProjectLighthouse/Types/LoginData.cs @@ -1,3 +1,4 @@ +#nullable enable using System; using System.IO; using System.Text; @@ -8,25 +9,25 @@ namespace LBPUnion.ProjectLighthouse.Types { /// The data sent from POST /LOGIN. /// public class LoginData { - public string Username { get; set; } -// public string GameVersion { get; set; } -// public int UnknownNumber { get; set; } // Seems to increment by 1000 every login attempt + public string Username { get; set; } = null!; + + public static readonly string UsernamePrefix = Encoding.ASCII.GetString(new byte[] { 0x04, 0x00, 0x20 }); /// /// Converts a X-I-5 Ticket into `LoginData`. /// https://www.psdevwiki.com/ps3/X-I-5-Ticket /// - public static LoginData CreateFromString(string str) { + public static LoginData? CreateFromString(string str) { str = str.Replace("\b", ""); // Remove backspace characters using MemoryStream ms = new(Encoding.ASCII.GetBytes(str)); using BinaryReader reader = new(ms); - string usernamePrefix = Encoding.ASCII.GetString(new byte[] { 0x04, 0x00, 0x20 }); + if(!str.Contains(UsernamePrefix)) return null; LoginData loginData = new(); - reader.BaseStream.Position = str.IndexOf(usernamePrefix, StringComparison.Ordinal) + usernamePrefix.Length; + reader.BaseStream.Position = str.IndexOf(UsernamePrefix, StringComparison.Ordinal) + UsernamePrefix.Length; loginData.Username = BinaryHelper.ReadString(reader).Replace("\0", string.Empty); return loginData;