Request.prototype.oauth = function (_oauth) { var self = this var form, query if (self.hasHeader('content-type') && self.getHeader('content-type').slice(0, 'application/x-www-form-urlencoded'.length) === 'application/x-www-form-urlencoded' ) { form = self.body } if (self.uri.query) { query = self.uri.query } var oa = {} for (var i in _oauth) { oa['oauth_' + i] = _oauth[i] } if ('oauth_realm' in oa) { delete oa.oauth_realm } if (!oa.oauth_version) { oa.oauth_version = '1.0' } if (!oa.oauth_timestamp) { oa.oauth_timestamp = Math.floor( Date.now() / 1000 ).toString() } if (!oa.oauth_nonce) { oa.oauth_nonce = uuid().replace(/-/g, '') } if (!oa.oauth_signature_method) { oa.oauth_signature_method = 'HMAC-SHA1' } var consumer_secret_or_private_key = oa.oauth_consumer_secret || oa.oauth_private_key delete oa.oauth_consumer_secret delete oa.oauth_private_key var token_secret = oa.oauth_token_secret delete oa.oauth_token_secret var baseurl = self.uri.protocol + '//' + self.uri.host + self.uri.pathname var params = self.qsLib.parse([].concat(query, form, self.qsLib.stringify(oa)).join('&')) var signature = oauth.sign( oa.oauth_signature_method, self.method, baseurl, params, consumer_secret_or_private_key, token_secret) var realm = _oauth.realm ? 'realm="' + _oauth.realm + '",' : '' var authHeader = 'OAuth ' + realm + Object.keys(oa).sort().map(function (i) {return i + '="' + oauth.rfc3986(oa[i]) + '"'}).join(',') authHeader += ',oauth_signature="' + oauth.rfc3986(signature) + '"' self.setHeader('Authorization', authHeader) return self }
var Userstream = function (options) { var signature, params; ReadableStream.call(this, { objectMode: true }); params = { oauth_version: '1.0', oauth_consumer_key: options.auth.consumerKey, oauth_timestamp: Math.floor(Date.now() / 1000).toString(), oauth_nonce: uuid().replace(/-/g, ''), oauth_signature_method: 'HMAC-SHA1', oauth_token: options.auth.token }; var pathString = '/1.1/user.json'; var requestParams = {} if(options.with) { requestParams['with'] = options.with; } if(options.replies) { requestParams['replies'] = options.replies; } signature = oauth.hmacsign( 'GET', 'https://userstream.twitter.com/1.1/user.json', params, options.auth.consumerSecret, options.auth.tokenSecret ); this.request = https.request({ host: 'userstream.twitter.com', headers: { authorization: 'OAuth ' + Object.keys(params).sort().map(function (key) { return key + '="' + oauth.rfc3986(params[key]) + '"'; }).join(',') + ',oauth_signature="' + oauth.rfc3986(signature) + '"' }, path: '/1.1/user.json', method: 'GET', query: querystring.stringify(requestParams) }); this.request.end(); this.request.on('response', this._onResponse.bind(this)); };
Request.prototype.oauth = function (_oauth) { var form if (this.headers['content-type'] && this.headers['content-type'].slice(0, 'application/x-www-form-urlencoded'.length) === 'application/x-www-form-urlencoded' ) { form = qs.parse(this.body) } if (this.uri.query) { form = qs.parse(this.uri.query) } if (!form) form = {} var oa = {} for (var i in form) oa[i] = form[i] for (var i in _oauth) oa['oauth_'+i] = _oauth[i] if (!oa.oauth_version) oa.oauth_version = '1.0' if (!oa.oauth_timestamp) oa.oauth_timestamp = Math.floor( Date.now() / 1000 ).toString() if (!oa.oauth_nonce) oa.oauth_nonce = uuid().replace(/-/g, '') oa.oauth_signature_method = 'HMAC-SHA1' var consumer_secret = oa.oauth_consumer_secret delete oa.oauth_consumer_secret var token_secret = oa.oauth_token_secret delete oa.oauth_token_secret var timestamp = oa.oauth_timestamp var baseurl = this.uri.protocol + '//' + this.uri.host + this.uri.pathname var signature = oauth.hmacsign(this.method, baseurl, oa, consumer_secret, token_secret) // oa.oauth_signature = signature for (var i in form) { if ( i.slice(0, 'oauth_') in _oauth) { // skip } else { delete oa['oauth_'+i] if (i !== 'x_auth_mode') delete oa[i] } } oa.oauth_timestamp = timestamp this.headers.Authorization = 'OAuth '+Object.keys(oa).sort().map(function (i) {return i+'="'+oauth.rfc3986(oa[i])+'"'}).join(',') this.headers.Authorization += ',oauth_signature="' + oauth.rfc3986(signature) + '"' return this }
Object.keys(oa).sort().map(function (i) {return i + '="' + oauth.rfc3986(oa[i]) + '"'}).join(',')
authorization: 'OAuth ' + Object.keys(params).sort().map(function (key) { return key + '="' + oauth.rfc3986(params[key]) + '"'; }).join(',') + ',oauth_signature="' + oauth.rfc3986(signature) + '"'
return params.map(function (i) { return i + '=' + wrap + oauth.rfc3986(oa[i]) + wrap }).join(sep)
return 'OAuth realm="",'+Object.keys(oa).map(function (i) { return i+'="'+oauth.rfc3986(oa[i])+'"' }).join(',');