Skip to content

Commit 920f71d

Browse files
committed
cleanup jwtgen scripts
1 parent 06336ea commit 920f71d

File tree

2 files changed

+59
-27
lines changed

2 files changed

+59
-27
lines changed

generate_jwt.js

+37-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,39 @@
11
import jwt from "jsonwebtoken";
2-
import * as dotenv from "dotenv";
3-
dotenv.config();
2+
import {
3+
SecretsManagerClient,
4+
GetSecretValueCommand,
5+
} from "@aws-sdk/client-secrets-manager";
6+
import { STSClient, GetCallerIdentityCommand } from "@aws-sdk/client-sts";
47

5-
const username = process.env.JWTGEN_USERNAME || "[email protected]"
8+
export const getSecretValue = async (secretId) => {
9+
const smClient = new SecretsManagerClient();
10+
const data = await smClient.send(
11+
new GetSecretValueCommand({ SecretId: secretId }),
12+
);
13+
if (!data.SecretString) {
14+
return null;
15+
}
16+
try {
17+
return JSON.parse(data.SecretString);
18+
} catch {
19+
return null;
20+
}
21+
};
22+
23+
const secrets = await getSecretValue("infra-core-api-config");
24+
const client = new STSClient({ region: "us-east-1" });
25+
const command = new GetCallerIdentityCommand({});
26+
let data;
27+
try {
28+
data = await client.send(command);
29+
} catch {
30+
console.error(
31+
`Could not get AWS STS credentials: are you logged in to AWS? Run "aws configure sso" to log in.`,
32+
);
33+
process.exit(1);
34+
}
35+
36+
const username = process.env.JWTGEN_USERNAME || data.UserId?.split(":")[1];
637
const payload = {
738
aud: "custom_jwt",
839
iss: "custom_jwt",
@@ -29,8 +60,9 @@ const payload = {
2960
ver: "1.0",
3061
};
3162

32-
const secretKey = process.env.JwtSigningKey;
33-
const token = jwt.sign(payload, secretKey, { algorithm: "HS256" });
63+
const token = jwt.sign(payload, secrets["jwt_key"], {
64+
algorithm: "HS256",
65+
});
3466
console.log(`USERNAME=${username}`);
3567
console.log("=====================");
3668
console.log(token);

get_msft_jwt.js

+22-22
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
import request from 'request';
1+
import request from "request";
22

33
const client_secret = process.env.AadClientSecret;
44
if (!client_secret) {
5-
console.error("Did not find client secret in environment.");
6-
process.exit(1);
5+
console.error("Did not find client secret in environment.");
6+
process.exit(1);
77
}
88

99
var options = {
10-
'method': 'POST',
11-
'url': 'https://login.microsoftonline.com/c8d9148f-9a59-4db3-827d-42ea0c2b6e2e/oauth2/token',
12-
'headers': {
13-
'Content-Type': 'application/x-www-form-urlencoded',
14-
'Cookie': 'esctx=PAQABBwEAAAApTwJmzXqdR4BN2miheQMYx8m4odNFiSkFXBDxAsyDVihl0yV2geMRVf-xYZ_GI34ZgJzPlzsLI4IyGrHFUcRyt_kOrGgfKtxKD_l8Shb9DAyh2xT4JeGXJhIyqsMO-lMmpvDuGjePONePVhmPE4TzQuQUh6V8Y4yWwBV10HljcSWz0Jp0DGs5MB4wMCl3CVwgAA; fpc=Asmn40XcT3RJkq8G_zKhA64gJa0wAQAAANHbRN4OAAAADPYZNQMAAADZ20TeDgAAABa8tnsBAAAAeN1E3g4AAAA; stsservicecookie=estsfd; x-ms-gateway-slice=estsfd'
15-
},
16-
form: {
17-
'grant_type': 'client_credentials',
18-
'client_id': '519866d4-45a8-44ae-9925-9fb61b85074e',
19-
'client_secret': client_secret,
20-
'resource': 'api://5e08cf0f-53bb-4e09-9df2-e9bdc3467296',
21-
'scope': 'api://5e08cf0f-53bb-4e09-9df2-e9bdc3467296/ACM.Events.Login'
22-
}
23-
};
24-
request(options, function (error, response) {
25-
if (error) throw new Error(error);
26-
console.log(JSON.parse(response.body)['access_token']);
27-
});
28-
10+
method: "POST",
11+
url: "https://login.microsoftonline.com/c8d9148f-9a59-4db3-827d-42ea0c2b6e2e/oauth2/token",
12+
headers: {
13+
"Content-Type": "application/x-www-form-urlencoded",
14+
Cookie:
15+
"esctx=PAQABBwEAAAApTwJmzXqdR4BN2miheQMYx8m4odNFiSkFXBDxAsyDVihl0yV2geMRVf-xYZ_GI34ZgJzPlzsLI4IyGrHFUcRyt_kOrGgfKtxKD_l8Shb9DAyh2xT4JeGXJhIyqsMO-lMmpvDuGjePONePVhmPE4TzQuQUh6V8Y4yWwBV10HljcSWz0Jp0DGs5MB4wMCl3CVwgAA; fpc=Asmn40XcT3RJkq8G_zKhA64gJa0wAQAAANHbRN4OAAAADPYZNQMAAADZ20TeDgAAABa8tnsBAAAAeN1E3g4AAAA; stsservicecookie=estsfd; x-ms-gateway-slice=estsfd",
16+
},
17+
form: {
18+
grant_type: "client_credentials",
19+
client_id: "519866d4-45a8-44ae-9925-9fb61b85074e",
20+
client_secret: client_secret,
21+
resource: "api://5e08cf0f-53bb-4e09-9df2-e9bdc3467296",
22+
scope: "api://5e08cf0f-53bb-4e09-9df2-e9bdc3467296/ACM.Events.Login",
23+
},
24+
};
25+
request(options, function (error, response) {
26+
if (error) throw new Error(error);
27+
console.log(JSON.parse(response.body)["access_token"]);
28+
});

0 commit comments

Comments
 (0)