Skip to content

Instantly share code, notes, and snippets.

@cpilsworth
Created September 18, 2022 08:05
Show Gist options
  • Save cpilsworth/6f775414421aeb05faebdf17836674da to your computer and use it in GitHub Desktop.
Save cpilsworth/6f775414421aeb05faebdf17836674da to your computer and use it in GitHub Desktop.
cli utility to convert an aem service token into a jwt
import { stdin, stdout, exit } from 'node:process';
import { parseArgs } from 'node:util';
import { default as auth } from '@adobe/jwt-auth';
import { default as fs } from 'fs';
const options = {
'file': { type: 'string' },
};
const { values } = parseArgs({ options });
async function getAccessToken(json) {
let creds = json.integration;
const config = {
clientId: creds.technicalAccount.clientId,
clientSecret: creds.technicalAccount.clientSecret,
technicalAccountId: creds.id,
orgId: creds.org,
metaScopes: [creds.metascopes],
privateKey: creds.privateKey,
};
return auth(config).then((token) => token.access_token);
}
function getJson() {
if (values.file) {
return JSON.parse(fs.readFileSync(values.file, "utf-8"));
} else {
try {
return JSON.parse(fs.readFileSync(stdin.fd, "utf-8"));
} catch (e) {
usage();
exit(-1);
}
}
}
function usage() {
console.log("Usage: jwt.mjs --file <file.json> | jwt.mjs < file.json");
}
try {
const json = getJson();
const token = await getAccessToken(json);
stdout.isTTY ? console.log(token) : stdout.write(token);
} catch (e) {
console.error(e);
exit(-1)
}
@cpilsworth
Copy link
Author

requires node 18

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