11#!/usr/bin/env node
22
3- var AWS = require ( 'aws-sdk' ) ;
4- var AWSCognito = require ( 'amazon-cognito-identity-js' ) ;
5- var apigClientFactory = require ( 'aws-api-gateway-client' ) . default ;
3+ var packageJson = require ( "./package.json" ) ;
64
7- var argv = require ( 'yargs' )
8- . option ( 'username' , {
9- describe : 'Username of the user' ,
5+ var AWS = require ( "aws-sdk" ) ;
6+ var AWSCognito = require ( "amazon-cognito-identity-js" ) ;
7+ var apigClientFactory = require ( "aws-api-gateway-client" ) . default ;
8+ var WindowMock = require ( "window-mock" ) . default ;
9+
10+ global . window = { localStorage : new WindowMock ( ) . localStorage } ;
11+ global . navigator = function ( ) {
12+ return null ;
13+ } ;
14+
15+ var argv = require ( "yargs" )
16+ . option ( "username" , {
17+ describe : "Username of the user" ,
1018 demandOption : true
1119 } )
12- . option ( ' password' , {
13- describe : ' Password of the user' ,
20+ . option ( " password" , {
21+ describe : " Password of the user" ,
1422 demandOption : true
1523 } )
16- . option ( ' user-pool-id' , {
17- describe : ' Cognito user pool id' ,
24+ . option ( " user-pool-id" , {
25+ describe : " Cognito user pool id" ,
1826 demandOption : true
1927 } )
20- . option ( ' app-client-id' , {
21- describe : ' Cognito user pool app client id' ,
28+ . option ( " app-client-id" , {
29+ describe : " Cognito user pool app client id" ,
2230 demandOption : true
2331 } )
24- . option ( ' cognito-region' , {
25- describe : ' Cognito region' ,
26- default : ' us-east-1'
32+ . option ( " cognito-region" , {
33+ describe : " Cognito region" ,
34+ default : " us-east-1"
2735 } )
28- . option ( ' identity-pool-id' , {
29- describe : ' Cognito identity pool id' ,
36+ . option ( " identity-pool-id" , {
37+ describe : " Cognito identity pool id" ,
3038 demandOption : true
3139 } )
32- . option ( ' invoke-url' , {
33- describe : ' API Gateway URL' ,
40+ . option ( " invoke-url" , {
41+ describe : " API Gateway URL" ,
3442 demandOption : true
3543 } )
36- . option ( ' api-gateway-region' , {
37- describe : ' API Gateway region' ,
38- default : ' us-east-1'
44+ . option ( " api-gateway-region" , {
45+ describe : " API Gateway region" ,
46+ default : " us-east-1"
3947 } )
40- . option ( ' path-template' , {
41- describe : ' API path template' ,
48+ . option ( " path-template" , {
49+ describe : " API path template" ,
4250 demandOption : true
4351 } )
44- . option ( ' method' , {
45- describe : ' API method' ,
46- default : ' GET'
52+ . option ( " method" , {
53+ describe : " API method" ,
54+ default : " GET"
4755 } )
48- . option ( ' params' , {
49- describe : ' API request params' ,
50- default : '{}'
56+ . option ( " params" , {
57+ describe : " API request params" ,
58+ default : "{}"
5159 } )
52- . option ( ' additional-params' , {
53- describe : ' API request additional params' ,
54- default : '{}'
60+ . option ( " additional-params" , {
61+ describe : " API request additional params" ,
62+ default : "{}"
5563 } )
56- . option ( ' body' , {
57- describe : ' API request body' ,
58- default : '{}'
64+ . option ( " body" , {
65+ describe : " API request body" ,
66+ default : "{}"
5967 } )
60- . argv ;
61-
68+ . help ( "h" )
69+ . alias ( "h" , "help" )
70+ . alias ( "v" , "version" )
71+ . version ( packageJson . version )
72+ . wrap ( null ) . argv ;
6273
6374function authenticate ( callback ) {
6475 var poolData = {
65- UserPoolId : argv . userPoolId ,
66- ClientId : argv . appClientId
76+ UserPoolId : argv . userPoolId ,
77+ ClientId : argv . appClientId
6778 } ;
6879
6980 AWS . config . update ( { region : argv . cognitoRegion } ) ;
@@ -77,14 +88,16 @@ function authenticate(callback) {
7788 Username : argv . username ,
7889 Password : argv . password
7990 } ;
80- var authenticationDetails = new AWSCognito . AuthenticationDetails ( authenticationData ) ;
91+ var authenticationDetails = new AWSCognito . AuthenticationDetails (
92+ authenticationData
93+ ) ;
8194
8295 var cognitoUser = new AWSCognito . CognitoUser ( userData ) ;
8396
84- console . log ( ' Authenticating with User Pool' ) ;
97+ console . log ( " Authenticating with User Pool" ) ;
8598
8699 cognitoUser . authenticateUser ( authenticationDetails , {
87- onSuccess : function ( result ) {
100+ onSuccess : function ( result ) {
88101 callback ( result . getIdToken ( ) . getJwtToken ( ) ) ;
89102 } ,
90103 onFailure : function ( err ) {
@@ -94,11 +107,13 @@ function authenticate(callback) {
94107}
95108
96109function getCredentials ( userToken , callback ) {
97- console . log ( ' Getting temporary credentials' ) ;
110+ console . log ( " Getting temporary credentials" ) ;
98111
99112 var logins = { } ;
100113
101- logins [ 'cognito-idp.' + argv . cognitoRegion + '.amazonaws.com/' + argv . userPoolId ] = userToken ;
114+ logins [
115+ "cognito-idp." + argv . cognitoRegion + ".amazonaws.com/" + argv . userPoolId
116+ ] = userToken ;
102117
103118 AWS . config . credentials = new AWS . CognitoIdentityCredentials ( {
104119 IdentityPoolId : argv . identityPoolId ,
@@ -116,7 +131,7 @@ function getCredentials(userToken, callback) {
116131}
117132
118133function makeRequest ( ) {
119- console . log ( ' Making API request' ) ;
134+ console . log ( " Making API request" ) ;
120135
121136 var apigClient = apigClientFactory . newClient ( {
122137 accessKey : AWS . config . credentials . accessKeyId ,
@@ -130,7 +145,8 @@ function makeRequest() {
130145 var additionalParams = JSON . parse ( argv . additionalParams ) ;
131146 var body = JSON . parse ( argv . body ) ;
132147
133- apigClient . invokeApi ( params , argv . pathTemplate , argv . method , additionalParams , body )
148+ apigClient
149+ . invokeApi ( params , argv . pathTemplate , argv . method , additionalParams , body )
134150 . then ( function ( result ) {
135151 console . dir ( {
136152 status : result . status ,
@@ -145,15 +161,12 @@ function makeRequest() {
145161 statusText : result . response . statusText ,
146162 data : result . response . data
147163 } ) ;
148- }
149- else {
164+ } else {
150165 console . log ( result . message ) ;
151166 }
152167 } ) ;
153168}
154169
155- authenticate (
156- function ( token ) {
157- getCredentials ( token , makeRequest ) ;
158- }
159- ) ;
170+ authenticate ( function ( token ) {
171+ getCredentials ( token , makeRequest ) ;
172+ } ) ;
0 commit comments