Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use CouchLogin tokens, if available

  • Loading branch information...
commit 7f25c06dfab0c9c56bd15ab34de2df43a81493c4 1 parent 95dc941
@isaacs isaacs authored
Showing with 42 additions and 10 deletions.
  1. +9 −0 index.js
  2. +33 −10 lib/request.js
View
9 index.js
@@ -6,6 +6,7 @@ module.exports = RegClient
var fs = require('fs')
, url = require('url')
, path = require('path')
+, CouchLogin = require('couch-login')
, npmlog
try {
@@ -55,6 +56,14 @@ function RegClient (options) {
}
}
+ if (this.auth && !this.alwaysAuth) {
+ // if we're always authing, then we just send the
+ // user/pass on every thing. otherwise, create a
+ // session, and use that.
+ this.token = options.token
+ this.couchLogin = new CouchLogin(this.registry, this.token)
+ }
+
this.email = options.email || null
this.defaultTag = options.tag || "latest"
View
43 lib/request.js
@@ -59,20 +59,39 @@ function regRequest (method, where, what, etag, nofollow, cb_) {
}
var remote = url.parse(where)
- , auth = authRequired && this.auth
-
- if (authRequired && !auth && this.username && this.password) {
- var a = this.username + ":" + this.password
- a = new Buffer(a, "utf8").toString("base64")
- auth = this.auth = a
+ , auth = this.auth
+
+ if (authRequired && !this.alwaysAuth) {
+ var couch = this.couchLogin
+ , token = couch && (this.token || couch.token)
+ , validToken = token && couch.valid(token)
+
+ if (!validToken) token = null
+ else this.token = token
+
+ if (couch && !token) {
+ // login to get a valid token
+ var a = { name: this.username, password: this.password }
+ var args = arguments
+ return this.couchLogin.login(a, function (er, cr, data) {
+ if (er || !couch.valid(couch.token)) {
+ er = er || new Error('login error')
+ return cb(er, cr, data)
+ }
+ this.token = this.couchLogin.token
+ return regRequest.call(this, method, where, what, etag, nofollow, cb_)
+ }.bind(this))
+ }
}
- if (authRequired && !auth) {
+ // now we either have a valid token, or an auth.
+
+ if (authRequired && !auth && !token) {
return cb(new Error(
"Cannot insert data into the registry without auth"))
}
- if (auth) {
+ if (auth && !token) {
remote.auth = new Buffer(auth, "base64").toString("utf8")
}
@@ -88,7 +107,7 @@ function regRequest (method, where, what, etag, nofollow, cb_) {
operation.attempt(function (currentAttempt) {
self.log.info("retry", "registry request attempt " + currentAttempt
+ " at " + (new Date()).toLocaleTimeString())
- makeRequest.call(self, method, remote, where, what, etag, nofollow
+ makeRequest.call(self, method, remote, where, what, etag, nofollow, token
, function (er, parsed, raw, response) {
// Only retry on 408, 5xx or no `response`.
var statusCode = response && response.statusCode
@@ -102,7 +121,7 @@ function regRequest (method, where, what, etag, nofollow, cb_) {
})
}
-function makeRequest (method, remote, where, what, etag, nofollow, cb_) {
+function makeRequest (method, remote, where, what, etag, nofollow, tok, cb_) {
var cbCalled = false
function cb () {
if (cbCalled) return
@@ -120,6 +139,10 @@ function makeRequest (method, remote, where, what, etag, nofollow, cb_) {
headers[method === "GET" ? "if-none-match" : "if-match"] = etag
}
+ if (tok) {
+ headers.cookie = 'AuthSession=' + tok.AuthSession
+ }
+
headers.accept = "application/json"
headers["user-agent"] = this.userAgent
Please sign in to comment.
Something went wrong with that request. Please try again.