jwt-decode icon indicating copy to clipboard operation
jwt-decode copied to clipboard

Use native Base64 APIs for decoding when available

Open jonkoops opened this issue 1 year ago • 4 comments

Checklist

  • [X] I have looked into the Readme and have not found a suitable solution or answer.
  • [X] I have searched the issues and have not found a suitable solution or answer.
  • [X] I have searched the Auth0 Community forums and have not found a suitable solution or answer.
  • [X] I agree to the terms within the Auth0 Code of Conduct.

Describe the problem you'd like to have solved

The proposal for native Base46 APIs that properly handle the Base64URL implementation has reached Stage 3 and has started being implemented in various engines and browsers. This provides an opportunity to use these APIs rather than the custom implementation now present in the package, and could possibly lead to improved performance as well.

Describe the ideal solution

Right now the ideal solution would be to use the built-in API if present and fall back to the custom internal implementation when not supported. Long term the best option would be to drop the internal implementation entirely.

Alternatives and current workarounds

No response

Additional context

No response

jonkoops avatar Oct 14 '24 10:10 jonkoops

The required API has now landed in Chrome, Firefox and Safari. It might be time to consider implementing this.

jonkoops avatar Sep 04 '25 15:09 jonkoops

I think this isn’t going to work for JWT decoding, because it usually expects a JSON string instead of a Uint8Array.

threeal avatar Sep 10 '25 06:09 threeal

Not sure what you mean. A string can be converted into an Uint8Array, and back to a string. JSON is just string data.

const data = Uint8Array.fromBase64("SGVsbG8gV29ybGQh")
const decoder = new TextDecoder();
const result = decoder.decode(data);

console.log(result); // "Hello World"

jonkoops avatar Sep 10 '25 07:09 jonkoops

I was thinking that compared to atob which already returning string, Uint8Array.fromBase64 return Uint8Array which need to be decoded first to string. But after looking through it, you're right, using Uint8Array.fromBase64 + TextDecoder is much simpler.

threeal avatar Sep 11 '25 03:09 threeal