npm: Upgrade to 1.3.19
[platform/upstream/nodejs.git] / deps / npm / node_modules / npm-registry-client / lib / adduser.js
1 module.exports = adduser
2
3 var crypto = require('crypto')
4
5 function sha (s) {
6   return crypto.createHash("sha1").update(s).digest("hex")
7 }
8
9 function adduser (username, password, email, cb) {
10
11   password = ("" + (password || "")).trim()
12   if (!password) return cb(new Error("No password supplied."))
13
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."))
18   }
19
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."))
23
24   var salt = crypto.randomBytes(30).toString('hex')
25     , userobj =
26       { name : username
27       , salt : salt
28       , password_sha : sha(password + salt)
29       , email : email
30       , _id : 'org.couchdb.user:'+username
31       , type : "user"
32       , roles : []
33       , date: new Date().toISOString()
34       }
35
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') }
42
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
49   }
50
51   cb = done.call(this, cb, pre)
52
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) {
57     s[kv[0]] = kv[1]
58     return s
59   }, {})
60
61   this.log.verbose("adduser", "before first PUT", logObj)
62
63   this.request('PUT'
64     , '/-/user/org.couchdb.user:'+encodeURIComponent(username)
65     , userobj
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)
76         }
77
78         if (!error || !response || response.statusCode !== 409) {
79           return cb(error, data, json, response)
80         }
81
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)
88               }
89               Object.keys(data).forEach(function (k) {
90                 if (!userobj[k] || k === 'roles') {
91                   userobj[k] = data[k]
92                 }
93               })
94               this.log.verbose("adduser", "userobj", logObj)
95               this.request('PUT'
96                 , '/-/user/org.couchdb.user:'+encodeURIComponent(username)
97                   + "/-rev/" + userobj._rev
98                 , userobj
99                 , cb )
100             }.bind(this))
101       }.bind(this))
102 }
103
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)
108     }
109
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)
116       }
117     }
118     this.conf.set('username', pre.username)
119     this.conf.set('_password', pre.password)
120     this.conf.set('_auth', pre.auth)
121
122     this.log.verbose("adduser", "back", [error, data, json])
123     if (!error) {
124       error = new Error( (response && response.statusCode || "") + " "+
125       "Could not create user\n"+JSON.stringify(data))
126     }
127     if (response
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"
131               +"\n"
132               +"    http://admin.npmjs.org/reset\n")
133     }
134
135     return cb(error)
136   }.bind(this)
137 }