fork from 1.3
This commit is contained in:
59
FreeSR.Database.Account/AccountDatabase.cs
Normal file
59
FreeSR.Database.Account/AccountDatabase.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
namespace FreeSR.Database.Account
|
||||
{
|
||||
using FreeSR.Database.Account.Model;
|
||||
using FreeSR.Database.Account.Util;
|
||||
using FreeSR.Database.Mongo;
|
||||
using MongoDB.Driver;
|
||||
|
||||
public class AccountDatabase : SRMongoDatabase<AccountModel>
|
||||
{
|
||||
private int _maxUid;
|
||||
|
||||
public AccountDatabase(IMongoDatabase database, string collectionName) : base(database, collectionName)
|
||||
{
|
||||
// AccountDatabase.
|
||||
}
|
||||
|
||||
public async Task<AccountModel> Create(string name, string password)
|
||||
{
|
||||
if (_maxUid == 0)
|
||||
_maxUid = await FetchMaxUid();
|
||||
|
||||
if (await GetByName(name) != null)
|
||||
return null;
|
||||
|
||||
var model = new AccountModel
|
||||
{
|
||||
Uid = Interlocked.Increment(ref _maxUid),
|
||||
Name = name,
|
||||
Password = password,
|
||||
CreationDateUtc = DateTime.UtcNow,
|
||||
Token = AccountTokenUtil.Generate()
|
||||
};
|
||||
|
||||
await Insert(model);
|
||||
return model;
|
||||
}
|
||||
|
||||
public async Task<AccountModel> GetByUid(int uid)
|
||||
{
|
||||
return await FindOne(account => account.Uid == uid);
|
||||
}
|
||||
|
||||
public async Task<AccountModel> GetByName(string name)
|
||||
{
|
||||
return await FindOne(account => account.Name == name);
|
||||
}
|
||||
|
||||
public async Task Update(AccountModel account)
|
||||
{
|
||||
await Update(model => model.Uid == account.Uid, account);
|
||||
}
|
||||
|
||||
private async Task<int> FetchMaxUid()
|
||||
{
|
||||
var maxUidAccount = await FindMax(account => account.Uid);
|
||||
return maxUidAccount?.Uid ?? 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
13
FreeSR.Database.Account/FreeSR.Database.Account.csproj
Normal file
13
FreeSR.Database.Account/FreeSR.Database.Account.csproj
Normal file
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>disable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\FreeSR.Database\FreeSR.Database.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
14
FreeSR.Database.Account/Model/AccountModel.cs
Normal file
14
FreeSR.Database.Account/Model/AccountModel.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace FreeSR.Database.Account.Model
|
||||
{
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
|
||||
[BsonIgnoreExtraElements]
|
||||
public class AccountModel
|
||||
{
|
||||
public int Uid { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Password { get; set; }
|
||||
public string Token { get; set; }
|
||||
public DateTime CreationDateUtc { get; set; }
|
||||
}
|
||||
}
|
||||
37
FreeSR.Database.Account/Util/AccountTokenUtil.cs
Normal file
37
FreeSR.Database.Account/Util/AccountTokenUtil.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
namespace FreeSR.Database.Account.Util
|
||||
{
|
||||
using FreeSR.Database.Account.Model;
|
||||
|
||||
public static class AccountTokenUtil
|
||||
{
|
||||
private const int AccountTokenLength = 128;
|
||||
private const string TokenCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
|
||||
private static Random s_random;
|
||||
|
||||
static AccountTokenUtil()
|
||||
{
|
||||
s_random = new Random();
|
||||
}
|
||||
|
||||
public static string Generate()
|
||||
{
|
||||
var token = "";
|
||||
|
||||
for (int i = 0; i < AccountTokenLength; i++)
|
||||
{
|
||||
token += TokenCharacters[s_random.Next(TokenCharacters.Length)];
|
||||
}
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
public static bool Verify(AccountModel accountModel, string clientToken)
|
||||
{
|
||||
if (accountModel == null)
|
||||
return false;
|
||||
|
||||
return string.Equals(accountModel.Token, clientToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user