Skip to content

Commit 6c3c741

Browse files
committed
fix serverAuthCode in ios
1 parent d7915e1 commit 6c3c741

11 files changed

+222
-6
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
**/.DS_Store
2+
13
.gradle/
24
.idea/
3-
.vscode/
45
bin/
56
build/
67
*.iml

.vscode/settings.json

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"files.exclude": {
3+
"**/.DS_Store": true,
4+
"**/.git": true,
5+
"**/.gitmodules": true,
6+
"**/*.booproj": true,
7+
"**/*.pidb": true,
8+
"**/*.suo": true,
9+
"**/*.user": true,
10+
"**/*.userprefs": true,
11+
"**/*.unityproj": true,
12+
"**/*.dll": true,
13+
"**/*.exe": true,
14+
"**/*.pdf": true,
15+
"**/*.mid": true,
16+
"**/*.midi": true,
17+
"**/*.wav": true,
18+
"**/*.gif": true,
19+
"**/*.ico": true,
20+
"**/*.jpg": true,
21+
"**/*.jpeg": true,
22+
"**/*.png": true,
23+
"**/*.psd": true,
24+
"**/*.tga": true,
25+
"**/*.tif": true,
26+
"**/*.tiff": true,
27+
"**/*.3ds": true,
28+
"**/*.3DS": true,
29+
"**/*.fbx": true,
30+
"**/*.FBX": true,
31+
"**/*.lxo": true,
32+
"**/*.LXO": true,
33+
"**/*.ma": true,
34+
"**/*.MA": true,
35+
"**/*.obj": true,
36+
"**/*.OBJ": true,
37+
"**/*.asset": true,
38+
"**/*.cubemap": true,
39+
"**/*.flare": true,
40+
"**/*.mat": true,
41+
"**/*.meta": true,
42+
"**/*.prefab": true,
43+
"**/*.unity": true,
44+
"build/": true,
45+
"Build/": true,
46+
"Library/": true,
47+
"library/": true,
48+
"obj/": true,
49+
"Obj/": true,
50+
"ProjectSettings/": true,
51+
"temp/": true,
52+
"Temp/": true
53+
},
54+
"dotnet.defaultSolution": "TestGoogleSignIn.sln"
55+
}

Editor.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/iOS.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/iOS/PListImporter.cs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#if UNITY_EDITOR
2+
using System.IO;
3+
4+
using UnityEngine;
5+
6+
using UnityEditor.AssetImporters;
7+
8+
[ScriptedImporter(1, "plist")]
9+
public class PListImporter : ScriptedImporter
10+
{
11+
public override void OnImportAsset(AssetImportContext ctx)
12+
{
13+
if(ctx.mainObject is TextAsset)
14+
return;
15+
16+
var subAsset = new TextAsset(File.ReadAllText(ctx.assetPath));
17+
ctx.AddObjectToAsset("text", subAsset);
18+
ctx.SetMainObject(subAsset);
19+
}
20+
}
21+
#endif

Editor/iOS/PListImporter.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/iOS/PListProcessor.cs

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#if UNITY_EDITOR
2+
using UnityEngine;
3+
4+
using UnityEditor;
5+
using UnityEditor.iOS.Xcode;
6+
7+
using UnityEditor.Build;
8+
using UnityEditor.Build.Reporting;
9+
10+
using System;
11+
using System.IO;
12+
using System.Linq;
13+
using System.Collections.Generic;
14+
15+
public class PListProcessor : IPostprocessBuildWithReport
16+
{
17+
public int callbackOrder => 99999;
18+
19+
public void OnPostprocessBuild(BuildReport report)
20+
{
21+
#if UNITY_IOS
22+
string projectBundleId = PlayerSettings.GetApplicationIdentifier(BuildTargetGroup.iOS);
23+
var plistFiles = AssetDatabase.FindAssets("glob:\"**/*.plist\"").Select((guid) => {
24+
var doc = new PlistDocument();
25+
doc.ReadFromFile(AssetDatabase.GUIDToAssetPath(guid));
26+
return doc;
27+
}).Where((doc) => {
28+
return doc.root.values.TryGetValue("BUNDLE_ID",out var element) && element.AsString() == projectBundleId;
29+
}).ToArray();
30+
31+
if(!(plistFiles?.Length > 0))
32+
return;
33+
34+
var google = plistFiles.FirstOrDefault();
35+
36+
if(!(google.root.values.TryGetValue("CLIENT_ID",out var CLIENT_ID) && CLIENT_ID?.AsString() is string clientID && clientID.EndsWith("googleusercontent.com")))
37+
throw new KeyNotFoundException("CLIENT_ID");
38+
if(!(google.root.values.TryGetValue("REVERSED_CLIENT_ID",out var REVERSED_CLIENT_ID) && REVERSED_CLIENT_ID?.AsString() is string reversedClientID && reversedClientID.StartsWith("com.googleusercontent")))
39+
throw new KeyNotFoundException("REVERSED_CLIENT_ID");
40+
41+
string plistPath = Path.Combine( report.summary.outputPath, "Info.plist" );
42+
43+
var info = new PlistDocument();
44+
info.ReadFromFile(plistPath);
45+
46+
info.root.SetString("GIDClientID",clientID);
47+
var CFBundleURLTypes = (info.root.values.TryGetValue("CFBundleURLTypes",out var element) ? element.AsArray() : null) ?? info.root.CreateArray("CFBundleURLTypes");
48+
if(!(CFBundleURLTypes?.values?.Count > 0 && CFBundleURLTypes.values.OfType<PlistElementDict>().Select((dict) => dict.values.TryGetValue("CFBundleURLSchemes",out var value) ? value?.AsArray() : null).OfType<PlistElementArray>().SelectMany((array) => array.values).Any((item) => item?.AsString() == reversedClientID)))
49+
{
50+
var dict = CFBundleURLTypes.AddDict();
51+
dict.SetString("CFBundleTypeRole","Editor");
52+
dict.CreateArray("CFBundleURLSchemes").AddString(reversedClientID);
53+
}
54+
55+
if(google.root.values.TryGetValue("WEB_CLIENT_ID",out var WEB_CLIENT_ID) && WEB_CLIENT_ID?.AsString() is string webClientID && !string.IsNullOrWhiteSpace(webClientID))
56+
{
57+
if(webClientID.EndsWith("googleusercontent.com"))
58+
info.root.SetString("GIDServerClientID",webClientID);
59+
else throw new ArgumentException("WebClientID");
60+
}
61+
62+
info.WriteToFile(plistPath);
63+
#endif
64+
}
65+
}
66+
67+
#endif

Editor/iOS/PListProcessor.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

GoogleSignIn/Impl/NativeFuture.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ internal NativeFuture(IntPtr ptr) : base(ptr) {
3434

3535
public GoogleSignInUser Result {
3636
get {
37-
IntPtr ptr = GoogleSignInImpl.GoogleSignIn_Result(SelfPtr());
37+
HandleRef self = SelfPtr();
38+
IntPtr ptr = GoogleSignInImpl.GoogleSignIn_Result(self);
3839
if (ptr == IntPtr.Zero) {
3940
return null;
4041
}
@@ -54,7 +55,7 @@ public GoogleSignInUser Result {
5455

5556
user.IdToken = GoogleSignInImpl.GoogleSignIn_GetIdToken(userPtr);
5657

57-
user.AuthCode = GoogleSignInImpl.GoogleSignIn_GetServerAuthCode(userPtr);
58+
user.AuthCode = GoogleSignInImpl.GoogleSignIn_GetServerAuthCode(self);
5859

5960
string url = GoogleSignInImpl.GoogleSignIn_GetImageUrl(userPtr);
6061
if (url?.Length > 0) {

Plugins/iOS/GoogleSignIn.mm

+5-3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ void UnpauseUnityPlayer() {
5555
struct SignInResult {
5656
int result_code;
5757
bool finished;
58+
NSString* serverAuthCode;
5859
};
5960

6061
std::unique_ptr<SignInResult> currentResult_;
@@ -243,6 +244,7 @@ bool GoogleSignIn_Configure(void *unused, bool useGameSignIn,
243244
hint:[GoogleSignInHandler sharedInstance]->loginHint
244245
completion:^(GIDSignInResult *result, NSError *error) {
245246
GIDGoogleUser *user = result.user;
247+
currentResult_.get()->serverAuthCode = result.serverAuthCode;
246248
[[GoogleSignInHandler sharedInstance] signIn:[GIDSignIn sharedInstance] didSignInForUser:user withError:error];
247249
}];
248250
result = currentResult_.get();
@@ -322,10 +324,10 @@ static size_t CopyNSString(NSString *src, char *dest, size_t len) {
322324
return src ? src.length + 1 : 0;
323325
}
324326

325-
size_t GoogleSignIn_GetServerAuthCode(GIDGoogleUser *guser, char *buf,
327+
size_t GoogleSignIn_GetServerAuthCode(SignInResult *result, char *buf,
326328
size_t len) {
327-
NSString *val = [guser.configuration serverClientID];
328-
return CopyNSString(val, buf, len);
329+
NSString *val = result->serverAuthCode;
330+
return CopyNSString(val, buf, len);
329331
}
330332

331333
size_t GoogleSignIn_GetDisplayName(GIDGoogleUser *guser, char *buf,

README.md

+31
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,37 @@ Add UPM dependency with branch tag `https://github.com/Thaina/google-signin-unit
3939
}
4040
```
4141

42+
Also, [New version of iOS recommend](https://developers.google.com/identity/sign-in/ios/quick-migration-guide#google_sign-in_sdk_v700) that we should set `GIDClientID` and `GIDServerClientID` into Info.plist
43+
44+
So I have add an editor tool `PListProcessor` that look for plist files in the project, extract `CLIENT_ID` and `WEB_CLIENT_ID` property of the plist which contain the `BUNDLE_ID` with the same name as bundle identifier of the project
45+
46+
The plist file in the project should be downloaded from Google Cloud Console credential page
47+
48+
Select iOS credential and download at ⬇
49+
50+
```xml
51+
<!-- This plist was the default format downloaded from your google cloud console -->
52+
<?xml version="1.0" encoding="UTF-8"?>
53+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
54+
<plist version="1.0">
55+
<dict>
56+
<key>CLIENT_ID</key>
57+
<string>{YourCloudProjectID}-yyyyyYYYYyyyyYYYYYYYYYYYYYYyyyyyy.apps.googleusercontent.com</string>
58+
<key>REVERSED_CLIENT_ID</key>
59+
<string>com.googleusercontent.apps.{YourCloudProjectID}-yyyyyYYYYyyyyYYYYYYYYYYYYYYyyyyyy</string>
60+
<key>PLIST_VERSION</key>
61+
<string>1</string>
62+
<key>BUNDLE_ID</key>
63+
<string>com.{YourCompany}.{YourProductName}</string>
64+
<!-- Optional, These 2 line below should be added manually if you need ServerAuthCode -->
65+
<key>WEB_CLIENT_ID</key>
66+
<string>{YourCloudProjectID}-zzzZZZZZZZZZZZZZZzzzzzzzzzzZZZzzz.apps.googleusercontent.com</string>
67+
</dict>
68+
</plist>
69+
```
70+
71+
Document below is original README, some information might be outdated
72+
4273
# Google Sign-In Unity Plugin
4374
_Copyright (c) 2017 Google Inc. All rights reserved._
4475

0 commit comments

Comments
 (0)