GameMenuHandler.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using AppleAuth.Enums;
  2. using AppleAuth.Extensions;
  3. using AppleAuth.Interfaces;
  4. using Newtonsoft.Json;
  5. using System;
  6. using System.Collections;
  7. using System.Text;
  8. using UnityEngine;
  9. using UnityEngine.Networking;
  10. using UnityEngine.UI;
  11. [Serializable]
  12. public class GameMenuHandler
  13. {
  14. public GameObject Parent;
  15. public Text AppleUserIdLabel;
  16. public Text AppleUserCredentialLabel;
  17. public void SetVisible(bool visible)
  18. {
  19. this.Parent.SetActive(visible);
  20. }
  21. public void SetupAppleData(string appleUserId, ICredential receivedCredential)
  22. {
  23. this.AppleUserIdLabel.text = "Apple User ID: " + appleUserId;
  24. if (receivedCredential == null)
  25. {
  26. this.AppleUserCredentialLabel.text = "NO CREDENTIALS RECEIVED\nProbably credential status for " + appleUserId + "was Authorized";
  27. return;
  28. }
  29. var appleIdCredential = receivedCredential as IAppleIDCredential;
  30. var passwordCredential = receivedCredential as IPasswordCredential;
  31. if (appleIdCredential != null)
  32. {
  33. var stringBuilder = new StringBuilder();
  34. stringBuilder.AppendLine("RECEIVED APPLE ID CREDENTIAL.\nYOU CAN LOGIN/CREATE A USER WITH THIS");
  35. stringBuilder.AppendLine("<b>Username:</b> " + appleIdCredential.User);
  36. stringBuilder.AppendLine("<b>Real user status:</b> " + appleIdCredential.RealUserStatus.ToString());
  37. if (appleIdCredential.State != null)
  38. stringBuilder.AppendLine("<b>State:</b> " + appleIdCredential.State);
  39. if (appleIdCredential.IdentityToken != null)
  40. {
  41. var identityToken = Encoding.UTF8.GetString(appleIdCredential.IdentityToken, 0, appleIdCredential.IdentityToken.Length);
  42. stringBuilder.AppendLine("<b>Identity token (" + appleIdCredential.IdentityToken.Length + " bytes)</b>");
  43. stringBuilder.AppendLine(identityToken.Substring(0, 45) + "...");
  44. }
  45. if (appleIdCredential.AuthorizationCode != null)
  46. {
  47. var authorizationCode = Encoding.UTF8.GetString(appleIdCredential.AuthorizationCode, 0, appleIdCredential.AuthorizationCode.Length);
  48. stringBuilder.AppendLine("<b>Authorization Code (" + appleIdCredential.AuthorizationCode.Length + " bytes)</b>");
  49. stringBuilder.AppendLine(authorizationCode.Substring(0, 45) + "...");
  50. }
  51. if (appleIdCredential.AuthorizedScopes != null)
  52. stringBuilder.AppendLine("<b>Authorized Scopes:</b> " + string.Join(", ", appleIdCredential.AuthorizedScopes));
  53. if (appleIdCredential.Email != null)
  54. {
  55. stringBuilder.AppendLine();
  56. stringBuilder.AppendLine("<b>EMAIL RECEIVED: YOU WILL ONLY SEE THIS ONCE PER SIGN UP. SEND THIS INFORMATION TO YOUR BACKEND!</b>");
  57. stringBuilder.AppendLine("<b>You can test this again by revoking credentials in Settings</b>");
  58. stringBuilder.AppendLine("<b>Email:</b> " + appleIdCredential.Email);
  59. }
  60. if (appleIdCredential.FullName != null)
  61. {
  62. var fullName = appleIdCredential.FullName;
  63. stringBuilder.AppendLine();
  64. stringBuilder.AppendLine("<b>NAME RECEIVED: YOU WILL ONLY SEE THIS ONCE PER SIGN UP. SEND THIS INFORMATION TO YOUR BACKEND!</b>");
  65. stringBuilder.AppendLine("<b>You can test this again by revoking credentials in Settings</b>");
  66. stringBuilder.AppendLine("<b>Name:</b> " + fullName.ToLocalizedString());
  67. stringBuilder.AppendLine("<b>Name (Short):</b> " + fullName.ToLocalizedString(PersonNameFormatterStyle.Short));
  68. stringBuilder.AppendLine("<b>Name (Medium):</b> " + fullName.ToLocalizedString(PersonNameFormatterStyle.Medium));
  69. stringBuilder.AppendLine("<b>Name (Long):</b> " + fullName.ToLocalizedString(PersonNameFormatterStyle.Long));
  70. stringBuilder.AppendLine("<b>Name (Abbreviated):</b> " + fullName.ToLocalizedString(PersonNameFormatterStyle.Abbreviated));
  71. if (appleIdCredential.FullName.PhoneticRepresentation != null)
  72. {
  73. var phoneticName = appleIdCredential.FullName.PhoneticRepresentation;
  74. stringBuilder.AppendLine("<b>Phonetic name:</b> " + phoneticName.ToLocalizedString());
  75. stringBuilder.AppendLine("<b>Phonetic name (Short):</b> " + phoneticName.ToLocalizedString(PersonNameFormatterStyle.Short));
  76. stringBuilder.AppendLine("<b>Phonetic name (Medium):</b> " + phoneticName.ToLocalizedString(PersonNameFormatterStyle.Medium));
  77. stringBuilder.AppendLine("<b>Phonetic name (Long):</b> " + phoneticName.ToLocalizedString(PersonNameFormatterStyle.Long));
  78. stringBuilder.AppendLine("<b>Phonetic name (Abbreviated):</b> " + phoneticName.ToLocalizedString(PersonNameFormatterStyle.Abbreviated));
  79. }
  80. }
  81. this.AppleUserCredentialLabel.text = stringBuilder.ToString();
  82. }
  83. else if (passwordCredential != null)
  84. {
  85. var stringBuilder = new StringBuilder();
  86. stringBuilder.AppendLine("USERNAME/PASSWORD RECEIVED (iCloud?)");
  87. stringBuilder.AppendLine("<b>Username:</b> " + passwordCredential.User);
  88. stringBuilder.AppendLine("<b>Password:</b> " + passwordCredential.Password);
  89. this.AppleUserCredentialLabel.text = stringBuilder.ToString();
  90. }
  91. else
  92. {
  93. this.AppleUserCredentialLabel.text = "Unknown credentials for user " + receivedCredential.User;
  94. }
  95. }
  96. public IEnumerator LoginByApple(string identityToken,string email,string fullName, Action<RequestResult> callback)
  97. {
  98. string url = "http://192.168.0.112:11433/SmartBowBusinessServer/gameLogin/loginByApple";
  99. WWWForm form = new WWWForm();
  100. form.AddField("identityToken", identityToken);
  101. form.AddField("email", email);
  102. form.AddField("fullName", fullName);
  103. form.AddField("serverIndex", 0);
  104. using (UnityWebRequest request = UnityWebRequest.Post(url, form))
  105. {
  106. request.timeout = 10;
  107. yield return request.SendWebRequest();
  108. RequestResult requestResult = new RequestResult();
  109. if (request.result == UnityWebRequest.Result.Success)
  110. {
  111. requestResult = JsonConvert.DeserializeObject<RequestResult>(request.downloadHandler.text);
  112. }
  113. if (callback != null) callback(requestResult);
  114. }
  115. }
  116. public class RequestResult
  117. {
  118. public int code = -9999;
  119. public object data;
  120. public string msg;
  121. }
  122. }