Skip to content

Instantly share code, notes, and snippets.

@kmclaugh
Created May 20, 2022 19:31
Show Gist options
  • Save kmclaugh/2b991b74cc1b8c988d1ec487bff3c297 to your computer and use it in GitHub Desktop.
Save kmclaugh/2b991b74cc1b8c988d1ec487bff3c297 to your computer and use it in GitHub Desktop.
Use JSON Web Token (jwt) in Google Data Studio
/*
* based on https://www.labnol.org/code/json-web-token-201128
* Use https://jwt.io/#debugger-io to verify
*/
function createJwt(privateKey, expiresInHours, data) {
// Sign token using HMAC with SHA-256 algorithm
const header = {
alg: "HS256",
typ: "JWT",
};
const now = Date.now();
const expires = new Date(now);
expires.setHours(expires.getHours() + expiresInHours);
// iat = issued time, exp = expiration time
const payload = {
exp: Math.round(expires.getTime() / 1000),
iat: Math.round(now / 1000),
};
// add user payload
Object.keys(data).forEach(function(key) {
payload[key] = data[key];
});
// TODO: cleanup
json = true;
function base64Encode(text, json) {
const data = json ? JSON.stringify(text) : text;
return Utilities.base64EncodeWebSafe(data).replace(/=+$/, "");
}
const toSign =
Utilities.base64Encode(
JSON.stringify(header),
Utilities.Charset.UTF_8
).replace(/=+$/, "") +
"." +
Utilities.base64Encode(
JSON.stringify(payload),
Utilities.Charset.UTF_8
).replace(/=+$/, "");
const signatureBytes = Utilities.computeHmacSha256Signature(
toSign,
privateKey
);
const signature = base64Encode(signatureBytes, false);
return toSign + "." + signature;
}
function generateAccessToken(privateKey, data) {
// Your super secret private key
const accessToken = createJwt(
privateKey,
6, // expires in 6 hours
data || {}
);
return accessToken;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment