Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replaced - and _ characters in jwt #172

Merged
merged 1 commit into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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