From f753a0ffc41b8e56d472dcf700f0440dfaa55bf2 Mon Sep 17 00:00:00 2001 From: rfi Date: Sun, 8 Oct 2023 09:27:30 +0700 Subject: [PATCH] api for sdk --- AscNet.Common/Config.cs | 2 +- AscNet.Common/Database/Account.cs | 65 ++++------------- .../Controllers/AccountController.cs | 72 ++++++++++++++++++- .../Controllers/ConfigController.cs | 1 - AscNet.SDKServer/Models/AscNetAuth.cs | 14 ++++ AscNet/Program.cs | 3 +- 6 files changed, 103 insertions(+), 54 deletions(-) create mode 100644 AscNet.SDKServer/Models/AscNetAuth.cs diff --git a/AscNet.Common/Config.cs b/AscNet.Common/Config.cs index a247c3a..6c29635 100644 --- a/AscNet.Common/Config.cs +++ b/AscNet.Common/Config.cs @@ -37,7 +37,7 @@ namespace AscNet.Common [Option(DefaultValue = (ushort)27017)] ushort Port { get; set; } - [Option(DefaultValue = "sf")] + [Option(DefaultValue = "asc_net")] string Name { get; set; } } diff --git a/AscNet.Common/Database/Account.cs b/AscNet.Common/Database/Account.cs index 34d704a..521fba3 100644 --- a/AscNet.Common/Database/Account.cs +++ b/AscNet.Common/Database/Account.cs @@ -14,40 +14,33 @@ namespace AscNet.Common.Database return collection.AsQueryable().FirstOrDefault(x => x.Uid == uid); } - public static Account? FromAccessToken(string token) + public static Account? FromToken(string token) { - return collection.AsQueryable().FirstOrDefault(x => x.AccessToken == token); + return collection.AsQueryable().FirstOrDefault(x => x.Token == token); } - public static Account? FromPhone(string phone) + public static Account? FromUsername(string username) { - return collection.AsQueryable().FirstOrDefault(x => x.PhoneNum == phone); + return collection.AsQueryable().FirstOrDefault(x => x.Username == username); } - public static Account? FromPhone(string phone, string password) + public static Account? FromUsername(string username, string password) { - return collection.AsQueryable().FirstOrDefault(x => x.PhoneNum == phone && x.Password == password); + return collection.AsQueryable().FirstOrDefault(x => x.Username == username && x.Password == password); } /// - public static Account Create(string phone, string password) + public static Account Create(string username, string password) { - if (collection.AsQueryable().FirstOrDefault(x => x.PhoneNum == phone) is not null) - throw new ArgumentException("Phone is already registered!", "phone"); + if (collection.AsQueryable().FirstOrDefault(x => x.Username == username) is not null) + throw new ArgumentException("Username is already registered!", "username"); Account account = new() { Uid = (collection.AsQueryable().OrderByDescending(x => x.Uid).FirstOrDefault()?.Uid ?? 0) + 1, - PhoneNum = phone, - Email = "", + Username = username, Password = password, - AccessToken = Guid.NewGuid().ToString(), - Age = 0, - IsActivation = false, - IsAdult = true, - IsGuest = false, - UnfreezeTime = 0, - IsReal = true + Token = Guid.NewGuid().ToString() }; collection.InsertOne(account); @@ -61,44 +54,16 @@ namespace AscNet.Common.Database [BsonRequired] public long Uid { get; set; } - [BsonElement("phone_num")] + [BsonElement("username")] [BsonRequired] - public string PhoneNum { get; set; } - - [BsonElement("email")] - [BsonRequired] - public string Email { get; set; } + public string Username { get; set; } [BsonElement("password")] [BsonRequired] public string Password { get; set; } - [BsonElement("access_token")] + [BsonElement("token")] [BsonRequired] - public string AccessToken { get; set; } - - [BsonElement("age")] - [BsonRequired] - public int Age { get; set; } - - [BsonElement("is_activation")] - [BsonRequired] - public bool IsActivation { get; set; } - - [BsonElement("is_adult")] - [BsonRequired] - public bool IsAdult { get; set; } - - [BsonElement("is_guest")] - [BsonRequired] - public bool IsGuest { get; set; } - - [BsonElement("unfreeze_time")] - [BsonRequired] - public int UnfreezeTime { get; set; } - - [BsonElement("is_real")] - [BsonRequired] - public bool IsReal { get; set; } + public string Token { get; set; } } } diff --git a/AscNet.SDKServer/Controllers/AccountController.cs b/AscNet.SDKServer/Controllers/AccountController.cs index 8ebc4bb..9838400 100644 --- a/AscNet.SDKServer/Controllers/AccountController.cs +++ b/AscNet.SDKServer/Controllers/AccountController.cs @@ -1,10 +1,80 @@ -namespace AscNet.SDKServer.Controllers +using System.Text; +using AscNet.Common.Database; +using AscNet.SDKServer.Models; +using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json; + +namespace AscNet.SDKServer.Controllers { public class AccountController : IRegisterable { public static void Register(WebApplication app) { + app.MapPost("/api/AscNet/register", (HttpContext ctx) => + { + AuthRequest? req = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(ctx.Request.BodyReader.ReadAsync().Result.Buffer)); + if (req is null) + { + return JsonConvert.SerializeObject(new + { + code = -1, + msg = "Invalid request" + }); + } + + try + { + Account account = Account.Create(req.Username, req.Password); + + return JsonConvert.SerializeObject(new + { + code = 0, + msg = "OK", + account + }); + } + catch (Exception ex) + { + return JsonConvert.SerializeObject(new + { + code = -1, + msg = ex.Message + }); + } + }); + + app.MapPost("/api/AscNet/login", (HttpContext ctx) => + { + AuthRequest? req = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(ctx.Request.BodyReader.ReadAsync().Result.Buffer)); + + if (req is null) + { + return JsonConvert.SerializeObject(new + { + code = -1, + msg = "Invalid request" + }); + } + + Account? account = Account.FromUsername(req.Username, req.Password); + + if (account == null) + { + return JsonConvert.SerializeObject(new + { + code = -1, + msg = "Invalid credentials!" + }); + } + + return JsonConvert.SerializeObject(new + { + code = 0, + msg = "OK", + account + }); + }); } } } diff --git a/AscNet.SDKServer/Controllers/ConfigController.cs b/AscNet.SDKServer/Controllers/ConfigController.cs index 53c940c..db61f27 100644 --- a/AscNet.SDKServer/Controllers/ConfigController.cs +++ b/AscNet.SDKServer/Controllers/ConfigController.cs @@ -1,6 +1,5 @@ using AscNet.Common.Util; using AscNet.SDKServer.Models; -using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; diff --git a/AscNet.SDKServer/Models/AscNetAuth.cs b/AscNet.SDKServer/Models/AscNetAuth.cs new file mode 100644 index 0000000..df026b3 --- /dev/null +++ b/AscNet.SDKServer/Models/AscNetAuth.cs @@ -0,0 +1,14 @@ +using Newtonsoft.Json; + +#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. +namespace AscNet.SDKServer.Models +{ + public class AuthRequest + { + [JsonProperty("username", NullValueHandling = NullValueHandling.Ignore)] + public string Username { get; set; } + + [JsonProperty("password", NullValueHandling = NullValueHandling.Ignore)] + public string Password { get; set; } + } +} diff --git a/AscNet/Program.cs b/AscNet/Program.cs index 5dfa032..c264a0c 100644 --- a/AscNet/Program.cs +++ b/AscNet/Program.cs @@ -1,4 +1,5 @@ -using AscNet.Common.Util; +using System.Runtime.InteropServices; +using AscNet.Common.Util; namespace AscNet {