Skip to content

Commit

Permalink
fix: replaced - and _ characters in jwt
Browse files Browse the repository at this point in the history
  • Loading branch information
peetya committed Jun 19, 2023
1 parent 2e346a3 commit cdb02eb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/core/jwt/jwtToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@
*/
export function parseJwtPayload(token) {
try {
return JSON.parse(atob(token.split('.')[1]));
let base64Payload = token.split('.')[1];
base64Payload = base64Payload.replace(/-/g, '+'); // replace - with +
base64Payload = base64Payload.replace(/_/g, '/'); // replace _ with /

return JSON.parse(atob(base64Payload));
} catch (e) {
return null;
}
Expand Down
8 changes: 8 additions & 0 deletions test/core/jwt/jwtToken/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ define(['core/jwt/jwtToken'], jwtToken => {
assert.equal(parseJwtPayload(), null, 'missing token returns null');
});

QUnit.test('parses payload object from full token with unsupported characters', assert => {
assert.expect(2);
const token = 'eyJhbGciOiJIUzI1NiJ9.eyJmb28iOiI_In0.qXbg9lEnmvDekuDfNqiAdqYb3Yx1iTLw7RyUGoz5I9w';
const result = parseJwtPayload(token);
assert.ok(typeof result === 'object', 'parsed payload is an object');
assert.equal(result.foo, '?');
});

QUnit.module('getJwtTTL');

const time1 = 1620651921250;
Expand Down

0 comments on commit cdb02eb

Please sign in to comment.