Skip to content

Commit

Permalink
Merge pull request #137 from PinguApps/135-session-token-util
Browse files Browse the repository at this point in the history
Added utility to convert secret and userId into session secret token
  • Loading branch information
pingu2k4 authored Aug 11, 2024
2 parents 60cf521 + 087477c commit ace3ebf
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/PinguApps.Appwrite.Shared/Utils/TokenUtils.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
25 changes: 25 additions & 0 deletions tests/PinguApps.Appwrite.Shared.Tests/Utils/TokenUtilsTests.cs
Original file line number Diff line number Diff line change
@@ -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<TokenUtils.SessionToken>(jsonBytes);
Assert.NotNull(session);
Assert.Equal(userId, session.Id);
Assert.Equal(secret, session.Secret);
}
}

0 comments on commit ace3ebf

Please sign in to comment.