(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}(g.oauth1 || (g.oauth1 = {})).a = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i merge with oauth data * -> percent encode key & value * -> sort * * @param {Object} request 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))))); } 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; }; /** * Create a Signing Key * @param {String} token_secret Secret Token * @return {String} Signing Key */ OAuth.prototype.getSigningKey = function(token_secret) { token_secret = token_secret || ''; if(!this.last_ampersand && !token_secret) { return this.percentEncode(this.consumer.secret); } return this.percentEncode(this.consumer.secret) + '&' + this.percentEncode(token_secret); }; /** * Get base url * @param {String} url * @return {String} */ OAuth.prototype.getBaseUrl = function(url) { return url.split('?')[0]; }; /** * Get data from String * @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]); } } return data; }; /** * Get data from url * @param {String} url * @return {Object} */ OAuth.prototype.deParamUrl = function(url) { var tmp = url.split('?'); if (tmp.length === 1) return {}; return this.deParam(tmp[1]); }; /** * Percent Encode * @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"); }; /** * Percent Encode Object * @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; } return result; }; /** * Get OAuth data as Header * @param {Object} oauth_data * @return {String} Header data key - value */ OAuth.prototype.toHeader = function(oauth_data) { var sorted = this.sortObject(oauth_data); var header_value = 'OAuth '; 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; 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 }; }; /** * 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)]; } return result; }; /** * Get Current Unix TimeStamp * @return {Int} current unix timestamp */ OAuth.prototype.getTimeStamp = function() { return parseInt(new Date().getTime()/1000, 10); }; ////////////////////// HELPER FUNCTIONS ////////////////////// /** * Merge object * @param {Object} obj1 * @param {Object} obj2 * @return {Object} */ 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; }; /** * Sort object by key * @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], }); } return result; }; },{}]},{},[])("oauth-1.0a") });