-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-signed-wss-url.js
More file actions
51 lines (46 loc) · 2.08 KB
/
get-signed-wss-url.js
File metadata and controls
51 lines (46 loc) · 2.08 KB
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
const AWS = require("aws-sdk");
const { Signer } = require("@aws-amplify/core");
const process = require("process");
const AmazonCognitoIdentity = require("amazon-cognito-identity-js");
const region = "eu-west-1";
const UserPoolId = "eu-west-1_0GLV9KO1p";
const IdentityPoolId = "eu-west-1:bce21571-e3a6-47a4-8032-fd015213405f";
const webSocketUrl = "wss://e4f3t7fs58.execute-api.eu-west-1.amazonaws.com/devbd";
//const webSocketUrl = wss://4rpyi2ae3f.execute-api.eu-west-1.amazonaws.com/prodbd/
const Logins = `cognito-idp.${region}.amazonaws.com/${UserPoolId}`;
const poolData = { UserPoolId, ClientId: "6timr8knllr4frovfvq8r2o6oo" };
const Pool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
AWS.config.region = region;
function getIdToken(Username, Password) {
return new Promise((resolve, reject) => {
const params = { Username, Password };
const authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(params);
var userData = { Username, Pool };
var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: (result) => resolve(result?.getIdToken()),
onFailure: (err) => reject(err),
});
});
}
async function refreshCredsWithToken(idToken) {
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
clientConfig: { region },
IdentityPoolId,
Logins: { [Logins]: idToken },
});
await AWS.config.credentials.getPromise();
}
async function printSignedWssUrl(usernane, password) {
const idToken = await getIdToken(usernane, password);
await refreshCredsWithToken(idToken.getJwtToken());
const params = {
access_key: AWS.config.credentials.data.Credentials.AccessKeyId,
secret_key: AWS.config.credentials.data.Credentials.SecretKey,
session_token: AWS.config.credentials.data.Credentials.SessionToken,
};
const signedWsUrl = await Signer.signUrl(webSocketUrl, params);
const username = idToken.decodePayload()["cognito:username"];
console.log(JSON.stringify({ username, signedWsUrl }));
}
printSignedWssUrl(process.argv[2], process.argv[3]);