diff --git a/oauth-1.0a.d.ts b/oauth-1.0a.d.ts index 9569bc1..9f2b29b 100644 --- a/oauth-1.0a.d.ts +++ b/oauth-1.0a.d.ts @@ -6,7 +6,6 @@ export as namespace OAuth; export = OAuth; declare class OAuth { - body_hash_function: OAuth.BodyHashFunction; consumer: OAuth.Consumer; hash_function: OAuth.HashFunction; @@ -22,17 +21,28 @@ declare class OAuth { /** * Sign a request. */ - authorize(request: OAuth.RequestOptions, token?: OAuth.Token): OAuth.Authorization; + authorize( + request: OAuth.RequestOptions, + token?: OAuth.Token, + verifier?: string + ): OAuth.Authorization; /** * Generate the oauth signature (i.e. oauth_signature). */ - getSignature(request: OAuth.RequestOptions, token_secret: string | undefined, oauth_data: OAuth.Data): string; + getSignature( + request: OAuth.RequestOptions, + token_secret: string | undefined, + oauth_data: OAuth.Data + ): string; /** * Generate the body signature (i.e. oauth_body_hash). */ - getBodyHash(request: OAuth.RequestOptions, token_secret: string | undefined): string; + getBodyHash( + request: OAuth.RequestOptions, + token_secret: string | undefined + ): string; /** * Encode the request attributes. @@ -44,7 +54,10 @@ declare class OAuth { /** * Encode the oauth data and the request parameter, */ - getParameterString(request: OAuth.RequestOptions, oauth_data: OAuth.Data): string; + getParameterString( + request: OAuth.RequestOptions, + oauth_data: OAuth.Data + ): string; /** * Generate the signing key. @@ -96,11 +109,12 @@ declare class OAuth { /** * Sort an object properties by keys. */ - sortObject(obj: O): Array<{key: keyof O, value: O[K]}>; + sortObject( + obj: O + ): Array<{ key: keyof O; value: O[K] }>; } declare namespace OAuth { - /** * OAuth data, including the signature. */ @@ -135,6 +149,7 @@ declare namespace OAuth { oauth_version: string; oauth_token?: string; oauth_body_hash?: string; + oauth_verifier?: string; } /** @@ -188,5 +203,4 @@ declare namespace OAuth { key: string; secret: string; } - } diff --git a/oauth-1.0a.js b/oauth-1.0a.js index 5c15dd4..c2af0b3 100644 --- a/oauth-1.0a.js +++ b/oauth-1.0a.js @@ -1,5 +1,5 @@ -if (typeof(module) !== 'undefined' && typeof(exports) !== 'undefined') { - module.exports = OAuth; +if (typeof module !== 'undefined' && typeof exports !== 'undefined') { + module.exports = OAuth; } /** @@ -7,45 +7,45 @@ if (typeof(module) !== 'undefined' && typeof(exports) !== 'undefined') { * @param {Object} opts consumer key and secret */ function OAuth(opts) { - if(!(this instanceof OAuth)) { - return new OAuth(opts); - } + if (!(this instanceof OAuth)) { + return new OAuth(opts); + } - if(!opts) { - opts = {}; - } + if (!opts) { + opts = {}; + } - if(!opts.consumer) { - throw new Error('consumer option is required'); - } + if (!opts.consumer) { + throw new Error('consumer option is required'); + } - this.consumer = opts.consumer; - this.nonce_length = opts.nonce_length || 32; - this.version = opts.version || '1.0'; - this.parameter_seperator = opts.parameter_seperator || ', '; - this.realm = opts.realm; + this.consumer = opts.consumer; + this.nonce_length = opts.nonce_length || 32; + this.version = opts.version || '1.0'; + this.parameter_seperator = opts.parameter_seperator || ', '; + this.realm = opts.realm; - if(typeof opts.last_ampersand === 'undefined') { - this.last_ampersand = true; - } else { - this.last_ampersand = opts.last_ampersand; - } + if (typeof opts.last_ampersand === 'undefined') { + this.last_ampersand = true; + } else { + this.last_ampersand = opts.last_ampersand; + } - // default signature_method is 'PLAINTEXT' - this.signature_method = opts.signature_method || 'PLAINTEXT'; + // default signature_method is 'PLAINTEXT' + this.signature_method = opts.signature_method || 'PLAINTEXT'; - if(this.signature_method == 'PLAINTEXT' && !opts.hash_function) { - opts.hash_function = function(base_string, key) { - return key; - } - } + if (this.signature_method == 'PLAINTEXT' && !opts.hash_function) { + opts.hash_function = function (base_string, key) { + return key; + }; + } - if(!opts.hash_function) { - throw new Error('hash_function option is required'); - } + if (!opts.hash_function) { + throw new Error('hash_function option is required'); + } - this.hash_function = opts.hash_function; - this.body_hash_function = opts.body_hash_function || this.hash_function; + this.hash_function = opts.hash_function; + this.body_hash_function = opts.body_hash_function || this.hash_function; } /** @@ -59,34 +59,42 @@ function OAuth(opts) { * @param {Object} key and secret token * @return {Object} OAuth Authorized data */ -OAuth.prototype.authorize = function(request, token) { - var oauth_data = { - oauth_consumer_key: this.consumer.key, - oauth_nonce: this.getNonce(), - oauth_signature_method: this.signature_method, - oauth_timestamp: this.getTimeStamp(), - oauth_version: this.version - }; +OAuth.prototype.authorize = function (request, token, verifier) { + var oauth_data = { + oauth_consumer_key: this.consumer.key, + oauth_nonce: this.getNonce(), + oauth_signature_method: this.signature_method, + oauth_timestamp: this.getTimeStamp(), + oauth_version: this.version + }; + + if (!token) { + token = {}; + } - if(!token) { - token = {}; - } + if (token.key !== undefined) { + oauth_data.oauth_token = token.key; + } - if(token.key !== undefined) { - oauth_data.oauth_token = token.key; - } + if (!request.data) { + request.data = {}; + } - if(!request.data) { - request.data = {}; - } + if (request.includeBodyHash) { + oauth_data.oauth_body_hash = this.getBodyHash(request, token.secret); + } - if(request.includeBodyHash) { - oauth_data.oauth_body_hash = this.getBodyHash(request, token.secret) - } + if (verifier) { + oauth_data.oauth_verifier = verifier; + } - oauth_data.oauth_signature = this.getSignature(request, token.secret, oauth_data); + oauth_data.oauth_signature = this.getSignature( + request, + token.secret, + oauth_data + ); - return oauth_data; + return oauth_data; }; /** @@ -96,22 +104,28 @@ OAuth.prototype.authorize = function(request, token) { * @param {Object} oauth_data OAuth data * @return {String} Signature */ -OAuth.prototype.getSignature = function(request, token_secret, oauth_data) { - return this.hash_function(this.getBaseString(request, oauth_data), this.getSigningKey(token_secret)); +OAuth.prototype.getSignature = function (request, token_secret, oauth_data) { + return this.hash_function( + this.getBaseString(request, oauth_data), + this.getSigningKey(token_secret) + ); }; /** * Create a OAuth Body Hash * @param {Object} request data */ -OAuth.prototype.getBodyHash = function(request, token_secret) { - var body = typeof request.data === 'string' ? request.data : JSON.stringify(request.data) +OAuth.prototype.getBodyHash = function (request, token_secret) { + var body = + typeof request.data === 'string' + ? request.data + : JSON.stringify(request.data); if (!this.body_hash_function) { throw new Error('body_hash_function option is required'); } - return this.body_hash_function(body, this.getSigningKey(token_secret)) + return this.body_hash_function(body, this.getSigningKey(token_secret)); }; /** @@ -120,8 +134,14 @@ OAuth.prototype.getBodyHash = function(request, token_secret) { * @param {Object} OAuth data * @return {String} Base String */ -OAuth.prototype.getBaseString = function(request, oauth_data) { - return request.method.toUpperCase() + '&' + this.percentEncode(this.getBaseUrl(request.url)) + '&' + this.percentEncode(this.getParameterString(request, oauth_data)); +OAuth.prototype.getBaseString = function (request, oauth_data) { + return ( + request.method.toUpperCase() + + '&' + + this.percentEncode(this.getBaseUrl(request.url)) + + '&' + + this.percentEncode(this.getParameterString(request, oauth_data)) + ); }; /** @@ -134,43 +154,56 @@ OAuth.prototype.getBaseString = function(request, oauth_data) { * @param {Object} OAuth data * @return {Object} Parameter string data */ -OAuth.prototype.getParameterString = function(request, oauth_data) { - var base_string_data; - if (oauth_data.oauth_body_hash) { - base_string_data = this.sortObject(this.percentEncodeData(this.mergeObject(oauth_data, this.deParamUrl(request.url)))); - } else { - base_string_data = this.sortObject(this.percentEncodeData(this.mergeObject(oauth_data, this.mergeObject(request.data, this.deParamUrl(request.url))))); - } +OAuth.prototype.getParameterString = function (request, oauth_data) { + var base_string_data; + if (oauth_data.oauth_body_hash) { + base_string_data = this.sortObject( + this.percentEncodeData( + this.mergeObject(oauth_data, this.deParamUrl(request.url)) + ) + ); + } else { + base_string_data = this.sortObject( + this.percentEncodeData( + this.mergeObject( + oauth_data, + this.mergeObject(request.data, this.deParamUrl(request.url)) + ) + ) + ); + } - var data_str = ''; - - //base_string_data to string - for(var i = 0; i < base_string_data.length; i++) { - var key = base_string_data[i].key; - var value = base_string_data[i].value; - // check if the value is an array - // this means that this key has multiple values - if (value && Array.isArray(value)){ - // sort the array first - value.sort(); - - var valString = ""; - // serialize all values for this key: e.g. formkey=formvalue1&formkey=formvalue2 - value.forEach((function(item, i){ - valString += key + '=' + item; - if (i < value.length){ - valString += "&"; - } - }).bind(this)); - data_str += valString; - } else { - data_str += key + '=' + value + '&'; - } + var data_str = ''; + + //base_string_data to string + for (var i = 0; i < base_string_data.length; i++) { + var key = base_string_data[i].key; + var value = base_string_data[i].value; + // check if the value is an array + // this means that this key has multiple values + if (value && Array.isArray(value)) { + // sort the array first + value.sort(); + + var valString = ''; + // serialize all values for this key: e.g. formkey=formvalue1&formkey=formvalue2 + value.forEach( + function (item, i) { + valString += key + '=' + item; + if (i < value.length) { + valString += '&'; + } + }.bind(this) + ); + data_str += valString; + } else { + data_str += key + '=' + value + '&'; } + } - //remove the last character - data_str = data_str.substr(0, data_str.length - 1); - return data_str; + //remove the last character + data_str = data_str.substr(0, data_str.length - 1); + return data_str; }; /** @@ -178,14 +211,18 @@ OAuth.prototype.getParameterString = function(request, oauth_data) { * @param {String} token_secret Secret Token * @return {String} Signing Key */ -OAuth.prototype.getSigningKey = function(token_secret) { - token_secret = token_secret || ''; +OAuth.prototype.getSigningKey = function (token_secret) { + token_secret = token_secret || ''; - if(!this.last_ampersand && !token_secret) { - return this.percentEncode(this.consumer.secret); - } + if (!this.last_ampersand && !token_secret) { + return this.percentEncode(this.consumer.secret); + } - return this.percentEncode(this.consumer.secret) + '&' + this.percentEncode(token_secret); + return ( + this.percentEncode(this.consumer.secret) + + '&' + + this.percentEncode(token_secret) + ); }; /** @@ -193,8 +230,8 @@ OAuth.prototype.getSigningKey = function(token_secret) { * @param {String} url * @return {String} */ -OAuth.prototype.getBaseUrl = function(url) { - return url.split('?')[0]; +OAuth.prototype.getBaseUrl = function (url) { + return url.split('?')[0]; }; /** @@ -202,33 +239,33 @@ OAuth.prototype.getBaseUrl = function(url) { * @param {String} string * @return {Object} */ -OAuth.prototype.deParam = function(string) { - var arr = string.split('&'); - var data = {}; - - for(var i = 0; i < arr.length; i++) { - var item = arr[i].split('='); - - // '' value - item[1] = item[1] || ''; - - // check if the key already exists - // this can occur if the QS part of the url contains duplicate keys like this: ?formkey=formvalue1&formkey=formvalue2 - if (data[item[0]]){ - // the key exists already - if (!Array.isArray(data[item[0]])) { - // replace the value with an array containing the already present value - data[item[0]] = [data[item[0]]]; - } - // and add the new found value to it - data[item[0]].push(decodeURIComponent(item[1])); - } else { - // it doesn't exist, just put the found value in the data object - data[item[0]] = decodeURIComponent(item[1]); - } +OAuth.prototype.deParam = function (string) { + var arr = string.split('&'); + var data = {}; + + for (var i = 0; i < arr.length; i++) { + var item = arr[i].split('='); + + // '' value + item[1] = item[1] || ''; + + // check if the key already exists + // this can occur if the QS part of the url contains duplicate keys like this: ?formkey=formvalue1&formkey=formvalue2 + if (data[item[0]]) { + // the key exists already + if (!Array.isArray(data[item[0]])) { + // replace the value with an array containing the already present value + data[item[0]] = [data[item[0]]]; + } + // and add the new found value to it + data[item[0]].push(decodeURIComponent(item[1])); + } else { + // it doesn't exist, just put the found value in the data object + data[item[0]] = decodeURIComponent(item[1]); } + } - return data; + return data; }; /** @@ -236,13 +273,12 @@ OAuth.prototype.deParam = function(string) { * @param {String} url * @return {Object} */ -OAuth.prototype.deParamUrl = function(url) { - var tmp = url.split('?'); +OAuth.prototype.deParamUrl = function (url) { + var tmp = url.split('?'); - if (tmp.length === 1) - return {}; + if (tmp.length === 1) return {}; - return this.deParam(tmp[1]); + return this.deParam(tmp[1]); }; /** @@ -250,13 +286,13 @@ OAuth.prototype.deParamUrl = function(url) { * @param {String} str * @return {String} percent encoded string */ -OAuth.prototype.percentEncode = function(str) { - return encodeURIComponent(str) - .replace(/\!/g, "%21") - .replace(/\*/g, "%2A") - .replace(/\'/g, "%27") - .replace(/\(/g, "%28") - .replace(/\)/g, "%29"); +OAuth.prototype.percentEncode = function (str) { + return encodeURIComponent(str) + .replace(/\!/g, '%21') + .replace(/\*/g, '%2A') + .replace(/\'/g, '%27') + .replace(/\(/g, '%28') + .replace(/\)/g, '%29'); }; /** @@ -264,26 +300,28 @@ OAuth.prototype.percentEncode = function(str) { * @param {Object} data * @return {Object} percent encoded data */ -OAuth.prototype.percentEncodeData = function(data) { - var result = {}; - - for(var key in data) { - var value = data[key]; - // check if the value is an array - if (value && Array.isArray(value)){ - var newValue = []; - // percentEncode every value - value.forEach((function(val){ - newValue.push(this.percentEncode(val)); - }).bind(this)); - value = newValue; - } else { - value = this.percentEncode(value); - } - result[this.percentEncode(key)] = value; +OAuth.prototype.percentEncodeData = function (data) { + var result = {}; + + for (var key in data) { + var value = data[key]; + // check if the value is an array + if (value && Array.isArray(value)) { + var newValue = []; + // percentEncode every value + value.forEach( + function (val) { + newValue.push(this.percentEncode(val)); + }.bind(this) + ); + value = newValue; + } else { + value = this.percentEncode(value); } + result[this.percentEncode(key)] = value; + } - return result; + return result; }; /** @@ -291,48 +329,57 @@ OAuth.prototype.percentEncodeData = function(data) { * @param {Object} oauth_data * @return {String} Header data key - value */ -OAuth.prototype.toHeader = function(oauth_data) { - var sorted = this.sortObject(oauth_data); +OAuth.prototype.toHeader = function (oauth_data) { + var sorted = this.sortObject(oauth_data); - var header_value = 'OAuth '; + var header_value = 'OAuth '; - if (this.realm) { - header_value += 'realm="' + this.realm + '"' + this.parameter_seperator; - } + if (this.realm) { + header_value += 'realm="' + this.realm + '"' + this.parameter_seperator; + } - for(var i = 0; i < sorted.length; i++) { - if (sorted[i].key.indexOf('oauth_') !== 0) - continue; + for (var i = 0; i < sorted.length; i++) { + if (sorted[i].key.indexOf('oauth_') !== 0) continue; - header_value += this.percentEncode(sorted[i].key) + '="' + this.percentEncode(sorted[i].value) + '"' + this.parameter_seperator; - } + header_value += + this.percentEncode(sorted[i].key) + + '="' + + this.percentEncode(sorted[i].value) + + '"' + + this.parameter_seperator; + } - return { - Authorization: header_value.substr(0, header_value.length - this.parameter_seperator.length) //cut the last chars - }; + return { + Authorization: header_value.substr( + 0, + header_value.length - this.parameter_seperator.length + ) //cut the last chars + }; }; /** * Create a random word characters string with input length * @return {String} a random word characters string */ -OAuth.prototype.getNonce = function() { - var word_characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; - var result = ''; - - for(var i = 0; i < this.nonce_length; i++) { - result += word_characters[parseInt(Math.random() * word_characters.length, 10)]; - } +OAuth.prototype.getNonce = function () { + var word_characters = + 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; + var result = ''; + + for (var i = 0; i < this.nonce_length; i++) { + result += + word_characters[parseInt(Math.random() * word_characters.length, 10)]; + } - return result; + return result; }; /** * Get Current Unix TimeStamp * @return {Int} current unix timestamp */ -OAuth.prototype.getTimeStamp = function() { - return parseInt(new Date().getTime()/1000, 10); +OAuth.prototype.getTimeStamp = function () { + return parseInt(new Date().getTime() / 1000, 10); }; ////////////////////// HELPER FUNCTIONS ////////////////////// @@ -343,15 +390,15 @@ OAuth.prototype.getTimeStamp = function() { * @param {Object} obj2 * @return {Object} */ -OAuth.prototype.mergeObject = function(obj1, obj2) { - obj1 = obj1 || {}; - obj2 = obj2 || {}; +OAuth.prototype.mergeObject = function (obj1, obj2) { + obj1 = obj1 || {}; + obj2 = obj2 || {}; - var merged_obj = obj1; - for(var key in obj2) { - merged_obj[key] = obj2[key]; - } - return merged_obj; + var merged_obj = obj1; + for (var key in obj2) { + merged_obj[key] = obj2[key]; + } + return merged_obj; }; /** @@ -359,19 +406,19 @@ OAuth.prototype.mergeObject = function(obj1, obj2) { * @param {Object} data * @return {Array} sorted array */ -OAuth.prototype.sortObject = function(data) { - var keys = Object.keys(data); - var result = []; - - keys.sort(); - - for(var i = 0; i < keys.length; i++) { - var key = keys[i]; - result.push({ - key: key, - value: data[key], - }); - } +OAuth.prototype.sortObject = function (data) { + var keys = Object.keys(data); + var result = []; + + keys.sort(); + + for (var i = 0; i < keys.length; i++) { + var key = keys[i]; + result.push({ + key: key, + value: data[key] + }); + } - return result; + return result; };