forked from PGR/ascnet
1
0
Fork 0

api for sdk

This commit is contained in:
rfi 2023-10-08 09:27:30 +07:00
parent eacf80ed5c
commit f753a0ffc4
6 changed files with 103 additions and 54 deletions

View File

@ -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; }
}

View File

@ -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);
}
/// <exception cref="ArgumentException"></exception>
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; }
}
}

View File

@ -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<AuthRequest>(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<AuthRequest>(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
});
});
}
}
}

View File

@ -1,6 +1,5 @@
using AscNet.Common.Util;
using AscNet.SDKServer.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

View File

@ -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; }
}
}

View File

@ -1,4 +1,5 @@
using AscNet.Common.Util;
using System.Runtime.InteropServices;
using AscNet.Common.Util;
namespace AscNet
{