Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Commit e3d214e

Browse files
committed
Include a Base64 decoder for extracting JWT payload on IE9
1 parent 248ab36 commit e3d214e

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

angularfire.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,19 +433,52 @@ angular.module("firebase").factory("angularFireAuth", [
433433
$route = $injector.get("$route");
434434
}
435435

436+
// Helper function to decode Base64 (polyfill for window.btoa on IE).
437+
// From: https://github.com/mshang/base64-js/blob/master/base64.js
438+
function decodeBase64(str) {
439+
var char_set =
440+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
441+
var output = ""; // final output
442+
var buf = ""; // binary buffer
443+
var bits = 8;
444+
for (var i = 0; i < str.length; ++i) {
445+
if (str[i] == "=") {
446+
break;
447+
}
448+
var c_num = char_set.indexOf(str.charAt(i));
449+
if (c_num == -1) {
450+
throw new Error("Not base64.");
451+
}
452+
var c_bin = c_num.toString(2);
453+
while (c_bin.length < 6) {
454+
c_bin = "0" + c_bin;
455+
}
456+
buf += c_bin;
457+
458+
while (buf.length >= bits) {
459+
var octet = buf.slice(0, bits);
460+
buf = buf.slice(bits);
461+
output += String.fromCharCode(parseInt(octet, 2));
462+
}
463+
}
464+
return output;
465+
}
466+
436467
// Helper function to extract claims from a JWT. Does *not* verify the
437468
// validity of the token.
438469
function deconstructJWT(token) {
439470
var segments = token.split(".");
440471
if (!segments instanceof Array || segments.length !== 3) {
441472
throw new Error("Invalid JWT");
442473
}
474+
var decoded = "";
443475
var claims = segments[1];
444476
if (window.atob) {
445-
return JSON.parse(decodeURIComponent(escape(window.atob(claims))));
477+
decoded = window.atob(claims);
446478
} else {
447-
throw new Error("window.atob not found, consider using a polyfill!");
479+
decoded = decodeBase64(claims);
448480
}
481+
return JSON.parse(decodeURIComponent(escape(decoded)));
449482
}
450483

451484
// Updates the provided model.

angularfire.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)