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

Commit

Permalink
Properly handle user-switching in adduser
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Jul 23, 2012
1 parent c0ff7e7 commit 5badc2f
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions lib/adduser.js
Expand Up @@ -39,7 +39,20 @@ function adduser (username, password, email, cb) {
, date: new Date().toISOString()
}

cb = done.call(this, cb)
// pluck off any other username/password/token. it needs to be the
// same as the user we're becoming now. replace them on error.
var pre = { username: this.username
, password: this.password
, auth: this.auth
, token: this.token }

this.token = null
this.couchLogin.token = null

This comment has been minimized.

Copy link
@leecookson

leecookson Jul 23, 2012

Contributor

This is failing since couchLogin isn't guaranteed to exist here.

npm ERR! TypeError: Cannot set property 'token' of undefined
npm ERR! at RegClient.adduser (/usr/local/lib/node_modules/npm/node_modules/npm-registry-client/lib/adduser.js:50:25)

This comment has been minimized.

Copy link
@isaacs

isaacs Jul 23, 2012

Author Contributor

Indeed. Fixed on 7814028, isaacs/npm@7216113

this.username = username
this.password = password
this.auth = new Buffer(username + ':' + password).toString('base64')

cb = done.call(this, cb, pre)

var logObj = Object.keys(userobj).map(function (k) {
if (k === 'salt' || k === 'password_sha') return [k, 'XXXXX']
Expand All @@ -50,6 +63,7 @@ function adduser (username, password, email, cb) {
}, {})

this.log.verbose("adduser", "before first PUT", logObj)

this.request('PUT'
, '/-/user/org.couchdb.user:'+encodeURIComponent(username)
, userobj
Expand Down Expand Up @@ -89,11 +103,21 @@ function adduser (username, password, email, cb) {
}.bind(this))
}

function done (cb) {
function done (cb, pre) {
return function (error, data, json, response) {
if (!error && (!response || response.statusCode === 201)) {
return cb(error, data, json, response)
}

// there was some kind of error, re-instate previous auth/token/etc.
this.couchLogin.token = this.token = pre.token
if (this.couchLogin.tokenSet) {
this.couchLogin.tokenSet(pre.token)
}
this.username = pre.username
this.password = pre.password
this.auth = pre.auth

this.log.verbose("adduser", "back", [error, data, json])
if (!error) {
error = new Error( (response && response.statusCode || "") + " "+
Expand Down

0 comments on commit 5badc2f

Please sign in to comment.