No Description
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.

UniWebViewAuthenticationFlowFacebook.cs 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // UniWebViewAuthenticationFlowFacebook.cs
  3. // Created by Wang Wei (@onevcat) on 2022-06-25.
  4. //
  5. // This file is a part of UniWebView Project (https://uniwebview.com)
  6. // By purchasing the asset, you are allowed to use this code in as many as projects
  7. // you want, only if you publish the final products under the name of the same account
  8. // used for the purchase.
  9. //
  10. // This asset and all corresponding files (such as source code) are provided on an
  11. // “as is” basis, without warranty of any kind, express of implied, including but not
  12. // limited to the warranties of merchantability, fitness for a particular purpose, and
  13. // noninfringement. In no event shall the authors or copyright holders be liable for any
  14. // claim, damages or other liability, whether in action of contract, tort or otherwise,
  15. // arising from, out of or in connection with the software or the use of other dealing in the software.
  16. //
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Collections.Specialized;
  20. using UnityEngine;
  21. using UnityEngine.Events;
  22. /// <summary>
  23. /// A predefined authentication flow for Facebook Login.
  24. ///
  25. /// It is not a standard OAuth2 flow, and using a plain web view. There once was a policy that Facebook did not allow
  26. /// any third-party customize authentication flow other than using their official SDK. Recently Facebook started to provide
  27. /// a so-called manual flow way to perform authentication. But it is originally only for Desktop apps, it is not stable
  28. /// and not standard.
  29. ///
  30. /// Facebook suggests "For mobile apps, use the Facebook SDKs for iOS and Android, and follow the separate guides for
  31. /// these platforms." So on mobile, use this class with your own risk since it might be invalidated or forbidden by
  32. /// Facebook in the future.
  33. ///
  34. /// This implementation is based on the manual flow described in the following document:
  35. /// https://developers.facebook.com/docs/facebook-login/guides/advanced/manual-flow
  36. ///
  37. /// See https://docs.uniwebview.com/guide/oauth2.html for a more detailed guide of authentication in UniWebView.
  38. /// </summary>
  39. public class UniWebViewAuthenticationFlowFacebook: UniWebViewAuthenticationCommonFlow {
  40. /// <summary>
  41. /// The App ID of your Facebook application
  42. /// </summary>
  43. public string appId = "";
  44. /// <summary>
  45. /// Optional to control this flow's behaviour.
  46. /// </summary>
  47. public UniWebViewAuthenticationFlowFacebookOptional optional;
  48. // The redirect URL should be exactly this one. Web view should inspect the loading of this URL to handle the result.
  49. private string redirectUri = "https://www.facebook.com/connect/login_success.html";
  50. // Only `token` response type is supported to use Facebook Login as the manual flow.
  51. private string responseType = "token";
  52. [field: SerializeField]
  53. public UnityEvent<UniWebViewAuthenticationFacebookToken> OnAuthenticationFinished { get; set; }
  54. [field: SerializeField]
  55. public UnityEvent<long, string> OnAuthenticationErrored { get; set; }
  56. private readonly UniWebViewAuthenticationConfiguration config =
  57. new UniWebViewAuthenticationConfiguration(
  58. "https://www.facebook.com/v14.0/dialog/oauth",
  59. // This `access_token` entry point is in fact not used in current auth model.
  60. "https://graph.facebook.com/v14.0/oauth/access_token"
  61. );
  62. /// <summary>
  63. /// Starts the authentication flow.
  64. ///
  65. /// This flow is executed in a customized web view and it is not a standard OAuth2 flow.
  66. /// </summary>
  67. public override void StartAuthenticationFlow() {
  68. var webView = gameObject.AddComponent<UniWebView>();
  69. // Facebook login deprecates the Web View login on Android. As a workaround, prevents to be a desktop browser to continue the manual flow.
  70. #if UNITY_ANDROID && !UNITY_EDITOR
  71. webView.SetUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 13_0_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15");
  72. #endif
  73. webView.OnPageFinished += (view, status, url) => {
  74. if (status != 200) {
  75. if (OnAuthenticationErrored != null) {
  76. OnAuthenticationErrored.Invoke(status, "Error while loading auth page.");
  77. }
  78. webView.Hide(false, UniWebViewTransitionEdge.Bottom, 0.25f, () => {
  79. Destroy(webView);
  80. });
  81. return;
  82. }
  83. if (url.StartsWith(redirectUri)) {
  84. UniWebViewLogger.Instance.Info("Received redirect url: " + url);
  85. var uri = new Uri(url);
  86. var response = UniWebViewAuthenticationUtils.ParseFormUrlEncodedString(uri.Fragment);
  87. try {
  88. VerifyState(response);
  89. var token = new UniWebViewAuthenticationFacebookToken(url, response);
  90. if (OnAuthenticationFinished != null) {
  91. OnAuthenticationFinished.Invoke(token);
  92. }
  93. }
  94. catch (Exception e) {
  95. var message = e.Message;
  96. var code = -1;
  97. if (e is AuthenticationResponseException ex) {
  98. code = ex.Code;
  99. }
  100. UniWebViewLogger.Instance.Critical("Exception on parsing response: " + e + ". Code: " + code +
  101. ". Message: " + message);
  102. if (OnAuthenticationErrored != null) {
  103. OnAuthenticationErrored.Invoke(code, message);
  104. }
  105. }
  106. finally {
  107. webView.Hide(false, UniWebViewTransitionEdge.Bottom, 0.25f, () => {
  108. Destroy(webView);
  109. });
  110. }
  111. }
  112. };
  113. webView.OnPageErrorReceived += (view, code, message) => {
  114. if (OnAuthenticationErrored != null) {
  115. OnAuthenticationErrored.Invoke(code, message);
  116. }
  117. };
  118. webView.Frame = new Rect(0, 0, Screen.width, Screen.height);
  119. webView.Load(GetAuthUrl());
  120. webView.SetShowToolbar(true, adjustInset: true);
  121. webView.Show(false, UniWebViewTransitionEdge.Bottom, 0.25f);
  122. }
  123. /// <summary>
  124. /// Starts the refresh flow with the standard OAuth 2.0.
  125. /// This implements the abstract method in `UniWebViewAuthenticationCommonFlow`.
  126. /// </summary>
  127. /// <param name="refreshToken">The refresh token received with a previous access token response.</param>
  128. public override void StartRefreshTokenFlow(string refreshToken) {
  129. Debug.LogError("Facebook does not provide a refresh token flow when building with the manual flow.");
  130. throw new NotImplementedException();
  131. }
  132. private string GetAuthUrl() {
  133. var builder = new UriBuilder(config.authorizationEndpoint);
  134. var query = new NameValueCollection();
  135. foreach (var kv in GetAuthenticationUriArguments()) {
  136. query.Add(kv.Key, kv.Value);
  137. }
  138. builder.Query = UniWebViewAuthenticationUtils.CreateQueryString(query);
  139. return builder.ToString();
  140. }
  141. private Dictionary<string, string> GetAuthenticationUriArguments() {
  142. var state = GenerateAndStoreState();
  143. var authorizeArgs = new Dictionary<string, string> {
  144. { "client_id", appId },
  145. { "redirect_uri", redirectUri },
  146. { "state", state},
  147. { "response_type", responseType }
  148. };
  149. if (optional != null) {
  150. if (!String.IsNullOrEmpty(optional.scope)) {
  151. authorizeArgs.Add("scope", optional.scope);
  152. }
  153. }
  154. return authorizeArgs;
  155. }
  156. }
  157. /// <summary>
  158. /// The authentication flow's optional settings for Facebook.
  159. /// </summary>
  160. [Serializable]
  161. public class UniWebViewAuthenticationFlowFacebookOptional {
  162. /// <summary>
  163. /// The scope string of all your required scopes.
  164. /// </summary>
  165. public string scope = "";
  166. }
  167. /// The token object from Facebook.
  168. public class UniWebViewAuthenticationFacebookToken {
  169. /// <summary>
  170. /// The access token received from Facebook Login.
  171. /// </summary>
  172. public string AccessToken { get; }
  173. /// <summary>
  174. /// The expiration duration that your app can access requested items of user data.
  175. /// </summary>
  176. public long DataAccessExpirationTime { get; }
  177. /// <summary>
  178. /// The expiration duration that the access token is valid for authentication purpose.
  179. /// </summary>
  180. public long ExpiresIn { get; }
  181. /// <summary>
  182. /// The raw value of the response of the exchange token request.
  183. /// If the predefined fields are not enough, you can parse the raw value to get the extra information.
  184. /// </summary>
  185. public string RawValue { get; }
  186. public UniWebViewAuthenticationFacebookToken(string response, Dictionary<string, string> values) {
  187. RawValue = response;
  188. AccessToken = values.ContainsKey("access_token") ? values["access_token"] as string : null ;
  189. if (AccessToken == null) {
  190. throw AuthenticationResponseException.InvalidResponse(response);
  191. }
  192. DataAccessExpirationTime = values.ContainsKey("data_access_expiration_time") ? long.Parse(values["data_access_expiration_time"]) : 0;
  193. ExpiresIn = values.ContainsKey("expires_in") ? long.Parse(values["expires_in"]) : 0;
  194. }
  195. }