diff --git a/ProjectLighthouse.sln.DotSettings b/ProjectLighthouse.sln.DotSettings index 6dab5440..dcd254e2 100644 --- a/ProjectLighthouse.sln.DotSettings +++ b/ProjectLighthouse.sln.DotSettings @@ -1,4 +1,5 @@  + MM <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb"><ExtraRule Prefix="" Suffix="" Style="aaBb" /></Policy> <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> diff --git a/ProjectLighthouse/Controllers/UserController.cs b/ProjectLighthouse/Controllers/UserController.cs index 14afc6e2..22e90f79 100644 --- a/ProjectLighthouse/Controllers/UserController.cs +++ b/ProjectLighthouse/Controllers/UserController.cs @@ -22,11 +22,11 @@ namespace ProjectLighthouse.Controllers { return this.Ok(user.Serialize()); } - [HttpPost("user/{username}")] - public async Task CreateUser(string username) { - await new Database().CreateUser(username); - return await GetUser(username); - } +// [HttpPost("user/{username}")] +// public async Task CreateUser(string username) { +// await new Database().CreateUser(username); +// return await GetUser(username); +// } [HttpPost("updateUser")] public async Task UpdateUser() { diff --git a/ProjectLighthouse/Database.cs b/ProjectLighthouse/Database.cs index 204e5e24..a78aadd2 100644 --- a/ProjectLighthouse/Database.cs +++ b/ProjectLighthouse/Database.cs @@ -1,38 +1,70 @@ +#nullable enable +using System; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using ProjectLighthouse.Types; namespace ProjectLighthouse { public class Database : DbContext { - protected override void OnConfiguring(DbContextOptionsBuilder options) => options.UseMySql( - ServerSettings.DbConnectionString, - MySqlServerVersion.LatestSupportedServerVersion - ); - - public async Task CreateUser(string username) { - await this.Database.ExecuteSqlRawAsync( - "INSERT INTO Locations (X, Y) VALUES ({0}, {1})", - 0, 0); - - Location l = new() { - X = 0, - Y = 0 - }; - - this.Locations.Add(l); - await this.SaveChangesAsync(); - - int locationId = l.Id; - - await this.Database.ExecuteSqlRawAsync( - "INSERT INTO Users (Username, Biography, Pins, LocationId) VALUES ({0}, {1}, {2}, {3})", - username, "No biography provided.", "", locationId); - - } - public DbSet Users { get; set; } public DbSet Locations { get; set; } public DbSet Slots { get; set; } public DbSet Comments { get; set; } + public DbSet Tokens { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder options) => options.UseMySql( + ServerSettings.DbConnectionString, + MySqlServerVersion.LatestSupportedServerVersion + ); + + public async Task CreateUser(string username) { + Location l = new(); // store to get id after submitting + this.Locations.Add(l); // add to table + await this.SaveChangesAsync(); // saving to the database returns the id and sets it on this entity + + User user = new() { + Username = username, + LocationId = l.Id, + Biography = "No biography provided" + }; + this.Users.Add(user); + + await this.SaveChangesAsync(); + + return user; + + } + + // MM_AUTH=psn_name:?:timestamp, potentially a user creation date?:?:user id?:user's IP:?:password? SHA1 + // just blindly trust the token for now while we get it working + public async Task AuthenticateUser(string mmAuth) { + Token token = new() { + MMAuth = mmAuth + }; + + string[] split; + try { + split = mmAuth.Split(":"); + } + catch(ArgumentOutOfRangeException e) { + return false; // Token doesn't contain :, cant be a valid token + } + + // TODO: don't use psn name to authenticate + User user = await this.Users.FirstOrDefaultAsync(u => u.Username == split[0]) + ?? await this.CreateUser(split[0]); + + token.UserId = user.UserId; + + return true; + } + + public async Task IsUserAuthenticated(string mmAuth) => await UserFromMMAuth(mmAuth) != null; + + public async Task UserFromMMAuth(string mmAuth) { + Token? token = await Tokens.FirstOrDefaultAsync(t => t.MMAuth == mmAuth); + if(token == null) return null; + return await Users.FirstOrDefaultAsync(u => u.UserId == token.UserId); + } } } \ No newline at end of file diff --git a/ProjectLighthouse/Types/Token.cs b/ProjectLighthouse/Types/Token.cs new file mode 100644 index 00000000..61455594 --- /dev/null +++ b/ProjectLighthouse/Types/Token.cs @@ -0,0 +1,9 @@ +using Microsoft.EntityFrameworkCore; + +namespace ProjectLighthouse.Types { + [Keyless] + public class Token { + public int UserId { get; set; } + public string MMAuth { get; set; } + } +} \ No newline at end of file