Skip to content

Commit

Permalink
Added LinkAppleGameCenter
Browse files Browse the repository at this point in the history
  • Loading branch information
oliexe committed Mar 7, 2024
1 parent 2dcb4db commit c7c3bb5
Show file tree
Hide file tree
Showing 19 changed files with 128 additions and 12 deletions.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
71 changes: 68 additions & 3 deletions Assets/Stash/Scripts/Core/StashClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public static StashClient Instance
}

/// <summary>
/// Links the players's account to Stash account for Apple Account & Google Account. A valid idToken is required.
/// Links the player's account to Stash account for Apple Account & Google Account.
/// Requires a valid JWT identity token issued by Apple or Google.
/// </summary>
/// <param name="challenge">Stash challenge from the deeplink.</param>
/// <param name="internalUserId">Internal user id, will be used to identify the purchases.</param>
Expand All @@ -48,10 +49,10 @@ public async Task<LinkResponse> LinkGoogleOrApple(string challenge, string inter
};

// Create the request body with the challenge and internal user id
var requestBody = new RequestBody()
var requestBody = new LinkBody()
{
code_challenge = challenge,
user = new RequestBody.User
user = new LinkBody.User
{
id = internalUserId
}
Expand All @@ -68,6 +69,7 @@ public async Task<LinkResponse> LinkGoogleOrApple(string challenge, string inter
try
{
// Parse the response data into a LinkResponse object
Debug.Log("[STASH] LinkGoogleOrApple successful Response: " + result.Data);
LinkResponse resultResponse = JsonUtility.FromJson<LinkResponse>(result.Data);
return resultResponse;
}
Expand All @@ -83,5 +85,68 @@ public async Task<LinkResponse> LinkGoogleOrApple(string challenge, string inter
throw new StashAPIRequestError(result.StatusCode, result.Data);
}
}

/// <summary>
/// Links an Apple Game Center account to the Stash user's account.
/// Requires signature generated using fetchItems(forIdentityVerificationSignature:)
/// </summary>
/// <param name="challenge">The challenge for linking the account.</param>
/// <param name="bundleId">The bundle ID of the iOS/macOS app. (CFBundleIdentifier)</param>
/// <param name="signature">The verification signature data that GameKit generates.</param>
/// <param name="salt">A random string that GameKit uses to compute the hash and randomize it.</param>
/// <param name="publicKeyUrl">The URL for the public encryption key.</param>
/// <param name="teamPlayerID">A unique identifier for a player of all the games that you distribute using your developer account.</param>
/// <param name="timestamp">The signature’s creation date and time.</param>
/// <returns>A LinkResponse object.</returns>
public async Task<LinkResponse> LinkAppleGameCenter(string challenge, string bundleId, string signature,
string salt, string publicKeyUrl, string teamPlayerID, string timestamp )
{

// Create the request body with the challenge and internal user id
var requestBody = new LinkGameCenterBody()
{
codeChallenge = challenge,
player = new LinkGameCenterBody.Player
{
bundleId = bundleId,
playerId = teamPlayerID
},
verification = new LinkGameCenterBody.Verification
{
signature = signature,
salt = salt,
publicKeyUrl = publicKeyUrl,
timestamp = timestamp
}
};

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

// Check the response status code
if (result.StatusCode == 200)
{
try
{
Debug.Log("[STASH] LinkAppleGameCenter successful Response: " + result.Data);
// Parse the response data into a LinkResponse object
LinkResponse resultResponse = JsonUtility.FromJson<LinkResponse>(result.Data);
return resultResponse;
}
catch
{
// Throw an error if there is an issue parsing the response data
throw new StashParseError(result.Data);
}
}
else
{
// Throw an error if the API request was not successful
throw new StashAPIRequestError(result.StatusCode, result.Data);
}
}

}
}
1 change: 1 addition & 0 deletions Assets/Stash/Scripts/Core/StashConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class StashConstants
public const string APIRootURL = "https://api-rest-f57w5ea4ya-uc.a.run.app";
public const string TestRootURL = "https://stash.requestcatcher.com";
public const string LinkAccount = "/sdk/link_code/link";
public const string LnkGameCenter = "/sdk/link_code/link_game_center";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Stash.Models
{
[Serializable]
public class RequestBody
public class LinkBody
{
public string code_challenge;
public User user;
Expand Down
28 changes: 28 additions & 0 deletions Assets/Stash/Scripts/Models/LinkGameCenterBody.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;

namespace Stash.Models
{
[Serializable]
public class LinkGameCenterBody
{
public string codeChallenge;
public Player player;
public Verification verification;

[Serializable]
public class Player
{
public string bundleId;
public string playerId;
}

[Serializable]
public class Verification
{
public string signature;
public string salt;
public string publicKeyUrl;
public string timestamp;
}
}
}
3 changes: 3 additions & 0 deletions Assets/Stash/Scripts/Models/LinkGameCenterBody.cs.meta

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

3 changes: 1 addition & 2 deletions Assets/Stash/Scripts/Models/LinkResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ namespace Stash.Models
{
public class LinkResponse
{
public string Key { get; set; }
public string Value { get; set; }
public string codeChallenge { get; set; }
}
}
8 changes: 8 additions & 0 deletions Assets/StashSamples.meta

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

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ public async Task Login()
Debug.Log($"GameKit TeamPlayerID: {TeamPlayerID} / GamePlayerID: {GamePlayerID}");
Debug.Log($"GameKit Timestamp: {Timestamp}");

//Save latest game center data to player prefs
PlayerPrefs.SetString("Signature", Signature);
PlayerPrefs.SetString("TeamPlayerID", TeamPlayerID);
PlayerPrefs.SetString("Salt", Salt);
PlayerPrefs.SetString("Timestamp", Timestamp);
PlayerPrefs.SetString("GamePlayerID", GamePlayerID);
PlayerPrefs.SetString("PublicKeyURL", fetchItemsResponse.PublicKeyUrl);

}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class DeeplinkHandler : MonoBehaviour

private void Awake()
{
StashClient.Instance.LinkGoogleOrApple("TEST", "TEST", "TEST");
if (Instance == null)
{
Instance = this;
Expand All @@ -34,19 +33,24 @@ private void Awake()
}
}

private void onDeepLinkActivated(string url)
private async void onDeepLinkActivated(string url)
{
//Extract the challenge parameter from the link.
stashChallenge = url.Split("/link?challenge=")[1];
if (!string.IsNullOrEmpty(stashChallenge))
{
//Work with the code challenge, prompt user for confirmation.
Debug.Log("Stash: Deep Link Challenge: " + stashChallenge);


//Show Confirm Panel
ConfirmPanel.SetActive(true);

//Get the Game Center Signature, Salt, Timestamp, TeamPlayerID and GamePlayerID from player prefs.
string Signature = PlayerPrefs.GetString("Signature");
string Salt = PlayerPrefs.GetString("Salt");
string Timestamp = PlayerPrefs.GetString("Timestamp");
string TeamPlayerID = PlayerPrefs.GetString("TeamPlayerID");
string PublicKeyURL = PlayerPrefs.GetString("PublicKeyURL");

//Call LinkAppleGameCenter
StashClient.Instance.LinkAppleGameCenter(stashChallenge, "com.Stash.iosdemo", Signature, Salt, PublicKeyURL, TeamPlayerID, Timestamp );
}
}

Expand Down
File renamed without changes.

0 comments on commit c7c3bb5

Please sign in to comment.