|
| 1 | +import { CognitoIdentityClient } from "@aws-sdk/client-cognito-identity"; |
| 2 | +import { fromCognitoIdentityPool } from "@aws-sdk/credential-provider-cognito-identity"; |
| 3 | +import { ILogger } from "../../bdcli/utils/logger_util.js"; |
| 4 | +import { UserPoolId } from "../boilingdata/boilingdata_api.js"; |
| 5 | +import { Signer } from "@aws-amplify/core"; |
| 6 | + |
| 7 | +interface AwsCredentials { |
| 8 | + readonly accessKeyId: string; |
| 9 | + readonly secretAccessKey: string; |
| 10 | + readonly sessionToken?: string; |
| 11 | + readonly credentialScope?: string; |
| 12 | +} |
| 13 | + |
| 14 | +async function getAwsCredentials(jwtIdToken: string, region: string): Promise<AwsCredentials> { |
| 15 | + const IdentityPoolId = "eu-west-1:bce21571-e3a6-47a4-8032-fd015213405f"; |
| 16 | + // const poolData = { UserPoolId, ClientId: "6timr8knllr4frovfvq8r2o6oo" }; |
| 17 | + // const Pool = new AmazonCognitoIdentity.CognitoUserPool(poolData); |
| 18 | + const Logins = `cognito-idp.${region}.amazonaws.com/${UserPoolId}`; |
| 19 | + const cognitoidentity = new CognitoIdentityClient({ |
| 20 | + credentials: fromCognitoIdentityPool({ |
| 21 | + client: new CognitoIdentityClient(), |
| 22 | + identityPoolId: IdentityPoolId, |
| 23 | + logins: { |
| 24 | + [Logins]: jwtIdToken, |
| 25 | + }, |
| 26 | + }), |
| 27 | + }); |
| 28 | + return cognitoidentity.config.credentials(); |
| 29 | +} |
| 30 | + |
| 31 | +export async function getSignedWssUrl(_logger: ILogger, token: string, region: string): Promise<string> { |
| 32 | + const creds = await getAwsCredentials(token, region); |
| 33 | + const url = "wss://4rpyi2ae3f.execute-api.eu-west-1.amazonaws.com/prodbd"; |
| 34 | + const signedWsUrl = Signer.signUrl(url, { |
| 35 | + access_key: creds.accessKeyId, |
| 36 | + secret_key: creds.secretAccessKey, |
| 37 | + session_token: creds.sessionToken, |
| 38 | + }); |
| 39 | + return signedWsUrl; |
| 40 | +} |
0 commit comments