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