diff --git a/src/PinguApps.Appwrite.Shared/Utils/TokenUtils.cs b/src/PinguApps.Appwrite.Shared/Utils/TokenUtils.cs new file mode 100644 index 00000000..ce89bb6a --- /dev/null +++ b/src/PinguApps.Appwrite.Shared/Utils/TokenUtils.cs @@ -0,0 +1,21 @@ +using System; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace PinguApps.Appwrite.Shared.Utils; +public static class TokenUtils +{ + internal record SessionToken( + [property: JsonPropertyName("id")] string Id, + [property: JsonPropertyName("secret")] string Secret + ); + + public static string GetSessionToken(string userId, string secret) + { + var session = new SessionToken(userId, secret); + + var jsonBytes = JsonSerializer.SerializeToUtf8Bytes(session); + + return Convert.ToBase64String(jsonBytes); + } +} diff --git a/tests/PinguApps.Appwrite.Shared.Tests/Utils/TokenUtilsTests.cs b/tests/PinguApps.Appwrite.Shared.Tests/Utils/TokenUtilsTests.cs new file mode 100644 index 00000000..46f815f3 --- /dev/null +++ b/tests/PinguApps.Appwrite.Shared.Tests/Utils/TokenUtilsTests.cs @@ -0,0 +1,25 @@ +using System.Text.Json; +using PinguApps.Appwrite.Shared.Utils; + +namespace PinguApps.Appwrite.Shared.Tests.Utils; +public class TokenUtilsTests +{ + [Fact] + public void GetSessionToken_ShouldReturnBase64EncodedString() + { + // Arrange + string userId = "testUser"; + string secret = "testSecret"; + + // Act + string result = TokenUtils.GetSessionToken(userId, secret); + + // Assert + Assert.NotNull(result); + var jsonBytes = Convert.FromBase64String(result); + var session = JsonSerializer.Deserialize(jsonBytes); + Assert.NotNull(session); + Assert.Equal(userId, session.Id); + Assert.Equal(secret, session.Secret); + } +}