@electorrent/xmlrpc
Advanced tools
Comparing version 1.3.2 to 1.4.0
@@ -7,2 +7,3 @@ var http = require('http') | ||
, Cookies = require('./cookies') | ||
, www_auth = require('www-authenticate') | ||
@@ -57,9 +58,7 @@ /** | ||
if (options.headers.Authorization == null && | ||
options.basic_auth != null && | ||
options.basic_auth.user != null && | ||
options.basic_auth.pass != null) | ||
{ | ||
var auth = options.basic_auth.user + ':' + options.basic_auth.pass | ||
options.headers['Authorization'] = 'Basic ' + new Buffer(auth).toString('base64') | ||
this.username = options.username | ||
this.password = options.password | ||
if (this.username === undefined && this.password === undefined) { | ||
this.auth = false | ||
} | ||
@@ -101,3 +100,15 @@ | ||
*/ | ||
Client.prototype.methodCall = function methodCall(method, params, callback) { | ||
Client.prototype.methodCall = function methodCall(method, params, callback, retry) { | ||
if (this.auth === undefined) { | ||
this.authenticate(function(err) { | ||
if (err) { | ||
callback(err) | ||
} else { | ||
this.methodCall(method, params, callback) | ||
} | ||
}.bind(this)) | ||
return | ||
} | ||
var options = this.options | ||
@@ -107,9 +118,18 @@ var xml = Serializer.serializeMethodCall(method, params, options.encoding) | ||
// Authenticate request using authenticator | ||
if (this.auth !== false) { | ||
this.auth.authenticate_request_options(options) | ||
if (this.auth.err) { | ||
return callback(this.auth.err) | ||
} | ||
} | ||
options.headers['Content-Length'] = Buffer.byteLength(xml, 'utf8') | ||
this.headersProcessors.composeRequest(options.headers) | ||
var request = transport.request(options, function(response) { | ||
if (this.auth !== false) { | ||
this.auth.get_challenge(response) | ||
} | ||
var body = [] | ||
response.on('data', function (chunk) { body.push(chunk) }) | ||
function __enrichError (err) { | ||
@@ -122,2 +142,13 @@ Object.defineProperty(err, 'req', { value: request }) | ||
if (response.statusCode == 401) { | ||
if (retry === false) { | ||
return callback(__enrichError(new Error("Unauthorized"))) | ||
} else { | ||
return this.methodCall(method, params, callback, false) | ||
} | ||
} | ||
var body = [] | ||
response.on('data', function (chunk) { body.push(chunk) }) | ||
if (response.statusCode == 404) { | ||
@@ -141,2 +172,5 @@ callback(__enrichError(new Error('Not Found'))) | ||
request.on('error', callback) | ||
request.on('timeout', function() { | ||
request.abort() | ||
}) | ||
request.write(xml, 'utf8') | ||
@@ -182,3 +216,27 @@ request.end() | ||
/** | ||
* Authenticates the client using basic auth of digest auth. | ||
* | ||
* @param {Function} callback - function(error) { ... } | ||
*/ | ||
Client.prototype.authenticate = function(callback) { | ||
var options = this.options | ||
var transport = this.isSecure ? https : http | ||
var request = transport.request(options, function(response) { | ||
var auth = www_auth.authenticator(this.username, this.password) | ||
auth.get_challenge(response) | ||
this.auth = auth | ||
callback() | ||
}.bind(this)) | ||
request.on('error', callback) | ||
request.on('timeout', function() { | ||
request.abort() | ||
}) | ||
request.end() | ||
} | ||
module.exports = Client | ||
@@ -10,7 +10,7 @@ { | ||
], | ||
"version": "1.3.2", | ||
"version": "1.4.0", | ||
"homepage": "https://github.com/tympanix/node-xmlrpc", | ||
"author": "Brandon Alexander <baalexander@gmail.com> (https://github.com/baalexander)", | ||
"contributors": [ | ||
"tympanix <tympanix@gmail.com>" | ||
"tympanix <tympanix@gmail.com>" | ||
], | ||
@@ -30,3 +30,3 @@ "repository": { | ||
"sax": "1.2.x", | ||
"urllib": "^2.29.1", | ||
"www-authenticate": "^0.6.2", | ||
"xmlbuilder": "8.2.x" | ||
@@ -33,0 +33,0 @@ }, |
46277
1078
+ Addedwww-authenticate@^0.6.2
+ Addedwww-authenticate@0.6.3(transitive)
- Removedurllib@^2.29.1
- Removedany-promise@1.3.0(transitive)
- Removedcall-bind-apply-helpers@1.0.2(transitive)
- Removedcall-bound@1.0.3(transitive)
- Removedcontent-type@1.0.5(transitive)
- Removedcopy-to@2.0.1(transitive)
- Removeddefault-user-agent@1.0.0(transitive)
- Removeddestroy@1.2.0(transitive)
- Removeddigest-header@1.1.0(transitive)
- Removeddunder-proto@1.0.1(transitive)
- Removedee-first@1.1.1(transitive)
- Removedend-of-stream@1.4.4(transitive)
- Removedes-define-property@1.0.1(transitive)
- Removedes-errors@1.3.0(transitive)
- Removedes-object-atoms@1.1.1(transitive)
- Removedescape-html@1.0.3(transitive)
- Removedextend-shallow@2.0.1(transitive)
- Removedformstream@1.5.1(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedget-intrinsic@1.2.7(transitive)
- Removedget-proto@1.0.1(transitive)
- Removedgopd@1.2.0(transitive)
- Removedhas-symbols@1.1.0(transitive)
- Removedhasown@2.0.2(transitive)
- Removedhumanize-ms@1.2.1(transitive)
- Removediconv-lite@0.6.3(transitive)
- Removedis-extendable@0.1.1(transitive)
- Removedmath-intrinsics@1.1.0(transitive)
- Removedmime@2.6.0(transitive)
- Removedminimist@1.2.8(transitive)
- Removedmkdirp@0.5.6(transitive)
- Removedms@2.1.3(transitive)
- Removedmz@2.7.0(transitive)
- Removednode-hex@1.0.1(transitive)
- Removedobject-assign@4.1.1(transitive)
- Removedobject-inspect@1.13.4(transitive)
- Removedonce@1.4.0(transitive)
- Removedos-name@1.0.3(transitive)
- Removedosx-release@1.1.0(transitive)
- Removedpause-stream@0.0.11(transitive)
- Removedpump@3.0.2(transitive)
- Removedqs@6.14.0(transitive)
- Removedsafer-buffer@2.1.2(transitive)
- Removedsemver@5.7.2(transitive)
- Removedside-channel@1.1.0(transitive)
- Removedside-channel-list@1.0.0(transitive)
- Removedside-channel-map@1.0.1(transitive)
- Removedside-channel-weakmap@1.0.2(transitive)
- Removedstatuses@1.5.0(transitive)
- Removedthenify@3.3.1(transitive)
- Removedthenify-all@1.6.0(transitive)
- Removedthrough@2.3.8(transitive)
- Removedunescape@1.0.1(transitive)
- Removedurllib@2.44.0(transitive)
- Removedutility@1.18.0(transitive)
- Removedwin-release@1.1.1(transitive)
- Removedwrappy@1.0.2(transitive)