Skip to content

Commit

Permalink
Adds NatsJwt.SetScopedUserClaims (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
ckasabula and Chuck Kasabula authored Aug 14, 2024
1 parent 247e8b6 commit 848530c
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
65 changes: 65 additions & 0 deletions NATS.Jwt.Tests/Models/NatsUserClaimsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,69 @@ public void Deserialize_NullUser_ShouldDeserializeNull()

Assert.Null(deserialized.User);
}

[Fact]
public void TestSetScoped()
{
var initUser = (NatsUser user) =>
{
user.Pub = new NatsPermission { Allow = ["allowed.>"], Deny = ["denied.>"], };
user.Sub = new NatsPermission { Allow = ["allowed.>"], Deny = ["denied.>"], };
user.Resp = new NatsResponsePermission { MaxMsgs = 100, Expires = TimeSpan.FromSeconds(60), };
user.Src = ["192.168.1.0/24"];
user.Times = [new TimeRange { Start = "09:00:00", End = "17:00:00", }];
user.Locale = "America/New_York";
user.Subs = 1000;
user.Data = 10_000_000;
user.Payload = 1024;
user.BearerToken = true;
user.AllowedConnectionTypes = ["STANDARD", "WEBSOCKET"];
};

var claims = new NatsUserClaims();
initUser(claims.User);
claims.SetScoped(true);

Assert.Equal(claims.User.Pub, new NatsPermission());
Assert.Equal(claims.User.Sub, new NatsPermission());
Assert.Equal(claims.User.Resp, default);
Assert.Equal(claims.User.Src, default);
Assert.Equal(claims.User.Times, default);
Assert.Equal(claims.User.Locale, default);
Assert.Equal(claims.User.Subs, 0);
Assert.Equal(claims.User.Data, 0);
Assert.Equal(claims.User.Payload, 0);
Assert.Equal(claims.User.BearerToken, default);
Assert.Equal(claims.User.AllowedConnectionTypes, default);

initUser(claims.User);
claims.SetScoped(false);

Assert.NotNull(claims.User.Pub);
Assert.NotNull(claims.User.Pub.Allow);
Assert.Equal(claims.User.Pub.Allow.Count, 1);
Assert.Equal(claims.User.Pub.Allow[0], "allowed.>");
Assert.NotNull(claims.User.Pub.Deny);
Assert.Equal(claims.User.Pub.Deny.Count, 1);
Assert.Equal(claims.User.Pub.Deny[0], "denied.>");
Assert.NotNull(claims.User.Sub);
Assert.NotNull(claims.User.Sub.Allow);
Assert.Equal(claims.User.Sub.Allow.Count, 1);
Assert.Equal(claims.User.Sub.Allow[0], "allowed.>");
Assert.NotNull(claims.User.Sub.Deny);
Assert.Equal(claims.User.Sub.Deny.Count, 1);
Assert.Equal(claims.User.Sub.Deny[0], "denied.>");
Assert.Equal(claims.User.Resp, new NatsResponsePermission { MaxMsgs = 100, Expires = TimeSpan.FromSeconds(60), });
Assert.Equal(claims.User.Src, default);
Assert.Equal(claims.User.Times, default);
Assert.Equal(claims.User.Locale, default);
Assert.Equal(claims.User.Subs, NatsJwt.NoLimit);
Assert.Equal(claims.User.Data, NatsJwt.NoLimit);
Assert.Equal(claims.User.Payload, NatsJwt.NoLimit);
Assert.Equal(claims.User.BearerToken, true);
Assert.NotNull(claims.User.AllowedConnectionTypes);
Assert.Equal(claims.User.AllowedConnectionTypes.Count, 2);
Assert.Equal(claims.User.AllowedConnectionTypes[0], "STANDARD");
Assert.Equal(claims.User.AllowedConnectionTypes[1], "WEBSOCKET");
}
}
31 changes: 31 additions & 0 deletions NATS.Jwt/Models/NatsUserClaims.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,35 @@ public record NatsUserClaims : JwtClaimsData
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
[JsonPropertyOrder(1)]
public NatsUser User { get; set; } = new();

/// <summary>
/// Sets the user claims as scoped or not.
/// </summary>
/// <param name="scoped">Indicates whether the user claims should be scoped or not.</param>
public void SetScoped(bool scoped)
{
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
User.Src = default;
User.Times = default;
User.Locale = default;

if (scoped)
{
User.Pub = new NatsPermission();
User.Sub = new NatsPermission();
User.Resp = default;
User.Subs = 0;
User.Data = 0;
User.Payload = 0;
User.BearerToken = default;
User.AllowedConnectionTypes = default;
}
else
{
User.Subs = NatsJwt.NoLimit;
User.Data = NatsJwt.NoLimit;
User.Payload = NatsJwt.NoLimit;
}
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
}
}
1 change: 1 addition & 0 deletions NATS.Jwt/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ NATS.Jwt.Models.NatsUser.Times.set -> void
NATS.Jwt.Models.NatsUserClaims
NATS.Jwt.Models.NatsUserClaims.User.get -> NATS.Jwt.Models.NatsUser!
NATS.Jwt.Models.NatsUserClaims.User.set -> void
NATS.Jwt.Models.NatsUserClaims.SetScoped(bool scoped) -> void
NATS.Jwt.Models.NatsWeightedMapping
NATS.Jwt.Models.NatsWeightedMapping.Cluster.get -> string!
NATS.Jwt.Models.NatsWeightedMapping.Cluster.set -> void
Expand Down

0 comments on commit 848530c

Please sign in to comment.