Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add custom claims with object value #498

Closed
althunibat opened this issue Oct 8, 2020 · 3 comments
Closed

add custom claims with object value #498

althunibat opened this issue Oct 8, 2020 · 3 comments

Comments

@althunibat
Copy link

i couldn't find a way to add custom claims to JwsDescriptor which accepts claim value as object, where it get automatically json serialized when jwt is written.

e.g.

descriptor.AddClaim("https://hasura.io/jwt/claims", new HasuraClaim
{
UserId = user.Id,
DefaultRole = "user",
Roles = new[] { "user" }
});

@ycrumeyrolle
Copy link
Collaborator

Hi, there is not way to serialize a custom object for the moment. You can this achieve by using a JwtObject and a JwtArray:

            var descriptor = new JwsDescriptor();
            descriptor.AddClaim("https//hasura.io/jwt/claims", new JwtObject
            {
                { "user_id", 123 },
                { "default_role", "user" },
                { "roles", new JwtArray { "user"} }
            });

This is "by design" for avoiding two issues:
1/ Casing issue : how should be cased the custom property names? With the JwtObject, you specify the casing.
2/ Type mismatch : custom objects contains custom value types and there is always debates on its representation in JSON. The JwtObject only support native JSON types (String, Number, Boolean & Null) and the DateTime because there is convention for the mapping (epoch time).

@ycrumeyrolle
Copy link
Collaborator

ycrumeyrolle commented Oct 9, 2020

For convenience it should be interessant to allow

                { "roles", new[] { "user"} }

for hiding the JwtArray in case of string array.

@ycrumeyrolle
Copy link
Collaborator

Fixed in version 2.0.0-beta.1

descriptor.Payload.Add("ClaimName_anonymous_type", new
{
stuff1 = "xyz789",
stuff2 = "abc123",
subObject = new
{
prop1 = "abc123",
prop2 = "xyz789"
},
Modules = new[]
{
new {
name = "module1",
prop1 = "abc123",
prop2 = "xyz789"
},
new {
name = "module2",
prop1 = "abc123",
prop2 = "xyz789"
}
}
});

If required, the JSON serialization behavior can be controled with the JsonSerializationBehavior.SerializerOptions property, for example by adding a JsonConverter<HasuraClaim> to JsonSerializationBehavior.SerializerOptions.Converters

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants