-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #137 from PinguApps/135-session-token-util
Added utility to convert secret and userId into session secret token
- Loading branch information
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
tests/PinguApps.Appwrite.Shared.Tests/Utils/TokenUtilsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |