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.

if using service worker, b64 decode of token in extractTokenPayload of oidcserviceworker.js fails for some tokens

See original GitHub issue

Issue and Steps to Reproduce

if using token like following one :

eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImtleXN0b3JlLUNIQU5HRS1NRSJ9.eyJzdWIiOiJ0ZXN0RXh0ZXJuYWwiLCJmYW1pbHlfbmFtZSI6IuOCouOCr-OCtSIsImdpdmVuX25hbWUiOiLlpKrpg44iLCJlbWFpbCI6InRlc3RAdGVzdC5jb20iLCJ1aWQiOiJ0ZXN0RXh0ZXJuYWwiLCJ1c2VyX3R5cGUiOiJleHRlcm5hbCIsImF1dGhfdGltZSI6MTY2MzIyMTczOCwibm9uY2UiOiJtbTRsRzNUNU5jbzAiLCJhdF9oYXNoIjoieGlfemNwYlMwZFY5MG45OUhPa21ldyIsImF1ZCI6ImZvbyIsImV4cCI6MTY2MzIyNTQwMSwiaWF0IjoxNjYzMjIxODAxLCJpc3MiOiJodHRwOi8vbG9jYWxob3N0OjMwMDIifQ.xQa9pfcfqO14BaEIF6UZQxe3i7mzqo0D4HxXnXgTMBAJEe2Q5424_nW5TIiGzcrD_yWG6yusv6hDauFkcRDBNjmHiyyqR3yESfCN3SXwTQvi7wIPiVvn92rynbU09EpPQcff9_5QCIFx9h7K-W91J-1WPnI3sxL6AwLfb-KIjoS_5UV1szW4IL6MeMXypbgUpAZksvlKkQM78qPgmC64KqdxKhYDxt8kFZjyg4jyFf_oIGp7FLDQyGA8u9B21gndrJEtRkKXB8Z3A2s5Ujv3cDpyV_gQa3OL1i42Ev7rU86OXCFFJsO71kL3PDKF9vg3n9m9DvClQhLTUIAwRGJ7hg

the decode of token in extractTokenPayload function of oidcserviceworker.js file fails in atob function call saying it is not encoded properly.

Versions

latest

Screenshots

Expected

decode of token should work

Actual

Uncaught DOMException: Failed to execute ‘atob’ on ‘Window’: The string to be decoded is not correctly encoded.

Additional Details

A Solution would be:

It is a unicode problem cf here https://stackoverflow.com/a/30106551/5277071 the 8th solution from this post: https://www.anycodings.com/1questions/145912/how-to-decode-jwt-token-in-javascript-without-using-a-library is good to solve the issue.

Following code solve the issue:

const b64DecodeUnicode = (str) =>
  decodeURIComponent(Array.prototype.map.call(atob(str), (c) => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)).join(''));
const parseJwt = (token) => JSON.parse(b64DecodeUnicode(token.split('.')[1].replace('-', '+').replace('_', '/')));
const extractTokenPayload = (accessToken) => {
  try {
    return parseJwt(accessToken);
  } catch (e) {
    console.warn(e);
  }
  return null;
};

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
guillaume-chervetcommented, Sep 21, 2022

Thank you very much @fffan64 , the version 6.6.3 fixed it !

0reactions
fffan64commented, Sep 22, 2022

sure, works fine ! thanks again

Read more comments on GitHub >

github_iconTop Results From Across the Web

' ' when getting claims/decode from token Java JWT Spring Boot
What you are decoding isn't the token, you're trying to decode the entire header value. Bearer isn't part of the token, ...
Read more >
Malformed token error with base64 encoded secret · Issue #783
Using a base64 encoded secret with an HS256 token generates a Malformed token error. A full output of the error is as follows:....
Read more >
javascript to Decode JWT token - Solved
i have a jwt token as one of the api response and can extract it. once its decoded, i will received a userID...
Read more >
Decoding a JWT token in NodeJS - Vinod Kumar Nair
A sample encoded JWT token. Here, if you look at carefully, JWT has three parts in it separated by a dot (.), whereas...
Read more >
react-oidc - bytemeta
if using service worker, b64 decode of token in extractTokenPayload of oidcserviceworker.js fails for some tokens. inomn. inomn OPEN · Updated 2 hours...
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