b64 to utf8 utils

This commit is contained in:
Kyle Spearrin 2018-04-23 13:45:40 -04:00
parent e5ae1cbb1c
commit 768ab7dd0a
2 changed files with 12 additions and 4 deletions

View File

@ -96,8 +96,8 @@ export class Utils {
} }
} }
static urlBase64Decode(str: string): string { static fromUrlB64ToUtf8(b64Str: string): string {
let output = str.replace(/-/g, '+').replace(/_/g, '/'); let output = b64Str.replace(/-/g, '+').replace(/_/g, '/');
switch (output.length % 4) { switch (output.length % 4) {
case 0: case 0:
break; break;
@ -111,7 +111,15 @@ export class Utils {
throw new Error('Illegal base64url string!'); throw new Error('Illegal base64url string!');
} }
return decodeURIComponent(escape(window.atob(output))); return Utils.fromB64ToUtf8(output);
}
static fromB64ToUtf8(b64Str: string): string {
if (Utils.isNode) {
return new Buffer(b64Str, 'base64').toString('utf8');
} else {
return decodeURIComponent(escape(window.atob(b64Str)));
}
} }
// ref: http://stackoverflow.com/a/2117523/1090359 // ref: http://stackoverflow.com/a/2117523/1090359

View File

@ -95,7 +95,7 @@ export class TokenService implements TokenServiceAbstraction {
throw new Error('JWT must have 3 parts'); throw new Error('JWT must have 3 parts');
} }
const decoded = Utils.urlBase64Decode(parts[1]); const decoded = Utils.fromUrlB64ToUtf8(parts[1]);
if (decoded == null) { if (decoded == null) {
throw new Error('Cannot decode the token'); throw new Error('Cannot decode the token');
} }