uruk-project / Jwt

JSON Web Token implementation for .Net & .Net Core
MIT License
81 stars 13 forks source link

Allow to add an array of string direcly within a JwtObject #500

Closed ycrumeyrolle closed 3 years ago

AtkinsonHD commented 3 years ago

Slightly different, put potentially related, I am trying to add an Array of JwtObject to a claim

jwsDescriptor.AddClaim("ClaimName", new JwtObject
            {
                { "stuff1", "xyz789" },
                { "stuff2", "abc123" },
                { "subObject" , new JwtObject
                    {
                        { "prop1" , "abc123" },
                        { "prop2" , "xyz789" }
                    }
                },
                { "Modules" , [
                     Insert Array of JwtObjects
                  ]
                }
            }

Everything I have tried so far hasn't worked, you don't seam to be able to have a JwtArray of JwtObjects?

The closest I got is below.

JwtArray ModulesArray = new JwtArray();  //No way or no need to set size

JwtObject Module1 = new JwtObject();    //this works
Module1.Add("Name", "mod1");

 JwtObject Module2 = new JwtObject();
Module2.Add("Name", "mod2");
Module2.Add("Users", "5");
Module2.Add("Type", "Named");

ModulesArray.Add(Module1);   //object reference error
ModulesArray.Add(Module2);
ycrumeyrolle commented 3 years ago

You have to use an array initializer with braces instead of brackets, in addition to a a new JwtArray like this:

jwsDescriptor.AddClaim("ClaimName", new JwtObject
            {
                { "stuff1", "xyz789" },
                { "stuff2", "abc123" },
                { "subObject" , new JwtObject
                    {
                        { "prop1" , "abc123" },
                        { "prop2" , "xyz789" }
                    }
                },
                { "Modules" , new JwtArray 
                    {
                     "a", "b", 3
                    }
                }
            });

Version 2.0 will support array initialization without requiring to instanctiate a new JwtArray (this class will be deprecated)

AtkinsonHD commented 3 years ago

Good to know about the JwtArray, I will avoid using it.

Not sure if you fully understood, what I was trying to do,

I am trying to create an Array of JWT Objects, a Nested Array. as below is standard JSON notation with [ ].

jwsDescriptor.AddClaim("ClaimName", new JwtObject
            {
                { "stuff1", "xyz789" },
                { "stuff2", "abc123" },
                { "subObject" , new JwtObject
                    {
                        { "prop1" , "abc123" },
                        { "prop2" , "xyz789" }
                    }
                },
                { "Modules" , [
                    {
                        { "name" , "module1" },
                        { "prop1" , "abc123" },
                        { "prop2" , "xyz789" }
                    },
                    {
                        { "name" , "module2" },
                        { "prop1" , "abc123" },
                        { "prop2" , "xyz789" }
                    }
                  ]
                }
            });
ycrumeyrolle commented 3 years ago

You are in C#, you cannot use JSON notation.

AtkinsonHD commented 3 years ago

I know :( , is there any way to achieve the above currently (Array of JWT Objects, a Nested Array) or is that the enhancement?

ycrumeyrolle commented 3 years ago

This is probably not ideal but...:

jwsDescriptor.AddClaim("ClaimName", new JwtObject
            {
                { "stuff1", "xyz789" },
                { "stuff2", "abc123" },
                {
                    "subObject" , new JwtObject
                    {
                        { "prop1" , "abc123" },
                        { "prop2" , "xyz789" }
                    }
                },
                {
                    "Modules" , new JwtArray(new List<JwtValue>()) {
                      new JwtObject{
                        { "name" , "module1" },
                        { "prop1" , "abc123" },
                        { "prop2" , "xyz789" }
                    },
                    new JwtObject{
                        { "name" , "module2" },
                        { "prop1" , "abc123" },
                        { "prop2" , "xyz789" }
                    }
                  }
                }
            });
ycrumeyrolle commented 3 years ago

It produce:

  "ClaimName": {
    "stuff1": "xyz789",
    "stuff2": "abc123",
    "subObject": {
      "prop1": "abc123",
      "prop2": "xyz789"
    },
    "Modules": [
      {
        "name": "module1",
        "prop1": "abc123",
        "prop2": "xyz789"
      },
      {
        "name": "module2",
        "prop1": "abc123",
        "prop2": "xyz789"
      }
    ]
  }
AtkinsonHD commented 3 years ago

Perfect

ycrumeyrolle commented 3 years ago

Fixed in #509, available in package version 2.0.0-beta.1

https://github.com/uruk-project/Jwt/blob/4fadb07e89ee14840ea69f343a6a1a473a355c70/samples/JwsCreationSample/Program.cs#L30-L58

JsonObject replaces JwtObject. Arrays are now supported, JwtArray is not necessary anymore.

Otherwise, JSON serialization of POCO is now also supported aswell as anonymous types: https://github.com/uruk-project/Jwt/blob/1c98645df6fbd76d5ad3ac4b1075563db519bed4/samples/JwsCreationSample/Program.cs#L62-L84