1- import { Signer } from "@aws-amplify/core" ;
21import { CognitoIdentity , CognitoIdentityCredentials } from "aws-sdk" ;
32import { CognitoIdToken , CognitoUserPool , CognitoUser , AuthenticationDetails } from "amazon-cognito-identity-js" ;
3+ import { getSignedWssUrl } from "./signature" ;
44
55// FIXME: Hard coded
66const region = "eu-west-1" ;
77const UserPoolId = "eu-west-1_0GLV9KO1p" ;
88const IdentityPoolId = "eu-west-1:bce21571-e3a6-47a4-8032-fd015213405f" ;
9- const webSocketUrl = "wss:// m9fhs4t5vh.execute-api.eu-west-1.amazonaws.com/dev " ;
9+ const webSocketHost = "m9fhs4t5vh.execute-api.eu-west-1.amazonaws.com" ;
1010const Logins = `cognito-idp.${ region } .amazonaws.com/${ UserPoolId } ` ;
1111const poolData = { UserPoolId, ClientId : "6timr8knllr4frovfvq8r2o6oo" } ;
1212const Pool = new CognitoUserPool ( poolData ) ;
@@ -18,38 +18,33 @@ export interface BDCredentials {
1818
1919function getIdToken ( Username : string , Password : string ) : Promise < CognitoIdToken > {
2020 return new Promise ( ( resolve , reject ) => {
21- const params = { Username, Password } ;
21+ const loginDetails = { Username, Password } ;
2222 const userData = { Username, Pool } ;
2323 const cognitoUser = new CognitoUser ( userData ) ;
24- const authenticationDetails = new AuthenticationDetails ( params ) ;
24+ const authenticationDetails = new AuthenticationDetails ( loginDetails ) ;
2525 cognitoUser . authenticateUser ( authenticationDetails , {
26- onSuccess : result => resolve ( result ?. getIdToken ( ) ) ,
27- onFailure : err => reject ( err ) ,
26+ onSuccess : ( result : any ) => resolve ( result ?. getIdToken ( ) ) ,
27+ onFailure : ( err : any ) => reject ( err ) ,
2828 } ) ;
2929 } ) ;
3030}
3131
3232async function refreshCredsWithToken ( idToken : string ) : Promise < CognitoIdentityCredentials > {
33- const creds = new CognitoIdentityCredentials (
34- {
35- IdentityPoolId,
36- Logins : { [ Logins ] : idToken } ,
37- } ,
38- { region } ,
39- ) ;
33+ const idParams = { IdentityPoolId, Logins : { [ Logins ] : idToken } } ;
34+ const creds = new CognitoIdentityCredentials ( idParams , { region } ) ;
4035 await creds . getPromise ( ) ;
4136 return creds ;
4237}
4338
4439export async function getBoilingDataCredentials ( username : string , password : string ) : Promise < BDCredentials > {
4540 const idToken = await getIdToken ( username , password ) ;
4641 const creds = await refreshCredsWithToken ( idToken . getJwtToken ( ) ) ;
47- const params = {
48- access_key : creds . data ?. Credentials ?. AccessKeyId ,
49- secret_key : ( < CognitoIdentity . Types . GetCredentialsForIdentityResponse > creds . data ) ?. Credentials ?. SecretKey ,
50- session_token : creds . data ?. Credentials ?. SessionToken ,
51- } ;
52- const signedWebsocketUrl = await Signer . signUrl ( webSocketUrl , params ) ;
42+ const accessKeyId = creds . data ?. Credentials ?. AccessKeyId ;
43+ const secretAccessKey = ( < CognitoIdentity . Types . GetCredentialsForIdentityResponse > creds . data ) ?. Credentials ?. SecretKey ;
44+ const sessionToken = creds . data ?. Credentials ?. SessionToken ;
45+ if ( ! accessKeyId || ! secretAccessKey ) throw new Error ( "Missing credentials (after refresh)!" ) ;
46+ const credentials = { accessKeyId , secretAccessKey , sessionToken } ;
47+ const signedWebsocketUrl = await getSignedWssUrl ( webSocketHost , credentials ) ;
5348 const cognitoUsername = idToken . decodePayload ( ) [ "cognito:username" ] ;
5449 return { cognitoUsername, signedWebsocketUrl } ;
5550}
0 commit comments