EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
APIActiveObjectManager.cpp
Go to the documentation of this file.
1 /*
2  ------------------------------------------------------------------------------------
3  LICENSE:
4  ------------------------------------------------------------------------------------
5  This file is part of EVEmu: EVE Online Server Emulator
6  Copyright 2006 - 2021 The EVEmu Team
7  For the latest information visit https://evemu.dev
8  ------------------------------------------------------------------------------------
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13 
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License along with
19  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21  http://www.gnu.org/copyleft/lesser.txt.
22  ------------------------------------------------------------------------------------
23  Author: Aknor Jaden
24 */
25 
26 #include "eve-server.h"
27 
29 
30 //std::string APIActiveObjectManager::m_hexCharMap("0123456789ABCDEF");
31 
33 : APIServiceManager(services)
34 {
35 }
36 
37 std::tr1::shared_ptr<std::string> APIActiveObjectManager::ProcessCall(const APICommandCall * pAPICommandCall)
38 {
39  sLog.Debug("APIActiveObjectManager::ProcessCall()", "EVEmu API - Active Object Service Manager");
40 
41  if( pAPICommandCall->find( "servicehandler" ) == pAPICommandCall->end() )
42  {
43  sLog.Error( "APIActiveObjectManager::ProcessCall()", "Cannot find 'servicehandler' specifier in pAPICommandCall packet" );
44  return std::tr1::shared_ptr<std::string>(new std::string(""));
45  }
46 /*
47  if( pAPICommandCall->find( "servicehandler" )->second == "CharacterList.xml.aspx" )
48  return _APIKeyRequest(pAPICommandCall);
49  else if( pAPICommandCall->find( "servicehandler" )->second == "CharacterSummary.xml.aspx" )
50  return _Characters(pAPICommandCall);
51  else if( pAPICommandCall->find( "servicehandler" )->second == "Character.xml.aspx" )
52  return _AccountStatus(pAPICommandCall);
53  else if( pAPICommandCall->find( "servicehandler" )->second == "Character.xml.aspx" )
54  return _AccountStatus(pAPICommandCall);
55  else if( pAPICommandCall->find( "servicehandler" )->second == "Character.xml.aspx" )
56  return _AccountStatus(pAPICommandCall);
57  else if( pAPICommandCall->find( "servicehandler" )->second == "Character.xml.aspx" )
58  return _AccountStatus(pAPICommandCall);
59  else if( pAPICommandCall->find( "servicehandler" )->second == "Character.xml.aspx" )
60  return _AccountStatus(pAPICommandCall);
61  else if( pAPICommandCall->find( "servicehandler" )->second == "Character.xml.aspx" )
62  return _AccountStatus(pAPICommandCall);
63  else if( pAPICommandCall->find( "servicehandler" )->second == "Character.xml.aspx" )
64  return _AccountStatus(pAPICommandCall);
65  else if( pAPICommandCall->find( "servicehandler" )->second == "Character.xml.aspx" )
66  return _AccountStatus(pAPICommandCall);
67  //else if( pAPICommandCall->find( "servicehandler" )->second == "TODO.xml.aspx" )
68  // return _TODO(pAPICommandCall);
69  else
70  {
71  sLog.Error("APIActiveObjectManager::ProcessCall()", "EVEmu API - Active Object Service Manager - ERROR: Cannot resolve '%s' as a valid service query for Admin Service Manager",
72  pAPICommandCall->find("servicehandler")->second.c_str() );
73  return std::tr1::shared_ptr<std::string>(new std::string(""));
74  }
75 */
76  return BuildErrorXMLResponse( "9999", "EVEmu API Server: Active Object Manager - Unknown call." );
77 }
78 /*
79 std::tr1::shared_ptr<std::string> APIActiveObjectManager::_APIKeyRequest(const APICommandCall * pAPICommandCall)
80 {
81  bool status = false;
82  uint32 userID, apiRole;
83  std::string username;
84  std::string password;
85  std::string keyType;
86  std::string action;
87  std::string apiLimitedKey;
88  std::string apiFullKey;
89  std::string accountID;
90  std::string keyTag;
91  \*
92  sLog.Debug("APIActiveObjectManager::_APIKeyRequest()", "EVEmu API - Active Object Service Manager - CALL: APIKeyRequest.xml.aspx");
93 
94  // 1: Decode arguments:
95  if( pAPICommandCall->find( "username" ) != pAPICommandCall->end() )
96  username = pAPICommandCall->find( "username" )->second;
97  else
98  {
99  sLog.Error( "APIActiveObjectManager::_APIKeyRequest()", "ERROR: No 'username' parameter found in call argument list - exiting with error" );
100  return BuildErrorXMLResponse( "203", "Authentication failure." );
101  }
102 
103  if( pAPICommandCall->find( "password" ) != pAPICommandCall->end() )
104  password = pAPICommandCall->find( "password" )->second;
105  else
106  {
107  sLog.Error( "APIActiveObjectManager::_APIKeyRequest()", "ERROR: No 'password' parameter found in call argument list - exiting with error" );
108  return BuildErrorXMLResponse( "203", "Authentication failure." );
109  }
110 
111  if( pAPICommandCall->find( "keytype" ) != pAPICommandCall->end() )
112  keyType = pAPICommandCall->find( "keytype" )->second;
113  else
114  {
115  sLog.Error( "APIActiveObjectManager::_APIKeyRequest()", "ERROR: No 'keytype' parameter found in call argument list - exiting with error" );
116  return BuildErrorXMLResponse( "203", "Authentication failure." );
117  }
118 
119  if( keyType != "full" )
120  if( keyType != "limited" )
121  {
122  sLog.Error( "APIActiveObjectManager::_APIKeyRequest()", "ERROR: 'keytype' parameter has invalid value '%s' - exiting with error", keyType.c_str() );
123  return BuildErrorXMLResponse( "203", "Authentication failure." );
124  }
125 
126  if( pAPICommandCall->find( "action" ) != pAPICommandCall->end() )
127  action = pAPICommandCall->find( "action" )->second;
128  else
129  {
130  sLog.Error( "APIActiveObjectManager::_APIKeyRequest()", "ERROR: No 'action' parameter found in call argument list - exiting with error" );
131  return BuildErrorXMLResponse( "203", "Authentication failure." );
132  }
133 
134  if( action != "new" )
135  if( action != "get" )
136  {
137  sLog.Error( "APIActiveObjectManager::_APIKeyRequest()", "ERROR: 'action' parameter has invalid value '%s' - exiting with error", action.c_str() );
138  return BuildErrorXMLResponse( "203", "Authentication failure." );
139  }
140 
141  // 2: Authenticate the username and password against the account table:
142  status = _AuthenticateUserNamePassword( username, password );
143  if( !status )
144  {
145  sLog.Error( "APIActiveObjectManager::_APIKeyRequest()", "ERROR: username='%s' password='%s' does not authenticate.", username.c_str(), password.c_str() );
146  return BuildErrorXMLResponse( "203", "Authentication failure." );
147  }
148  // 2a: Get accountID using username, now that it's been validated:
149  status = m_db.GetAccountIdFromUsername( username, &accountID );
150  if( !status )
151  {
152  sLog.Error( "APIActiveObjectManager::_APIKeyRequest()", "ERROR: username='%s' cannot be found in 'account' table.", username.c_str() );
153  return BuildErrorXMLResponse( "203", "Authentication failure." );
154  }
155 
156  // 3: Determine if this account's userID exists:
157  status = m_db.GetApiAccountInfoUsingAccountID( accountID, &userID, &apiFullKey, &apiLimitedKey, &apiRole );
158 
159  // 4: Generate new random 64-character hexadecimal API Keys:
160  // Write new API Key to database if key request 'action' is 'new':
161  if( !status )
162  {
163  // 4a: If userID does not exist for this accountID, then insert a new row into the 'accountApi' table regardless of 'get' or 'new':
164  apiLimitedKey = _GenerateAPIKey();
165  apiFullKey = _GenerateAPIKey();
166  status = m_db.InsertNewUserIdApiKeyInfoToDatabase( atol(accountID.c_str()), apiFullKey, apiLimitedKey, EVEAPI::Roles::Player );
167  }
168 
169  if( action == "new" )
170  if( keyType == "limited" )
171  apiLimitedKey = _GenerateAPIKey();
172  else //if( keyType == "full" )
173  apiFullKey = _GenerateAPIKey();
174 
175  if( action == "new" )
176  {
177  if( status )
178  // 4b: If userID already exists, generate new API keys and write them back to the database under that userID:
179  status = m_db.UpdateUserIdApiKeyDatabaseRow( userID, apiFullKey, apiLimitedKey );
180  }
181 
182  // 5: Build XML document to return to API client:
183  userID = 0;
184  apiFullKey = "";
185  apiLimitedKey = "";
186  apiRole = 0;
187  //status = m_db.GetAccountIdFromUsername( username, &accountID );
188 
189  status = m_db.GetApiAccountInfoUsingAccountID( accountID, &userID, &apiFullKey, &apiLimitedKey, &apiRole );
190  if( !status )
191  {
192  sLog.Error( "APIActiveObjectManager::_APIKeyRequest()", "ERROR: username='%s' cannot be found in 'account' table.", username.c_str() );
193  return BuildErrorXMLResponse( "203", "Authentication failure." );
194  }
195 
196  // 6: Return the userID and the API key requested in the xml structure:
197  _BuildXMLHeader();
198  {
199  _BuildSingleXMLTag( "userID", std::string(itoa(userID)) );
200  if( keyType == "full" )
201  {
202  keyTag = "apiFullKey";
203  _BuildSingleXMLTag( keyTag, apiFullKey );
204  }
205  else
206  {
207  keyTag = "apiLimitedKey";
208  _BuildSingleXMLTag( keyTag, apiLimitedKey );
209  }
210  _BuildSingleXMLTag( "expiresOn", Win32TimeToString(Win32TimeNow() + 180*Win32Time_Day) );
211  }
212  _CloseXMLHeader( EVEAPI::CacheStyles::Long );
213  *\
214  return _GetXMLDocumentString();
215 }
216 
217 std::tr1::shared_ptr<std::string> APIActiveObjectManager::_Characters(const APICommandCall * pAPICommandCall)
218 {
219 
220  sLog.Error( "APIActiveObjectManager::_Characters()", "TODO: Insert code to validate userID and apiKey" );
221  \*
222  sLog.Debug("APIActiveObjectManager::_Characters()", "EVEmu API - Account Service Manager - CALL: Characters.xml.aspx");
223 
224  if( pAPICommandCall->find( "userid" ) == pAPICommandCall->end() )
225  {
226  sLog.Error( "APIActiveObjectManager::_Characters()", "ERROR: No 'userID' parameter found in call argument list - exiting with error" );
227  return BuildErrorXMLResponse( "106", "Must provide userID parameter for authentication." );
228  }
229 
230  uint32 status = 0;
231  uint32 accountID = 0;
232  std::vector<std::string> charIDList;
233  std::vector<std::string> charNameList;
234  std::vector<std::string> charCorpIDList;
235  std::vector<std::string> charCorpNameList;
236 
237  std::string userID = pAPICommandCall->find( "userid" )->second;
238 
239  status = m_db.GetAccountIdFromUserID( userID, &accountID );
240  if( !status )
241  {
242  sLog.Error( "APIActiveObjectManager::_Characters()", "ERROR: userID='%s' cannot be found in 'accountApi' table.", userID.c_str() );
243  return BuildErrorXMLResponse( "203", "Authentication failure." );
244  }
245 
246  if( !( m_accountDB.GetCharactersList(accountID, charIDList, charNameList, charCorpIDList, charCorpNameList) ) )
247  {
248  sLog.Error( "APIActiveObjectManager::_Characters()", "ERROR: m_accountDB.GetCharactersList() call failed for unknown reason - exiting with error" );
249  BuildErrorXMLResponse( "9999", "EVEmu API Server: Account Manager - Characters.xml.aspx STUB" );
250  }
251 
252  std::vector<std::string> rowset;
253  _BuildXMLHeader();
254  {
255  _BuildXMLTag( "result" );
256  {
257  rowset.push_back("name");
258  rowset.push_back("characterID");
259  rowset.push_back("corporationName");
260  rowset.push_back("corporationID");
261  _BuildXMLRowSet( "characters", "characterID", &rowset );
262  {
263  for(uint32 i=0; i<charIDList.size(); i++)
264  {
265  rowset.clear();
266  rowset.push_back(charNameList.at(i));
267  rowset.push_back(charIDList.at(i));
268  rowset.push_back(charCorpNameList.at(i));
269  rowset.push_back(charCorpIDList.at(i));
270  _BuildXMLRow( &rowset );
271  }
272  }
273  _CloseXMLRowSet(); // close rowset "characters"
274  }
275  _CloseXMLTag(); // close tag "result"
276  }
277  _CloseXMLHeader( EVEAPI::CacheStyles::Long );
278  *\
279  return _GetXMLDocumentString();
280 }
281 
282 std::tr1::shared_ptr<std::string> APIActiveObjectManager::_AccountStatus(const APICommandCall * pAPICommandCall)
283 {
284  sLog.Error( "APIActiveObjectManager::_AccountStatus()", "TODO: Insert code to validate userID and apiKey" );
285  \*
286  if( pAPICommandCall->find( "userid" ) == pAPICommandCall->end() )
287  {
288  sLog.Error( "APIActiveObjectManager::_AccountStatus()", "ERROR: No 'userID' parameter found in call argument list - exiting with error" );
289  return BuildErrorXMLResponse( "106", "Must provide userID parameter for authentication." );
290  }
291 
292  uint32 accountID = 0;
293  std::vector<std::string> accountInfoList;
294 
295  if( !(m_db.GetAccountIdFromUserID( pAPICommandCall->find( "userid" )->second, &accountID )) )
296  {
297  sLog.Error( "APIActiveObjectManager::_AccountStatus()", "ERROR: Could not find 'accountID' in 'accountApi' table - exiting with error" );
298  return BuildErrorXMLResponse( "106", "Must provide userID parameter for authentication." );
299  }
300 
301  if( !(m_accountDB.GetAccountInfo( accountID, accountInfoList )) )
302  {
303  sLog.Error( "APIActiveObjectManager::_AccountStatus()", "ERROR: Could not find 'accountID' in 'account' table; there is an invalid 'accountID' referenced by api account 'userID' = %s - exiting with error", pAPICommandCall->find( "userid" )->second.c_str() );
304  return BuildErrorXMLResponse( "106", "Must provide userID parameter for authentication." );
305  }
306 
307  _BuildXMLHeader();
308  {
309  _BuildXMLTag( "result" );
310  {
311  _BuildSingleXMLTag( "paidUntil", "2100-12-31 12:00:00" );
312  _BuildSingleXMLTag( "createDate", "2000-01-01 12:00:00" );
313  _BuildSingleXMLTag( "logonCount", accountInfoList.at(2) );
314  _BuildSingleXMLTag( "logonMinutes", "1000" );
315  }
316  _CloseXMLTag(); // close tag "result"
317  }
318  _CloseXMLHeader( EVEAPI::CacheStyles::Short );
319  *\
320  return _GetXMLDocumentString();
321 }
322 \*
323 std::string APIActiveObjectManager::_GenerateAPIKey()
324 {
325  std::string key = "";
326 
327  for( int i=0; i<64; i++ )
328  key += m_hexCharMap.substr(static_cast<uint16>(MakeRandomInt(0, 15)), 1);
329 
330  return key;
331 }
332 */
std::tr1::shared_ptr< std::string > BuildErrorXMLResponse(std::string errorCode, std::string errorMessage)
APIActiveObjectManager(const PyServiceMgr &services)
std::map< std::string, std::string > APICommandCall
#define sLog
Evaluates to a NewLog instance.
Definition: LogNew.h:250
Generic Base Class used to derive classes for specific service handlers (character, corporation, etc)
std::tr1::shared_ptr< std::string > ProcessCall(const APICommandCall *pAPICommandCall)