Skip to content

Instantly share code, notes, and snippets.

@zamirkhan
Created August 1, 2017 20:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zamirkhan/e5e6284ceb522edf073b0439b5350a30 to your computer and use it in GitHub Desktop.
Save zamirkhan/e5e6284ceb522edf073b0439b5350a30 to your computer and use it in GitHub Desktop.
import AWS from 'aws-sdk';
import config from './config.js';
import sigV4Client from './sigV4Client';
export function getAwsCredentials(userToken) {
const authenticator = `cognito-idp.${config.cognito.REGION}.amazonaws.com/${config.cognito.USER_POOL_ID}`;
AWS.config.update({ region: config.cognito.REGION });
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: config.cognito.IDENTITY_POOL_ID,
Logins: {
[authenticator]: userToken
}
});
return AWS.config.credentials.getPromise();
}
const chakram = require('chakram'),
expect = chakram.expect;
const util = require('util');
const aws = require('aws-sdk');
const amazonCognitoIdentity = require('amazon-cognito-identity-js');
const config = require('../secrets.json');
const myconfig = require('./config.js');
const awsLib = require('./awsLib.js'); // provided in gist
import sigV4Client from './sigV4Client.js'; // identical to the sigV4Client from the tutorial
const cognitoUserPool = amazonCognitoIdentity.CognitoUserPool;
// Set the AWS region
aws.config.update({ region: config.AwsRegion });
// The API portion being tested
const card_route = "/cards" // my modification - I'm creating "cards" instead of "notes"
const cisp = new aws.CognitoIdentityServiceProvider({ apiVersion: '2016-04-18' });
const params = {
AuthFlow: 'ADMIN_NO_SRP_AUTH',
ClientId: config.AwsAppClientId,
UserPoolId: config.CognitoUserPoolId,
AuthParameters: {
USERNAME: config.CognitoTestUsername,
PASSWORD: config.CognitoTestPassword
}
};
var adminInitiateAuthPromise = cisp.adminInitiateAuth(params).promise();
const getSignedRequest = function (method, path, headers, queryParams, body) {
const signedRequest = sigV4Client
.newClient({
accessKey: aws.config.credentials.accessKeyId,
secretKey: aws.config.credentials.secretAccessKey,
sessionToken: aws.config.credentials.sessionToken,
region: myconfig.default.apiGateway.REGION,
endpoint: myconfig.default.apiGateway.URL,
})
.signRequest({
method,
path,
headers,
queryParams,
body
});
return signedRequest;
}
describe("cards API", function () {
var opts = {};
before("test setup", function () {
return adminInitiateAuthPromise
.then(function (auth) {
const idToken = auth.AuthenticationResult.IdToken;
return awsLib.getAwsCredentials(idToken);
});
});
it("should support a put (create) and delete endpoints", function () {
const body = JSON.stringify({
'url': 'my_new_url',
'title': 'My new title',
'description': 'My new description'
});
const method = 'POST';
const signedRequest = getSignedRequest(method, card_route, {}, {}, body);
const opts = {
'headers': signedRequest.headers,
'body': signedRequest.body
};
console.log(signedRequest);
return chakram.request(signedRequest.method, signedRequest.url, opts).then(function (postResponse) {
// ... the test should continue here. but this request returns the 403 error I described.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment