Skip to content

Instantly share code, notes, and snippets.

@rfaisal
Last active December 4, 2015 19:28
Show Gist options
  • Select an option

  • Save rfaisal/9879506 to your computer and use it in GitHub Desktop.

Select an option

Save rfaisal/9879506 to your computer and use it in GitHub Desktop.
var crypto = require('crypto');
exports.createZumoJwt = function zumoJwt(masterKey, lifeTimeHrs, aud, userId, claims) {
function base64(input) {
return new Buffer(input, 'utf8').toString('base64');
}
function urlFriendly(b64) {
return b64.replace(/\+/g, '-').replace(/\//g, '_').replace(new RegExp("=", "g"), '');
}
function signature(input) {
var key = crypto.createHash('sha256').update(masterKey + "JWTSig").digest('binary');
var str = crypto.createHmac('sha256', key).update(input).digest('base64');
return urlFriendly(str);
}
var s1 = '{"alg":"HS256","typ":"JWT","kid":0}';
var j2 = {
"exp": new Date(Date.now() + 60 * 60 * lifeTimeHrs * 1000),
"iss":"urn:microsoft:windows-azure:zumo",
"ver":2,
"aud":aud,
"uid":userId
};
if(claims){
j2["urn:microsoft:credentials"]= claims;
}
var s2 = JSON.stringify(j2);
var b1 = urlFriendly(base64(s1));
var b2 = urlFriendly(base64(s2));
var b3 = signature(b1 + "." + b2);
return [b1,b2,b3].join(".");
}
@darcwader
Copy link
Copy Markdown

the line 16 has a syntax error 'extra ;' after new Date

@rfaisal
Copy link
Copy Markdown
Author

rfaisal commented Dec 4, 2015

Thanks, fixed it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment