Permalink
Browse files

look for registry-specific auth

Also clean out some of this lint.
  • Loading branch information...
1 parent f852f07 commit 0a5893b96ad980faa11bf2a9c1a00af1d00e2502 @othiym23 othiym23 committed Jun 20, 2014
Showing with 46 additions and 40 deletions.
  1. +46 −40 lib/request.js
View
@@ -65,10 +65,9 @@ function regRequest (method, uri, options, cb_) {
, adduserNew = /^\/?-\/user\/org\.couchdb\.user:([^\/]+)/
, nu = where.match(adduserNew)
, uc = where.match(adduserChange)
- , alwaysAuth = this.conf.get('always-auth')
+ , alwaysAuth = this.conf.get("always-auth")
, isDel = method === "DELETE"
, isWrite = what || isDel
- , authRequired = (authThis || alwaysAuth || isWrite) && !nu || uc || isDel
// resolve to a full url on the registry
if (!where.match(/^https?:\/\//)) {
@@ -94,32 +93,39 @@ function regRequest (method, uri, options, cb_) {
this.log.verbose("request", "where is", where)
var remote = url.parse(where)
- , auth = this.conf.get('_auth')
-
- if (authRequired && !auth) {
- var un = this.conf.get('username')
- var pw = this.conf.get('_password')
+ if ((authThis || alwaysAuth || isWrite) && !nu || uc || isDel) {
+ // 1. see if there's multi-registry auth in there
+ var auth = this.conf.get(toNerfDart(where) + ":_auth")
+
+ // 2. check for (deprecated) generic auth
+ if (!auth) auth = this.conf.get("_auth")
+
+ // 3. check to see if npmconf has unbundled the credentials
+ if (!auth) {
+ var un = this.conf.get("username")
+ var pw = this.conf.get("_password")
+
+ if (!(un && pw)) {
+ return cb(new Error(
+ "This request requires auth credentials. Run `npm login` and repeat the request."
+ ))
+ }
- if (!(un && pw)) {
- return cb(new Error(
- "This request requires auth credentials. Run `npm login` and repeat the request."
- ))
+ auth = new Buffer(un + ":" + pw).toString("base64")
}
- auth = new Buffer(un + ':' + pw).toString('base64')
- }
-
- if (auth && authRequired) {
- remote.auth = new Buffer(auth, "base64").toString("utf8")
+ if (auth) {
+ remote.auth = new Buffer(auth, "base64").toString("utf8")
+ }
}
// Tuned to spread 3 attempts over about a minute.
// See formula at <https://github.com/tim-kos/node-retry>.
var operation = retry.operation({
- retries: this.conf.get('fetch-retries') || 2,
- factor: this.conf.get('fetch-retry-factor'),
- minTimeout: this.conf.get('fetch-retry-mintimeout') || 10000,
- maxTimeout: this.conf.get('fetch-retry-maxtimeout') || 60000
+ retries: this.conf.get("fetch-retries") || 2,
+ factor: this.conf.get("fetch-retry-factor"),
+ minTimeout: this.conf.get("fetch-retry-mintimeout") || 10000,
+ maxTimeout: this.conf.get("fetch-retry-maxtimeout") || 60000
})
var self = this
@@ -130,7 +136,7 @@ function regRequest (method, uri, options, cb_) {
, function (er, parsed, raw, response) {
if (!er || (er.message && er.message.match(/^SSL Error/))) {
if (er)
- er.code = 'ESSL'
+ er.code = "ESSL"
return cb(er, parsed, raw, response)
}
@@ -142,7 +148,7 @@ function regRequest (method, uri, options, cb_) {
var statusRetry = !statusCode || timeout || serverError
if (er && statusRetry && operation.retry(er)) {
self.log.info("retry", "will retry, error on last attempt: " + er)
- return
+ return undefined
}
if (response)
this.log.verbose("headers", response.headers)
@@ -159,36 +165,36 @@ function makeRequest (method, remote, where, what, etag, nofollow, cb_) {
cb_.apply(null, arguments)
}
- var strict = this.conf.get('strict-ssl')
+ var strict = this.conf.get("strict-ssl")
if (strict === undefined) strict = true
var opts = { url: remote
, method: method
, encoding: null // tell request let body be Buffer instance
- , ca: this.conf.get('ca')
- , localAddress: this.conf.get('local-address')
- , cert: this.conf.get('cert')
- , key: this.conf.get('key')
+ , ca: this.conf.get("ca")
+ , localAddress: this.conf.get("local-address")
+ , cert: this.conf.get("cert")
+ , key: this.conf.get("key")
, strictSSL: strict }
, headers = opts.headers = {}
if (etag) {
this.log.verbose("etag", etag)
headers[method === "GET" ? "if-none-match" : "if-match"] = etag
}
- headers['npm-session'] = this.sessionToken
+ headers["npm-session"] = this.sessionToken
headers.version = this.version || pkg.version
if (this.refer) {
headers.referer = this.refer
}
headers.accept = "application/json"
- headers['accept-encoding'] = 'gzip'
+ headers["accept-encoding"] = "gzip"
- headers["user-agent"] = this.conf.get('user-agent') ||
- 'node/' + process.version
+ headers["user-agent"] = this.conf.get("user-agent") ||
+ "node/" + process.version
- var tokenKey = toKey(url.format(remote))
+ var tokenKey = toNerfDart(url.format(remote)) + ":_authToken"
this.log.silly("tokenKey", tokenKey)
var authToken = this.conf.get(tokenKey)
if (authToken) {
@@ -197,11 +203,11 @@ function makeRequest (method, remote, where, what, etag, nofollow, cb_) {
headers.authorization = "Bearer " + authToken
}
- var p = this.conf.get('proxy')
- var sp = this.conf.get('https-proxy') || p
+ var p = this.conf.get("proxy")
+ var sp = this.conf.get("https-proxy") || p
opts.proxy = remote.protocol === "https:" ? sp : p
- // figure out wth 'what' is
+ // figure out wth "what" is
if (what) {
if (Buffer.isBuffer(what) || typeof what === "string") {
opts.body = what
@@ -245,7 +251,7 @@ function decodeResponseBody(cb) {
response.socket.destroy()
}
- if (response.headers['content-encoding'] !== 'gzip') return cb(er, response, data)
+ if (response.headers["content-encoding"] !== "gzip") return cb(er, response, data)
zlib.gunzip(data, function (er, buf) {
if (er) return cb(er, response, data)
@@ -262,7 +268,7 @@ function requestDone (method, where, cb) {
var urlObj = url.parse(where)
if (urlObj.auth)
- urlObj.auth = '***'
+ urlObj.auth = "***"
this.log.http(response.statusCode, url.format(urlObj))
var parsed
@@ -303,7 +309,7 @@ function requestDone (method, where, cb) {
if (!w.match(/^-/) && parsed.error === "not_found") {
w = w.split("/")
name = w[w.indexOf("_rewrite") + 1]
- er = new Error("404 Not Found: "+name)
+ er = new Error("404 Not Found: " + name)
er.code = "E404"
er.pkgid = name
} else {
@@ -323,11 +329,11 @@ function requestDone (method, where, cb) {
}.bind(this)
}
-function toKey(uri) {
+function toNerfDart(uri) {
var parsed = url.parse(uri)
parsed.pathname = "/"
delete parsed.protocol
delete parsed.auth
- return url.format(parsed) + ":_authToken"
+ return url.format(parsed)
}

0 comments on commit 0a5893b

Please sign in to comment.