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 3da1816
Show file tree
Hide file tree
Showing 10 changed files with 191 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.

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
var plistFiles = AssetDatabase.FindAssets("t:TextAsset").Select((guid) => AssetDatabase.GUIDToAssetPath(guid)).Where((path) => path.EndsWith(".plist")).Select((path) => {
var asset = AssetDatabase.LoadAssetAtPath<TextAsset>(path);
var doc = new PlistDocument();
doc.ReadFromString(asset.text);
return doc;
}).Where((doc) => {
return doc.root.values.TryGetValue("BUNDLE_ID",out var element) && element.AsString() == PlayerSettings.GetApplicationIdentifier(BuildTargetGroup.iOS);
}).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.

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.

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

0 comments on commit 3da1816

Please sign in to comment.