diff options
Diffstat (limited to 'node_modules/couch-login/couch-login.js')
-rw-r--r-- | node_modules/couch-login/couch-login.js | 35 |
1 files changed, 26 insertions, 9 deletions
diff --git a/node_modules/couch-login/couch-login.js b/node_modules/couch-login/couch-login.js index a2b323439..f0b2d4388 100644 --- a/node_modules/couch-login/couch-login.js +++ b/node_modules/couch-login/couch-login.js @@ -3,6 +3,7 @@ var request = require('request') , crypto = require('crypto') , YEAR = (1000 * 60 * 60 * 24 * 365) , BASIC = {} +, assert = require('assert') module.exports = CouchLogin @@ -63,6 +64,7 @@ Object.defineProperty(CouchLogin.prototype, 'constructor', { value: CouchLogin, enumerable: false }) function decorate (req, res) { + assert(this instanceof CouchLogin) req.couch = res.couch = this // backed by some sort of set(k,v,cb), get(k,cb) session storage. @@ -86,10 +88,12 @@ function decorate (req, res) { } function anon () { + assert(this instanceof CouchLogin) return new CouchLogin(this.couch, NaN) } function makeReq (meth, body, f) { return function madeReq (p, d, cb) { + assert(this instanceof CouchLogin) f = f || (this.token !== this.token) if (!f && !valid(this.token)) { // lazily get the token. @@ -129,19 +133,24 @@ function makeReq (meth, body, f) { return function madeReq (p, d, cb) { req.proxy = this.proxy } + // we're handling cookies, don't do it for us. + req.jar = false + request(req, function (er, res, data) { // update cookie. if (er || res.statusCode !== 200) return cb(er, res, data) addToken.call(this, res) - return cb(er, res, data) + return cb.call(this, er, res, data) }.bind(this)) }} function login (auth, cb) { + assert(this instanceof CouchLogin) if (this.token === BASIC) { this.auth = new Buffer(auth.name + ':' + auth.password).toString('base64') this.name = auth.name - return process.nextTick(cb, null, { statusCode: 200 }, { ok: true }) + cb = cb.bind(this, null, { statusCode: 200 }, { ok: true }) + return process.nextTick(cb) } var a = { name: auth.name, password: auth.password } var req = makeReq('post', true, true) @@ -150,10 +159,11 @@ function login (auth, cb) { return cb(er, cr, data) this.name = auth.name cb(er, cr, data) - }) + }.bind(this)) } function changePass (auth, cb) { + assert(this instanceof CouchLogin) if (!auth.name || !auth.password) return cb(new Error('invalid auth')) var u = '/_users/org.couchdb.user:' + auth.name @@ -181,7 +191,6 @@ function changePass (auth, cb) { data.date = new Date().toISOString() this.put(u + '?rev=' + data._rev, data, function (er, res, data) { - console.error('back from changepass', er, data, this.name) if (er || res.statusCode >= 400) return cb(er, res, data) if (this.name && this.name !== auth.name) @@ -196,6 +205,7 @@ function changePass (auth, cb) { // // WATCH OUT! function deleteAccount (name, cb) { + assert(this instanceof CouchLogin) var u = '/_users/org.couchdb.user:' + name this.get(u, thenPut.bind(this)) @@ -216,9 +226,11 @@ function deleteAccount (name, cb) { function signup (auth, cb) { - if (this.token && this.token !== BASIC) + assert(this instanceof CouchLogin) + if (this.token && this.token !== BASIC) { + return this.logout(function (er, res, data) { - if (er || res.statusCode !== 200) { + if (er || res && res.statusCode !== 200) { return cb(er, res, data) } @@ -228,6 +240,7 @@ function signup (auth, cb) { this.signup(auth, cb) }.bind(this)) + } // make a new user record. var newSalt = crypto.randomBytes(30).toString('hex') @@ -262,6 +275,7 @@ function signup (auth, cb) { } function addToken (res) { + assert(this instanceof CouchLogin) // not doing the whole login session cookie thing. if (this.token === BASIC) return @@ -309,9 +323,11 @@ function addToken (res) { function logout (cb) { + assert(this instanceof CouchLogin) if (!this.token && this.tokenGet) { return this.tokenGet(function (er, tok) { - if (er || !tok) return cb() + if (er || !tok) + return cb(null, { statusCode: 200 }, {}) this.token = tok this.logout(cb) }.bind(this)) @@ -320,7 +336,7 @@ function logout (cb) { if (!valid(this.token)) { this.token = null if (this.tokenDel) this.tokenDel() - return process.nextTick(cb) + return process.nextTick(cb.bind(this, null, { statusCode: 200 }, {})) } var h = { cookie: 'AuthSession=' + this.token.AuthSession } @@ -333,7 +349,8 @@ function logout (cb) { } this.token = null - if (this.tokenDel) this.tokenDel() + if (this.tokenDel) + this.tokenDel() cb(er, res, data) }.bind(this)) } |