GoogleSignIn.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // <copyright file="GoogleSignIn.cs" company="Google Inc.">
  2. // Copyright (C) 2017 Google Inc. All Rights Reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. // </copyright>
  16. namespace Google {
  17. using System;
  18. using System.Runtime.Serialization;
  19. using System.Threading.Tasks;
  20. using Google.Impl;
  21. using UnityEngine;
  22. /// <summary>
  23. /// Google sign in API.
  24. /// </summary>
  25. /// <remarks>This class implements the GoogleSignInAPI for Unity.
  26. /// Typical usage is to set the Configuration options as needed, then
  27. /// get the DefaultInstance and call signIn or signInSilently. See
  28. /// the <a href="https://developers.google.com/identity">
  29. /// Google Sign-In API documentation for more details.</a>
  30. /// <para>
  31. /// <code>
  32. /// private static readonly GoogleSignInConfiguration configuration =
  33. /// new GoogleSignInConfiguration {
  34. /// WebClientId = "<your client id here >",
  35. /// RequestIdToken = true
  36. /// };
  37. ///
  38. /// public void OnSignIn() {
  39. /// GoogleSignIn.Configuration = configuration;
  40. /// GoogleSignIn.Configuration.UseGameSignIn = false;
  41. /// GoogleSignIn.Configuration.RequestIdToken = true;
  42. /// GoogleSignIn.DefaultInstance.SignIn().ContinueWith(
  43. /// OnAuthenticationFinished);
  44. /// }
  45. /// </code>
  46. /// </para>
  47. /// </remarks>
  48. public class GoogleSignIn {
  49. #if !UNITY_ANDROID && !UNITY_IOS
  50. static GoogleSignIn() {
  51. Debug.LogError("This platform is not supported");
  52. }
  53. #endif
  54. private static GoogleSignIn theInstance = null;
  55. private static GoogleSignInConfiguration theConfiguration = null;
  56. private ISignInImpl impl;
  57. ///<summary> The configuration settings for Google Sign-in.</summary>
  58. ///<remarks> The configuration should be set before calling the sign-in
  59. /// methods. Once the configuration is set it cannot be changed.
  60. ///</remarks>
  61. public static GoogleSignInConfiguration Configuration {
  62. set {
  63. // Can set the configuration until the singleton is created.
  64. if (theInstance == null || theConfiguration == value || theConfiguration == null) {
  65. theConfiguration = value;
  66. } else {
  67. throw new SignInException(GoogleSignInStatusCode.DeveloperError,
  68. "DefaultInstance already created. " +
  69. " Cannot change configuration after creation.");
  70. }
  71. }
  72. get {
  73. return theConfiguration;
  74. }
  75. }
  76. /// <summary>
  77. /// Singleton instance of this class.
  78. /// </summary>
  79. /// <value>The instance.</value>
  80. public static GoogleSignIn DefaultInstance {
  81. get {
  82. if (theInstance == null) {
  83. #if UNITY_ANDROID || UNITY_IOS
  84. theInstance = new GoogleSignIn(new GoogleSignInImpl(Configuration));
  85. #else
  86. theInstance = new GoogleSignIn(null);
  87. throw new SignInException(
  88. GoogleSignInStatusCode.DeveloperError,
  89. "This platform is not supported by GoogleSignIn");
  90. #endif
  91. }
  92. return theInstance;
  93. }
  94. }
  95. internal GoogleSignIn(GoogleSignInImpl impl) {
  96. this.impl = impl;
  97. }
  98. public void EnableDebugLogging(bool flag) {
  99. impl.EnableDebugLogging(flag);
  100. }
  101. /// <summary>Starts the authentication process.</summary>
  102. /// <remarks>
  103. /// The authenication process is started and may display account picker
  104. /// popups and consent prompts based on the state of authentication and
  105. /// the requested elements.
  106. /// </remarks>
  107. public Task<GoogleSignInUser> SignIn() {
  108. var tcs = new TaskCompletionSource<GoogleSignInUser>();
  109. SignInHelperObject.Instance.StartCoroutine(
  110. impl.SignIn().WaitForResult(tcs));
  111. return tcs.Task;
  112. }
  113. /// <summary>Starts the silent authentication process.</summary>
  114. /// <remarks>
  115. /// The authenication process is started and will attempt to sign in without
  116. /// displaying any UI. If this cannot be done, the developer should call
  117. /// SignIn().
  118. /// </remarks>
  119. public Task<GoogleSignInUser> SignInSilently() {
  120. var tcs = new TaskCompletionSource<GoogleSignInUser>();
  121. SignInHelperObject.Instance.StartCoroutine(
  122. impl.SignInSilently().WaitForResult(tcs));
  123. return tcs.Task;
  124. }
  125. /// <summary>
  126. /// Signs out the User.
  127. /// </summary>
  128. /// <remarks>Future sign-in attempts will require the user to select the
  129. /// account to use when signing in.
  130. /// </remarks>
  131. public void SignOut() {
  132. theConfiguration = null;
  133. impl.SignOut();
  134. }
  135. /// <summary>
  136. /// Disconnect this instance.
  137. /// </summary>
  138. /// <remarks>When the user is disconnected, it revokes all access that may
  139. /// have been granted to this application. This includes any server side
  140. /// access tokens derived from server auth codes. As a result, future
  141. /// sign-in attempts will require the user to re-consent to the requested
  142. /// scopes.
  143. /// </remarks>
  144. public void Disconnect() {
  145. impl.Disconnect();
  146. }
  147. /// <summary>
  148. /// Sign in exception. This is a checked exception for handling specific
  149. /// errors during the sign-in process.
  150. /// </summary>
  151. [Serializable]
  152. public class SignInException : Exception {
  153. internal SignInException(GoogleSignInStatusCode status) {
  154. Status = status;
  155. }
  156. public SignInException(GoogleSignInStatusCode status, string message) :
  157. base(message) {
  158. Status = status;
  159. }
  160. public SignInException(GoogleSignInStatusCode status, string message,
  161. Exception innerException) : base(message, innerException) {
  162. Status = status;
  163. }
  164. protected SignInException(GoogleSignInStatusCode status,
  165. SerializationInfo info,
  166. StreamingContext context) :
  167. base(info, context) {
  168. Status = status;
  169. }
  170. public GoogleSignInStatusCode Status {
  171. get;
  172. internal set;
  173. }
  174. }
  175. }
  176. internal interface ISignInImpl {
  177. Future<GoogleSignInUser> SignIn();
  178. Future<GoogleSignInUser> SignInSilently();
  179. void EnableDebugLogging(bool flag);
  180. void SignOut();
  181. void Disconnect();
  182. }
  183. } // namespace Google