request/lib/oauth.js

Maintainability

58.89

Lines of code

144

Created with Raphaël 2.1.002550751002015-5-18

2015-5-18
Maintainability: 58.89

Created with Raphaël 2.1.00501001502002015-5-18

2015-5-18
Lines of Code: 144

Difficulty

36.16

Estimated Errors

1.58

Function weight

By Complexity

Created with Raphaël 2.1.0<anonymous>.onRequest17

By SLOC

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