How to generate jti ( JWT Id) in java script?

124 Views Asked by At

To fetch EPIC data through mirth I want to create JWT so I followed documentation https://fhir.epic.com/Documentation?docId=oauth2&section=BackendOAuth2Guide.

I dont know to how to generate jti which is required field in payload. There are no specific rules or steps available online to follow and generate jti. Please guide me.

I tried the accepted answers as viewers suggested I tried generating jti using the accepted answers here.

Got this r9d2ex8zs for following code

return Math.random().toString(36).split('').filter( function(value, index, self) { 
    return self.indexOf(value) === index;
}).join('').substr(2,50);

Got this h75tvne5n00000000000000000 for following syntax

return ((Math.random().toString(36)+'00000000000000000').slice(2, 50)).slice(2, 50);
2

There are 2 best solutions below

1
Spomky-Labs On BEST ANSWER

You can generate a random string. No need for any dependencies or external API. The function in the accepted answer is fine for this use case.

0
Ashavan On

Your best option for generating the JWT and all of the necessary fields in the payload is to use a library. This will make the process simpler and less prone to error. You can find a variety of libraries in numerous languages here.

If for some reason you can't or don't want to use a library, the jti is simply a unique identifier for the JWT while the JWT is active (i.e., before the exp timestamp). There are a couple of strategies you can use to generate it:

  1. Use the uuid package to generate a v4 UUID.
  2. Use Math.random() multiplied by a very large number to generate a random integer. Something like Math.floor(Math.random() * 1000000000000).
  3. Use an external API like random.org to get a random number or string.