mirror of
https://github.com/clockworklabs/SpacetimeDB.git
synced 2026-05-13 11:17:50 -04:00
bf37b2947b
# Description of Changes This exposes JWT claims for csharp modules, similar to how they are exposed to rust modules in https://github.com/clockworklabs/SpacetimeDB/pull/3288. This adds the new types `AuthCtx` and `JwtClaims`, and adds an `AuthCtx` to the `ReducerContext`. `AuthCtx` represents the credentials associated with the request, and `JwtClaims` represents a jwt token. One difference from the rust version is that I didn't create helpers to build an `AuthCtx` from a jwt payload. The reason is that we would need to be able to compute the identity from the payload claims, which requires a blake3 hash implementation. The first two c# libraries I found had issues at runtime ([Blake3](https://www.nuget.org/packages/Blake3) is wrapping a rust implementation, and [HashifyNet](https://github.com/Deskasoft/HashifyNET/tree/main/HashifyNet/Algorithms/Blake3) seems to be broken by our trimming because it uses reflection heavily). I can look into taking the implementation from `HashifyNet`, since it is MIT licensed, but I don't think we need to block merging on that. # API and ABI breaking changes This adds the new types `AuthCtx` and `JwtClaims`, and adds an `AuthCtx` to the `ReducerContext`. This also adds a csharp wrapper for the get_jwt ABI function added in https://github.com/clockworklabs/SpacetimeDB/pull/3288. # Expected complexity level and risk 2. # Testing This has a very minimal unit test of JwtClaims. I manually tested using this locally with the csharp quickstart, and I was able to print jwt tokens inside the module.
19 lines
446 B
C#
19 lines
446 B
C#
namespace Runtime.Tests;
|
|
|
|
using SpacetimeDB;
|
|
|
|
public class JwtClaimsTest
|
|
{
|
|
[Fact]
|
|
public void TestSubject()
|
|
{
|
|
var jwt = new JwtClaims(
|
|
"{\"sub\":\"123\",\"name\":\"John Doe\",\"iss\":\"example.com\"}",
|
|
Identity.FromHexString(
|
|
"c200ef884364c1a99be0298dc68f2004e6b97c09d1b1658b7db22f51fb662059"
|
|
)
|
|
);
|
|
Assert.Equal("123", jwt.Subject);
|
|
}
|
|
}
|