Replace deprated escape() function with jwt-decode (#21990)

Closes #22034
This commit is contained in:
Abhishek Govula 2023-07-27 20:37:01 +05:30 committed by GitHub
parent 29b67fc8df
commit 0536078282
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 28 deletions

View file

@ -52,6 +52,7 @@
}, },
"dependencies": { "dependencies": {
"base64-js": "^1.5.1", "base64-js": "^1.5.1",
"js-sha256": "^0.9.0" "js-sha256": "^0.9.0",
"jwt-decode": "^3.1.2"
} }
} }

View file

@ -16,6 +16,7 @@
*/ */
import base64 from 'base64-js'; import base64 from 'base64-js';
import sha256 from 'js-sha256'; import sha256 from 'js-sha256';
import jwtDecode from 'jwt-decode';
if (typeof Promise === 'undefined') { if (typeof Promise === 'undefined') {
throw Error('Keycloak requires an environment that supports Promises. Make sure that you include the appropriate polyfill.'); throw Error('Keycloak requires an environment that supports Promises. Make sure that you include the appropriate polyfill.');
@ -969,7 +970,7 @@ function Keycloak (config) {
if (refreshToken) { if (refreshToken) {
kc.refreshToken = refreshToken; kc.refreshToken = refreshToken;
kc.refreshTokenParsed = decodeToken(refreshToken); kc.refreshTokenParsed = jwtDecode(refreshToken);
} else { } else {
delete kc.refreshToken; delete kc.refreshToken;
delete kc.refreshTokenParsed; delete kc.refreshTokenParsed;
@ -977,7 +978,7 @@ function Keycloak (config) {
if (idToken) { if (idToken) {
kc.idToken = idToken; kc.idToken = idToken;
kc.idTokenParsed = decodeToken(idToken); kc.idTokenParsed = jwtDecode(idToken);
} else { } else {
delete kc.idToken; delete kc.idToken;
delete kc.idTokenParsed; delete kc.idTokenParsed;
@ -985,7 +986,7 @@ function Keycloak (config) {
if (token) { if (token) {
kc.token = token; kc.token = token;
kc.tokenParsed = decodeToken(token); kc.tokenParsed = jwtDecode(token);
kc.sessionId = kc.tokenParsed.session_state; kc.sessionId = kc.tokenParsed.session_state;
kc.authenticated = true; kc.authenticated = true;
kc.subject = kc.tokenParsed.sub; kc.subject = kc.tokenParsed.sub;
@ -1020,30 +1021,6 @@ function Keycloak (config) {
} }
} }
function decodeToken(str) {
str = str.split('.')[1];
str = str.replace(/-/g, '+');
str = str.replace(/_/g, '/');
switch (str.length % 4) {
case 0:
break;
case 2:
str += '==';
break;
case 3:
str += '=';
break;
default:
throw 'Invalid token';
}
str = decodeURIComponent(escape(atob(str)));
str = JSON.parse(str);
return str;
}
function createUUID() { function createUUID() {
var hexDigits = '0123456789abcdef'; var hexDigits = '0123456789abcdef';
var s = generateRandomString(36, hexDigits).split(""); var s = generateRandomString(36, hexDigits).split("");

View file

@ -356,6 +356,9 @@ importers:
js-sha256: js-sha256:
specifier: ^0.9.0 specifier: ^0.9.0
version: 0.9.0 version: 0.9.0
jwt-decode:
specifier: ^3.1.2
version: 3.1.2
devDependencies: devDependencies:
'@rollup/plugin-commonjs': '@rollup/plugin-commonjs':
specifier: ^25.0.3 specifier: ^25.0.3
@ -4813,6 +4816,10 @@ packages:
object.values: 1.1.6 object.values: 1.1.6
dev: true dev: true
/jwt-decode@3.1.2:
resolution: {integrity: sha512-UfpWE/VZn0iP50d8cz9NrZLM9lSWhcJ+0Gt/nm4by88UL+J1SiKN8/5dkjMmbEzwL2CAe+67GsegCbIKtbp75A==}
dev: false
/kolorist@1.8.0: /kolorist@1.8.0:
resolution: {integrity: sha512-Y+60/zizpJ3HRH8DCss+q95yr6145JXZo46OTpFvDZWLfRCE4qChOyk1b26nMaNpfHHgxagk9dXT5OP0Tfe+dQ==} resolution: {integrity: sha512-Y+60/zizpJ3HRH8DCss+q95yr6145JXZo46OTpFvDZWLfRCE4qChOyk1b26nMaNpfHHgxagk9dXT5OP0Tfe+dQ==}
dev: true dev: true