2015-09-04 07:13:08 +00:00
|
|
|
/*jslint browser: true, sloppy: true */
|
2015-09-03 11:58:00 +00:00
|
|
|
//adapted from https://tools.ietf.org/html/draft-ietf-jose-json-web-signature-08#appendix-C
|
|
|
|
|
2015-09-04 07:13:08 +00:00
|
|
|
function base64urlEncode(arg) {
|
2015-09-03 11:58:00 +00:00
|
|
|
var s = window.btoa(arg); // Regular base64 encoder
|
|
|
|
s = s.split('=')[0]; // Remove any trailing '='s
|
2015-09-04 07:13:08 +00:00
|
|
|
s = s.replace(/\+/g, '-'); // 62nd char of encoding
|
2015-09-03 11:58:00 +00:00
|
|
|
s = s.replace(/\//g, '_'); // 63rd char of encoding
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2015-09-04 07:13:08 +00:00
|
|
|
function base64urlDecode(s) {
|
2015-09-03 11:58:00 +00:00
|
|
|
s = s.replace(/-/g, '+'); // 62nd char of encoding
|
|
|
|
s = s.replace(/_/g, '/'); // 63rd char of encoding
|
|
|
|
switch (s.length % 4) { // Pad with trailing '='s
|
2015-09-04 07:13:08 +00:00
|
|
|
case 0: // No pad chars in this case
|
|
|
|
break;
|
|
|
|
case 2: // Two pad chars
|
|
|
|
s += "==";
|
|
|
|
break;
|
|
|
|
case 3: // One pad char
|
|
|
|
s += "=";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw "Illegal base64url string!";
|
2015-09-03 11:58:00 +00:00
|
|
|
}
|
|
|
|
return window.atob(s); // Standard base64 decoder
|
2015-09-04 07:13:08 +00:00
|
|
|
}
|