11import { CognitoIdentity , CognitoIdentityCredentials } from "aws-sdk" ;
22import { CognitoIdToken , CognitoUserPool , CognitoUser , AuthenticationDetails } from "amazon-cognito-identity-js" ;
33import { getSignedWssUrl } from "./signature" ;
4+ import { BDAWSRegion } from "boilingdata/boilingdata" ;
45
5- // FIXME: Hard coded
6- const region = "eu-west-1" ;
6+ const IDP_REGION = "eu-west-1" ;
77const UserPoolId = "eu-west-1_0GLV9KO1p" ;
8+ const Logins = `cognito-idp.${ IDP_REGION } .amazonaws.com/${ UserPoolId } ` ;
89const IdentityPoolId = "eu-west-1:bce21571-e3a6-47a4-8032-fd015213405f" ;
9- const webSocketHost = "m9fhs4t5vh.execute-api.eu-west-1.amazonaws.com" ;
10- const Logins = `cognito-idp.${ region } .amazonaws.com/${ UserPoolId } ` ;
1110const poolData = { UserPoolId, ClientId : "6timr8knllr4frovfvq8r2o6oo" } ;
1211const Pool = new CognitoUserPool ( poolData ) ;
1312
@@ -31,20 +30,30 @@ function getIdToken(Username: string, Password: string): Promise<CognitoIdToken>
3130
3231async function refreshCredsWithToken ( idToken : string ) : Promise < CognitoIdentityCredentials > {
3332 const idParams = { IdentityPoolId, Logins : { [ Logins ] : idToken } } ;
34- const creds = new CognitoIdentityCredentials ( idParams , { region } ) ;
33+ const creds = new CognitoIdentityCredentials ( idParams , { region : IDP_REGION } ) ;
3534 await creds . getPromise ( ) ;
3635 return creds ;
3736}
3837
39- export async function getBoilingDataCredentials ( username : string , password : string ) : Promise < BDCredentials > {
38+ function getWsApiDomain ( region : string ) : string {
39+ return `${ region } .api.boilingdata.com` ;
40+ // return `api.boilingdata.com`;
41+ }
42+
43+ export async function getBoilingDataCredentials (
44+ username : string ,
45+ password : string ,
46+ region : BDAWSRegion = "eu-west-1" ,
47+ ) : Promise < BDCredentials > {
48+ const webSocketHost = getWsApiDomain ( region ) ;
4049 const idToken = await getIdToken ( username , password ) ;
4150 const creds = await refreshCredsWithToken ( idToken . getJwtToken ( ) ) ;
4251 const accessKeyId = creds . data ?. Credentials ?. AccessKeyId ;
4352 const secretAccessKey = ( < CognitoIdentity . Types . GetCredentialsForIdentityResponse > creds . data ) ?. Credentials ?. SecretKey ;
4453 const sessionToken = creds . data ?. Credentials ?. SessionToken ;
4554 if ( ! accessKeyId || ! secretAccessKey ) throw new Error ( "Missing credentials (after refresh)!" ) ;
4655 const credentials = { accessKeyId, secretAccessKey, sessionToken } ;
47- const signedWebsocketUrl = await getSignedWssUrl ( webSocketHost , credentials ) ;
56+ const signedWebsocketUrl = await getSignedWssUrl ( webSocketHost , credentials , region , "wss" , "" ) ;
4857 const cognitoUsername = idToken . decodePayload ( ) [ "cognito:username" ] ;
4958 return { cognitoUsername, signedWebsocketUrl } ;
5059}
0 commit comments