@studyportals/aws4sign
Advanced tools
Comparing version 1.0.2 to 1.1.0
module.exports = require('./lib/AWS4Sign'); |
module.exports = class AWS4Sign{ | ||
/** | ||
/** | ||
* Constructor for the class | ||
* | ||
* @param {Object} CryptoJS | ||
*/ | ||
constructor(CryptoJS){ | ||
this.CryptoJS = CryptoJS; | ||
} | ||
/** | ||
* Configuring the object | ||
* | ||
* @param {Object} options | ||
@@ -12,107 +22,104 @@ * @param {Object} options.awsCredentials this has to be the AWS.config.credentials object obtained after logging in with Cognito Federated Identities | ||
* @param {String} options.region the AWS region of the service you are making the request for | ||
* @param {String} options.service the AWS standard service name, for a the whole list visit http://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html#genref-aws-service-namespaces | ||
* @param {Object} CryptoJS | ||
* @param {String} options.service the AWS standard service name, | ||
* for a the whole list visit http://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html#genref-aws-service-namespaces | ||
*/ | ||
constructor( options, CryptoJS ){ | ||
configure( options ){ | ||
this.CryptoJS = CryptoJS; | ||
this.configure( options ); | ||
} | ||
configure( options ){ | ||
this.method = (typeof options.method === 'string') ? options.method.toUpperCase() : 'GET'; | ||
if( typeof(options.url) !== 'string' || options.url.length === 0 ){ | ||
this.method = (typeof options.method === 'string') ? options.method.toUpperCase() : 'GET'; | ||
if( typeof(options.url) !== 'string' || options.url.length == 0 ){ | ||
throw new Error('URL must be defined'); | ||
} | ||
this.CanonicalURI = this.getCanonicalURI( options.url ); | ||
throw new Error('URL must be defined'); | ||
} | ||
this.CanonicalURI = this.getCanonicalURI( options.url ); | ||
if( typeof(options.service) !== 'string' || options.service.length === 0 ){ | ||
if( typeof(options.service) !== 'string' || options.service.length == 0 ){ | ||
throw new Error('Service must be defined'); | ||
} | ||
this.service = options.service; | ||
throw new Error('Service must be defined'); | ||
} | ||
this.service = options.service; | ||
if( typeof(options.region) !== 'string' || options.region.length === 0 ){ | ||
if( typeof(options.region) !== 'string' || options.region.length == 0 ){ | ||
throw new Error('Region must be defined'); | ||
} | ||
this.region = options.region; | ||
throw new Error('Region must be defined'); | ||
} | ||
this.region = options.region; | ||
this.awsCredentials = options.awsCredentials; | ||
this.awsCredentials = options.awsCredentials; | ||
this.QueryStringObject = {}; | ||
if( options.url.indexOf('?') !== -1 ){ | ||
this.QueryStringObject = {}; | ||
if( options.url.indexOf('?') !== -1 ){ | ||
this.buildQueryStringObject( options.url.substr(options.url.indexOf('?') + 1) ); | ||
} | ||
if( this.method === 'GET' && typeof options.params === 'object' ){ | ||
this.buildQueryStringObject( options.url.substr(options.url.indexOf('?') + 1) ); | ||
} | ||
if( this.method == 'GET' && typeof options.params === 'object' ){ | ||
this.buildQueryStringObject( options.params ); | ||
} | ||
if( typeof options.params === 'object' && this.method !== 'GET' ){ | ||
this.buildQueryStringObject( options.params ); | ||
} | ||
if( typeof options.params === 'object' && this.method !== 'GET' ){ | ||
this.params = options.params; | ||
} | ||
this.params = options.params; | ||
} | ||
this.CanonicalQueryString = this.getCanonicalQueryString(this.QueryStringObject); | ||
this.CleanQueryString = this.getCleanQueryString(); | ||
this.CanonicalQueryString = this.getCanonicalQueryString(this.QueryStringObject); | ||
this.CleanQueryString = this.getCleanQueryString(); | ||
this.initHeaders( options ); | ||
this.normalizedHeaders = this.normalizeHeaders( this.headers ); | ||
this.setCleanURL( options ); | ||
} | ||
this.initHeaders( options ); | ||
this.normalizedHeaders = this.normalizeHeaders( this.headers ); | ||
this.setCleanURL( options ); | ||
} | ||
initHeaders(options){ | ||
initHeaders(options){ | ||
this.headers = { | ||
'Host': this.getHost(options.url), | ||
'X-Amz-Date': this.getISO8601Date() | ||
}; | ||
this.headers = { | ||
'Host': this.getHost(options.url), | ||
'X-Amz-Date': this.getISO8601Date() | ||
}; | ||
if( typeof options.params === 'object' | ||
&& this.method.toUpperCase() !== 'GET' | ||
&& Object.keys(options.params).length > 0 ){ | ||
if( typeof options.params === 'object' | ||
&& this.method.toUpperCase() !== 'GET' | ||
&& Object.keys(options.params).length > 0 ){ | ||
this.headers['Content-Type'] = 'application/json'; | ||
} | ||
} | ||
this.headers['Content-Type'] = 'application/json'; | ||
} | ||
} | ||
setCleanURL(options){ | ||
setCleanURL(options){ | ||
if( options.url.indexOf('?') !== -1 ){ | ||
if( options.url.indexOf('?') !== -1 ){ | ||
this.cleanURL = options.url.substr( 0, options.url.indexOf('?') ); | ||
} else { | ||
this.cleanURL = options.url.substr( 0, options.url.indexOf('?') ); | ||
} else { | ||
this.cleanURL = options.url; | ||
} | ||
this.cleanURL = options.url; | ||
} | ||
if( this.CleanQueryString.length > 0 ){ | ||
if( this.CleanQueryString.length > 0 ){ | ||
this.cleanURL += '?' + this.CleanQueryString; | ||
} | ||
} | ||
this.cleanURL += '?' + this.CleanQueryString; | ||
} | ||
} | ||
buildQueryStringObject(params){ | ||
buildQueryStringObject(params){ | ||
if( typeof params === "string" ){ | ||
if( typeof params === "string" ){ | ||
params.split('&').forEach((param) => { | ||
params.split('&').forEach((param) => { | ||
let pair = param.split('='); | ||
this.QueryStringObject[pair[0]] = encodeURIComponent( pair[1] ); | ||
}); | ||
} else if( typeof params === 'object' ){ | ||
let pair = param.split('='); | ||
this.QueryStringObject[pair[0]] = encodeURIComponent( pair[1] ); | ||
}); | ||
} else if( typeof params === 'object' ){ | ||
for( let key in params ){ | ||
for( let key in params ){ | ||
if(params.hasOwnProperty(key)){ | ||
this.QueryStringObject[key] = encodeURIComponent( params[key] ); | ||
} | ||
} | ||
} | ||
this.QueryStringObject[key] = encodeURIComponent( params[key] ); | ||
} | ||
} | ||
} | ||
} | ||
/** | ||
/** | ||
* Based on the type of <i>elem</i> parameters: | ||
@@ -127,5 +134,5 @@ * If the type is String, extracts, if any, the querystring part of the url | ||
*/ | ||
getCanonicalQueryString(elem){ | ||
getCanonicalQueryString(elem){ | ||
let cqs = ''; | ||
let cqs = ''; | ||
if(typeof elem === 'string') { | ||
@@ -142,3 +149,3 @@ | ||
cqs = decodeURI( qsArray.join("&").trim() ); | ||
cqs = encodeURI( cqs ); | ||
cqs = encodeURI( cqs ); | ||
} | ||
@@ -151,5 +158,5 @@ } | ||
elem[key] = decodeURIComponent( elem[key] ); | ||
elem[key] = decodeURIComponent( elem[key] ); | ||
qsArray[index] = (key + '=' + encodeURIComponent(elem[key]) ); | ||
elem[key] = encodeURIComponent( elem[key] ); | ||
elem[key] = encodeURIComponent( elem[key] ); | ||
}); | ||
@@ -160,5 +167,5 @@ cqs = qsArray.sort().join("&").trim(); | ||
return cqs; | ||
} | ||
} | ||
/** | ||
/** | ||
* Extracts the uri and returns the normalized paths according to RFC 3986. | ||
@@ -169,5 +176,5 @@ * | ||
*/ | ||
getCanonicalURI(url){ | ||
getCanonicalURI(url){ | ||
let regex = '//'; | ||
let regex = '//'; | ||
let begin = url.indexOf(regex); | ||
@@ -191,5 +198,5 @@ if(begin !== -1){ | ||
return encodeURI(url); | ||
} | ||
} | ||
/** | ||
/** | ||
* Extracts the host information and saves it as object variable. | ||
@@ -199,3 +206,3 @@ * | ||
*/ | ||
getHost(url){ | ||
getHost(url){ | ||
@@ -219,3 +226,3 @@ let regex = '//'; | ||
/** | ||
/** | ||
* Returns a string representing the current date in YYYYMMDD format. | ||
@@ -238,3 +245,3 @@ * | ||
/** | ||
/** | ||
* Returns a string representing the current time in ISO8601 format | ||
@@ -259,3 +266,3 @@ * YYYYMMDD'T'HHMMSS'Z' | ||
/** | ||
/** | ||
* Extracts the headers from passed object, set them to the request and normalize them for the canonical request. | ||
@@ -268,3 +275,3 @@ * The normalized values are saved in the <i>headers</i> variable. | ||
let objectHeaders = []; | ||
let objectHeaders = []; | ||
@@ -279,4 +286,4 @@ if(typeof reqHeaders !== 'undefined' && reqHeaders !== null){ | ||
newVal = newVal.replace(' ',' '); | ||
} | ||
newVal = newVal.replace(' ',' '); | ||
} | ||
@@ -287,6 +294,6 @@ objectHeaders[newKey] = newVal; | ||
return objectHeaders; | ||
return objectHeaders; | ||
} | ||
/** | ||
/** | ||
* Returns the headers of the request in canonical form. | ||
@@ -301,3 +308,3 @@ * | ||
/** | ||
/** | ||
* Returns the headers entries of the request in canonical form. | ||
@@ -309,3 +316,3 @@ * | ||
let canonicalHeadersEntry = ""; | ||
let canonicalHeadersEntry = ''; | ||
let objectHeaders = this.normalizedHeaders; | ||
@@ -321,3 +328,3 @@ | ||
/** | ||
/** | ||
* Prepare the request adding the Authorization and the X-Amz-Security-Token | ||
@@ -328,5 +335,5 @@ * headers. | ||
*/ | ||
prepareRequest(){ | ||
prepareRequest(){ | ||
//create the Authorization header | ||
//create the Authorization header | ||
let authorization = 'AWS4-HMAC-SHA256 ' | ||
@@ -337,7 +344,7 @@ + 'Credential=' + this.awsCredentials.accessKeyId + '/' + this.getDate() + '/' + this.region + '/' + this.service + '/aws4_request, ' | ||
this.headers['Authorization'] = authorization; | ||
this.headers['X-Amz-Security-Token'] = this.awsCredentials.sessionToken; | ||
} | ||
this.headers['Authorization'] = authorization; | ||
this.headers['X-Amz-Security-Token'] = this.awsCredentials.sessionToken; | ||
} | ||
/** | ||
/** | ||
* Creates the signature to add to the request. | ||
@@ -366,3 +373,3 @@ * | ||
/** | ||
/** | ||
* Returns the SHA-256 hashed canonical request represented as a string of | ||
@@ -380,3 +387,3 @@ * lowercase hexademical characters. | ||
/** | ||
/** | ||
* Method to get the request in canonical form once that all fields have been set. | ||
@@ -396,3 +403,3 @@ * | ||
/** | ||
/** | ||
* Returns the hashed payload of the request as a lowercase hexadecimal string. | ||
@@ -404,55 +411,55 @@ * | ||
let payload = ''; | ||
let payload = ''; | ||
if(this.method === 'GET' ){ | ||
payload = this.CryptoJS.SHA256(''); | ||
} else { | ||
payload = this.CryptoJS.SHA256(''); | ||
} else { | ||
let encrypt = (typeof this.params !== 'undefined') ? JSON.stringify(this.params) : ''; | ||
payload = this.CryptoJS.SHA256( encrypt ); | ||
} | ||
let encrypt = (typeof this.params !== 'undefined') ? JSON.stringify(this.params) : ''; | ||
payload = this.CryptoJS.SHA256( encrypt ); | ||
} | ||
return payload.toString().toLowerCase(); | ||
return payload.toString().toLowerCase(); | ||
} | ||
/** | ||
/** | ||
* Utility method to convert a simple javascript object into query string. | ||
* @returns {string} | ||
*/ | ||
getCleanQueryString(){ | ||
getCleanQueryString(){ | ||
let str = []; | ||
let str = []; | ||
for(let p in this.QueryStringObject){ | ||
if (this.QueryStringObject.hasOwnProperty(p)) { | ||
if (this.QueryStringObject.hasOwnProperty(p)) { | ||
str.push( p + "=" + this.QueryStringObject[p] ); | ||
} | ||
} | ||
} | ||
return str.join("&"); | ||
} | ||
} | ||
/** | ||
/** | ||
* Signs the request and returns an object containing the headers for the request, method, url and data | ||
* @returns {object} | ||
*/ | ||
getXmlHttpRequestOptions(){ | ||
getXmlHttpRequestOptions(){ | ||
this.prepareRequest(); | ||
this.prepareRequest(); | ||
let options = { | ||
url: this.cleanURL, | ||
method: this.method, | ||
headers: this.headers | ||
}; | ||
let options = { | ||
url: this.cleanURL, | ||
method: this.method, | ||
headers: this.headers | ||
}; | ||
if( this.method !== 'GET' && typeof this.params !== 'undefined' ){ | ||
if( this.method !== 'GET' && typeof this.params !== 'undefined' ){ | ||
options.data = JSON.stringify(this.params); | ||
} | ||
options.data = JSON.stringify(this.params); | ||
} | ||
return options; | ||
} | ||
return options; | ||
} | ||
} |
{ | ||
"name": "@studyportals/aws4sign", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
340
13209