Skip to content

Commit

Permalink
Merge pull request #2828 from playgameservices/incremental-authorization
Browse files Browse the repository at this point in the history
Incremental authorization
  • Loading branch information
ozdemir08 authored Jan 7, 2020
2 parents b06fa21 + d5c134d commit dc10a3c
Show file tree
Hide file tree
Showing 11 changed files with 412 additions and 3 deletions.
35 changes: 33 additions & 2 deletions samples/SmokeTest/Source/Assets/SmokeTest/MainGui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ public enum Ui
Leaderboards,
Video,
UserInfo,
PopupGravity
PopupGravity,
Permissions
}

public void Start()
Expand Down Expand Up @@ -351,7 +352,11 @@ internal void ShowRegularUi()
{
SetUI(Ui.PopupGravity);
}
else if (GUI.Button(this.CalcGrid(1, 5), "Sign Out"))
else if (GUI.Button(this.CalcGrid(1, 5), "Permissions"))
{
SetUI(Ui.Permissions);
}
else if (GUI.Button(this.CalcGrid(0, 6), "Sign Out"))
{
this.DoSignOut();
}
Expand Down Expand Up @@ -936,6 +941,29 @@ private void ShowPopupGravityUi()
}
}

private void ShowPermissionsUi()
{
DrawStatus();
DrawTitle("Permissions Ui");

if (GUI.Button(CalcGrid(0, 1), "Has Permission - Email"))
{
Status = "Email Permission " + PlayGamesPlatform.Instance.HasPermission("email");
}
else if (GUI.Button(CalcGrid(1, 1), "Request Permission- Email"))
{
Status = "Asking permission for email";
PlayGamesPlatform.Instance.RequestPermission(
code => { Status = "Result code " + code; },
"email");
}
else if (GUI.Button(CalcGrid(1, 6), "Back"))
{
SetUI(Ui.Main);
ShowEffect(true);
}
}

internal void ShowUserInfoUi()
{
GUI.Label(
Expand Down Expand Up @@ -1117,6 +1145,9 @@ internal void OnGUI()
case Ui.PopupGravity:
ShowPopupGravityUi();
break;
case Ui.Permissions:
ShowPermissionsUi();
break;
default:
// check for a status of interest, and if there
// is one, then don't touch it. Otherwise
Expand Down
22 changes: 22 additions & 0 deletions source/PluginDev/Assets/GooglePlayGames/BasicApi/DummyClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,28 @@ public void SubmitScore(
}
}

/// <summary>Asks user to give permissions for the given scopes.</summary>
/// <param name="callback">Callback used to indicate the outcome of the operation.</param>
/// <param name="scopes">Scope to ask permission for</param>
public void RequestPermissions(Action<SignInStatus> callback, string[] scopes)
{
LogUsage();
if (callback != null)
{
callback.Invoke(SignInStatus.Failed);
}
}

/// <summary>Returns whether or not user has given permissions for given scopes.</summary>
/// <seealso cref="GooglePlayGames.BasicApi.IPlayGamesClient.HasPermissions"/>
/// <param name="scopes">array of scopes</param>
/// <returns><c>true</c>, if given, <c>false</c> otherwise.</returns>
public bool HasPermissions(string[] scopes)
{
LogUsage();
return false;
}

/// <summary>
/// Returns a real-time multiplayer client.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,21 @@ void SubmitScore(string leaderboardId, long score,
void SubmitScore(string leaderboardId, long score, string metadata,
Action<bool> successOrFailureCalllback);

/// <summary>
/// Asks user to give permissions for the given scopes.
/// </summary>
/// <param name="callback">Callback used to indicate the outcome of the operation.</param>
/// <param name="scopes">list of scopes to ask permission for</param>
void RequestPermissions(Action<SignInStatus> callback, string[] scopes);

/// <summary>
/// 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>
/// <returns><c>true</c>, if given, <c>false</c> otherwise.</returns>
bool HasPermissions(string[] scopes);

/// <summary>
/// Returns a real-time multiplayer client.
/// </summary>
Expand Down
42 changes: 42 additions & 0 deletions source/PluginDev/Assets/GooglePlayGames/BasicApi/SignInStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
namespace GooglePlayGames.BasicApi
{
public enum SignInStatus
{
/// <summary>The operation was successful.</summary>
Success,

/// <summary>
/// The client attempted to connect to the service but the user is not signed in. The client may
/// choose to continue without using the API. Alternately, if {@link Status#hasResolution} returns
/// {@literal true} the client may call {@link Status#startResolutionForResult(Activity, int)} to
/// prompt the user to sign in. After the sign in activity returns with {@link Activity#RESULT_OK}
/// further attempts should succeed.
/// </summary>
UiSignInRequired,

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

/// <summary>An internal error occurred.</summary>
InternalError,

/// <summary>The sign in was canceled.</summary>
Canceled,

/// <summary>
/// A sign in process is currently in progress and the current one cannot continue. e.g. the user
/// clicks the SignInButton multiple times and more than one sign in intent was launched.
/// </summary>
AlreadyInProgress,

/// <summary>
/// Failure reason is unknown. Check adb log to see details if any.
/// </summary>
Failed,

/// <summary>
/// Currently not authenticated. Silent or interactive sign in is required.
/// </summary>
NotAuthenticated,
}
}

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

Original file line number Diff line number Diff line change
Expand Up @@ -1280,6 +1280,53 @@ public void LoadScores(ILeaderboard board, Action<bool> callback)
(PlayGamesLeaderboard) board, scoreData, callback));
}

/// <summary>Asks user to give permissions for the given scopes.</summary>
/// <param name="callback">Callback used to indicate the outcome of the operation.</param>
/// <param name="scopes">Scope to ask permission for</param>
public void RequestPermission(Action<SignInStatus> callback, string scope)
{
RequestPermissions(callback, new string[] {scope});
}

/// <summary>Asks user to give permissions for the given scopes.</summary>
/// <param name="callback">Callback used to indicate the outcome of the operation.</param>
/// <param name="scopes">List of scopes to ask permission for</param>
public void RequestPermissions(Action<SignInStatus> callback, string[] scopes)
{
if (!IsAuthenticated())
{
GooglePlayGames.OurUtils.Logger.e(
"HasPermissions can only be called after authentication.");
callback(SignInStatus.NotAuthenticated);
return;
}

mClient.RequestPermissions(callback, scopes);
}

/// <summary>Returns whether or not user has given permissions for given scopes.</summary>
/// <param name="scope">scope</param>
/// <returns><c>true</c>, if given, <c>false</c> otherwise.</returns>
public bool HasPermission(string scope)
{
return HasPermissions(new string[] {scope});
}

/// <summary>Returns whether or not user has given permissions for given scopes.</summary>
/// <param name="scopes">array of scopes</param>
/// <returns><c>true</c>, if given, <c>false</c> otherwise.</returns>
public bool HasPermissions(string[] scopes)
{
if (!IsAuthenticated())
{
GooglePlayGames.OurUtils.Logger.e(
"HasPermissions can only be called after authentication.");
return false;
}

return mClient.HasPermissions(scopes);
}

/// <summary>
/// Check if the leaderboard is currently loading.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,37 @@ public void SubmitScore(string leaderboardId, long score, string metadata,
}
}

public void RequestPermissions(Action<SignInStatus> callback, string[] scopes)
{
callback = AsOnGameThreadCallback(callback);
mTokenClient.RequestPermissions((code =>
{
UpdateClients();
callback(code);
}), scopes);
}

private void UpdateClients()
{
lock (GameServicesLock)
{
var account = mTokenClient.GetAccount();
mSavedGameClient = new AndroidSavedGameClient(account);
mEventsClient = new AndroidEventsClient(account);
mVideoClient = new AndroidVideoClient(mVideoClient.IsCaptureSupported(), account);
mRealTimeClient = new AndroidRealTimeMultiplayerClient(this, account);
mTurnBasedClient = new AndroidTurnBasedMultiplayerClient(this, account);
mTurnBasedClient.RegisterMatchDelegate(mConfiguration.MatchDelegate);
}
}

/// <summary>Returns whether or not user has given permissions for given scopes.</summary>
/// <seealso cref="GooglePlayGames.BasicApi.IPlayGamesClient.HasPermissions"/>
public bool HasPermissions(string[] scopes)
{
return mTokenClient.HasPermissions(scopes);
}

///<summary></summary>
/// <seealso cref="GooglePlayGames.BasicApi.IPlayGamesClient.GetRtmpClient"/>
public IRealTimeMultiplayerClient GetRtmpClient()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace GooglePlayGames.Android
{
using System;
using System.Linq;
using BasicApi;
using OurUtils;
using UnityEngine;
Expand Down Expand Up @@ -132,6 +133,91 @@ public void FetchTokens(bool silent, Action<int> callback)
PlayGamesHelperObject.RunOnGameThread(() => DoFetchToken(silent, callback));
}

public void RequestPermissions(Action<SignInStatus> callback, string[] scopes)
{
using (var bridgeClass = new AndroidJavaClass(HelperFragmentClass))
using (var currentActivity = AndroidHelperFragment.GetActivity())
using (var task =
bridgeClass.CallStatic<AndroidJavaObject>("showRequestPermissionsUi", currentActivity,
oauthScopes.Union(scopes).ToArray()))
{
AndroidTaskUtils.AddOnSuccessListener<AndroidJavaObject>(task, /* disposeResult= */ false,
accountWithNewScopes =>
{
if (accountWithNewScopes == null)
{
callback(SignInStatus.InternalError);
return;
}
account = accountWithNewScopes;
email = account.Call<string>("getEmail");
idToken = account.Call<string>("getIdToken");
authCode = account.Call<string>("getServerAuthCode");
oauthScopes = oauthScopes.Union(scopes).ToList();
callback(SignInStatus.Success);
});

AndroidTaskUtils.AddOnFailureListener(task, e =>
{
var failCode = ToSignInStatus(e.Call<int>("getStatusCode"));
OurUtils.Logger.e("Exception requesting new permissions: " + failCode);
callback(failCode);
});
}
}

private 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.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>Returns whether or not user has given permissions for given scopes.</summary>
/// <param name="scopes">array of scopes</param>
/// <returns><c>true</c>, if given, <c>false</c> otherwise.</returns>
public bool HasPermissions(string[] scopes)
{
using (var bridgeClass = new AndroidJavaClass(HelperFragmentClass))
using (var currentActivity = AndroidHelperFragment.GetActivity())
{
return bridgeClass.CallStatic<bool>("hasPermissions", currentActivity, scopes);
}
}

private void DoFetchToken(bool silent, Action<int> callback)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#if UNITY_ANDROID
namespace GooglePlayGames
{
using GooglePlayGames.BasicApi;
using System;

internal interface TokenClient
Expand Down Expand Up @@ -67,6 +68,10 @@ void GetAnotherServerAuthCode(bool reAuthenticateIfNeeded,
void SetHidePopups(bool flag);

void FetchTokens(bool silent, Action<int> callback);

void RequestPermissions(Action<SignInStatus> callback, string[] scopes);

bool HasPermissions(string[] scopes);
}
}
#endif
Loading

0 comments on commit dc10a3c

Please sign in to comment.