Register.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using UnityEngine.Networking;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.SceneManagement;
  7. using System.Security.Cryptography;
  8. using System.Text;
  9. using System;
  10. public class Register : MonoBehaviour {
  11. public GameObject username;
  12. public GameObject password;
  13. public GameObject email;
  14. public Text errorText;
  15. public Button registerButton;
  16. private string Username;
  17. private string Password;
  18. private string Email;
  19. private string form;
  20. string salt;
  21. private string registerUserUrl = "http://nordh.xyz/narKampen/dbFiles/Register.php?";
  22. private Color errorColor;
  23. private EventSystem system;
  24. // Start is called before the first frame update
  25. void Start() {
  26. registerButton.onClick.AddListener(RegisterAction);
  27. system = EventSystem.current;
  28. errorColor = errorText.color;
  29. }
  30. // Update is called once per frame
  31. void Update() {
  32. Username = username.GetComponent<InputField>().text;
  33. Password = password.GetComponent<InputField>().text;
  34. Email = email.GetComponent<InputField>().text;
  35. if (Input.GetKeyDown(KeyCode.Tab)) {
  36. Selectable next = system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnDown();
  37. if (next != null) {
  38. InputField inputfield = next.GetComponent<InputField>();
  39. if (inputfield != null) inputfield.OnPointerClick(new PointerEventData(system)); //if it's an input field, also set the text caret
  40. system.SetSelectedGameObject(next.gameObject, new BaseEventData(system));
  41. }
  42. }
  43. }
  44. void RegisterAction() {
  45. string errorMessage = "";
  46. if (Username == "" || Username.Length < 3) {
  47. errorMessage = LocalizationManager.Instance.GetText("REGISTER_ERROR_USERNAME_EMPTY");
  48. }
  49. if (Password == "") {
  50. if (errorMessage != "") {
  51. errorMessage += "\n";
  52. }
  53. errorMessage += LocalizationManager.Instance.GetText("REGISTER_ERROR_PASSWORD_EMPTY");
  54. }
  55. if (Password.Length < 6) {
  56. if (errorMessage != "") {
  57. errorMessage += "\n";
  58. }
  59. errorMessage += LocalizationManager.Instance.GetText("REGISTER_ERROR_PASSWORD_TO_SHORT");
  60. }
  61. if (Email == "") {
  62. if (errorMessage != "") {
  63. errorMessage += "\n";
  64. }
  65. errorMessage += LocalizationManager.Instance.GetText("REGISTER_ERROR_EMAIL_EMPTY");
  66. }
  67. if (errorMessage != "") {
  68. errorText.text = errorMessage;
  69. errorColor.a = 1;
  70. } else {
  71. errorColor.a = 0;
  72. StartCoroutine(RegisterUser());
  73. }
  74. errorText.color = errorColor;
  75. }
  76. IEnumerator RegisterUser() {
  77. // TODO register user at server, check for already in use for username and email.
  78. // TODO - Save OS and device id
  79. string deviceId = SystemInfo.deviceUniqueIdentifier;
  80. string os = SystemInfo.operatingSystem;
  81. string salt = GetSalt();
  82. string password = getPassword();
  83. string postUrl = registerUserUrl + "name=" + UnityWebRequest.EscapeURL(Username) + "&password=" + UnityWebRequest.EscapeURL(password) + "&email=" + UnityWebRequest.EscapeURL(Email) + "&s=" + UnityWebRequest.EscapeURL(salt) + "&did=" + deviceId + "&os=" + UnityWebRequest.EscapeURL(os);
  84. UnityWebRequest www = UnityWebRequest.Get(postUrl);
  85. yield return www.SendWebRequest();
  86. if (www.error != null) {
  87. errorText.text = LocalizationManager.Instance.GetText("REGISTER_ERROR_CONNECTION_FAIL");
  88. errorColor.a = 1;
  89. errorText.color = errorColor;
  90. } else {
  91. string result = www.downloadHandler.text;
  92. if (int.TryParse(www.downloadHandler.text, out int userId)) {
  93. errorColor.a = 0;
  94. errorText.color = errorColor;
  95. Database.Instance.KeepSignedIn(Username, userId, false);
  96. SceneManager.LoadScene("MainMenu");
  97. } else {
  98. errorText.text = LocalizationManager.Instance.GetText("REGISTER_ERROR_USER_PASS_ALREADY_IN_USE");
  99. errorColor.a = 1;
  100. errorText.color = errorColor;
  101. }
  102. }
  103. }
  104. private string getPassword() {
  105. byte[] pass;
  106. salt = GetSalt();
  107. byte[] pwd = Encoding.UTF8.GetBytes(salt + Password);
  108. SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
  109. pass = sha1.ComputeHash(pwd);
  110. return Convert.ToBase64String(pass);
  111. }
  112. private string GetSalt() {
  113. if (this.salt == null) {
  114. RNGCryptoServiceProvider rnd = new RNGCryptoServiceProvider();
  115. int saltLength = 32;
  116. byte[] slt = new byte[saltLength];
  117. rnd.GetNonZeroBytes(slt);
  118. this.salt = Convert.ToBase64String(slt);
  119. }
  120. return this.salt;
  121. }
  122. }