Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
Handle reauthing on 403 properly
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Feb 11, 2013
1 parent d9a2a16 commit 39c2c46
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ var url = require("url")
, request = require("request")
, retry = require("retry")

function regRequest (method, where, what, etag, nofollow, cb_) {
function regRequest (method, where, what, etag, nofollow, reauthed, cb_) {
if (typeof cb_ !== "function") cb_ = reauthed, reauthed = false
if (typeof cb_ !== "function") cb_ = nofollow, nofollow = false
if (typeof cb_ !== "function") cb_ = etag, etag = null
if (typeof cb_ !== "function") cb_ = what, what = null
Expand Down Expand Up @@ -84,7 +85,7 @@ function regRequest (method, where, what, etag, nofollow, cb_) {
this.conf.set('_token', this.couchLogin.token)
return regRequest.call(this,
method, where, what,
etag, nofollow, cb_)
etag, nofollow, reauthed, cb_)
}.bind(this))
}
}
Expand All @@ -110,7 +111,6 @@ function regRequest (method, where, what, etag, nofollow, cb_) {
})

var self = this
var reauthed = false;
operation.attempt(function (currentAttempt) {
self.log.info("trying", "registry request attempt " + currentAttempt
+ " at " + (new Date()).toLocaleTimeString())
Expand All @@ -129,14 +129,17 @@ function regRequest (method, where, what, etag, nofollow, cb_) {
( statusCode === 401 ||
statusCode === 400 ||
statusCode === 403 )
if (reauth)
reauthed = true

var timeout = statusCode === 408
var serverError = statusCode >= 500
var statusRetry = !statusCode || timeout || serverError
if (reauth && this.conf.get('_auth') && this.conf.get('_token')) {
this.conf.del('_token')
this.couchLogin.token = null
return regRequest.call(this, method, where, what, etag, nofollow, cb_)
return regRequest.call(this, method, where, what,
etag, nofollow, reauthed, cb_)
}
if (er && statusRetry && operation.retry(er)) {
self.log.info("retry", "will retry, error on last attempt: " + er)
Expand Down

0 comments on commit 39c2c46

Please sign in to comment.