Skip to content

Commit 18debb6

Browse files
jwoptiojayair
authored andcommitted
add option to pass access token in a header (AnomalyInnovations#6)
* add option to pass access token in a header * Update readme * took out es6 object destructuring assignment and switched back to not stringify output
1 parent 70fecb1 commit 18debb6

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ $ apig-test \
2727
--method='GET' \
2828
--params='{}' \
2929
--additional-params='{}' \
30+
--access-token-header='cognito-access-token' \
3031
--body='{}'
3132
```
3233

@@ -71,6 +72,9 @@ This command takes the following options:
7172
- `additional-params`
7273
Any additional params (including the querystring) as a JSON string. Defaults to `'{}'`.
7374

75+
- `access-token-header`
76+
Header field on which to pass the access token.
77+
7478
- `body`
7579
The request body as a JSON string. Defaults to `'{}'`.
7680

index.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ var argv = require("yargs")
6969
describe: "API request body",
7070
default: "{}"
7171
})
72+
.option("access-token-header", {
73+
describe: "Header to use to pass access token with request"
74+
})
7275
.help("h")
7376
.alias("h", "help")
7477
.alias("v", "version")
@@ -102,7 +105,10 @@ function authenticate(callback) {
102105

103106
cognitoUser.authenticateUser(authenticationDetails, {
104107
onSuccess: function(result) {
105-
callback(result.getIdToken().getJwtToken());
108+
callback({
109+
idToken: result.getIdToken().getJwtToken(),
110+
accessToken: result.getAccessToken().getJwtToken()
111+
});
106112
},
107113
onFailure: function(err) {
108114
console.log(err.message ? err.message : err);
@@ -119,14 +125,16 @@ function authenticate(callback) {
119125
});
120126
}
121127

122-
function getCredentials(userToken, callback) {
128+
function getCredentials(userTokens, callback) {
123129
console.log("Getting temporary credentials");
124130

125131
var logins = {};
132+
const idToken = userTokens.idToken;
133+
const accessToken = userTokens.accessToken;
126134

127135
logins[
128136
"cognito-idp." + argv.cognitoRegion + ".amazonaws.com/" + argv.userPoolId
129-
] = userToken;
137+
] = idToken;
130138

131139
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
132140
IdentityPoolId: argv.identityPoolId,
@@ -139,11 +147,11 @@ function getCredentials(userToken, callback) {
139147
return;
140148
}
141149

142-
callback();
150+
callback(userTokens);
143151
});
144152
}
145153

146-
function makeRequest() {
154+
function makeRequest(userTokens) {
147155
console.log("Making API request");
148156

149157
var apigClient = apigClientFactory.newClient({
@@ -159,6 +167,12 @@ function makeRequest() {
159167
var additionalParams = JSON.parse(argv.additionalParams);
160168
var body = JSON.parse(argv.body);
161169

170+
if (argv.accessTokenHeader) {
171+
const tokenHeader = {};
172+
tokenHeader[argv.accessTokenHeader] = userTokens.accessToken;
173+
additionalParams.headers = Object.assign({}, additionalParams.headers, tokenHeader);
174+
}
175+
162176
apigClient
163177
.invokeApi(params, argv.pathTemplate, argv.method, additionalParams, body)
164178
.then(function(result) {
@@ -181,6 +195,6 @@ function makeRequest() {
181195
});
182196
}
183197

184-
authenticate(function(token) {
185-
getCredentials(token, makeRequest);
198+
authenticate(function(tokens) {
199+
getCredentials(tokens, makeRequest);
186200
});

0 commit comments

Comments
 (0)