2012-06-12 00:30:44 +08:00
|
|
|
module.exports = adduser
|
|
|
|
|
2014-08-01 00:05:30 +08:00
|
|
|
var url = require("url")
|
2015-01-09 06:37:26 +08:00
|
|
|
var assert = require("assert")
|
|
|
|
|
|
|
|
function adduser (uri, params, cb) {
|
|
|
|
assert(typeof uri === "string", "must pass registry URI to adduser")
|
|
|
|
assert(
|
|
|
|
params && typeof params === "object",
|
|
|
|
"must pass params to adduser"
|
|
|
|
)
|
|
|
|
assert(typeof cb === "function", "must pass callback to adduser")
|
|
|
|
|
|
|
|
assert(params.auth && typeof params.auth, "must pass auth to adduser")
|
|
|
|
var auth = params.auth
|
|
|
|
assert(typeof auth.username === "string", "must include username in auth")
|
|
|
|
assert(typeof auth.password === "string", "must include password in auth")
|
|
|
|
assert(typeof auth.email === "string", "must include email in auth")
|
|
|
|
|
|
|
|
// normalize registry URL
|
|
|
|
if (uri.slice(-1) !== "/") uri += "/"
|
|
|
|
|
|
|
|
var username = auth.username.trim()
|
|
|
|
var password = auth.password.trim()
|
|
|
|
var email = auth.email.trim()
|
|
|
|
|
|
|
|
// validation
|
2014-08-01 00:05:30 +08:00
|
|
|
if (!username) return cb(new Error("No username supplied."))
|
2012-06-12 00:30:44 +08:00
|
|
|
if (!password) return cb(new Error("No password supplied."))
|
|
|
|
if (!email) return cb(new Error("No email address supplied."))
|
|
|
|
if (!email.match(/^[^@]+@[^\.]+\.[^\.]+/)) {
|
|
|
|
return cb(new Error("Please use a real email address."))
|
|
|
|
}
|
|
|
|
|
2015-01-09 06:37:26 +08:00
|
|
|
var userobj = {
|
|
|
|
_id : "org.couchdb.user:"+username,
|
|
|
|
name : username,
|
|
|
|
password : password,
|
|
|
|
email : email,
|
|
|
|
type : "user",
|
|
|
|
roles : [],
|
|
|
|
date : new Date().toISOString()
|
2012-07-25 03:34:13 +08:00
|
|
|
}
|
|
|
|
|
2015-01-09 06:37:26 +08:00
|
|
|
var token = this.config.couchToken
|
|
|
|
if (this.couchLogin) this.couchLogin.token = null
|
|
|
|
|
|
|
|
cb = done.call(this, token, cb)
|
2012-07-25 03:34:13 +08:00
|
|
|
|
|
|
|
var logObj = Object.keys(userobj).map(function (k) {
|
2015-01-09 06:37:26 +08:00
|
|
|
if (k === "password") return [k, "XXXXX"]
|
2012-07-25 03:34:13 +08:00
|
|
|
return [k, userobj[k]]
|
|
|
|
}).reduce(function (s, kv) {
|
|
|
|
s[kv[0]] = kv[1]
|
|
|
|
return s
|
|
|
|
}, {})
|
|
|
|
|
|
|
|
this.log.verbose("adduser", "before first PUT", logObj)
|
2012-06-12 00:30:44 +08:00
|
|
|
|
2015-01-09 06:37:26 +08:00
|
|
|
var client = this
|
2012-06-12 00:30:44 +08:00
|
|
|
|
2015-01-09 06:37:26 +08:00
|
|
|
uri = url.resolve(uri, "-/user/org.couchdb.user:" + encodeURIComponent(username))
|
|
|
|
var options = {
|
|
|
|
method : "PUT",
|
|
|
|
body : userobj,
|
|
|
|
auth : auth
|
|
|
|
}
|
|
|
|
this.request(
|
|
|
|
uri,
|
|
|
|
options,
|
|
|
|
function (error, data, json, response) {
|
|
|
|
if (!error || !response || response.statusCode !== 409) {
|
|
|
|
return cb(error, data, json, response)
|
|
|
|
}
|
|
|
|
|
|
|
|
client.log.verbose("adduser", "update existing user")
|
|
|
|
return client.request(
|
|
|
|
uri+"?write=true",
|
|
|
|
{ body : userobj, auth : auth },
|
|
|
|
function (er, data, json, response) {
|
|
|
|
if (er || data.error) {
|
|
|
|
return cb(er, data, json, response)
|
|
|
|
}
|
|
|
|
Object.keys(data).forEach(function (k) {
|
|
|
|
if (!userobj[k] || k === "roles") {
|
|
|
|
userobj[k] = data[k]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
client.log.verbose("adduser", "userobj", logObj)
|
|
|
|
client.request(uri+"/-rev/"+userobj._rev, options, cb)
|
2012-06-12 00:30:44 +08:00
|
|
|
}
|
2015-01-09 06:37:26 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2012-06-12 00:30:44 +08:00
|
|
|
|
2015-01-09 06:37:26 +08:00
|
|
|
function done (token, cb) {
|
2014-09-25 05:41:07 +08:00
|
|
|
return function (error, data, json, response) {
|
|
|
|
if (!error && (!response || response.statusCode === 201)) {
|
|
|
|
return cb(error, data, json, response)
|
|
|
|
}
|
|
|
|
|
2015-01-09 06:37:26 +08:00
|
|
|
// there was some kind of error, reinstate previous auth/token/etc.
|
|
|
|
if (client.couchLogin) {
|
|
|
|
client.couchLogin.token = token
|
|
|
|
if (client.couchLogin.tokenSet) {
|
|
|
|
client.couchLogin.tokenSet(token)
|
2014-09-25 05:41:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-09 06:37:26 +08:00
|
|
|
client.log.verbose("adduser", "back", [error, data, json])
|
2014-09-25 05:41:07 +08:00
|
|
|
if (!error) {
|
|
|
|
error = new Error(
|
|
|
|
(response && response.statusCode || "") + " " +
|
|
|
|
"Could not create user\n" + JSON.stringify(data)
|
|
|
|
)
|
2012-07-25 03:34:13 +08:00
|
|
|
}
|
2014-09-25 05:41:07 +08:00
|
|
|
|
|
|
|
if (response && (response.statusCode === 401 || response.statusCode === 403)) {
|
2015-01-09 06:37:26 +08:00
|
|
|
client.log.warn("adduser", "Incorrect username or password\n" +
|
|
|
|
"You can reset your account by visiting:\n" +
|
|
|
|
"\n" +
|
|
|
|
" https://npmjs.org/forgot\n")
|
2014-09-25 05:41:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return cb(error)
|
2015-01-09 06:37:26 +08:00
|
|
|
}
|
2014-09-25 05:41:07 +08:00
|
|
|
}
|
2012-06-12 00:30:44 +08:00
|
|
|
}
|