mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-05-20 16:22:27 +00:00
Add methods for authentication
This commit is contained in:
parent
223caa44af
commit
51b652f36d
4 changed files with 73 additions and 31 deletions
|
@ -1,4 +1,5 @@
|
||||||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
||||||
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=MM/@EntryIndexedValue">MM</s:String>
|
||||||
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=Method/@EntryIndexedValue"><Policy Inspect="True" Prefix="" Suffix="" Style="AaBb"><ExtraRule Prefix="" Suffix="" Style="aaBb" /></Policy></s:String>
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=Method/@EntryIndexedValue"><Policy Inspect="True" Prefix="" Suffix="" Style="AaBb"><ExtraRule Prefix="" Suffix="" Style="aaBb" /></Policy></s:String>
|
||||||
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateConstants/@EntryIndexedValue"><Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /></s:String>
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateConstants/@EntryIndexedValue"><Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /></s:String>
|
||||||
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticReadonly/@EntryIndexedValue"><Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /></s:String>
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticReadonly/@EntryIndexedValue"><Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /></s:String>
|
||||||
|
|
|
@ -22,11 +22,11 @@ namespace ProjectLighthouse.Controllers {
|
||||||
return this.Ok(user.Serialize());
|
return this.Ok(user.Serialize());
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("user/{username}")]
|
// [HttpPost("user/{username}")]
|
||||||
public async Task<IActionResult> CreateUser(string username) {
|
// public async Task<IActionResult> CreateUser(string username) {
|
||||||
await new Database().CreateUser(username);
|
// await new Database().CreateUser(username);
|
||||||
return await GetUser(username);
|
// return await GetUser(username);
|
||||||
}
|
// }
|
||||||
|
|
||||||
[HttpPost("updateUser")]
|
[HttpPost("updateUser")]
|
||||||
public async Task<IActionResult> UpdateUser() {
|
public async Task<IActionResult> UpdateUser() {
|
||||||
|
|
|
@ -1,38 +1,70 @@
|
||||||
|
#nullable enable
|
||||||
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using ProjectLighthouse.Types;
|
using ProjectLighthouse.Types;
|
||||||
|
|
||||||
namespace ProjectLighthouse {
|
namespace ProjectLighthouse {
|
||||||
public class Database : DbContext {
|
public class Database : DbContext {
|
||||||
|
public DbSet<User> Users { get; set; }
|
||||||
|
public DbSet<Location> Locations { get; set; }
|
||||||
|
public DbSet<Slot> Slots { get; set; }
|
||||||
|
public DbSet<Comment> Comments { get; set; }
|
||||||
|
public DbSet<Token> Tokens { get; set; }
|
||||||
|
|
||||||
protected override void OnConfiguring(DbContextOptionsBuilder options) => options.UseMySql(
|
protected override void OnConfiguring(DbContextOptionsBuilder options) => options.UseMySql(
|
||||||
ServerSettings.DbConnectionString,
|
ServerSettings.DbConnectionString,
|
||||||
MySqlServerVersion.LatestSupportedServerVersion
|
MySqlServerVersion.LatestSupportedServerVersion
|
||||||
);
|
);
|
||||||
|
|
||||||
public async Task CreateUser(string username) {
|
public async Task<User> CreateUser(string username) {
|
||||||
await this.Database.ExecuteSqlRawAsync(
|
Location l = new(); // store to get id after submitting
|
||||||
"INSERT INTO Locations (X, Y) VALUES ({0}, {1})",
|
this.Locations.Add(l); // add to table
|
||||||
0, 0);
|
await this.SaveChangesAsync(); // saving to the database returns the id and sets it on this entity
|
||||||
|
|
||||||
Location l = new() {
|
User user = new() {
|
||||||
X = 0,
|
Username = username,
|
||||||
Y = 0
|
LocationId = l.Id,
|
||||||
|
Biography = "No biography provided"
|
||||||
};
|
};
|
||||||
|
this.Users.Add(user);
|
||||||
|
|
||||||
this.Locations.Add(l);
|
|
||||||
await this.SaveChangesAsync();
|
await this.SaveChangesAsync();
|
||||||
|
|
||||||
int locationId = l.Id;
|
return user;
|
||||||
|
|
||||||
await this.Database.ExecuteSqlRawAsync(
|
|
||||||
"INSERT INTO Users (Username, Biography, Pins, LocationId) VALUES ({0}, {1}, {2}, {3})",
|
|
||||||
username, "No biography provided.", "", locationId);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public DbSet<User> Users { get; set; }
|
// MM_AUTH=psn_name:?:timestamp, potentially a user creation date?:?:user id?:user's IP:?:password? SHA1
|
||||||
public DbSet<Location> Locations { get; set; }
|
// just blindly trust the token for now while we get it working
|
||||||
public DbSet<Slot> Slots { get; set; }
|
public async Task<bool> AuthenticateUser(string mmAuth) {
|
||||||
public DbSet<Comment> Comments { get; set; }
|
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<bool> IsUserAuthenticated(string mmAuth) => await UserFromMMAuth(mmAuth) != null;
|
||||||
|
|
||||||
|
public async Task<User?> 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
9
ProjectLighthouse/Types/Token.cs
Normal file
9
ProjectLighthouse/Types/Token.cs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace ProjectLighthouse.Types {
|
||||||
|
[Keyless]
|
||||||
|
public class Token {
|
||||||
|
public int UserId { get; set; }
|
||||||
|
public string MMAuth { get; set; }
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue