auth.inc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #include <YSI_Coding\y_hooks>
  2. forward OnPlayerLogin(playerid);
  3. forward OnPlayerRegister(playerid);
  4. forward Account_Create(playerid);
  5. forward Account_Validate(playerid, bool:success);
  6. static AccountName[MAX_PLAYERS][MAX_PLAYER_NAME];
  7. static AccountIP[MAX_PLAYERS][MAX_PLAYER_IP];
  8. static AccountGPCI[MAX_PLAYERS][MAX_PLAYER_GPCI];
  9. static AccountSQLID[MAX_PLAYERS];
  10. static AccountPassword[MAX_PLAYERS][BCRYPT_HASH_LENGTH];
  11. static PasswordAttempts[MAX_PLAYERS];
  12. static bool:LoggedIn[MAX_PLAYERS];
  13. hook OnPlayerConnect(playerid)
  14. {
  15. GetPlayerName(playerid, AccountName[playerid], MAX_PLAYER_NAME);
  16. GetPlayerIp(playerid, AccountIP[playerid], MAX_PLAYER_IP);
  17. gpci(playerid, AccountGPCI[playerid], MAX_PLAYER_GPCI);
  18. AccountSQLID[playerid] = 0;
  19. AccountPassword[playerid][0] = EOS;
  20. PasswordAttempts[playerid] = 0;
  21. LoggedIn[playerid] = false;
  22. Ban_CheckUser(playerid);
  23. }
  24. hook OnPlayerPassedBanCheck(playerid)
  25. {
  26. inline const OnAccountCheck()
  27. {
  28. if(cache_num_rows())
  29. {
  30. cache_get_value_name_int(0, "id", AccountSQLID[playerid]);
  31. cache_get_value_name(0, "password", AccountPassword[playerid]);
  32. Account_PromptLogin(playerid);
  33. }
  34. else Account_PromptRegister(playerid);
  35. }
  36. MySQL_TQueryInline(MySQL_GetHandle(), using inline OnAccountCheck, "SELECT id, password FROM accounts WHERE name = '%e'", AccountName[playerid]);
  37. }
  38. Account_PromptLogin(playerid)
  39. {
  40. inline const _response(response, listitem, string:inputtext[])
  41. {
  42. #pragma unused listitem
  43. if(!response) return Kick(playerid);
  44. bcrypt_verify(playerid, "Account_Validate", inputtext, AccountPassword[playerid]);
  45. }
  46. Dialog_ShowCallback(playerid, using inline _response, DIALOG_STYLE_PASSWORD, "GTA Stories", "{FFFFFF}This account is {33AA33}registered.\n{FFFFFF}Please enter your password below in order to authenticate:", "Login", "Quit");
  47. return 1;
  48. }
  49. Account_PromptRegister(playerid)
  50. {
  51. inline _response(response, listitem, string:inputtext[])
  52. {
  53. #pragma unused listitem
  54. if(!response) return Kick(playerid);
  55. if(!(3 <= strlen(inputtext) <= 20))
  56. {
  57. SendErrorMessage(playerid, "Password length must be between 3 and 20 characters.");
  58. return Account_PromptRegister(playerid);
  59. }
  60. if(IsNumeric(inputtext))
  61. {
  62. SendErrorMessage(playerid, "The password must contain letters.");
  63. return Account_PromptRegister(playerid);
  64. }
  65. bcrypt_hash(playerid, "Account_Create", inputtext, BCRYPT_COST, "i", playerid);
  66. }
  67. Dialog_ShowCallback(playerid, using inline _response, DIALOG_STYLE_PASSWORD, "GTA Stories", "{FFFFFF}Your name is currently not registered!\nPlease enter a safe and secure password below to register with your master account:", "Register", "Quit");
  68. return 1;
  69. }
  70. public Account_Validate(playerid, bool:success)
  71. {
  72. if(!success)
  73. {
  74. PasswordAttempts[playerid]++;
  75. if(PasswordAttempts[playerid] >= 3)
  76. {
  77. SendErrorMessage(playerid, "You have been kicked from the server because you mistyped the password three times.");
  78. return Kick(playerid);
  79. }
  80. SendErrorMessage(playerid, "The password you entered is incorrect, please try again.");
  81. return Account_PromptLogin(playerid);
  82. }
  83. CallRemoteFunction("OnPlayerLogin", "i", playerid);
  84. return 1;
  85. }
  86. public Account_Create(playerid)
  87. {
  88. new hash[BCRYPT_HASH_LENGTH];
  89. bcrypt_get_hash(hash, sizeof(hash));
  90. inline const OnRegister()
  91. {
  92. AccountSQLID[playerid] = cache_insert_id();
  93. AccountPassword[playerid] = hash;
  94. CallRemoteFunction("OnPlayerRegister", "i", playerid);
  95. }
  96. MySQL_TQueryInline(MySQL_GetHandle(), using inline OnRegister, "INSERT INTO accounts (name, password, ip, gpci) VALUES ('%e', '%e', '%e', '%e')", AccountName[playerid], hash, AccountIP[playerid], AccountGPCI[playerid]);
  97. }
  98. public OnPlayerLogin(playerid)
  99. {
  100. LoggedIn[playerid] = true;
  101. new query[134];
  102. mysql_format(MySQL_GetHandle(), query, sizeof(query), "UPDATE accounts SET ip = '%e', last_login = CURRENT_TIMESTAMP() WHERE id = %d", AccountIP[playerid], AccountSQLID[playerid]);
  103. mysql_tquery(MySQL_GetHandle(), query);
  104. }
  105. public OnPlayerRegister(playerid)
  106. {
  107. LoggedIn[playerid] = true;
  108. }
  109. stock Account_GetSQLID(playerid)
  110. {
  111. return AccountSQLID[playerid];
  112. }
  113. stock Account_GetPassword(playerid)
  114. {
  115. return AccountPassword[playerid];
  116. }
  117. stock Account_SetPassword(playerid, const hash[])
  118. {
  119. format(AccountPassword[playerid], BCRYPT_HASH_LENGTH, hash);
  120. }
  121. stock Account_GetName(playerid)
  122. {
  123. return AccountName[playerid];
  124. }
  125. stock Account_SetName(playerid, const name[])
  126. {
  127. format(AccountName[playerid], MAX_PLAYER_NAME, name);
  128. }
  129. stock Account_GetIP(playerid)
  130. {
  131. return AccountIP[playerid];
  132. }
  133. stock Account_SetIP(playerid, const ip[])
  134. {
  135. format(AccountIP[playerid], MAX_PLAYER_IP, ip);
  136. }
  137. stock Account_GetGPCI(playerid)
  138. {
  139. return AccountGPCI[playerid];
  140. }
  141. stock Account_SetGPCI(playerid, const gpci[])
  142. {
  143. format(AccountGPCI[playerid], MAX_PLAYER_GPCI, gpci);
  144. }
  145. stock bool:IsAccountLoggedIn(playerid)
  146. {
  147. if(LoggedIn[playerid])
  148. {
  149. return true;
  150. }
  151. return false;
  152. }
  153. stock Account_SetLoggedIn(playerid, bool:status)
  154. {
  155. LoggedIn[playerid] = status;
  156. }