-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_jwt.js
68 lines (64 loc) · 1.76 KB
/
generate_jwt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import jwt from "jsonwebtoken";
import {
SecretsManagerClient,
GetSecretValueCommand,
} from "@aws-sdk/client-secrets-manager";
import { STSClient, GetCallerIdentityCommand } from "@aws-sdk/client-sts";
export const getSecretValue = async (secretId) => {
const smClient = new SecretsManagerClient();
const data = await smClient.send(
new GetSecretValueCommand({ SecretId: secretId }),
);
if (!data.SecretString) {
return null;
}
try {
return JSON.parse(data.SecretString);
} catch {
return null;
}
};
const secrets = await getSecretValue("infra-core-api-config");
const client = new STSClient({ region: "us-east-1" });
const command = new GetCallerIdentityCommand({});
let data;
try {
data = await client.send(command);
} catch {
console.error(
`Could not get AWS STS credentials: are you logged in to AWS? Run "aws configure sso" to log in.`,
);
process.exit(1);
}
const username = process.env.JWTGEN_USERNAME || data.UserId?.split(":")[1];
const payload = {
aud: "custom_jwt",
iss: "custom_jwt",
iat: Math.floor(Date.now() / 1000),
nbf: Math.floor(Date.now() / 1000),
exp: Math.floor(Date.now() / 1000) + 3600 * 24, // Token expires after 24 hour
acr: "1",
aio: "AXQAi/8TAAAA",
amr: ["pwd"],
appid: "your-app-id",
appidacr: "1",
email: username,
groups: ["0"],
idp: "https://login.microsoftonline.com",
ipaddr: "192.168.1.1",
name: "Doe, John",
oid: "00000000-0000-0000-0000-000000000000",
rh: "rh-value",
scp: "user_impersonation",
sub: "subject",
tid: "tenant-id",
unique_name: username,
uti: "uti-value",
ver: "1.0",
};
const token = jwt.sign(payload, secrets["jwt_key"], {
algorithm: "HS256",
});
console.log(`USERNAME=${username}`);
console.log("=====================");
console.log(token);