Register.cs 4.7 KB

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