-
Notifications
You must be signed in to change notification settings - Fork 254
/
Copy pathGoogleSignIn.mm
346 lines (304 loc) · 10.1 KB
/
GoogleSignIn.mm
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/**
* Copyright 2017 Google Inc.
*
* 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.
*/
#import "GoogleSignIn.h"
#import <GoogleSignIn/GIDAuthentication.h>
#import <GoogleSignIn/GIDGoogleUser.h>
#import <GoogleSignIn/GIDProfileData.h>
#import <GoogleSignIn/GIDSignIn.h>
#import <memory>
// These values are in the Unity plugin code. The iOS specific
// codes are mapped to these.
static const int kStatusCodeSuccessCached = -1;
static const int kStatusCodeSuccess = 0;
static const int kStatusCodeApiNotConnected = 1;
static const int kStatusCodeCanceled = 2;
static const int kStatusCodeInterrupted = 3;
static const int kStatusCodeInvalidAccount = 4;
static const int kStatusCodeTimeout = 5;
static const int kStatusCodeDeveloperError = 6;
static const int kStatusCodeInternalError = 7;
static const int kStatusCodeNetworkError = 8;
static const int kStatusCodeError = 9;
/**
* Helper method to pause the Unity player. This is done when showing any UI.
*/
void UnpauseUnityPlayer() {
dispatch_async(dispatch_get_main_queue(), ^{
if (UnityIsPaused() > 0) {
UnityPause(0);
}
});
}
// result for pending operation. Access to this should be protected using the
// resultLock.
struct SignInResult {
int result_code;
bool finished;
};
std::unique_ptr<SignInResult> currentResult_;
NSRecursiveLock *resultLock = [NSRecursiveLock alloc];
@implementation GoogleSignInHandler
/**
* Overload the presenting of the UI so we can pause the Unity player.
*/
- (void)signIn:(GIDSignIn *)signIn
presentViewController:(UIViewController *)viewController {
UnityPause(true);
[UnityGetGLViewController() presentViewController:viewController
animated:YES
completion:nil];
}
/**
* Overload the dismissing so we can resume the Unity player.
*/
- (void)signIn:(GIDSignIn *)signIn
dismissViewController:(UIViewController *)viewController {
UnityPause(false);
[UnityGetGLViewController() dismissViewControllerAnimated:YES completion:nil];
}
/**
* The sign-in flow has finished and was successful if |error| is |nil|.
* Map the errors from the iOS SDK back to the Android values for consistency's
* sake in the Unity layer.
*/
- (void)signIn:(GIDSignIn *)signIn
didSignInForUser:(GIDGoogleUser *)user
withError:(NSError *)_error {
if (_error == nil) {
if (currentResult_) {
currentResult_->result_code = kStatusCodeSuccess;
currentResult_->finished = true;
} else {
NSLog(@"No currentResult to set status on!");
}
NSLog(@"didSignInForUser: SUCCESS");
} else {
NSLog(@"didSignInForUser: %@", _error.localizedDescription);
if (currentResult_) {
switch (_error.code) {
case kGIDSignInErrorCodeUnknown:
currentResult_->result_code = kStatusCodeError;
break;
case kGIDSignInErrorCodeKeychain:
currentResult_->result_code = kStatusCodeInternalError;
break;
case kGIDSignInErrorCodeHasNoAuthInKeychain:
currentResult_->result_code = kStatusCodeError;
break;
case kGIDSignInErrorCodeCanceled:
currentResult_->result_code = kStatusCodeCanceled;
break;
default:
NSLog(@"Unmapped error code: %ld, returning Error",
static_cast<long>(_error.code));
currentResult_->result_code = kStatusCodeError;
}
currentResult_->finished = true;
UnpauseUnityPlayer();
} else {
NSLog(@"No currentResult to set status on!");
}
}
}
// Finished disconnecting |user| from the app successfully if |error| is |nil|.
- (void)signIn:(GIDSignIn *)signIn
didDisconnectWithUser:(GIDGoogleUser *)user
withError:(NSError *)_error {
if (_error == nil) {
NSLog(@"didDisconnectWithUser: SUCCESS");
} else {
NSLog(@"didDisconnectWithUser: %@", _error);
}
}
@end
/**
* These are the external "C" methods that are imported by the Unity C# code.
* The parameters are intended to be primative, easy to marshall.
*/
extern "C" {
/**
* This method does nothing in the iOS implementation. It is here
* to make the API uniform between Android and iOS.
*/
void *GoogleSignIn_Create(void *data) { return NULL; }
void GoogleSignIn_EnableDebugLogging(void *unused, bool flag) {
if (flag) {
NSLog(@"GoogleSignIn: No optional logging available on iOS");
}
}
/**
* Configures the GIDSignIn instance. The first parameter is unused in iOS.
* It is here to make the API between Android and iOS uniform.
*/
bool GoogleSignIn_Configure(void *unused, bool useGameSignIn,
const char *webClientId, bool requestAuthCode,
bool forceTokenRefresh, bool requestEmail,
bool requestIdToken, bool hidePopups,
const char **additionalScopes, int scopeCount,
const char *accountName) {
if (webClientId) {
[GIDSignIn sharedInstance].serverClientID =
[NSString stringWithUTF8String:webClientId];
}
[GIDSignIn sharedInstance].shouldFetchBasicProfile = true;
int scopeSize = scopeCount;
if (scopeSize) {
NSMutableArray *tmpary =
[[NSMutableArray alloc] initWithCapacity:scopeSize];
for (int i = 0; i < scopeCount; i++) {
[tmpary addObject:[NSString stringWithUTF8String:additionalScopes[i]]];
}
[GIDSignIn sharedInstance].scopes = tmpary;
}
if (accountName) {
[GIDSignIn sharedInstance].loginHint =
[NSString stringWithUTF8String:accountName];
}
return !useGameSignIn;
}
/**
Starts the sign-in process. Returns and error result if error, null otherwise.
*/
static SignInResult *startSignIn() {
bool busy = false;
[resultLock lock];
if (!currentResult_ || currentResult_->finished) {
currentResult_.reset(new SignInResult());
currentResult_->result_code = 0;
currentResult_->finished = false;
} else {
busy = true;
}
[resultLock unlock];
if (busy) {
NSLog(@"ERROR: There is already a pending sign-in operation.");
// Returned to the caller, should be deleted by calling
// GoogleSignIn_DisposeFuture().
return new SignInResult{.result_code = kStatusCodeDeveloperError,
.finished = true};
}
return nullptr;
}
/**
* Sign-In. The return value is a pointer to the currentResult object.
*/
void *GoogleSignIn_SignIn() {
SignInResult *result = startSignIn();
if (!result) {
[[GIDSignIn sharedInstance] signIn];
result = currentResult_.get();
}
return result;
}
bool GoogleSignIn_CanBeSilent() {
return [GIDSignIn sharedInstance].hasPreviousSignIn;
}
/**
* Attempt a silent sign-in. Return value is the pointer to the currentResult
* object.
*/
void *GoogleSignIn_SignInSilently() {
SignInResult *result = startSignIn();
if (!result) {
[[GIDSignIn sharedInstance] restorePreviousSignIn];
result = currentResult_.get();
}
return result;
}
void GoogleSignIn_Signout() {
GIDSignIn *signIn = [GIDSignIn sharedInstance];
[signIn signOut];
}
void GoogleSignIn_Disconnect() {
GIDSignIn *signIn = [GIDSignIn sharedInstance];
[signIn disconnect];
}
bool GoogleSignIn_Pending(SignInResult *result) {
volatile bool ret;
[resultLock lock];
ret = !result->finished;
[resultLock unlock];
return ret;
}
GIDGoogleUser *GoogleSignIn_Result(SignInResult *result) {
if (result && result->finished) {
GIDGoogleUser *guser = [GIDSignIn sharedInstance].currentUser;
return guser;
}
return nullptr;
}
int GoogleSignIn_Status(SignInResult *result) {
if (result) {
return result->result_code;
}
return kStatusCodeDeveloperError;
}
void GoogleSignIn_DisposeFuture(SignInResult *result) {
if (result == currentResult_.get()) {
currentResult_.reset(nullptr);
} else {
delete result;
}
}
/**
* Private helper function to copy NSString to char*. If the destination is
* non-null, the contents of src are copied up to len bytes (using strncpy). The
* then len is returned. Otherwise returns length of the string to copy + 1.
*/
static size_t CopyNSString(NSString *src, char *dest, size_t len) {
if (dest && src && len) {
const char *string = [src UTF8String];
strncpy(dest, string, len);
return len;
}
return src ? src.length + 1 : 0;
}
size_t GoogleSignIn_GetServerAuthCode(GIDGoogleUser *guser, char *buf,
size_t len) {
NSString *val = [guser serverAuthCode];
return CopyNSString(val, buf, len);
}
size_t GoogleSignIn_GetDisplayName(GIDGoogleUser *guser, char *buf,
size_t len) {
NSString *val = [guser.profile name];
return CopyNSString(val, buf, len);
}
size_t GoogleSignIn_GetEmail(GIDGoogleUser *guser, char *buf, size_t len) {
NSString *val = [guser.profile email];
return CopyNSString(val, buf, len);
}
size_t GoogleSignIn_GetFamilyName(GIDGoogleUser *guser, char *buf, size_t len) {
NSString *val = [guser.profile familyName];
return CopyNSString(val, buf, len);
}
size_t GoogleSignIn_GetGivenName(GIDGoogleUser *guser, char *buf, size_t len) {
NSString *val = [guser.profile givenName];
return CopyNSString(val, buf, len);
}
size_t GoogleSignIn_GetIdToken(GIDGoogleUser *guser, char *buf, size_t len) {
NSString *val = [guser.authentication idToken];
return CopyNSString(val, buf, len);
}
size_t GoogleSignIn_GetImageUrl(GIDGoogleUser *guser, char *buf, size_t len) {
NSURL *url = [guser.profile imageURLWithDimension:128];
NSString *val = url ? [url absoluteString] : nullptr;
return CopyNSString(val, buf, len);
}
size_t GoogleSignIn_GetUserId(GIDGoogleUser *guser, char *buf, size_t len) {
NSString *val = [guser userID];
return CopyNSString(val, buf, len);
}
} // extern "C"