Skip to content

Commit

Permalink
Added CustomLogin
Browse files Browse the repository at this point in the history
  • Loading branch information
oliexe committed May 27, 2024
1 parent fb2372d commit a3724d3
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 54 deletions.
108 changes: 55 additions & 53 deletions Assets/Stash/Scripts/Core/StashClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Stash.Core.Exceptions;
using Stash.Models;


namespace Stash.Core
{
public static class StashClient
Expand Down Expand Up @@ -62,59 +63,6 @@ public static async Task<LinkResponse> LinkAccount(string challenge, string play
}
}

/// <summary>
/// Links the player's account to Stash account for Apple Account & Google Account.
///
/// </summary>
/// <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>
/// <returns>Returns a confirmation response, or throws StashAPIRequestError if fails.</returns>
public static async Task<LinkResponse> 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<RequestHeader> { authorizationHeader });

// Check the response status code
if (result.StatusCode == 200)
{
try
{
// 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 StashRequestError(result.StatusCode, result.Data);
}
}

/// <summary>
/// Links an Apple Game Center account to the Stash user's account.
Expand Down Expand Up @@ -230,5 +178,59 @@ public static async Task<LinkResponse> LinkGooglePlayGames(string challenge, str
throw new StashRequestError(result.StatusCode, result.Data);
}
}

/// <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>
/// <param name="code">Stash code challenge from the log in deeplink.</param>
/// <param name="playerId">Player identification, that will be used to identify purchases.</param>
/// <param name="idToken">Valid identification token (OICD) of the player.</param>
/// <returns>Returns a confirmation response, or throws StashAPIRequestError if fails.</returns>
public static async Task<LinkResponse> 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<RequestHeader> { authorizationHeader });

// Check the response status code
if (result.StatusCode == 200)
{
try
{
// 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 StashRequestError(result.StatusCode, result.Data);
}
}
}
}
3 changes: 2 additions & 1 deletion Assets/Stash/Scripts/Core/StashConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

}
}
17 changes: 17 additions & 0 deletions Assets/Stash/Scripts/Models/CustomLoginBody.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
3 changes: 3 additions & 0 deletions Assets/Stash/Scripts/Models/CustomLoginBody.cs.meta

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

0 comments on commit a3724d3

Please sign in to comment.