openct-tasks/bebras-server-modules-libs/jwt.js

27 lines
918 B
JavaScript

// browser JWT lib
// crypto-js libs required
function signJWT(data, key) {
var header = {
alg: 'HS256',
typ: 'JWT'
}
function base64url(source) {
encodedSource = CryptoJS.enc.Base64.stringify(source);
encodedSource = encodedSource.replace(/=+$/, '');
encodedSource = encodedSource.replace(/\+/g, '-');
encodedSource = encodedSource.replace(/\//g, '_');
return encodedSource;
}
var stringifiedHeader = CryptoJS.enc.Utf8.parse(JSON.stringify(header));
var encodedHeader = base64url(stringifiedHeader);
var stringifiedData = CryptoJS.enc.Utf8.parse(JSON.stringify(data));
var encodedData = base64url(stringifiedData);
var signature = encodedHeader + "." + encodedData;
signature = CryptoJS.HmacSHA256(signature, key);
signature = base64url(signature);
return encodedHeader + '.' + encodedData + '.' + signature
}