Created
September 18, 2022 08:05
-
-
Save cpilsworth/6f775414421aeb05faebdf17836674da to your computer and use it in GitHub Desktop.
cli utility to convert an aem service token into a jwt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
requires node 18