Skip to content

Commit 4614570

Browse files
authored
Merge pull request #20 from HadiTeam/bugfix/google_sign_in_get_user_id
GoogleSignInImpl.GoogleSignIn_GetUserId #18
2 parents cdb93a3 + b2eeeea commit 4614570

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

GoogleSignIn/Impl/GoogleSignInImpl.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -192,18 +192,25 @@ static void GoogleSignIn_Signout(HandleRef self)
192192

193193
internal static string GoogleSignIn_GetUserId(HandleRef self)
194194
{
195+
string idTokenFull = null;
195196
try
196197
{
197-
var idTokenPart = googleIdTokenCredential?.Call<string>("getIdToken")?.Split('.')?.ElementAtOrDefault(1);
198+
idTokenFull = googleIdTokenCredential?.Call<string>("getIdToken");
199+
var idTokenPart = idTokenFull?.Split('.')?.ElementAtOrDefault(1);
198200
if(!(idTokenPart?.Length is int length && length > 1))
199201
return null;
200202

203+
// Replace URL-safe characters and fix padding
204+
idTokenPart = idTokenPart.Replace('-', '+').Replace('_', '/');
201205
string fill = new string('=',(4 - (idTokenPart.Length % 4)) % 4);
202-
var jobj = Newtonsoft.Json.Linq.JObject.Parse(Encoding.UTF8.GetString(Convert.FromBase64String(idTokenPart + fill)));
206+
var idTokenFromBase64 = Convert.FromBase64String(idTokenPart + fill);
207+
var idToken = Encoding.UTF8.GetString(idTokenFromBase64);
208+
var jobj = Newtonsoft.Json.Linq.JObject.Parse(idToken);
203209
return jobj?["sub"]?.ToString();
204210
}
205211
catch(Exception e)
206212
{
213+
// Debug.LogException(new Exception($"GoogleSignIn_GetUserId.idTokenFull {idTokenFull}"));
207214
Debug.LogException(e);
208215
return null;
209216
}

0 commit comments

Comments
 (0)