Register.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. // TODO Localizaion!
  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. byte[] pwd = Encoding.UTF8.GetBytes(salt + Password);
  83. SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
  84. byte[] pass = sha1.ComputeHash(pwd);
  85. 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);
  86. Debug.Log(postUrl);
  87. UnityWebRequest www = UnityWebRequest.Get(postUrl);
  88. yield return www.SendWebRequest();
  89. if (www.error != null) {
  90. errorText.text = LocalizationManager.Instance.GetText("REGISTER_ERROR_CONNECTION_FAIL");
  91. errorColor.a = 1;
  92. errorText.color = errorColor;
  93. } else {
  94. string result = www.downloadHandler.text;
  95. if (int.TryParse(www.downloadHandler.text, out int userId)) {
  96. errorColor.a = 0;
  97. errorText.color = errorColor;
  98. Database.Instance.KeepSignedIn(Username, userId, false);
  99. SceneManager.LoadScene("MainMenu");
  100. } else {
  101. errorText.text = LocalizationManager.Instance.GetText("REGISTER_ERROR_USER_PASS_ALREADY_IN_USE");
  102. errorColor.a = 1;
  103. errorText.color = errorColor;
  104. }
  105. }
  106. }
  107. private string GetSalt() {
  108. RNGCryptoServiceProvider rnd = new RNGCryptoServiceProvider();
  109. int saltLength = 32;
  110. byte[] salt = new byte[saltLength];
  111. rnd.GetNonZeroBytes(salt);
  112. return Convert.ToBase64String(salt);
  113. }
  114. }