Skip to content

Commit

Permalink
fix serverAuthCode in ios
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaina committed Jun 13, 2024
1 parent d7915e1 commit 76d2985
Show file tree
Hide file tree
Showing 11 changed files with 222 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
**/.DS_Store

.gradle/
.idea/
.vscode/
bin/
build/
*.iml
Expand Down
55 changes: 55 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"files.exclude": {
"**/.DS_Store": true,
"**/.git": true,
"**/.gitmodules": true,
"**/*.booproj": true,
"**/*.pidb": true,
"**/*.suo": true,
"**/*.user": true,
"**/*.userprefs": true,
"**/*.unityproj": true,
"**/*.dll": true,
"**/*.exe": true,
"**/*.pdf": true,
"**/*.mid": true,
"**/*.midi": true,
"**/*.wav": true,
"**/*.gif": true,
"**/*.ico": true,
"**/*.jpg": true,
"**/*.jpeg": true,
"**/*.png": true,
"**/*.psd": true,
"**/*.tga": true,
"**/*.tif": true,
"**/*.tiff": true,
"**/*.3ds": true,
"**/*.3DS": true,
"**/*.fbx": true,
"**/*.FBX": true,
"**/*.lxo": true,
"**/*.LXO": true,
"**/*.ma": true,
"**/*.MA": true,
"**/*.obj": true,
"**/*.OBJ": true,
"**/*.asset": true,
"**/*.cubemap": true,
"**/*.flare": true,
"**/*.mat": true,
"**/*.meta": true,
"**/*.prefab": true,
"**/*.unity": true,
"build/": true,
"Build/": true,
"Library/": true,
"library/": true,
"obj/": true,
"Obj/": true,
"ProjectSettings/": true,
"temp/": true,
"Temp/": true
},
"dotnet.defaultSolution": "TestGoogleSignIn.sln"
}
8 changes: 8 additions & 0 deletions Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Editor/iOS.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions Editor/iOS/PListImporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#if UNITY_EDITOR
using System.IO;

using UnityEngine;

using UnityEditor.AssetImporters;

[ScriptedImporter(1, "plist")]
public class PListImporter : ScriptedImporter
{
public override void OnImportAsset(AssetImportContext ctx)
{
if(ctx.mainObject is TextAsset)
return;

var subAsset = new TextAsset(File.ReadAllText(ctx.assetPath));
ctx.AddObjectToAsset("text", subAsset);
ctx.SetMainObject(subAsset);
}
}
#endif
11 changes: 11 additions & 0 deletions Editor/iOS/PListImporter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions Editor/iOS/PListProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#if UNITY_EDITOR
using UnityEngine;

using UnityEditor;
using UnityEditor.iOS.Xcode;

using UnityEditor.Build;
using UnityEditor.Build.Reporting;

using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;

public class PListProcessor : IPostprocessBuildWithReport
{
public int callbackOrder => 99999;

public void OnPostprocessBuild(BuildReport report)
{
#if UNITY_IOS
string projectBundleId = PlayerSettings.GetApplicationIdentifier(BuildTargetGroup.iOS);
var plistFiles = AssetDatabase.FindAssets("glob:\"**/*.plist\"").Select((guid) => {
var doc = new PlistDocument();
doc.ReadFromFile(AssetDatabase.GUIDToAssetPath(guid));
return doc;
}).Where((doc) => {
return doc.root.values.TryGetValue("BUNDLE_ID",out var element) && element.AsString() == projectBundleId;
}).ToArray();

if(!(plistFiles?.Length > 0))
return;

var google = plistFiles.FirstOrDefault();

if(!(google.root.values.TryGetValue("CLIENT_ID",out var CLIENT_ID) && CLIENT_ID?.AsString() is string clientID && clientID.EndsWith("googleusercontent.com")))
throw new KeyNotFoundException("CLIENT_ID");
if(!(google.root.values.TryGetValue("REVERSED_CLIENT_ID",out var REVERSED_CLIENT_ID) && REVERSED_CLIENT_ID?.AsString() is string reversedClientID && reversedClientID.StartsWith("com.googleusercontent")))
throw new KeyNotFoundException("REVERSED_CLIENT_ID");

string plistPath = Path.Combine( report.summary.outputPath, "Info.plist" );

var info = new PlistDocument();
info.ReadFromFile(plistPath);

info.root.SetString("GIDClientID",clientID);
var CFBundleURLTypes = (info.root.values.TryGetValue("CFBundleURLTypes",out var element) ? element.AsArray() : null) ?? info.root.CreateArray("CFBundleURLTypes");
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)))
{
var dict = CFBundleURLTypes.AddDict();
dict.SetString("CFBundleTypeRole","Editor");
dict.CreateArray("CFBundleURLSchemes").AddString(reversedClientID);
}

if(google.root.values.TryGetValue("WEB_CLIENT_ID",out var WEB_CLIENT_ID) && WEB_CLIENT_ID?.AsString() is string webClientID && !string.IsNullOrWhiteSpace(webClientID))
{
if(webClientID.EndsWith("googleusercontent.com"))
info.root.SetString("GIDServerClientID",webClientID);
else throw new ArgumentException("WebClientID");
}

info.WriteToFile(plistPath);
#endif
}
}

#endif
11 changes: 11 additions & 0 deletions Editor/iOS/PListProcessor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions GoogleSignIn/Impl/NativeFuture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ internal NativeFuture(IntPtr ptr) : base(ptr) {

public GoogleSignInUser Result {
get {
IntPtr ptr = GoogleSignInImpl.GoogleSignIn_Result(SelfPtr());
HandleRef self = SelfPtr();
IntPtr ptr = GoogleSignInImpl.GoogleSignIn_Result(self);
if (ptr == IntPtr.Zero) {
return null;
}
Expand All @@ -54,7 +55,7 @@ public GoogleSignInUser Result {

user.IdToken = GoogleSignInImpl.GoogleSignIn_GetIdToken(userPtr);

user.AuthCode = GoogleSignInImpl.GoogleSignIn_GetServerAuthCode(userPtr);
user.AuthCode = GoogleSignInImpl.GoogleSignIn_GetServerAuthCode(self);

string url = GoogleSignInImpl.GoogleSignIn_GetImageUrl(userPtr);
if (url?.Length > 0) {
Expand Down
8 changes: 5 additions & 3 deletions Plugins/iOS/GoogleSignIn.mm
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ void UnpauseUnityPlayer() {
struct SignInResult {
int result_code;
bool finished;
NSString* serverAuthCode;
};

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

size_t GoogleSignIn_GetServerAuthCode(GIDGoogleUser *guser, char *buf,
size_t GoogleSignIn_GetServerAuthCode(SignInResult *result, char *buf,
size_t len) {
NSString *val = [guser.configuration serverClientID];
return CopyNSString(val, buf, len);
NSString *val = result->serverAuthCode;
return CopyNSString(val, buf, len);
}

size_t GoogleSignIn_GetDisplayName(GIDGoogleUser *guser, char *buf,
Expand Down
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,37 @@ Add UPM dependency with branch tag `https://github.com/Thaina/google-signin-unit
}
```

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

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

The plist file in the project should be downloaded from Google Cloud Console credential page

Select iOS credential and download at ⬇ button

```xml
<!-- This plist was the default format downloaded from your google cloud console -->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CLIENT_ID</key>
<string>{YourCloudProjectID}-yyyyyYYYYyyyyYYYYYYYYYYYYYYyyyyyy.apps.googleusercontent.com</string>
<key>REVERSED_CLIENT_ID</key>
<string>com.googleusercontent.apps.{YourCloudProjectID}-yyyyyYYYYyyyyYYYYYYYYYYYYYYyyyyyy</string>
<key>PLIST_VERSION</key>
<string>1</string>
<key>BUNDLE_ID</key>
<string>com.{YourCompany}.{YourProductName}</string>
<!-- Optional, These 2 lines below should be added manually if you need ServerAuthCode -->
<key>WEB_CLIENT_ID</key>
<string>{YourCloudProjectID}-zzzZZZZZZZZZZZZZZzzzzzzzzzzZZZzzz.apps.googleusercontent.com</string>
</dict>
</plist>
```

### Document below is original README, some information might be outdated

# Google Sign-In Unity Plugin
_Copyright (c) 2017 Google Inc. All rights reserved._

Expand Down

0 comments on commit 76d2985

Please sign in to comment.