Skip to content

Commit

Permalink
Merge branch 'feature/SDK-3994' into 'develop'
Browse files Browse the repository at this point in the history
SDK-3994 Improve auth with saved token

See merge request sdk_group/store-unity-sdk!363
  • Loading branch information
kseniashabalina committed Jan 13, 2025
2 parents df03cfe + 22a60b9 commit fdb3367
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 14 deletions.
8 changes: 3 additions & 5 deletions Assets/Xsolla.Demo/Login/Scripts/Login/Auth/SavedTokenAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ public class SavedTokenAuth : LoginAuthorization
{
public override void TryAuth(object[] _, Action onSuccess, Action<Error> onError)
{
var success = XsollaAuth.AuthViaSavedToken();
if (success)
onSuccess?.Invoke();
else
onError?.Invoke(null);
XsollaAuth.AuthBySavedToken(
() => onSuccess?.Invoke(),
_ => onError?.Invoke(null));
}
}
}
62 changes: 57 additions & 5 deletions Assets/Xsolla/Auth/XsollaAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static void SignIn(string username, string password, Action onSuccess, Ac
requestData,
response =>
{
XsollaToken.Create(response.access_token, response.refresh_token);
XsollaToken.Create(response.access_token, response.refresh_token, response.expires_in);
onSuccess?.Invoke();
},
onError,
Expand Down Expand Up @@ -376,9 +376,61 @@ public static void AuthWithSocialNetworkAccessToken(string accessToken, string a
/// <summary>
/// Authenticates the user by saved token. Returns `true` if the token is loaded successfully and the user is authenticated
/// </summary>
[Obsolete("Use AuthBySavedToken instead")]
public static bool AuthViaSavedToken()
{
return XsollaToken.TryLoadInstance();
var isTokenLoaded = XsollaToken.TryLoadInstance();
if (!isTokenLoaded)
{
XDebug.Log("Failed to auth via saved token");
return false;
}

var expirationTime = XsollaToken.ExpirationTime;
if (expirationTime <= 0)
{
XDebug.Log("XsollaToken has no expiration time");
return true;
}

if (expirationTime >= DateTimeOffset.UtcNow.ToUnixTimeSeconds())
return true;

XDebug.Log("XsollaToken is expired. Trying to refresh it");
RefreshToken(() => { }, _ => { });
return true;
}

/// <summary>
/// Authenticates the user by saved token
/// <param name="onSuccess">Called after successful authentication.</param>
/// <param name="onError">Called after the request resulted with an error.</param>
/// </summary>
public static void AuthBySavedToken(Action onSuccess, Action<Error> onError)
{
if (!XsollaToken.TryLoadInstance())
{
XDebug.Log("Failed to auth via saved token");
onError?.Invoke(new Error(errorMessage: "Failed to auth via saved token"));
return;
}

var expirationTime = XsollaToken.ExpirationTime;
if (expirationTime <= 0)
{
XDebug.Log("XsollaToken has no expiration time, trying to refresh it");
RefreshToken(onSuccess, onError);
return;
}

if (expirationTime < DateTimeOffset.UtcNow.ToUnixTimeSeconds())
{
XDebug.Log("XsollaToken is expired, trying to refresh it");
RefreshToken(() => onSuccess?.Invoke(), e => onError?.Invoke(e));
return;
}

onSuccess?.Invoke();
}

/// <summary>
Expand Down Expand Up @@ -685,7 +737,7 @@ public static void RefreshToken(Action onSuccess, Action<Error> onError, string
requestData,
response =>
{
XsollaToken.Create(response.access_token, response.refresh_token);
XsollaToken.Create(response.access_token, response.refresh_token, response.expires_in);
onSuccess?.Invoke();
},
error => onError?.Invoke(error));
Expand Down Expand Up @@ -716,7 +768,7 @@ public static void ExchangeCodeToToken(string code, Action onSuccess, Action<Err
requestData,
response =>
{
XsollaToken.Create(response.access_token, response.refresh_token);
XsollaToken.Create(response.access_token, response.refresh_token, response.expires_in);
onSuccess?.Invoke();
},
error => onError?.Invoke(error));
Expand Down Expand Up @@ -785,4 +837,4 @@ private static string GetScope()
return "offline";
}
}
}
}
1 change: 1 addition & 0 deletions Assets/Xsolla/Core/Token/Internal/TokenData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ internal class TokenData
{
public string accessToken;
public string refreshToken;
public int expirationTime;
}
}
46 changes: 42 additions & 4 deletions Assets/Xsolla/Core/Token/XsollaToken.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using UnityEngine;

namespace Xsolla.Core
Expand All @@ -10,6 +11,8 @@ public static class XsollaToken

public static string RefreshToken => Instance?.refreshToken;

public static int ExpirationTime => Instance?.expirationTime ?? 0;

public static bool Exists => Instance != null;

private static TokenData Instance { get; set; }
Expand All @@ -20,7 +23,9 @@ public static void Create(string accessToken)
accessToken = accessToken
};

XDebug.Log($"XsollaToken created (access only)\nAccess token: {accessToken}");
XDebug.Log("XsollaToken created (access only)"
+ $"\nAccess token: {accessToken}");

SaveInstance();
}

Expand All @@ -31,7 +36,26 @@ public static void Create(string accessToken, string refreshToken)
refreshToken = refreshToken
};

XDebug.Log($"XsollaToken created (access and refresh)\nAccess token: {accessToken}\nRefresh token: {refreshToken}");
XDebug.Log("XsollaToken created (access and refresh)"
+ $"\nAccess token: {accessToken}"
+ $"\nRefresh token: {refreshToken}");

SaveInstance();
}

public static void Create(string accessToken, string refreshToken, int expiresIn)
{
Instance = new TokenData {
accessToken = accessToken,
refreshToken = refreshToken,
expirationTime = (int) DateTimeOffset.UtcNow.AddSeconds(expiresIn).ToUnixTimeSeconds()
};

XDebug.Log("XsollaToken created (access and refresh and expiration time)"
+ $"\nAccess token: {accessToken}"
+ $"\nRefresh token: {refreshToken}"
+ $"\nExpiration time: {DateTimeOffset.FromUnixTimeSeconds(ExpirationTime).ToLocalTime()}");

SaveInstance();
}

Expand Down Expand Up @@ -64,9 +88,23 @@ public static bool TryLoadInstance()
Instance = data;

if (string.IsNullOrEmpty(RefreshToken))
XDebug.Log($"XsollaToken loaded (access only)\nAccess token: {AccessToken}");
{
XDebug.Log("XsollaToken loaded (access only)"
+ $"\nAccess token: {AccessToken}");
}
else if (ExpirationTime <= 0)
{
XDebug.Log("XsollaToken loaded (access and refresh)"
+ $"\nAccess token: {AccessToken}"
+ $"\nRefresh token: {RefreshToken}");
}
else
XDebug.Log($"XsollaToken loaded (access and refresh)\nAccess token: {AccessToken}\nRefresh token: {RefreshToken}");
{
XDebug.Log("XsollaToken loaded (access and refresh and expiration time)"
+ $"\nAccess token: {AccessToken}"
+ $"\nRefresh token: {RefreshToken}"
+ $"\nExpiration time: {DateTimeOffset.FromUnixTimeSeconds(ExpirationTime).ToLocalTime()}");
}

return true;
}
Expand Down

0 comments on commit fdb3367

Please sign in to comment.