question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

support arrays in custom JWT claims

See original GitHub issue

Support for custom claims is great! But it chokes when we try to use an array –

code: “jwt-invalid-claims”
path: “$”
message: “x-hasura-* claims: expected Text, encountered Array”

Use case here is user “groups” (multiple). Not essential but including in the JWT would simplify our permission rules and save us some joins.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:16
  • Comments:22 (9 by maintainers)

github_iconTop GitHub Comments

5reactions
Albert-Gaocommented, May 24, 2020

Need this feature,

otherwise, for a multi-tenant app, you might need to write Hasura Action for every query/mutation out there…

Any update on this?

In the documentation:

You can find this is allowed for x-hasura-allowed-roles:

{
  "sub": "1234567890",
  "name": "John Doe",
  "admin": true,
  "iat": 1516239022,
  "https://hasura.io/jwt/claims": {
    "x-hasura-allowed-roles": ["editor","user", "mod"],
    "x-hasura-default-role": "user",
    "x-hasura-user-id": "1234567890",
    "x-hasura-org-id": "123",
    "x-hasura-custom": "custom-value"
  }
}

So why we can not use it for the other claims?

Furthermore, the Postgres Array type is not a valid JSON format, one needs to manipulate JSON string to get it work. Not to mention, when work with AWS, which does not support JSON typed claims, so you have to pass a JSON string, and in that JSON string, we can only put a stringfied array there, like “{1,2,3}”, because otherwise, JSON.stringfy won’t even work., will this stringfied array still work?

4reactions
patrykwegrzyncommented, Jun 22, 2020

Not sure if this helps I use something like this with nodejs , it will only works for the strings tho

function toPgArray(arr) {
  const m = arr.map(e=> `"${e}"`).join(",")
  return `{${m}}`
}

const ids = ["e63baafd-a10f-4d88-8828-0d423d698522"];
toPgArray(ids) 

results in

{
  "https://hasura.io/jwt/claims": {
  "sub": "1234567890",
  "name": "John Doe",
  "admin": true,
  "iat": 1516239022,
    "x-hasura-allowed-organisations": "{\"e63baafd-a10f-4d88-8828-0d423d698524\"}"
  }
}

the other solution would be to make proxy service that can convert arrays in claim to pg array format

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add a claim to JWT as an array? - Stack Overflow
I add claim as json array to claim for AuthorizedCompanies and parse it in client side.What is the design pattern here if any...
Read more >
How to Validate Multiple Values of a Custom Claim in a JWT ...
1. If claims are in the format of arrays as below and want to validate that both values are present "groups": [ "group1",...
Read more >
Help using JWT Policy In Apigee - custom claim as array
Solved: We got the JWT policy support in our orgs. When I am trying to assign an array to our custom claims, the...
Read more >
Optional Custom Claim in jwt IDToken appearing as Array not ...
As this is defined as a string and configured as a string in the attribute, not an array, It should not come out...
Read more >
Best Practices for Implementing Claims Succesfully | Curity
Once the custom claims issuing is complete, the main API endpoints will receive a useful JWT access token. On every request, the API...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found