diff --git a/lib/cradle.js b/lib/cradle.js index 5c72246..40ff37f 100644 --- a/lib/cradle.js +++ b/lib/cradle.js @@ -33,7 +33,7 @@ cradle.setup = function (settings) { cradle.Connection = function Connection(/* variable args */) { var args = Array.prototype.slice.call(arguments), - host, port, remote, options = {}; + host, port, remote, auth, options = {}; if (typeof(args[0]) === 'string') { remote = args[0].replace('http://', '').split(':'); @@ -46,6 +46,7 @@ cradle.Connection = function Connection(/* variable args */) { options = args[0]; host = options.host; port = parseInt(options.port); + auth = options.auth; // The host and port were passed separately } else if (args.length >= 2) { host = args[0]; @@ -55,6 +56,7 @@ cradle.Connection = function Connection(/* variable args */) { this.host = host || cradle.host; this.port = port || cradle.port; + this.auth = auth || cradle.auth; this.options = cradle.merge({}, cradle.options, options); this.socket = http.createClient(this.port, this.host); @@ -74,6 +76,11 @@ cradle.Connection.prototype.rawRequest = function (method, path, options, data, // HTTP Headers headers = cradle.merge({ host: this.host }, headers || {}); + // Set HTTP Basic Auth + if (this.auth) { + headers['Authorization'] = "Basic " + new Buffer(this.auth.user + ':' + this.auth.pass).toString('base64'); + } + path = (path || '/').replace('http://','').replace(/\/{2,}/g, '/'); if (path.charAt(0) !== '/') { path = '/' + path }