You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

272 line
6.8 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4. using UnityEngine;
  5. namespace SUISS.Cloud
  6. {
  7. public class OAuthRequest : CustomYieldInstruction
  8. {
  9. private OAuthRequest(string endPoint, string username, string password, string scope)
  10. {
  11. if (this.IsLocked(endPoint))
  12. {
  13. this._request = OAuthRequest._busy[endPoint];
  14. }
  15. else
  16. {
  17. string value = "password";
  18. WWWForm wwwform = new WWWForm();
  19. wwwform.AddField("scope", scope);
  20. wwwform.AddField("grant_type", value);
  21. wwwform.AddField("password", password);
  22. wwwform.AddField("username", username);
  23. Dictionary<string, string> headers = new Dictionary<string, string>
  24. {
  25. {
  26. "Content-Type",
  27. "application/x-www-form-urlencoded"
  28. }
  29. };
  30. this._request = new WWW(endPoint, wwwform.data, headers);
  31. OAuthRequest._busy[endPoint] = this._request;
  32. }
  33. }
  34. private OAuthRequest(string endPoint, string refreshToken, string scope)
  35. {
  36. if (this.IsLocked(endPoint))
  37. {
  38. this._request = OAuthRequest._busy[endPoint];
  39. }
  40. else
  41. {
  42. string value = "refresh_token";
  43. WWWForm wwwform = new WWWForm();
  44. wwwform.AddField("scope", scope);
  45. wwwform.AddField("grant_type", value);
  46. wwwform.AddField("refresh_token", refreshToken);
  47. Dictionary<string, string> headers = new Dictionary<string, string>
  48. {
  49. {
  50. "Content-Type",
  51. "application/x-www-form-urlencoded"
  52. }
  53. };
  54. this._request = new WWW(endPoint, wwwform.data, headers);
  55. OAuthRequest._busy[endPoint] = this._request;
  56. }
  57. }
  58. public static OAuthRequest LoginPassword(string endPoint, string username, string password, string scope)
  59. {
  60. return new OAuthRequest(endPoint, username, password, scope);
  61. }
  62. public static OAuthRequest LoginRefreshToken(string endPoint, string refreshToken, string scope)
  63. {
  64. return new OAuthRequest(endPoint, refreshToken, scope);
  65. }
  66. public bool IsDone
  67. {
  68. get
  69. {
  70. this.ProcessResults();
  71. return this._resultsProcessed;
  72. }
  73. }
  74. public OAuthRequest.OAuthResponse Result
  75. {
  76. get
  77. {
  78. this.ProcessResults();
  79. return this._result;
  80. }
  81. }
  82. public ApiError Error
  83. {
  84. get
  85. {
  86. this.ProcessResults();
  87. return this._error;
  88. }
  89. }
  90. public int HttpStatus
  91. {
  92. get
  93. {
  94. if (this._request.isDone)
  95. {
  96. if (this._request.responseHeaders.ContainsKey("STATUS"))
  97. {
  98. Match match = Regex.Match(this._request.responseHeaders["STATUS"], "HTTP\\/\\d.\\d\\s(\\d{1,3})");
  99. if (match.Success)
  100. {
  101. return int.Parse(match.Groups[1].Value);
  102. }
  103. }
  104. return 0;
  105. }
  106. return -1;
  107. }
  108. }
  109. public override bool keepWaiting
  110. {
  111. get
  112. {
  113. return !this._request.isDone;
  114. }
  115. }
  116. private bool IsLocked(string endPoint)
  117. {
  118. return OAuthRequest._busy.ContainsKey(endPoint) && !OAuthRequest._busy[endPoint].isDone;
  119. }
  120. private void ProcessResults()
  121. {
  122. if (this._request.isDone && !this._resultsProcessed)
  123. {
  124. int httpStatus = this.HttpStatus;
  125. if (httpStatus <= 0)
  126. {
  127. this._error = new ApiError
  128. {
  129. Status = 0,
  130. Error = (this._request.error ?? "unknown error"),
  131. ErrorCode = 0,
  132. ServerError = false,
  133. NoInternet = true
  134. };
  135. }
  136. else if (200 <= httpStatus && httpStatus < 300 && !string.IsNullOrEmpty(this._request.text))
  137. {
  138. bool flag = false;
  139. object json = Json.Decode(this._request.text, ref flag);
  140. if (!flag)
  141. {
  142. this._error = new ApiError
  143. {
  144. Status = 0,
  145. Error = "server returned invalid json",
  146. ErrorCode = 0,
  147. ServerError = true,
  148. NoInternet = false
  149. };
  150. }
  151. else if (this.GetJsonString(json, "token_type", string.Empty) == "bearer")
  152. {
  153. string jsonString = this.GetJsonString(json, "access_token", string.Empty);
  154. string jsonString2 = this.GetJsonString(json, "refresh_token", string.Empty);
  155. string jsonString3 = this.GetJsonString(json, "scope", string.Empty);
  156. DateTime expires = DateTime.UtcNow.AddSeconds((double)this.GetJsonInt(json, "expires_in", 0));
  157. this._result = new OAuthRequest.OAuthResponse
  158. {
  159. AccessToken = new AccessToken(jsonString, jsonString3, expires),
  160. RefreshToken = jsonString2,
  161. Scope = jsonString3
  162. };
  163. }
  164. else
  165. {
  166. this._error = new ApiError
  167. {
  168. Status = httpStatus,
  169. Error = "server returned unknown token type.",
  170. ErrorCode = 0,
  171. ServerError = true,
  172. NoInternet = false
  173. };
  174. }
  175. }
  176. else
  177. {
  178. bool flag2 = false;
  179. object obj = Json.Decode(this._request.text, ref flag2);
  180. if (flag2)
  181. {
  182. Dictionary<string, object> dictionary = obj as Dictionary<string, object>;
  183. if (dictionary != null && dictionary.ContainsKey("code") && dictionary.ContainsKey("message") && dictionary.ContainsKey("error"))
  184. {
  185. this._error = new ApiError
  186. {
  187. Status = this.GetJsonInt(obj, "code", 0),
  188. Error = this.GetJsonString(obj, "message", string.Empty),
  189. ErrorCode = this.GetJsonInt(obj, "error", 0),
  190. ServerError = false,
  191. NoInternet = false
  192. };
  193. }
  194. else
  195. {
  196. flag2 = false;
  197. }
  198. }
  199. if (!flag2)
  200. {
  201. this._error = new ApiError
  202. {
  203. Status = httpStatus,
  204. Error = (string.IsNullOrEmpty(this._request.text) ? "server returned nothing" : this._request.text),
  205. ErrorCode = 0,
  206. ServerError = true,
  207. NoInternet = false
  208. };
  209. }
  210. }
  211. this._resultsProcessed = true;
  212. }
  213. }
  214. private string GetJsonString(object json, string key, string defaultValue)
  215. {
  216. if (json is Dictionary<string, object>)
  217. {
  218. Dictionary<string, object> dictionary = (Dictionary<string, object>)json;
  219. if (dictionary.ContainsKey(key) && dictionary[key] is string)
  220. {
  221. return (string)dictionary[key];
  222. }
  223. }
  224. return defaultValue;
  225. }
  226. private int GetJsonInt(object json, string key, int defaultValue)
  227. {
  228. if (json is Dictionary<string, object>)
  229. {
  230. Dictionary<string, object> dictionary = (Dictionary<string, object>)json;
  231. if (dictionary.ContainsKey(key) && dictionary[key] is double)
  232. {
  233. return (int)((double)dictionary[key]);
  234. }
  235. }
  236. return defaultValue;
  237. }
  238. private static Dictionary<string, WWW> _busy = new Dictionary<string, WWW>();
  239. private WWW _request;
  240. private ApiError _error;
  241. private OAuthRequest.OAuthResponse _result;
  242. private bool _resultsProcessed;
  243. public class OAuthResponse
  244. {
  245. public AccessToken AccessToken { get; set; }
  246. public string RefreshToken { get; set; }
  247. public string Scope { get; set; }
  248. }
  249. }
  250. }