From c7c3bb5cf4a138e9fc483f4efef34d9c77a85918 Mon Sep 17 00:00:00 2001 From: Ondrej Rehacek Date: Thu, 7 Mar 2024 01:56:53 +0100 Subject: [PATCH] Added LinkAppleGameCenter --- Assets/{ => Stash}/Images.meta | 0 Assets/{ => Stash}/Images/StashLogo.png | Bin Assets/{ => Stash}/Images/StashLogo.png.meta | 0 Assets/{ => Stash}/Scenes.meta | 0 .../Scenes/StashGameCenterSample.unity | 0 .../Scenes/StashGameCenterSample.unity.meta | 0 Assets/Stash/Scripts/Core/StashClient.cs | 71 +++++++++++++++++- Assets/Stash/Scripts/Core/StashConstants.cs | 1 + .../Models/{RequestBody.cs => LinkBody.cs} | 2 +- .../{RequestBody.cs.meta => LinkBody.cs.meta} | 0 .../Scripts/Models/LinkGameCenterBody.cs | 28 +++++++ .../Scripts/Models/LinkGameCenterBody.cs.meta | 3 + Assets/Stash/Scripts/Models/LinkResponse.cs | 3 +- Assets/StashSamples.meta | 8 ++ Assets/{ => StashSamples}/Scripts.meta | 0 .../Scripts/AppleGameCenterExampleScript.cs | 8 ++ .../AppleGameCenterExampleScript.cs.meta | 0 .../Scripts/DeeplinkHandler.cs | 16 ++-- .../Scripts/DeeplinkHandler.cs.meta | 0 19 files changed, 128 insertions(+), 12 deletions(-) rename Assets/{ => Stash}/Images.meta (100%) rename Assets/{ => Stash}/Images/StashLogo.png (100%) rename Assets/{ => Stash}/Images/StashLogo.png.meta (100%) rename Assets/{ => Stash}/Scenes.meta (100%) rename Assets/{ => Stash}/Scenes/StashGameCenterSample.unity (100%) rename Assets/{ => Stash}/Scenes/StashGameCenterSample.unity.meta (100%) rename Assets/Stash/Scripts/Models/{RequestBody.cs => LinkBody.cs} (89%) rename Assets/Stash/Scripts/Models/{RequestBody.cs.meta => LinkBody.cs.meta} (100%) create mode 100644 Assets/Stash/Scripts/Models/LinkGameCenterBody.cs create mode 100644 Assets/Stash/Scripts/Models/LinkGameCenterBody.cs.meta create mode 100644 Assets/StashSamples.meta rename Assets/{ => StashSamples}/Scripts.meta (100%) rename Assets/{ => StashSamples}/Scripts/AppleGameCenterExampleScript.cs (83%) rename Assets/{ => StashSamples}/Scripts/AppleGameCenterExampleScript.cs.meta (100%) rename Assets/{ => StashSamples}/Scripts/DeeplinkHandler.cs (65%) rename Assets/{ => StashSamples}/Scripts/DeeplinkHandler.cs.meta (100%) diff --git a/Assets/Images.meta b/Assets/Stash/Images.meta similarity index 100% rename from Assets/Images.meta rename to Assets/Stash/Images.meta diff --git a/Assets/Images/StashLogo.png b/Assets/Stash/Images/StashLogo.png similarity index 100% rename from Assets/Images/StashLogo.png rename to Assets/Stash/Images/StashLogo.png diff --git a/Assets/Images/StashLogo.png.meta b/Assets/Stash/Images/StashLogo.png.meta similarity index 100% rename from Assets/Images/StashLogo.png.meta rename to Assets/Stash/Images/StashLogo.png.meta diff --git a/Assets/Scenes.meta b/Assets/Stash/Scenes.meta similarity index 100% rename from Assets/Scenes.meta rename to Assets/Stash/Scenes.meta diff --git a/Assets/Scenes/StashGameCenterSample.unity b/Assets/Stash/Scenes/StashGameCenterSample.unity similarity index 100% rename from Assets/Scenes/StashGameCenterSample.unity rename to Assets/Stash/Scenes/StashGameCenterSample.unity diff --git a/Assets/Scenes/StashGameCenterSample.unity.meta b/Assets/Stash/Scenes/StashGameCenterSample.unity.meta similarity index 100% rename from Assets/Scenes/StashGameCenterSample.unity.meta rename to Assets/Stash/Scenes/StashGameCenterSample.unity.meta diff --git a/Assets/Stash/Scripts/Core/StashClient.cs b/Assets/Stash/Scripts/Core/StashClient.cs index 5fac66c..1d72d2d 100644 --- a/Assets/Stash/Scripts/Core/StashClient.cs +++ b/Assets/Stash/Scripts/Core/StashClient.cs @@ -32,7 +32,8 @@ public static StashClient Instance } /// - /// 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. /// /// Stash challenge from the deeplink. /// Internal user id, will be used to identify the purchases. @@ -48,10 +49,10 @@ public async Task 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 } @@ -68,6 +69,7 @@ public async Task 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(result.Data); return resultResponse; } @@ -83,5 +85,68 @@ public async Task LinkGoogleOrApple(string challenge, string inter throw new StashAPIRequestError(result.StatusCode, result.Data); } } + + /// + /// Links an Apple Game Center account to the Stash user's account. + /// Requires signature generated using fetchItems(forIdentityVerificationSignature:) + /// + /// The challenge for linking the account. + /// The bundle ID of the iOS/macOS app. (CFBundleIdentifier) + /// The verification signature data that GameKit generates. + /// A random string that GameKit uses to compute the hash and randomize it. + /// The URL for the public encryption key. + /// A unique identifier for a player of all the games that you distribute using your developer account. + /// The signature’s creation date and time. + /// A LinkResponse object. + public async Task 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(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); + } + } + } } \ No newline at end of file diff --git a/Assets/Stash/Scripts/Core/StashConstants.cs b/Assets/Stash/Scripts/Core/StashConstants.cs index aaf83cd..476fdd8 100644 --- a/Assets/Stash/Scripts/Core/StashConstants.cs +++ b/Assets/Stash/Scripts/Core/StashConstants.cs @@ -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"; } } \ No newline at end of file diff --git a/Assets/Stash/Scripts/Models/RequestBody.cs b/Assets/Stash/Scripts/Models/LinkBody.cs similarity index 89% rename from Assets/Stash/Scripts/Models/RequestBody.cs rename to Assets/Stash/Scripts/Models/LinkBody.cs index e888ad7..23c4b22 100644 --- a/Assets/Stash/Scripts/Models/RequestBody.cs +++ b/Assets/Stash/Scripts/Models/LinkBody.cs @@ -3,7 +3,7 @@ namespace Stash.Models { [Serializable] - public class RequestBody + public class LinkBody { public string code_challenge; public User user; diff --git a/Assets/Stash/Scripts/Models/RequestBody.cs.meta b/Assets/Stash/Scripts/Models/LinkBody.cs.meta similarity index 100% rename from Assets/Stash/Scripts/Models/RequestBody.cs.meta rename to Assets/Stash/Scripts/Models/LinkBody.cs.meta diff --git a/Assets/Stash/Scripts/Models/LinkGameCenterBody.cs b/Assets/Stash/Scripts/Models/LinkGameCenterBody.cs new file mode 100644 index 0000000..e12568a --- /dev/null +++ b/Assets/Stash/Scripts/Models/LinkGameCenterBody.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/Assets/Stash/Scripts/Models/LinkGameCenterBody.cs.meta b/Assets/Stash/Scripts/Models/LinkGameCenterBody.cs.meta new file mode 100644 index 0000000..8d4192e --- /dev/null +++ b/Assets/Stash/Scripts/Models/LinkGameCenterBody.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 553d7a81f0db4677b20775168c5c8bdf +timeCreated: 1709770034 \ No newline at end of file diff --git a/Assets/Stash/Scripts/Models/LinkResponse.cs b/Assets/Stash/Scripts/Models/LinkResponse.cs index e1dc319..91f00f0 100644 --- a/Assets/Stash/Scripts/Models/LinkResponse.cs +++ b/Assets/Stash/Scripts/Models/LinkResponse.cs @@ -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; } } } \ No newline at end of file diff --git a/Assets/StashSamples.meta b/Assets/StashSamples.meta new file mode 100644 index 0000000..fa7d74a --- /dev/null +++ b/Assets/StashSamples.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: d2633f3d3da07420cad506e27f4ccddb +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts.meta b/Assets/StashSamples/Scripts.meta similarity index 100% rename from Assets/Scripts.meta rename to Assets/StashSamples/Scripts.meta diff --git a/Assets/Scripts/AppleGameCenterExampleScript.cs b/Assets/StashSamples/Scripts/AppleGameCenterExampleScript.cs similarity index 83% rename from Assets/Scripts/AppleGameCenterExampleScript.cs rename to Assets/StashSamples/Scripts/AppleGameCenterExampleScript.cs index 5e6548b..1ebf806 100644 --- a/Assets/Scripts/AppleGameCenterExampleScript.cs +++ b/Assets/StashSamples/Scripts/AppleGameCenterExampleScript.cs @@ -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 { diff --git a/Assets/Scripts/AppleGameCenterExampleScript.cs.meta b/Assets/StashSamples/Scripts/AppleGameCenterExampleScript.cs.meta similarity index 100% rename from Assets/Scripts/AppleGameCenterExampleScript.cs.meta rename to Assets/StashSamples/Scripts/AppleGameCenterExampleScript.cs.meta diff --git a/Assets/Scripts/DeeplinkHandler.cs b/Assets/StashSamples/Scripts/DeeplinkHandler.cs similarity index 65% rename from Assets/Scripts/DeeplinkHandler.cs rename to Assets/StashSamples/Scripts/DeeplinkHandler.cs index 06846e6..514b04f 100644 --- a/Assets/Scripts/DeeplinkHandler.cs +++ b/Assets/StashSamples/Scripts/DeeplinkHandler.cs @@ -14,7 +14,6 @@ public class DeeplinkHandler : MonoBehaviour private void Awake() { - StashClient.Instance.LinkGoogleOrApple("TEST", "TEST", "TEST"); if (Instance == null) { Instance = this; @@ -34,7 +33,7 @@ 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]; @@ -42,11 +41,16 @@ private void onDeepLinkActivated(string url) { //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 ); } } diff --git a/Assets/Scripts/DeeplinkHandler.cs.meta b/Assets/StashSamples/Scripts/DeeplinkHandler.cs.meta similarity index 100% rename from Assets/Scripts/DeeplinkHandler.cs.meta rename to Assets/StashSamples/Scripts/DeeplinkHandler.cs.meta