1 module.exports = adduser
3 var crypto = require('crypto')
6 return crypto.createHash("sha1").update(s).digest("hex")
9 function adduser (username, password, email, cb) {
11 password = ("" + (password || "")).trim()
12 if (!password) return cb(new Error("No password supplied."))
14 email = ("" + (email || "")).trim()
15 if (!email) return cb(new Error("No email address supplied."))
16 if (!email.match(/^[^@]+@[^\.]+\.[^\.]+/)) {
17 return cb(new Error("Please use a real email address."))
20 if (password.indexOf(":") !== -1) return cb(new Error(
21 "Sorry, ':' chars are not allowed in passwords.\n"+
22 "See <https://issues.apache.org/jira/browse/COUCHDB-969> for why."))
24 var salt = crypto.randomBytes(30).toString('hex')
28 , password_sha : sha(password + salt)
30 , _id : 'org.couchdb.user:'+username
33 , date: new Date().toISOString()
36 // pluck off any other username/password/token. it needs to be the
37 // same as the user we're becoming now. replace them on error.
38 var pre = { username: this.conf.get('username')
39 , password: this.conf.get('_password')
40 , auth: this.conf.get('_auth')
41 , token: this.conf.get('_token') }
43 this.conf.del('_token')
44 this.conf.del('username')
45 this.conf.del('_auth')
46 this.conf.del('_password')
47 if (this.couchLogin) {
48 this.couchLogin.token = null
51 cb = done.call(this, cb, pre)
53 var logObj = Object.keys(userobj).map(function (k) {
54 if (k === 'salt' || k === 'password_sha') return [k, 'XXXXX']
55 return [k, userobj[k]]
56 }).reduce(function (s, kv) {
61 this.log.verbose("adduser", "before first PUT", logObj)
64 , '/-/user/org.couchdb.user:'+encodeURIComponent(username)
66 , function (error, data, json, response) {
67 // if it worked, then we just created a new user, and all is well.
68 // but if we're updating a current record, then it'll 409 first
69 if (error && !this.conf.get('_auth')) {
70 // must be trying to re-auth on a new machine.
71 // use this info as auth
72 var b = new Buffer(username + ":" + password)
73 this.conf.set('_auth', b.toString("base64"))
74 this.conf.set('username', username)
75 this.conf.set('_password', password)
78 if (!error || !response || response.statusCode !== 409) {
79 return cb(error, data, json, response)
82 this.log.verbose("adduser", "update existing user")
83 return this.request('GET'
84 , '/-/user/org.couchdb.user:'+encodeURIComponent(username)
85 , function (er, data, json, response) {
86 if (er || data.error) {
87 return cb(er, data, json, response)
89 Object.keys(data).forEach(function (k) {
94 this.log.verbose("adduser", "userobj", logObj)
96 , '/-/user/org.couchdb.user:'+encodeURIComponent(username)
97 + "/-rev/" + userobj._rev
104 function done (cb, pre) {
105 return function (error, data, json, response) {
106 if (!error && (!response || response.statusCode === 201)) {
107 return cb(error, data, json, response)
110 // there was some kind of error, re-instate previous auth/token/etc.
111 this.conf.set('_token', pre.token)
112 if (this.couchLogin) {
113 this.couchLogin.token = pre.token
114 if (this.couchLogin.tokenSet) {
115 this.couchLogin.tokenSet(pre.token)
118 this.conf.set('username', pre.username)
119 this.conf.set('_password', pre.password)
120 this.conf.set('_auth', pre.auth)
122 this.log.verbose("adduser", "back", [error, data, json])
124 error = new Error( (response && response.statusCode || "") + " "+
125 "Could not create user\n"+JSON.stringify(data))
128 && (response.statusCode === 401 || response.statusCode === 403)) {
129 this.log.warn("adduser", "Incorrect username or password\n"
130 +"You can reset your account by visiting:\n"
132 +" http://admin.npmjs.org/reset\n")