Skip to content

Commit

Permalink
Adding the env switch, StashClient renamed to StashAuth
Browse files Browse the repository at this point in the history
  • Loading branch information
oliexe committed Jun 20, 2024
1 parent 4bb7eba commit e944187
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 21 deletions.
Binary file not shown.
2 changes: 1 addition & 1 deletion Assets/Samples/Scripts/DeeplinkExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public async void ProcessLinking()
PlayGames.RefreshCredentials();
try
{
LinkResponse response = await StashClient.LinkGooglePlayGames(_stashChallenge, InternalPlayerId, PlayGames.AuthCode);
LinkResponse response = await StashAuth.LinkGooglePlayGames(_stashChallenge, InternalPlayerId, PlayGames.AuthCode);

Debug.Log("[STASH][Google Play Games] Account linked successfully.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
using UnityEngine;
using Stash.Core.Exceptions;
using Stash.Models;

using Stash.Scripts.Core;

namespace Stash.Core
{
public static class StashClient
/// <summary>
/// Linking the player's account to Stash web shop or using 3rd party authentication provider.
/// </summary>

public static class StashAuth
{
/// <summary>
/// Links the player's account to Stash account for Apple Account & Google Account.
Expand All @@ -16,8 +20,12 @@ public static class StashClient
/// <param name="challenge">Stash code challenge from the deeplink.</param>
/// <param name="playerId">Player identification, that will be used to identify purchases.</param>
/// <param name="idToken">Valid JWT token of the player.</param>
/// <param name="environment">Stash API environment (Defaults to Test).</param>
/// <returns>Returns a confirmation response, or throws StashAPIRequestError if fails.</returns>
public static async Task<LinkResponse> LinkAccount(string challenge, string playerId, string idToken)
public static async Task<LinkResponse> LinkAccount(string challenge,
string playerId,
string idToken,
StashEnvironment environment = StashEnvironment.Test)
{
// Create the authorization header with the access token
RequestHeader authorizationHeader = new()
Expand All @@ -27,7 +35,7 @@ public static async Task<LinkResponse> LinkAccount(string challenge, string play
};

// Create the request body with the challenge and internal user id
var requestBody = new LinkBody()
var requestBody = new LinkBody
{
codeChallenge = challenge,
user = new LinkBody.User
Expand All @@ -37,7 +45,7 @@ public static async Task<LinkResponse> LinkAccount(string challenge, string play
};

// Set the URL for the link account endpoint
const string requestUrl = StashConstants.RootUrlTest + StashConstants.LinkAccount;
string requestUrl = environment.GetRootUrl() + StashConstants.LinkAccount;
// Make a POST request to link the access token
Response result = await RestClient.Post(requestUrl, JsonUtility.ToJson(requestBody), new List<RequestHeader> { authorizationHeader });

Expand All @@ -56,11 +64,9 @@ public static async Task<LinkResponse> LinkAccount(string challenge, string play
throw new StashParseError(result.Data);
}
}
else
{
// Throw an error if the API request was not successful
throw new StashRequestError(result.StatusCode, result.Data);
}

// Throw an error if the API request was not successful
throw new StashRequestError(result.StatusCode, result.Data);
}


Expand All @@ -76,9 +82,10 @@ public static async Task<LinkResponse> LinkAccount(string challenge, string play
/// <param name="salt">A random string that GameKit uses to compute the hash and randomize it. (Base64 Encoded)</param>
/// <param name="publicKeyUrl">The URL for the public encryption key.</param>
/// <param name="timestamp">The signature’s creation date and time.</param>
/// <param name="environment">Stash API environment (Defaults to Test).</param>
/// <returns>A LinkResponse object.</returns>
public static async Task<LinkResponse> LinkAppleGameCenter(string challenge, string playerId, string bundleId, string teamPlayerID, string signature,
string salt, string publicKeyUrl, string timestamp )
string salt, string publicKeyUrl, string timestamp, StashEnvironment environment = StashEnvironment.Test)
{
// Create the request body with the challenge and internal user id
var requestBody = new LinkGameCenterBody()
Expand Down Expand Up @@ -106,7 +113,7 @@ public static async Task<LinkResponse> LinkAppleGameCenter(string challenge, str
};

// Set the URL for the link account endpoint
const string requestUrl = StashConstants.RootUrlTest + StashConstants.LinkAppleGameCenter;
string requestUrl = environment.GetRootUrl() + StashConstants.LinkAppleGameCenter;
// Make a POST request to link the access token
Response result = await RestClient.Post(requestUrl, JsonUtility.ToJson(requestBody));

Expand Down Expand Up @@ -139,7 +146,7 @@ public static async Task<LinkResponse> LinkAppleGameCenter(string challenge, str
/// <param name="playerId">Player identification, that will be used to identify purchases.</param>
/// <param name="authCode">The authorization code generated using RequestServerSideAccess</param>
/// <returns>A LinkResponse object.</returns>
public static async Task<LinkResponse> LinkGooglePlayGames(string challenge, string playerId, string authCode)
public static async Task<LinkResponse> LinkGooglePlayGames(string challenge, string playerId, string authCode, StashEnvironment environment = StashEnvironment.Test)
{
// Create the request body with the challenge and internal user id
var requestBody = new LinkGooglePlayGamesBody()
Expand All @@ -153,7 +160,7 @@ public static async Task<LinkResponse> LinkGooglePlayGames(string challenge, str
};

// Set the URL for the link account endpoint
const string requestUrl = StashConstants.RootUrlTest + StashConstants.LinkGooglePlayGames;
string requestUrl = environment.GetRootUrl() + StashConstants.LinkGooglePlayGames;
// Make a POST request to link the access token
Response result = await RestClient.Post(requestUrl, JsonUtility.ToJson(requestBody));

Expand All @@ -179,7 +186,7 @@ public static async Task<LinkResponse> LinkGooglePlayGames(string challenge, str
}
}

/// <summary>
/// <summary>
/// Log in to stash account created using 3rd party authentication provider.
/// For use with bespoke login provider. Not intended for general account linking.
/// </summary>
Expand All @@ -188,7 +195,7 @@ public static async Task<LinkResponse> LinkGooglePlayGames(string challenge, str
/// <param name="idToken">Valid identification token (OICD) of the player.</param>
/// <param name="profileImageUrl">URL to the player's profile image/avatar to be displayed during login and on web shop.</param>
/// <returns>Returns a confirmation response, or throws StashAPIRequestError if fails.</returns>
public static async Task<LinkResponse> CustomLogin(string code, string playerId, string idToken, string profileImageUrl)
public static async Task<LinkResponse> CustomLogin(string code, string playerId, string idToken, string profileImageUrl, StashEnvironment environment = StashEnvironment.Test)
{
// Create the authorization header with the access token
RequestHeader authorizationHeader = new()
Expand All @@ -209,7 +216,7 @@ public static async Task<LinkResponse> CustomLogin(string code, string playerId,
};

// Set the URL for the link account endpoint
const string requestUrl = StashConstants.RootUrlTest + StashConstants.CustomLogin;
string requestUrl = environment.GetRootUrl() + StashConstants.CustomLogin;
// Make a POST request to link the access token
Response result = await RestClient.Post(requestUrl, JsonUtility.ToJson(requestBody), new List<RequestHeader> { authorizationHeader });

Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions Assets/Stash/Scripts/Core/StashConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ namespace Stash.Core
{
public class StashConstants
{
//Root URLs
public const string RootUrl = "https://api.stash.gg";
public const string RootUrlTest = "https://test-api.stash.gg";

//Account Linking
public const string LinkAccount = "/sdk/link_code/link";
public const string CustomLogin = "/sdk/custom_login/approve_with_jwt";
public const string LinkAppleGameCenter = "/sdk/link_code/link_apple_game_center";
Expand Down
22 changes: 22 additions & 0 deletions Assets/Stash/Scripts/Core/StashEnvironment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Stash.Scripts.Core
{
public enum StashEnvironment
{
Test,
Production
}

public static class StashEnvironmentAdapter {
public static string GetRootUrl(this StashEnvironment env) {
switch (env)
{
case StashEnvironment.Test:
return Stash.Core.StashConstants.RootUrlTest;
case StashEnvironment.Production:
default:
return Stash.Core.StashConstants.RootUrl;

}
}
}
}
3 changes: 3 additions & 0 deletions Assets/Stash/Scripts/Core/StashEnvironment.cs.meta

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

5 changes: 2 additions & 3 deletions ProjectSettings/GvhProjectSettings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
<projectSettings>
<projectSetting name="Google.IOSResolver.VerboseLoggingEnabled" value="False" />
<projectSetting name="Google.PackageManagerResolver.VerboseLoggingEnabled" value="False" />
<projectSetting name="Google.VersionHandler.VerboseLoggingEnabled" value="True" />
<projectSetting name="Google.VersionHandler.VersionHandlingEnabled" value="True" />
<projectSetting name="Google.VersionHandler.VerboseLoggingEnabled" value="False" />
<projectSetting name="GooglePlayServices.AutoResolverEnabled" value="False" />
<projectSetting name="GooglePlayServices.PromptBeforeAutoResolution" value="False" />
<projectSetting name="GooglePlayServices.UseJetifier" value="True" />
</projectSettings>

0 comments on commit e944187

Please sign in to comment.