Skip to content

Commit

Permalink
Implement new Authenticate method with different interactivities and …
Browse files Browse the repository at this point in the history
…enumerated result codes.

PiperOrigin-RevId: 289671807
Change-Id: I5c3b67ff8631cec3369696c034ee3b499558eba6
  • Loading branch information
ozdemir08 authored and copybara-github committed Jan 14, 2020
1 parent c5c81bd commit b1357da
Show file tree
Hide file tree
Showing 9 changed files with 284 additions and 80 deletions.
35 changes: 27 additions & 8 deletions samples/SmokeTest/Source/Assets/SmokeTest/MainGui.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="MainGui.cs" company="Google Inc.">
// <copyright file="MainGui.cs" company="Google Inc.">
// Copyright (C) 2014 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -304,7 +304,22 @@ internal void ShowNotAuthUi()
.EnableSavedGames()
.Build());
}
else if (GUI.Button(this.CalcGrid(0, 4), "Nearby Connections"))
else if (GUI.Button(this.CalcGrid(0, 4), "SignInInteractivity - NoPrompt"))
{
this.DoAuthenticate(SignInInteractivity.NoPrompt,
new PlayGamesClientConfiguration.Builder().Build());
}
else if (GUI.Button(this.CalcGrid(1, 4), "SignInInteractivity - CanPromptOnce"))
{
this.DoAuthenticate(SignInInteractivity.CanPromptOnce,
new PlayGamesClientConfiguration.Builder().Build());
}
else if (GUI.Button(this.CalcGrid(0, 5), "SignInInteractivity - CanPromptAlways"))
{
this.DoAuthenticate(SignInInteractivity.CanPromptAlways,
new PlayGamesClientConfiguration.Builder().Build());
}
else if (GUI.Button(this.CalcGrid(1, 5), "Nearby Connections"))
{
SetUI(Ui.NearbyConnections);
}
Expand Down Expand Up @@ -1180,17 +1195,16 @@ internal void EndStandBy()
mStandby = false;
}

internal void DoAuthenticate(PlayGamesClientConfiguration configuration)
internal void DoAuthenticate(SignInInteractivity interactivity, PlayGamesClientConfiguration configuration)
{
SetStandBy("Authenticating...");

ClientConfiguration = configuration;
PlayGamesPlatform.InitializeInstance(ClientConfiguration);
PlayGamesPlatform.Activate();
Social.localUser.Authenticate((bool success) =>
PlayGamesPlatform.Instance.Authenticate(interactivity, (code) =>
{
EndStandBy();
if (success)
if (code == SignInStatus.Success)
{
Status = "Authenticated. Hello, " + Social.localUser.userName + " (" +
Social.localUser.id + ")";
Expand All @@ -1205,13 +1219,18 @@ internal void DoAuthenticate(PlayGamesClientConfiguration configuration)
}
else
{
Status = "*** Failed to authenticate.";
Status = "*** Failed to authenticate with " + code;
}

ShowEffect(success);
ShowEffect(code == SignInStatus.Success);
});
}

internal void DoAuthenticate(PlayGamesClientConfiguration configuration)
{
DoAuthenticate(SignInInteractivity.CanPromptAlways, configuration);
}

internal void DoSignOut()
{
((PlayGamesPlatform) Social.Active).SignOut();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ public class DummyClient : IPlayGamesClient
/// </remarks>
/// <param name="callback">Callback when completed.</param>
/// <param name="silent">If set to <c>true</c> silent.</param>
public void Authenticate(Action<bool, string> callback, bool silent)
public void Authenticate(bool silent, Action<SignInStatus> callback)
{
LogUsage();
if (callback != null)
{
callback(false, "Not implemented on this platform");
callback(SignInStatus.Failed);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public interface IPlayGamesClient
/// Once the callback returns true, the user is considered to be authenticated
/// forever after.
/// </remarks>
/// <param name="callback">Callback.</param>
/// <param name="silent">If set to <c>true</c> silent.</param>
void Authenticate(Action<bool, string> callback, bool silent);
/// <param name="callback">Callback</param>
void Authenticate(bool silent, Action<SignInStatus> callback);

/// <summary>
/// Returns whether or not user is authenticated.
Expand Down Expand Up @@ -312,7 +312,7 @@ void SubmitScore(string leaderboardId, long score, string metadata,
void RequestPermissions(Action<SignInStatus> callback, string[] scopes);

/// <summary>
/// Returns whether or not user has given permissions for given scopes.
/// Returns whether or not user has given permissions for given scopes
/// </summary>
/// <seealso cref="GooglePlayGames.BasicApi.IPlayGamesClient.HasPermissions"/>
/// <param name="scopes">list of scopes</param>
Expand Down
75 changes: 75 additions & 0 deletions source/PluginDev/Assets/GooglePlayGames/BasicApi/SignInHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System.Collections.Generic;
using UnityEngine;

namespace GooglePlayGames.BasicApi
{
public class SignInHelper
{
private static int True = 0;
private static int False = 1;
private const string PromptSignInKey = "prompt_sign_in";

public static SignInStatus ToSignInStatus(int code)
{
Dictionary<int, SignInStatus> dictionary = new Dictionary<int, SignInStatus>()
{
{
/* CommonUIStatus.UI_BUSY */ -12, SignInStatus.AlreadyInProgress
},
{
/* CommonStatusCodes.SUCCESS */ 0, SignInStatus.Success
},
{
/* CommonStatusCodes.SIGN_IN_REQUIRED */ 4, SignInStatus.UiSignInRequired
},
{
/* CommonStatusCodes.NETWORK_ERROR */ 7, SignInStatus.NetworkError
},
{
/* CommonStatusCodes.INTERNAL_ERROR */ 8, SignInStatus.InternalError
},
{
/* CommonStatusCodes.DEVELOPER_ERROR */ 10, SignInStatus.DeveloperError
},
{
/* CommonStatusCodes.CANCELED */ 16, SignInStatus.Canceled
},
{
/* CommonStatusCodes.API_NOT_CONNECTED */ 17, SignInStatus.Failed
},
{
/* GoogleSignInStatusCodes.SIGN_IN_FAILED */ 12500, SignInStatus.Failed
},
{
/* GoogleSignInStatusCodes.SIGN_IN_CANCELLED */ 12501, SignInStatus.Canceled
},
{
/* GoogleSignInStatusCodes.SIGN_IN_CURRENTLY_IN_PROGRESS */ 12502, SignInStatus.AlreadyInProgress
},
};

return dictionary.ContainsKey(code) ? dictionary[code] : SignInStatus.Failed;
}

/// <summary>
/// Used during authentication to save if the user should be prompted to interactive sign in next time they
/// try to authenticate with SignInInteractivity.CanPromptOnce.
/// </summary>
/// <param name="value"></param>
public static void SetPromptUiSignIn(bool value)
{
PlayerPrefs.SetInt(PromptSignInKey, value ? True : False);
}

/// <summary>
/// Used during authentication with SignInInteractivity.CanPromptOnce to understand whether or not the user should be
/// prompted to interactive sign in.
/// </summary>
/// <returns></returns>
public static bool ShouldPromptUiSignIn()
{
return PlayerPrefs.GetInt(PromptSignInKey, True) != False;
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace GooglePlayGames.BasicApi
{
public enum SignInInteractivity
{
/// <summary>no UIs will be shown (if UIs are needed, it will fail rather than show them).</summary>
NoPrompt,

/// <summary>
/// This may show UIs, consent dialogs, etc.
/// At the end of the process, callback will be invoked to notify of the result.
/// Once the callback returns true, the user is considered to be authenticated
/// forever after.
/// </summary>
CanPromptAlways,

/// <summary>When this is selected, PlayGamesPlatform.Authenticate does the followings in order:
/// 1. Attempt to silent sign in.
/// 2. If silent sign in fails, check if user has previously declined to sign in and don’t prompt interactive
/// sign in if they have.
/// 3. Check the internet connection and fail with NO_INTERNET_CONNECTION if there is no internet connection.
/// 4. Prompt interactive sign in.
/// 5. If the interactive sign in is cancelled, then memorize this for the 2nd step of the next sign.
/// </summary>
CanPromptOnce
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ public enum SignInStatus
/// </summary>
UiSignInRequired,

/// <summary>
/// The application is misconfigured. This error is not recoverable and will be treated as fatal.
/// The developer should look at the logs after this to determine more actionable information.
/// </summary>
DeveloperError,

/// <summary>A network error occurred. Retrying should resolve the problem.</summary>
NetworkError,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,37 @@ public void Authenticate(Action<bool> callback, bool silent)
/// triggers normal (not silent) authentication.
/// </param>
public void Authenticate(Action<bool, string> callback, bool silent)
{
Authenticate(silent ? SignInInteractivity.NoPrompt : SignInInteractivity.CanPromptAlways, status =>
{
if (status == SignInStatus.Success)
{
callback(true, "Authentication succeeded");
}
else if (status == SignInStatus.Canceled)
{
callback(false, "Authentication canceled");
GooglePlayGames.OurUtils.Logger.d("Authentication canceled");
}
else if (status == SignInStatus.DeveloperError)
{
callback(false, "Authentication failed - developer error");
GooglePlayGames.OurUtils.Logger.d("Authentication failed - developer error");
}
else
{
callback(false, "Authentication failed");
GooglePlayGames.OurUtils.Logger.d("Authentication failed");
}
});
}

/// <summary>
/// Authenticate the local user with the Google Play Games service.
/// </summary>
/// <param name="callback">The callback to call when authentication finishes.</param>
/// <param name="signInInteractivity"><see cref="SignInInteractivity"/></param>
public void Authenticate(SignInInteractivity signInInteractivity, Action<SignInStatus> callback)
{
// make a platform-specific Play Games client
if (mClient == null)
Expand All @@ -378,8 +409,94 @@ public void Authenticate(Action<bool, string> callback, bool silent)
mClient = PlayGamesClientFactory.GetPlatformPlayGamesClient(mConfiguration);
}

// authenticate!
mClient.Authenticate(callback, silent);
if (callback == null)
{
callback = status => { };
}

switch (signInInteractivity)
{
case SignInInteractivity.NoPrompt:
mClient.Authenticate( /* silent= */ true, code =>
{
// SignInStatus.UiSignInRequired is returned when silent sign in fails or when there is no
// internet connection.
if (code == SignInStatus.UiSignInRequired &&
Application.internetReachability == NetworkReachability.NotReachable)
{
callback(SignInStatus.NetworkError);
}
else
{
callback(code);
}
});
break;

case SignInInteractivity.CanPromptAlways:
mClient.Authenticate( /* silent= */ false, code =>
{
// SignInStatus.Canceled is returned when interactive sign in fails or when there is no internet connection.
if (code == SignInStatus.Canceled &&
Application.internetReachability == NetworkReachability.NotReachable)
{
callback(SignInStatus.NetworkError);
}
else
{
callback(code);
}
});
break;

case SignInInteractivity.CanPromptOnce:

// 1. Silent sign in first
mClient.Authenticate( /* silent= */ true, silentSignInResultCode =>
{
if (silentSignInResultCode == SignInStatus.Success)
{
OurUtils.Logger.d("Successful, triggering callback");
callback(silentSignInResultCode);
return;
}

// 2. Check the shared pref and bail out if it's true.
if (!SignInHelper.ShouldPromptUiSignIn())
{
OurUtils.Logger.d(
"User cancelled sign in attempt in the previous attempt. Triggering callback with silentSignInResultCode");
callback(silentSignInResultCode);
return;
}

// 3. Check internet connection
if (Application.internetReachability == NetworkReachability.NotReachable)
{
OurUtils.Logger.d("No internet connection");
callback(SignInStatus.NetworkError);
return;
}

// 4. Interactive sign in
mClient.Authenticate( /* silent= */ false, interactiveSignInResultCode =>
{
// 5. Save that the user has cancelled the interactive sign in.
if (interactiveSignInResultCode == SignInStatus.Canceled)
{
OurUtils.Logger.d("Cancelled, saving this to a shared pref");
SignInHelper.SetPromptUiSignIn(false);
}

callback(interactiveSignInResultCode);
});
});
break;

default:
PlayGamesHelperObject.RunOnGameThread(() => callback(SignInStatus.Failed));
break;
}
}

/// <summary>
Expand Down Expand Up @@ -520,18 +637,18 @@ public void GetAnotherServerAuthCode(bool reAuthenticateIfNeeded,
}
else if (mClient != null && reAuthenticateIfNeeded)
{
mClient.Authenticate((success, msg) =>
mClient.Authenticate(false, (status) =>
{
if (success)
if (status == SignInStatus.Success)
{
callback(mClient.GetServerAuthCode());
}
else
{
OurUtils.Logger.e("Re-authentication failed: " + msg);
OurUtils.Logger.e("Re-authentication failed: " + status);
callback(null);
}
}, false);
});
}
else
{
Expand Down
Loading

0 comments on commit b1357da

Please sign in to comment.