"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/request/lib/oauth.js" (8 Mar 2017, 3949 Bytes) of package /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 'use strict'
    2 
    3 var url = require('url')
    4   , qs = require('qs')
    5   , caseless = require('caseless')
    6   , uuid = require('uuid')
    7   , oauth = require('oauth-sign')
    8   , crypto = require('crypto')
    9 
   10 
   11 function OAuth (request) {
   12   this.request = request
   13   this.params = null
   14 }
   15 
   16 OAuth.prototype.buildParams = function (_oauth, uri, method, query, form, qsLib) {
   17   var oa = {}
   18   for (var i in _oauth) {
   19     oa['oauth_' + i] = _oauth[i]
   20   }
   21   if (!oa.oauth_version) {
   22     oa.oauth_version = '1.0'
   23   }
   24   if (!oa.oauth_timestamp) {
   25     oa.oauth_timestamp = Math.floor( Date.now() / 1000 ).toString()
   26   }
   27   if (!oa.oauth_nonce) {
   28     oa.oauth_nonce = uuid().replace(/-/g, '')
   29   }
   30   if (!oa.oauth_signature_method) {
   31     oa.oauth_signature_method = 'HMAC-SHA1'
   32   }
   33 
   34   var consumer_secret_or_private_key = oa.oauth_consumer_secret || oa.oauth_private_key
   35   delete oa.oauth_consumer_secret
   36   delete oa.oauth_private_key
   37 
   38   var token_secret = oa.oauth_token_secret
   39   delete oa.oauth_token_secret
   40 
   41   var realm = oa.oauth_realm
   42   delete oa.oauth_realm
   43   delete oa.oauth_transport_method
   44 
   45   var baseurl = uri.protocol + '//' + uri.host + uri.pathname
   46   var params = qsLib.parse([].concat(query, form, qsLib.stringify(oa)).join('&'))
   47 
   48   oa.oauth_signature = oauth.sign(
   49     oa.oauth_signature_method,
   50     method,
   51     baseurl,
   52     params,
   53     consumer_secret_or_private_key,
   54     token_secret)
   55 
   56   if (realm) {
   57     oa.realm = realm
   58   }
   59 
   60   return oa
   61 }
   62 
   63 OAuth.prototype.buildBodyHash = function(_oauth, body) {
   64   if (['HMAC-SHA1', 'RSA-SHA1'].indexOf(_oauth.signature_method || 'HMAC-SHA1') < 0) {
   65     this.request.emit('error', new Error('oauth: ' + _oauth.signature_method +
   66       ' signature_method not supported with body_hash signing.'))
   67   }
   68 
   69   var shasum = crypto.createHash('sha1')
   70   shasum.update(body || '')
   71   var sha1 = shasum.digest('hex')
   72 
   73   return new Buffer(sha1).toString('base64')
   74 }
   75 
   76 OAuth.prototype.concatParams = function (oa, sep, wrap) {
   77   wrap = wrap || ''
   78 
   79   var params = Object.keys(oa).filter(function (i) {
   80     return i !== 'realm' && i !== 'oauth_signature'
   81   }).sort()
   82 
   83   if (oa.realm) {
   84     params.splice(0, 0, 'realm')
   85   }
   86   params.push('oauth_signature')
   87 
   88   return params.map(function (i) {
   89     return i + '=' + wrap + oauth.rfc3986(oa[i]) + wrap
   90   }).join(sep)
   91 }
   92 
   93 OAuth.prototype.onRequest = function (_oauth) {
   94   var self = this
   95   self.params = _oauth
   96 
   97   var uri = self.request.uri || {}
   98     , method = self.request.method || ''
   99     , headers = caseless(self.request.headers)
  100     , body = self.request.body || ''
  101     , qsLib = self.request.qsLib || qs
  102 
  103   var form
  104     , query
  105     , contentType = headers.get('content-type') || ''
  106     , formContentType = 'application/x-www-form-urlencoded'
  107     , transport = _oauth.transport_method || 'header'
  108 
  109   if (contentType.slice(0, formContentType.length) === formContentType) {
  110     contentType = formContentType
  111     form = body
  112   }
  113   if (uri.query) {
  114     query = uri.query
  115   }
  116   if (transport === 'body' && (method !== 'POST' || contentType !== formContentType)) {
  117     self.request.emit('error', new Error('oauth: transport_method of body requires POST ' +
  118       'and content-type ' + formContentType))
  119   }
  120 
  121   if (!form && typeof _oauth.body_hash === 'boolean') {
  122     _oauth.body_hash = self.buildBodyHash(_oauth, self.request.body.toString())
  123   }
  124 
  125   var oa = self.buildParams(_oauth, uri, method, query, form, qsLib)
  126 
  127   switch (transport) {
  128     case 'header':
  129       self.request.setHeader('Authorization', 'OAuth ' + self.concatParams(oa, ',', '"'))
  130       break
  131 
  132     case 'query':
  133       var href = self.request.uri.href += (query ? '&' : '?') + self.concatParams(oa, '&')
  134       self.request.uri = url.parse(href)
  135       self.request.path = self.request.uri.path
  136       break
  137 
  138     case 'body':
  139       self.request.body = (form ? form + '&' : '') + self.concatParams(oa, '&')
  140       break
  141 
  142     default:
  143       self.request.emit('error', new Error('oauth: transport_method invalid'))
  144   }
  145 }
  146 
  147 exports.OAuth = OAuth