"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/mksnapshot/node_modules/request/lib/oauth.js" (7 Feb 2017, 3124 Bytes) of archive /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 qs = require('qs')
    4   , caseless = require('caseless')
    5   , uuid = require('node-uuid')
    6   , oauth = require('oauth-sign')
    7 
    8 
    9 function OAuth (request) {
   10   this.request = request
   11 }
   12 
   13 OAuth.prototype.buildParams = function (_oauth, uri, method, query, form, qsLib) {
   14   var oa = {}
   15   for (var i in _oauth) {
   16     oa['oauth_' + i] = _oauth[i]
   17   }
   18   if (!oa.oauth_version) {
   19     oa.oauth_version = '1.0'
   20   }
   21   if (!oa.oauth_timestamp) {
   22     oa.oauth_timestamp = Math.floor( Date.now() / 1000 ).toString()
   23   }
   24   if (!oa.oauth_nonce) {
   25     oa.oauth_nonce = uuid().replace(/-/g, '')
   26   }
   27   if (!oa.oauth_signature_method) {
   28     oa.oauth_signature_method = 'HMAC-SHA1'
   29   }
   30 
   31   var consumer_secret_or_private_key = oa.oauth_consumer_secret || oa.oauth_private_key
   32   delete oa.oauth_consumer_secret
   33   delete oa.oauth_private_key
   34 
   35   var token_secret = oa.oauth_token_secret
   36   delete oa.oauth_token_secret
   37 
   38   var realm = oa.oauth_realm
   39   delete oa.oauth_realm
   40   delete oa.oauth_transport_method
   41 
   42   var baseurl = uri.protocol + '//' + uri.host + uri.pathname
   43   var params = qsLib.parse([].concat(query, form, qsLib.stringify(oa)).join('&'))
   44 
   45   oa.oauth_signature = oauth.sign(
   46     oa.oauth_signature_method,
   47     method,
   48     baseurl,
   49     params,
   50     consumer_secret_or_private_key,
   51     token_secret)
   52 
   53   if (realm) {
   54     oa.realm = realm
   55   }
   56 
   57   return oa
   58 }
   59 
   60 OAuth.prototype.concatParams = function (oa, sep, wrap) {
   61   wrap = wrap || ''
   62 
   63   var params = Object.keys(oa).filter(function (i) {
   64     return i !== 'realm' && i !== 'oauth_signature'
   65   }).sort()
   66 
   67   if (oa.realm) {
   68     params.splice(0, 1, 'realm')
   69   }
   70   params.push('oauth_signature')
   71 
   72   return params.map(function (i) {
   73     return i + '=' + wrap + oauth.rfc3986(oa[i]) + wrap
   74   }).join(sep)
   75 }
   76 
   77 OAuth.prototype.onRequest = function (_oauth) {
   78   var self = this
   79     , request = self.request
   80 
   81   var uri = request.uri || {}
   82     , method = request.method || ''
   83     , headers = caseless(request.headers)
   84     , body = request.body || ''
   85     , qsLib = request.qsLib || qs
   86 
   87   var form
   88     , query
   89     , contentType = headers.get('content-type') || ''
   90     , formContentType = 'application/x-www-form-urlencoded'
   91     , transport = _oauth.transport_method || 'header'
   92 
   93   if (contentType.slice(0, formContentType.length) === formContentType) {
   94     contentType = formContentType
   95     form = body
   96   }
   97   if (uri.query) {
   98     query = uri.query
   99   }
  100   if (transport === 'body' && (method !== 'POST' || contentType !== formContentType)) {
  101     throw new Error('oauth: transport_method of \'body\' requires \'POST\' ' +
  102       'and content-type \'' + formContentType + '\'')
  103   }
  104 
  105   var oa = this.buildParams(_oauth, uri, method, query, form, qsLib)
  106 
  107   switch (transport) {
  108     case 'header':
  109       request.setHeader('Authorization', 'OAuth ' + this.concatParams(oa, ',', '"'))
  110       break
  111 
  112     case 'query':
  113       request.path = (query ? '&' : '?') + this.concatParams(oa, '&')
  114       break
  115 
  116     case 'body':
  117       request.body = (form ? form + '&' : '') + this.concatParams(oa, '&')
  118       break
  119 
  120     default:
  121       throw new Error('oauth: transport_method invalid')
  122   }
  123 }
  124 
  125 exports.OAuth = OAuth