PronounsPage/server/jwt-cli.ts
2025-03-15 16:30:26 +01:00

45 lines
1.3 KiB
TypeScript

import path from 'path';
import './dotenv.ts';
import jwt from './jwt.ts';
// Command-line script for encrypting and decrypting JWTs. Useful for
// debugging and testing.
// Example usage:
// echo '{"key": "value"}' | APP_ENV=development PORT=3001 pnpm run-file server/jwt-cli.ts encrypt
// echo 'eyJ...' | APP_ENV=development pnpm run-file server/jwt-cli.ts decrypt
/**
* Main function.
*/
(async () => {
if (process.argv.length < 3) {
console.error(`Usage: ${path.relative(process.cwd(), process.argv[1])} {encrypt|decrypt}`);
process.exit(2);
}
const operation = process.argv[2];
const input = await new Promise<string>((resolve) => {
let data = '';
process.stdin.on('data', (chunk) => data += chunk);
process.stdin.on('end', () => resolve(data));
});
const domainBase = process.env.NUXT_PUBLIC_DOMAIN_BASE ?? '';
switch (operation) {
case 'encrypt':
console.log(await jwt.sign('_', JSON.parse(input), '365d', domainBase));
break;
case 'decrypt':
console.log(JSON.stringify(await jwt.validate('_', input, domainBase), null, 2));
break;
default:
console.error('Invalid operation! Use \'encrypt\' or \'decrypt\'.');
process.exit(1);
}
})();