Skip to content

Commit 64a1da5

Browse files
authored
Merge pull request #961 from D00MK1D/develop
[For review and improvement] E-mail requirement and Password change features
2 parents c6ff06f + d2b1347 commit 64a1da5

23 files changed

Lines changed: 1414 additions & 100 deletions

File tree

Arrowgene.Ddon.Database/IDatabase.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,15 @@ public interface IDatabase
6565
DatabaseMeta GetMeta();
6666

6767
// Account
68-
Account? CreateAccount(string name, string mail, string hash);
68+
Account? CreateAccount(string name, string mail, string hash, string mailToken);
6969
Account SelectAccountById(int accountId);
7070
Account? SelectAccountByName(string accountName);
71+
Account? SelectAccountByEmail(string email);
7172
Account? SelectAccountByLoginToken(string loginToken);
73+
Account? SelectAccountByPasswordTokenAndName(string accountName, string passwordToken);
74+
Account? SelectAccountByMailTokenAndName(string accountName, string mailToken);
75+
Account? SelectAccountByEmailAndName(string accountName, string email);
76+
7277
bool UpdateAccount(Account account);
7378
bool DeleteAccount(int accountId);
7479
Storages SelectAllStoragesByCharacterId(uint characterId);

Arrowgene.Ddon.Database/Sql/Core/DdonSqlDbAccount.cs

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Data.Common;
33
using System.Text;
44
using Arrowgene.Ddon.Database.Model;
5+
using Arrowgene.Ddon.Shared.Model;
56

67
namespace Arrowgene.Ddon.Database.Sql.Core;
78

@@ -19,9 +20,13 @@ public partial class DdonSqlDb : SqlDb
1920
private static readonly string SqlSelectAccountById = $"SELECT \"id\", {BuildQueryField(AccountFields)} FROM \"account\" WHERE \"id\"=@id;";
2021
private static readonly string SqlSelectAccountByName = $"SELECT \"id\", {BuildQueryField(AccountFields)} FROM \"account\" WHERE \"normal_name\"=@normal_name;";
2122
private static readonly string SqlSelectAccountByLoginToken = $"SELECT \"id\", {BuildQueryField(AccountFields)} FROM \"account\" WHERE \"login_token\"=@login_token;";
23+
private static readonly string SqlSelectAccountByPasswordTokenAndName = $"SELECT \"id\", {BuildQueryField(AccountFields)} FROM \"account\" WHERE \"password_token\"=@password_token AND \"normal_name\"=@normal_name;";
24+
private static readonly string SqlSelectAccountByMailTokenAndName = $"SELECT \"id\", {BuildQueryField(AccountFields)} FROM \"account\" WHERE \"mail_token\"=@mail_token AND \"normal_name\"=@normal_name;";
25+
private static readonly string SqlSelectAccountByEmail = $"SELECT \"id\", {BuildQueryField(AccountFields)} FROM \"account\" WHERE UPPER(\"mail\")=UPPER(@mail);";
26+
private static readonly string SqlSelectAccountByEmailAndName = $"SELECT \"id\", {BuildQueryField(AccountFields)} FROM \"account\" WHERE UPPER(\"mail\")=UPPER(@mail) AND \"normal_name\"=@normal_name;";
2227
private static readonly string SqlUpdateAccount = $"UPDATE \"account\" SET {BuildQueryUpdate(AccountFields)} WHERE \"id\"=@id;";
2328

24-
public override Account? CreateAccount(string name, string mail, string hash)
29+
public override Account? CreateAccount(string name, string mail, string hash, string mailToken)
2530
{
2631
Account account = new();
2732
account.Name = name;
@@ -30,9 +35,9 @@ public partial class DdonSqlDb : SqlDb
3035
account.Hash = hash;
3136
account.State = AccountStateType.User;
3237
account.Created = DateTime.UtcNow;
33-
account.MailToken = Convert.ToBase64String(Encoding.UTF8.GetBytes(mail));
34-
account.PasswordToken = Convert.ToBase64String(Encoding.UTF8.GetBytes(name));
35-
account.LoginToken = Convert.ToBase64String(Encoding.UTF8.GetBytes(name));
38+
account.MailToken = mailToken;
39+
account.PasswordToken = null;
40+
account.LoginToken = null;
3641
account.LoginTokenCreated = DateTime.UtcNow;
3742
account.MailVerifiedAt = DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc);
3843
account.LastAuthentication = DateTime.UtcNow;
@@ -77,6 +82,18 @@ public partial class DdonSqlDb : SqlDb
7782
return account;
7883
}
7984

85+
public override Account? SelectAccountByEmail(string email)
86+
{
87+
Account account = null;
88+
ExecuteReader(SqlSelectAccountByEmail,
89+
command => { AddParameter(command, "@mail", email); }, reader =>
90+
{
91+
if (reader.Read()) account = ReadAccount(reader);
92+
});
93+
94+
return account;
95+
}
96+
8097
public override Account SelectAccountById(int accountId)
8198
{
8299
Account account = null;
@@ -99,6 +116,49 @@ public override Account SelectAccountById(int accountId)
99116
return account;
100117
}
101118

119+
public override Account? SelectAccountByPasswordTokenAndName(string accountName, string passwordToken)
120+
{
121+
Account account = null;
122+
ExecuteReader(SqlSelectAccountByPasswordTokenAndName,
123+
command => {
124+
AddParameter(command, "@password_token", passwordToken);
125+
AddParameter(command, "@normal_name", accountName);
126+
}, reader =>
127+
{
128+
if (reader.Read()) account = ReadAccount(reader);
129+
});
130+
131+
return account;
132+
}
133+
public override Account? SelectAccountByMailTokenAndName(string accountName, string mailToken)
134+
{
135+
Account account = null;
136+
ExecuteReader(SqlSelectAccountByMailTokenAndName,
137+
command => {
138+
AddParameter(command, "@mail_token", mailToken);
139+
AddParameter(command, "@normal_name", accountName);
140+
}, reader =>
141+
{
142+
if (reader.Read()) account = ReadAccount(reader);
143+
});
144+
145+
return account;
146+
}
147+
public override Account? SelectAccountByEmailAndName(string accountName, string email)
148+
{
149+
Account account = null;
150+
ExecuteReader(SqlSelectAccountByEmailAndName,
151+
command => {
152+
AddParameter(command, "@mail", email);
153+
AddParameter(command, "@normal_name", accountName);
154+
}, reader =>
155+
{
156+
if (reader.Read()) account = ReadAccount(reader);
157+
});
158+
159+
return account;
160+
}
161+
102162
public override bool UpdateAccount(Account account)
103163
{
104164
int rowsAffected = ExecuteNonQuery(SqlUpdateAccount, command =>

Arrowgene.Ddon.Database/Sql/SqlDb.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,14 @@ public bool MigrateDatabase(DatabaseMigrator migrator, uint toVersion)
325325
public abstract bool CreateMeta(DatabaseMeta meta);
326326

327327
public abstract DatabaseMeta GetMeta();
328-
public abstract Account? CreateAccount(string name, string mail, string hash);
328+
public abstract Account? CreateAccount(string name, string mail, string hash, string mailToken);
329329
public abstract Account SelectAccountById(int accountId);
330330
public abstract Account? SelectAccountByName(string accountName);
331+
public abstract Account? SelectAccountByEmail(string email);
331332
public abstract Account? SelectAccountByLoginToken(string loginToken);
333+
public abstract Account? SelectAccountByPasswordTokenAndName(string accountName, string passwordToken);
334+
public abstract Account? SelectAccountByMailTokenAndName(string accountName, string mailToken);
335+
public abstract Account? SelectAccountByEmailAndName(string accountName, string email);
332336
public abstract bool UpdateAccount(Account account);
333337
public abstract bool DeleteAccount(int accountId);
334338
public abstract Storages SelectAllStoragesByCharacterId(uint characterId);

Arrowgene.Ddon.GameServer/Handler/ConnectionLoginHandler.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ public override S2CConnectionLoginRes Handle(GameClient client, C2SConnectionLog
8585

8686
Logger.Info(client, "Logged Into GameServer");
8787

88-
// update login token for client
89-
// client.Account.LoginToken = GameToken.GenerateLoginToken();
9088
client.Account.LoginTokenCreated = now;
9189
if (!Database.UpdateAccount(client.Account))
9290
{

Arrowgene.Ddon.LoginServer/Handler/ClientLoginHandler.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public override L2CLoginRes Handle(LoginClient client, C2LLoginReq request)
5454
{
5555
if (account == null)
5656
{
57+
L2CEjectionNtc message = new L2CEjectionNtc { Message = "Invalid login attempt" };
58+
client.Send(message);
5759
throw new ResponseErrorException(ErrorCode.ERROR_CODE_AUTH_ONETIME_TOKEN_FAIL, "Invalid OneTimeToken");
5860
}
5961
}
@@ -66,7 +68,7 @@ public override L2CLoginRes Handle(LoginClient client, C2LLoginReq request)
6668
account = Database.SelectAccountByName(oneTimeToken);
6769
if (account == null)
6870
{
69-
account = Database.CreateAccount(oneTimeToken, oneTimeToken, oneTimeToken)
71+
account = Database.CreateAccount(oneTimeToken, oneTimeToken, oneTimeToken, oneTimeToken)
7072
?? throw new ResponseErrorException(ErrorCode.ERROR_CODE_AUTH_ACCOUNT_GENERATION_FAIL, "Could not create account from OneTimeToken, choose another token");
7173

7274
Logger.Info(client, "Created new account from OneTimeToken");

Arrowgene.Ddon.Shared/Model/GameToken.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Text;
33
using Arrowgene.Ddon.Shared.Crypto;
44

@@ -10,7 +10,7 @@ public class GameToken
1010
public const int LoginTokenLength = 20;
1111
public const int GameTokenLength = 20;
1212

13-
public static string GenerateLoginToken()
13+
public static string GenerateToken()
1414
{
1515
StringBuilder sb = new StringBuilder();
1616
for (int i = 0; i < LoginTokenLength; i++)

Arrowgene.Ddon.Test/Database/CharacterDatabaseTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void TestCharacterOperations()
2020
setting.WipeOnStartup = true;
2121
IDatabase database = DdonDatabaseBuilder.BuildSqLite(setting, true);
2222

23-
Account account = database.CreateAccount("test", "test", "test");
23+
Account account = database.CreateAccount("test", "test", "test", "test");
2424
Assert.NotNull(account);
2525

2626
Character c = new Character();

Arrowgene.Ddon.Test/Database/DatabaseMigratorTest.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public void ExecuteReader(DbConnection conn, string sql, Action<DbCommand> comma
189189
public void ExecuteQuerySafe(DbConnection? connectionIn, Action<DbConnection> work) {}
190190
T IDatabase.ExecuteQuerySafe<T>(DbConnection? connectionIn, Func<DbConnection, T> work) { throw new NotImplementedException(); }
191191

192-
public Account CreateAccount(string name, string mail, string hash) { return new Account(); }
192+
public Account CreateAccount(string name, string mail, string hash, string mailToken) { return new Account(); }
193193
public bool CreateCharacter(Character character) { return true; }
194194
public bool CreateDatabase() { return true; }
195195
public bool CreatePawn(Pawn pawn) { return true; }
@@ -285,7 +285,11 @@ public void Execute(DbConnection conn, string sql, bool rethrowException = false
285285
public bool ReplaceCompletedQuest(uint characterCommonId, QuestId questId, QuestType questType, uint count, DbConnection? connectionIn = null) { return true; }
286286
public Account SelectAccountById(int accountId) { return new Account(); }
287287
public Account SelectAccountByLoginToken(string loginToken) { return new Account(); }
288+
public Account SelectAccountByPasswordTokenAndName(string accountName, string passwordToken) { return new Account(); }
289+
public Account SelectAccountByMailTokenAndName(string accountName, string mailToken) { return new Account(); }
290+
public Account SelectAccountByEmailAndName(string accountName, string email) { return new Account(); }
288291
public Account SelectAccountByName(string accountName) { return new Account(); }
292+
public Account SelectAccountByEmail(string email) { return new Account(); }
289293
public List<BazaarExhibition> SelectActiveBazaarExhibitionsByItemIdExcludingOwn(uint itemId, uint excludedCharacterId, DbConnection? connectionIn = null) { return new List<BazaarExhibition>(); }
290294
public List<BazaarExhibition> SelectActiveBazaarExhibitionsByItemIdsExcludingOwn(List<uint> itemIds, uint excludedCharacterId, DbConnection? connectionIn = null) { return new List<BazaarExhibition>(); }
291295
public List<AbilityId> SelectAllUnlockedSecretAbilities(uint commonId, DbConnection? connectionIn = null) { return new List<AbilityId>(); }

0 commit comments

Comments
 (0)