From a3724d3ec64255ea3076c7909a024cc511917411 Mon Sep 17 00:00:00 2001 From: Ondrej Rehacek Date: Mon, 27 May 2024 23:29:13 +0200 Subject: [PATCH] Added CustomLogin --- Assets/Stash/Scripts/Core/StashClient.cs | 108 +++++++++--------- Assets/Stash/Scripts/Core/StashConstants.cs | 3 +- .../Stash/Scripts/Models/CustomLoginBody.cs | 17 +++ .../Scripts/Models/CustomLoginBody.cs.meta | 3 + 4 files changed, 77 insertions(+), 54 deletions(-) create mode 100644 Assets/Stash/Scripts/Models/CustomLoginBody.cs create mode 100644 Assets/Stash/Scripts/Models/CustomLoginBody.cs.meta diff --git a/Assets/Stash/Scripts/Core/StashClient.cs b/Assets/Stash/Scripts/Core/StashClient.cs index 91cd9ef..253e819 100644 --- a/Assets/Stash/Scripts/Core/StashClient.cs +++ b/Assets/Stash/Scripts/Core/StashClient.cs @@ -4,6 +4,7 @@ using Stash.Core.Exceptions; using Stash.Models; + namespace Stash.Core { public static class StashClient @@ -62,59 +63,6 @@ public static async Task LinkAccount(string challenge, string play } } - /// - /// Links the player's account to Stash account for Apple Account & Google Account. - /// - /// - /// Stash code challenge from the deeplink. - /// Player identification, that will be used to identify purchases. - /// Valid JWT token of the player. - /// Returns a confirmation response, or throws StashAPIRequestError if fails. - public static async Task LinkCustom(string challenge, string playerId, string idToken) - { - // Create the authorization header with the access token - RequestHeader authorizationHeader = new() - { - Key = "Authorization", - Value = "Bearer " + idToken - }; - - // Create the request body with the challenge and internal user id - var requestBody = new LinkBody() - { - codeChallenge = challenge, - user = new LinkBody.User - { - id = playerId - } - }; - - // Set the URL for the link account endpoint - const string requestUrl = StashConstants.RootUrlTest + StashConstants.LinkCustomAccount; - // Make a POST request to link the access token - Response result = await RestClient.Post(requestUrl, JsonUtility.ToJson(requestBody), new List { authorizationHeader }); - - // Check the response status code - if (result.StatusCode == 200) - { - try - { - // 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 StashRequestError(result.StatusCode, result.Data); - } - } /// /// Links an Apple Game Center account to the Stash user's account. @@ -230,5 +178,59 @@ public static async Task LinkGooglePlayGames(string challenge, str throw new StashRequestError(result.StatusCode, result.Data); } } + + /// + /// Log in to stash account created using 3rd party authentication provider. + /// For use with bespoke login provider. Not intended for general account linking. + /// + /// Stash code challenge from the log in deeplink. + /// Player identification, that will be used to identify purchases. + /// Valid identification token (OICD) of the player. + /// Returns a confirmation response, or throws StashAPIRequestError if fails. + public static async Task CustomLogin(string code, string playerId, string idToken) + { + // Create the authorization header with the access token + RequestHeader authorizationHeader = new() + { + Key = "Authorization", + Value = "Bearer " + idToken + }; + + // Create the request body with the challenge and internal user id + var requestBody = new CustomLoginBody() + { + code = code, + user = new CustomLoginBody.User + { + id = playerId + } + }; + + // Set the URL for the link account endpoint + const string requestUrl = StashConstants.RootUrlTest + StashConstants.CustomLogin; + // Make a POST request to link the access token + Response result = await RestClient.Post(requestUrl, JsonUtility.ToJson(requestBody), new List { authorizationHeader }); + + // Check the response status code + if (result.StatusCode == 200) + { + try + { + // 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 StashRequestError(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 6c4bde6..0aed169 100644 --- a/Assets/Stash/Scripts/Core/StashConstants.cs +++ b/Assets/Stash/Scripts/Core/StashConstants.cs @@ -6,8 +6,9 @@ public class StashConstants public const string RootUrlTest = "https://test-api.stash.gg"; public const string LinkAccount = "/sdk/link_code/link"; - public const string LinkCustomAccount = "/sdk/custom_login/approve"; + public const string CustomLogin = "/sdk/custom_login/approve_with_jwt"; public const string LinkAppleGameCenter = "/sdk/link_code/link_apple_game_center"; public const string LinkGooglePlayGames = "/sdk/link_code/link_google_play"; + } } diff --git a/Assets/Stash/Scripts/Models/CustomLoginBody.cs b/Assets/Stash/Scripts/Models/CustomLoginBody.cs new file mode 100644 index 0000000..87d1fa9 --- /dev/null +++ b/Assets/Stash/Scripts/Models/CustomLoginBody.cs @@ -0,0 +1,17 @@ +using System; + +namespace Stash.Models +{ + [Serializable] + public class CustomLoginBody + { + public string code; + public User user; + + [Serializable] + public class User + { + public string id; + } + } +} \ No newline at end of file diff --git a/Assets/Stash/Scripts/Models/CustomLoginBody.cs.meta b/Assets/Stash/Scripts/Models/CustomLoginBody.cs.meta new file mode 100644 index 0000000..1e7ce31 --- /dev/null +++ b/Assets/Stash/Scripts/Models/CustomLoginBody.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 69c87b1bba164062b0e250081b6fd981 +timeCreated: 1716844725 \ No newline at end of file