auth.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "auth.hpp"
  4. #include <string.h>
  5. #include "../common/showmsg.hpp"
  6. #include "../common/sql.hpp"
  7. #include "http.hpp"
  8. #include "sqllock.hpp"
  9. #include "web.hpp"
  10. bool isAuthorized(const Request &request, bool checkGuildLeader) {
  11. if (!request.has_file("AuthToken") || !request.has_file("AID"))
  12. return false;
  13. if (checkGuildLeader && !request.has_file("GDID"))
  14. return false;
  15. auto token_str = request.get_file_value("AuthToken").content;
  16. auto token = token_str.c_str();
  17. auto account_id = std::stoi(request.get_file_value("AID").content);
  18. SQLLock loginlock(LOGIN_SQL_LOCK);
  19. loginlock.lock();
  20. auto handle = loginlock.getHandle();
  21. SqlStmt * stmt = SqlStmt_Malloc(handle);
  22. if (SQL_SUCCESS != SqlStmt_Prepare(stmt,
  23. "SELECT `account_id` FROM `%s` WHERE (`account_id` = ? AND `web_auth_token` = ? AND `web_auth_token_enabled` = '1')",
  24. login_table)
  25. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_INT, &account_id, sizeof(account_id))
  26. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_STRING, (void *)token, strlen(token))
  27. || SQL_SUCCESS != SqlStmt_Execute(stmt)
  28. ) {
  29. SqlStmt_ShowDebug(stmt);
  30. SqlStmt_Free(stmt);
  31. loginlock.unlock();
  32. return false;
  33. }
  34. if (SqlStmt_NumRows(stmt) <= 0) {
  35. ShowWarning("Request with AID %d and token %s unverified\n", account_id, token);
  36. SqlStmt_Free(stmt);
  37. loginlock.unlock();
  38. return false;
  39. }
  40. SqlStmt_Free(stmt);
  41. loginlock.unlock();
  42. if (!checkGuildLeader) {
  43. // we're done, auth ok
  44. return true;
  45. }
  46. auto guild_id = std::stoi(request.get_file_value("GDID").content);
  47. SQLLock charlock(CHAR_SQL_LOCK);
  48. charlock.lock();
  49. handle = charlock.getHandle();
  50. stmt = SqlStmt_Malloc(handle);
  51. if (SQL_SUCCESS != SqlStmt_Prepare(stmt,
  52. "SELECT `account_id` FROM `%s` LEFT JOIN `%s` using (`char_id`) WHERE (`%s`.`account_id` = ? AND `%s`.`guild_id` = ?) LIMIT 1",
  53. guild_db_table, char_db_table, char_db_table, guild_db_table)
  54. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_INT, &account_id, sizeof(account_id))
  55. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_INT, &guild_id, sizeof(guild_id))
  56. || SQL_SUCCESS != SqlStmt_Execute(stmt)
  57. ) {
  58. SqlStmt_ShowDebug(stmt);
  59. SqlStmt_Free(stmt);
  60. charlock.unlock();
  61. return false;
  62. }
  63. if (SqlStmt_NumRows(stmt) <= 0) {
  64. ShowDebug("Request with AID %d GDID %d and token %s unverified\n", account_id, guild_id, token);
  65. SqlStmt_Free(stmt);
  66. charlock.unlock();
  67. return false;
  68. }
  69. SqlStmt_Free(stmt);
  70. charlock.unlock();
  71. return true;
  72. }