1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
/*
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "BattlenetAccountMgr.h"
#include "AccountMgr.h"
#include "DatabaseEnv.h"
#include "SRP6.h"
#include "Util.h"
using GameAccountMgr = AccountMgr;
using BnetSRP6_OLD = Trinity::Crypto::SRP::BnetSRP6v1<Trinity::Crypto::SHA256>;
using BnetSRP6 = Trinity::Crypto::SRP::BnetSRP6v2<Trinity::Crypto::SHA256>;
enum class SrpVersion : int8
{
v1 = 1, // password length limit 16 characters, case-insensitive, uses SHA256 to generate verifier
v2 = 2 // password length limit 128 characters, case-sensitive, uses PBKDF2 with SHA512 to generate verifier
};
AccountOpResult Battlenet::AccountMgr::CreateBattlenetAccount(std::string email, std::string password, bool withGameAccount, std::string* gameAccountName)
{
if (utf8length(email) > MAX_BNET_EMAIL_STR)
return AccountOpResult::AOR_NAME_TOO_LONG;
if (utf8length(password) > MAX_BNET_PASS_STR)
return AccountOpResult::AOR_PASS_TOO_LONG;
Utf8ToUpperOnlyLatin(email);
if (GetId(email))
return AccountOpResult::AOR_NAME_ALREADY_EXIST;
std::string srpUsername = GetSrpUsername(email);
auto [salt, verifier] = Trinity::Crypto::SRP6::MakeRegistrationData<BnetSRP6>(srpUsername, password);
LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_INS_BNET_ACCOUNT);
stmt->setString(0, email);
stmt->setInt8(1, AsUnderlyingType(SrpVersion::v2));
stmt->setBinary(2, salt);
stmt->setBinary(3, std::move(verifier));
LoginDatabase.DirectExecute(stmt);
uint32 newAccountId = GetId(email);
ASSERT(newAccountId);
if (withGameAccount)
{
*gameAccountName = std::to_string(newAccountId) + "#1";
std::string gameAccountPassword = password.substr(0, MAX_PASS_STR);
Utf8ToUpperOnlyLatin(gameAccountPassword);
GameAccountMgr::instance()->CreateAccount(*gameAccountName, gameAccountPassword, email, newAccountId, 1);
}
return AccountOpResult::AOR_OK;
}
AccountOpResult Battlenet::AccountMgr::ChangePassword(uint32 accountId, std::string newPassword)
{
std::string username;
if (!GetName(accountId, username))
return AccountOpResult::AOR_NAME_NOT_EXIST; // account doesn't exist
if (utf8length(newPassword) > MAX_BNET_PASS_STR)
return AccountOpResult::AOR_PASS_TOO_LONG;
std::string srpUsername = GetSrpUsername(username);
auto [salt, verifier] = Trinity::Crypto::SRP6::MakeRegistrationData<BnetSRP6>(srpUsername, newPassword);
LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_UPD_BNET_LOGON);
stmt->setInt8(0, AsUnderlyingType(SrpVersion::v2));
stmt->setBinary(1, salt);
stmt->setBinary(2, std::move(verifier));
stmt->setUInt32(3, accountId);
LoginDatabase.Execute(stmt);
return AccountOpResult::AOR_OK;
}
bool Battlenet::AccountMgr::CheckPassword(uint32 accountId, std::string password)
{
std::string username;
if (!GetName(accountId, username))
return false;
LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_BNET_CHECK_PASSWORD);
stmt->setUInt32(0, accountId);
if (PreparedQueryResult result = LoginDatabase.Query(stmt))
{
Trinity::Crypto::SRP::Salt salt = (*result)[1].GetBinary<Trinity::Crypto::SRP::SALT_LENGTH>();
Trinity::Crypto::SRP::Verifier verifier = (*result)[2].GetBinary();
switch (SrpVersion((*result)[0].GetInt8()))
{
case SrpVersion::v1:
Utf8ToUpperOnlyLatin(password);
return BnetSRP6_OLD(username, salt, verifier).CheckCredentials(username, password);
case SrpVersion::v2:
return BnetSRP6(username, salt, verifier).CheckCredentials(username, password);
default:
break;
}
}
return false;
}
AccountOpResult Battlenet::AccountMgr::LinkWithGameAccount(std::string_view email, std::string_view gameAccountName)
{
uint32 bnetAccountId = GetId(email);
if (!bnetAccountId)
return AccountOpResult::AOR_NAME_NOT_EXIST;
uint32 gameAccountId = GameAccountMgr::GetId(gameAccountName);
if (!gameAccountId)
return AccountOpResult::AOR_NAME_NOT_EXIST;
if (GetIdByGameAccount(gameAccountId))
return AccountOpResult::AOR_ACCOUNT_BAD_LINK;
LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_UPD_BNET_GAME_ACCOUNT_LINK);
stmt->setUInt32(0, bnetAccountId);
stmt->setUInt8(1, GetMaxIndex(bnetAccountId) + 1);
stmt->setUInt32(2, gameAccountId);
LoginDatabase.Execute(stmt);
return AccountOpResult::AOR_OK;
}
AccountOpResult Battlenet::AccountMgr::UnlinkGameAccount(std::string_view gameAccountName)
{
uint32 gameAccountId = GameAccountMgr::GetId(gameAccountName);
if (!gameAccountId)
return AccountOpResult::AOR_NAME_NOT_EXIST;
if (!GetIdByGameAccount(gameAccountId))
return AccountOpResult::AOR_ACCOUNT_BAD_LINK;
LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_UPD_BNET_GAME_ACCOUNT_LINK);
stmt->setNull(0);
stmt->setNull(1);
stmt->setUInt32(2, gameAccountId);
LoginDatabase.Execute(stmt);
return AccountOpResult::AOR_OK;
}
uint32 Battlenet::AccountMgr::GetId(std::string_view username)
{
LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_BNET_ACCOUNT_ID_BY_EMAIL);
stmt->setString(0, username);
if (PreparedQueryResult result = LoginDatabase.Query(stmt))
return (*result)[0].GetUInt32();
return 0;
}
bool Battlenet::AccountMgr::GetName(uint32 accountId, std::string& name)
{
LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_BNET_ACCOUNT_EMAIL_BY_ID);
stmt->setUInt32(0, accountId);
if (PreparedQueryResult result = LoginDatabase.Query(stmt))
{
name = (*result)[0].GetString();
return true;
}
return false;
}
uint32 Battlenet::AccountMgr::GetIdByGameAccount(uint32 gameAccountId)
{
LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_BNET_ACCOUNT_ID_BY_GAME_ACCOUNT);
stmt->setUInt32(0, gameAccountId);
if (PreparedQueryResult result = LoginDatabase.Query(stmt))
return (*result)[0].GetUInt32();
return 0;
}
QueryCallback Battlenet::AccountMgr::GetIdByGameAccountAsync(uint32 gameAccountId)
{
LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_BNET_ACCOUNT_ID_BY_GAME_ACCOUNT);
stmt->setUInt32(0, gameAccountId);
return LoginDatabase.AsyncQuery(stmt);
}
uint8 Battlenet::AccountMgr::GetMaxIndex(uint32 accountId)
{
LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_BNET_MAX_ACCOUNT_INDEX);
stmt->setUInt32(0, accountId);
PreparedQueryResult result = LoginDatabase.Query(stmt);
if (result)
return (*result)[0].GetUInt8();
return 0;
}
std::string Battlenet::AccountMgr::GetSrpUsername(std::string name)
{
Utf8ToUpperOnlyLatin(name);
return ByteArrayToHexStr(Trinity::Crypto::SHA256::GetDigestOf(name));
}
|