Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Paramters concating

  • Loading branch information...
commit e89758071672f227655e08ff801d9d07a7567615 1 parent b0e312c
@harrisiirak authored
View
1  .gitignore
@@ -1 +1,2 @@
node_modules
+.DS_Store
View
5 examples/twitter/twitter.js
@@ -20,6 +20,11 @@ var client = oauth.createClient(oauth.Version.OAUTH1, {
}
});
+console.log(client);
+client.getRequestToken(function() {
+
+});
+
//console.log(client);
app.listen(8000);
View
116 lib/adapters/oauth1.js
@@ -1,9 +1,18 @@
var util = require('util');
+var crypto = require('crypto');
+var URL = require('url');
+var querystring = require('querystring');
+
var adapter = require('./adapter');
// OAuth header
-function AuthenticationHeader(consumerKey, consumerSecret, signatureMethod, version, realm, url, method, fields) {
+function AuthenticationHeader(consumerKey, consumerSecret, signatureMethod, version, realm, url, params, method, fields) {
this._fields = {} || fields;
+ this._fields['oauth_consumer_key'] = consumerKey;
+ this._fields['oauth_signature_method'] = signatureMethod;
+ this._fields['oauth_timestamp'] = this._getTimestamp();
+ this._fields['oauth_nonce'] = this._generateNonce();
+
this._consumerKey = consumerKey;
this._consumerSecret = consumerSecret;
this._signatureMethod = signatureMethod;
@@ -11,14 +20,69 @@ function AuthenticationHeader(consumerKey, consumerSecret, signatureMethod, vers
this._realm = realm;
this._url = url;
this._method = method;
+
+ // Map signature values
+ var signatureValues = [];
+ this._mapDictionaryToArray(signatureValues, params);
+ this._mapDictionaryToArray(signatureValues, this._fields);
+
+ console.log(signatureValues);
+ /*
+ for (var c = params.length, i = 0; i < c; i++) {
+ this._signatureValues.push(params[i]);
+ }
+ */
}
-AuthenticationHeader.prototype._encode = function(data) {
+AuthenticationHeader._nonceKey = 'abcdefghijklmnopqrstuvwxyz0123456789'; // Base string for unique nonce generation
+AuthenticationHeader._nonceLength = 64;
+AuthenticationHeader.prototype._encode = function(data) {
+ if ((data instanceof String)) {
+ return encodeURIComponent(data)
+ .replace(/\!/g, '%21')
+ .replace(/\'/g, '%27')
+ .replace(/\(/g, '%28')
+ .replace(/\)/g, '%29')
+ .replace(/\*/g, '%2A');
+ }
+
+ return '';
};
AuthenticationHeader.prototype._decode = function(data) {
+ if ((data instanceof String)) {
+ return decodeURIComponent(data.replace(/\+/g, ' '));
+ }
+
+ return '';
+};
+
+AuthenticationHeader.prototype._mapDictionaryToArray = function(map, object) {
+ for (var key in object) {
+ if (typeof(object[key]) === 'object') {
+ this._mapDictionaryToArray(map, object[key]);
+ } else {
+ map.push([key, object[key]]);
+ }
+ }
+ return map;
+};
+
+AuthenticationHeader.prototype._generateNonce = function(data) {
+ var nonce = '';
+ var keyLength = AuthenticationHeader._nonceKey.length;
+
+ for (var i = 0; i < AuthenticationHeader._nonceLength; i++) {
+ nonce += AuthenticationHeader._nonceKey[Math.floor(Math.random() * keyLength)];
+ }
+
+ return nonce;
+};
+
+AuthenticationHeader.prototype._getTimestamp = function(data) {
+ return Math.floor((new Date()).getTime() / 1000);
};
AuthenticationHeader.prototype.getHeader = function() {
@@ -34,27 +98,45 @@ function Adapter(options) {
// TODO: Validate options
this._options = options;
this._options.version = '1.0A';
-
- return this;
}
util.inherits(Adapter, adapter.Adapter); // Inherit base adapter
-Adapter.prototype._sendRequest = function(url, method, authenticationHeader) {
-
+Adapter.prototype._sendRequest = function(url, method, body, authenticationHeader) {
+ url += '?b5=%3D%253D&a3=a&c%40=&a2=r%20b';
+ body = 'c2&a3=2+q';
+
+ var targetURL = URL.parse(url);
+ var queryParams = [];
+ var targetURLQueryParams = querystring.parse(targetURL.query) || {};
+ var targetURLBodyParams = body && body.length > 0 ? querystring.parse(body) : {};
+
+ // Append query params
+ for (var i in targetURLQueryParams) {
+ var value = {};
+ value[i] = targetURLQueryParams[i];
+ queryParams.push(value);
+ }
+
+ for (var i in targetURLBodyParams) {
+ var value = {};
+ value[i] = targetURLBodyParams[i];
+ queryParams.push(value);
+ }
+
+ var header = new AuthenticationHeader(this._options.consumerKey,
+ this._options.consumerSecret,
+ this._options.signatureMethod,
+ this._options.version,
+ this._options.realm,
+ url,
+ queryParams,
+ method,
+ {});
};
Adapter.prototype.getRequestToken = function(callback) {
- this._sendRequest(this._options.requestURL, 'GET', new AuthenticationHeader(this._options.consumerKey,
- this._options.consumerSecret,
- this._options.signatureMethod,
- this._options.version,
- this._options.realm,
- this._options.requestURL,
- 'GET',
- {
-
- }));
+ this._sendRequest(this._options.requestURL, 'GET', null);
};
Adapter.prototype.getAccessToken = function() {
@@ -62,4 +144,4 @@ Adapter.prototype.getAccessToken = function() {
};
module.exports.Adapter = Adapter;
-module.exports.Header = Header;
+module.exports.AuthenticationHeader = AuthenticationHeader;
View
29 lib/client.js
@@ -1 +1,30 @@
+function Client() {
+
+};
+
+// Define adapter versions
+Client.Version = {
+ OAUTH1: 1,
+ OAUTH2: 2
+};
+
+Client.Signature = {
+ HMACSHA1: 'HMAC-SHA1',
+ PLAINTEXT: 'PLAINTEXT'
+};
+
+Client.createClient = function(version, options) {
+ switch (version) {
+ case Client.Version.OAUTH1:
+ return new (require('./adapters/oauth1').Adapter)(options);
+ break;
+
+ case Client.Version.OAUTH2:
+ break;
+ }
+
+ throw Error('Invalid client version');
+};
+
+module.exports.Client = Client;
View
2  lib/index.js
@@ -1 +1 @@
-module.exports = require('lib/client').Client;
+module.exports.Client = require('./client.js').Client;
Please sign in to comment.
Something went wrong with that request. Please try again.