Meiki
Meiki
CDCloudflare Developers
Created by Meiki on 7/21/2023 in #workers-help
base64UrlEncoding in Worker without Wrangler
Hi y'all! I have the following function to generate JWT tokens to the Google Ads API
const crypto = require('crypto');
const base64url = require('base64url');
const date = Math.floor(Date.now()/1000);
let header = {
"alg": "RS256",
"typ": "JWT"
};

let payload = {
"iss": "",
"sub": "",
"scope": "https://www.googleapis.com/auth/adwords",
"aud": "https://oauth2.googleapis.com/token",
"exp": date + 3600,
"iat": date
};

let encodedHeader = base64url.encode(JSON.stringify(header));
let encodedPayload = base64url.encode(JSON.stringify(payload));

let signatureInput = `${encodedHeader}.${encodedPayload}`;
let privateKey = ‘’;

let signer = crypto.createSign('RSA-SHA256');
signer.update(signatureInput);

let signature = base64url.fromBase64(signer.sign(privateKey, 'base64'));

let jwt = `${encodedHeader}.${encodedPayload}.${signature}`;

console.log(jwt);
const crypto = require('crypto');
const base64url = require('base64url');
const date = Math.floor(Date.now()/1000);
let header = {
"alg": "RS256",
"typ": "JWT"
};

let payload = {
"iss": "",
"sub": "",
"scope": "https://www.googleapis.com/auth/adwords",
"aud": "https://oauth2.googleapis.com/token",
"exp": date + 3600,
"iat": date
};

let encodedHeader = base64url.encode(JSON.stringify(header));
let encodedPayload = base64url.encode(JSON.stringify(payload));

let signatureInput = `${encodedHeader}.${encodedPayload}`;
let privateKey = ‘’;

let signer = crypto.createSign('RSA-SHA256');
signer.update(signatureInput);

let signature = base64url.fromBase64(signer.sign(privateKey, 'base64'));

let jwt = `${encodedHeader}.${encodedPayload}.${signature}`;

console.log(jwt);
I wanted to add this into a Worker for my integration, but realized the Node.js integration for CF Workers requires wrangler. While Crypto is available as a runtime API, unfortunately the package I'm using (or
node:buffer
node:buffer
) isn't. I am learning how to use Wrangler, but I'm in a bit of a time pinch currently. Is there a way to do this in the GUI, or am I stuck needing to develop locally?
3 replies