PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/deps/npm/node_modules/npm-registry-client/lib/adduser.js

https://github.com/dshaw/node
JavaScript | 137 lines | 111 code | 19 blank | 7 comment | 28 complexity | c5b8165247ff98fca791b2b571bc87bb MD5 | raw file
  1. module.exports = adduser
  2. var crypto = require('crypto')
  3. function sha (s) {
  4. return crypto.createHash("sha1").update(s).digest("hex")
  5. }
  6. function adduser (username, password, email, cb) {
  7. password = ("" + (password || "")).trim()
  8. if (!password) return cb(new Error("No password supplied."))
  9. email = ("" + (email || "")).trim()
  10. if (!email) return cb(new Error("No email address supplied."))
  11. if (!email.match(/^[^@]+@[^\.]+\.[^\.]+/)) {
  12. return cb(new Error("Please use a real email address."))
  13. }
  14. if (password.indexOf(":") !== -1) return cb(new Error(
  15. "Sorry, ':' chars are not allowed in passwords.\n"+
  16. "See <https://issues.apache.org/jira/browse/COUCHDB-969> for why."))
  17. var salt = crypto.randomBytes(30).toString('hex')
  18. , userobj =
  19. { name : username
  20. , salt : salt
  21. , password_sha : sha(password + salt)
  22. , email : email
  23. , _id : 'org.couchdb.user:'+username
  24. , type : "user"
  25. , roles : []
  26. , date: new Date().toISOString()
  27. }
  28. // pluck off any other username/password/token. it needs to be the
  29. // same as the user we're becoming now. replace them on error.
  30. var pre = { username: this.conf.get('username')
  31. , password: this.conf.get('_password')
  32. , auth: this.conf.get('_auth')
  33. , token: this.conf.get('_token') }
  34. this.conf.del('_token')
  35. this.conf.del('username')
  36. this.conf.del('_auth')
  37. this.conf.del('_password')
  38. if (this.couchLogin) {
  39. this.couchLogin.token = null
  40. }
  41. cb = done.call(this, cb, pre)
  42. var logObj = Object.keys(userobj).map(function (k) {
  43. if (k === 'salt' || k === 'password_sha') return [k, 'XXXXX']
  44. return [k, userobj[k]]
  45. }).reduce(function (s, kv) {
  46. s[kv[0]] = kv[1]
  47. return s
  48. }, {})
  49. this.log.verbose("adduser", "before first PUT", logObj)
  50. this.request('PUT'
  51. , '/-/user/org.couchdb.user:'+encodeURIComponent(username)
  52. , userobj
  53. , function (error, data, json, response) {
  54. // if it worked, then we just created a new user, and all is well.
  55. // but if we're updating a current record, then it'll 409 first
  56. if (error && !this.conf.get('_auth')) {
  57. // must be trying to re-auth on a new machine.
  58. // use this info as auth
  59. var b = new Buffer(username + ":" + password)
  60. this.conf.set('_auth', b.toString("base64"))
  61. this.conf.set('username', username)
  62. this.conf.set('_password', password)
  63. }
  64. if (!error || !response || response.statusCode !== 409) {
  65. return cb(error, data, json, response)
  66. }
  67. this.log.verbose("adduser", "update existing user")
  68. return this.request('GET'
  69. , '/-/user/org.couchdb.user:'+encodeURIComponent(username)
  70. , function (er, data, json, response) {
  71. if (er || data.error) {
  72. return cb(er, data, json, response)
  73. }
  74. Object.keys(data).forEach(function (k) {
  75. if (!userobj[k]) {
  76. userobj[k] = data[k]
  77. }
  78. })
  79. this.log.verbose("adduser", "userobj", logObj)
  80. this.request('PUT'
  81. , '/-/user/org.couchdb.user:'+encodeURIComponent(username)
  82. + "/-rev/" + userobj._rev
  83. , userobj
  84. , cb )
  85. }.bind(this))
  86. }.bind(this))
  87. }
  88. function done (cb, pre) {
  89. return function (error, data, json, response) {
  90. if (!error && (!response || response.statusCode === 201)) {
  91. return cb(error, data, json, response)
  92. }
  93. // there was some kind of error, re-instate previous auth/token/etc.
  94. this.conf.set('_token', pre.token)
  95. if (this.couchLogin) {
  96. this.couchLogin.token = pre.token
  97. if (this.couchLogin.tokenSet) {
  98. this.couchLogin.tokenSet(pre.token)
  99. }
  100. }
  101. this.conf.set('username', pre.username)
  102. this.conf.set('_password', pre.password)
  103. this.conf.set('_auth', pre.auth)
  104. this.log.verbose("adduser", "back", [error, data, json])
  105. if (!error) {
  106. error = new Error( (response && response.statusCode || "") + " "+
  107. "Could not create user\n"+JSON.stringify(data))
  108. }
  109. if (response
  110. && (response.statusCode === 401 || response.statusCode === 403)) {
  111. this.log.warn("adduser", "Incorrect username or password\n"
  112. +"You can reset your account by visiting:\n"
  113. +"\n"
  114. +" http://admin.npmjs.org/reset\n")
  115. }
  116. return cb(error)
  117. }.bind(this)
  118. }