"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/npm-registry-client/lib/adduser.js" (8 Mar 2017, 3832 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 module.exports = adduser
    2 
    3 var url = require('url')
    4 var assert = require('assert')
    5 
    6 function adduser (uri, params, cb) {
    7   assert(typeof uri === 'string', 'must pass registry URI to adduser')
    8   assert(
    9     params && typeof params === 'object',
   10     'must pass params to adduser'
   11   )
   12   assert(typeof cb === 'function', 'must pass callback to adduser')
   13 
   14   assert(params.auth && typeof params.auth, 'must pass auth to adduser')
   15   var auth = params.auth
   16   assert(typeof auth.username === 'string', 'must include username in auth')
   17   assert(typeof auth.password === 'string', 'must include password in auth')
   18   assert(typeof auth.email === 'string', 'must include email in auth')
   19 
   20   // normalize registry URL
   21   if (uri.slice(-1) !== '/') uri += '/'
   22 
   23   var username = auth.username.trim()
   24   var password = auth.password.trim()
   25   var email = auth.email.trim()
   26 
   27   // validation
   28   if (!username) return cb(new Error('No username supplied.'))
   29   if (!password) return cb(new Error('No password supplied.'))
   30   if (!email) return cb(new Error('No email address supplied.'))
   31   if (!email.match(/^[^@]+@[^\.]+\.[^\.]+/)) {
   32     return cb(new Error('Please use a real email address.'))
   33   }
   34 
   35   var userobj = {
   36     _id: 'org.couchdb.user:' + username,
   37     name: username,
   38     password: password,
   39     email: email,
   40     type: 'user',
   41     roles: [],
   42     date: new Date().toISOString()
   43   }
   44 
   45   var token = this.config.couchToken
   46   if (this.couchLogin) this.couchLogin.token = null
   47 
   48   cb = done.call(this, token, cb)
   49 
   50   var logObj = Object.keys(userobj).map(function (k) {
   51     if (k === 'password') return [k, 'XXXXX']
   52     return [k, userobj[k]]
   53   }).reduce(function (s, kv) {
   54     s[kv[0]] = kv[1]
   55     return s
   56   }, {})
   57 
   58   this.log.verbose('adduser', 'before first PUT', logObj)
   59 
   60   var client = this
   61 
   62   uri = url.resolve(uri, '-/user/org.couchdb.user:' + encodeURIComponent(username))
   63   var options = {
   64     method: 'PUT',
   65     body: userobj,
   66     auth: auth
   67   }
   68   this.request(
   69     uri,
   70     options,
   71     function (error, data, json, response) {
   72       if (!error || !response || response.statusCode !== 409) {
   73         return cb(error, data, json, response)
   74       }
   75 
   76       client.log.verbose('adduser', 'update existing user')
   77       return client.request(
   78         uri + '?write=true',
   79         { auth: auth },
   80         function (er, data, json, response) {
   81           if (er || data.error) {
   82             return cb(er, data, json, response)
   83           }
   84           Object.keys(data).forEach(function (k) {
   85             if (!userobj[k] || k === 'roles') {
   86               userobj[k] = data[k]
   87             }
   88           })
   89           client.log.verbose('adduser', 'userobj', logObj)
   90           client.request(uri + '/-rev/' + userobj._rev, options, cb)
   91         }
   92       )
   93     }
   94   )
   95 
   96   function done (token, cb) {
   97     return function (error, data, json, response) {
   98       if (!error && (!response || response.statusCode === 201)) {
   99         return cb(error, data, json, response)
  100       }
  101 
  102       // there was some kind of error, reinstate previous auth/token/etc.
  103       if (client.couchLogin) {
  104         client.couchLogin.token = token
  105         if (client.couchLogin.tokenSet) {
  106           client.couchLogin.tokenSet(token)
  107         }
  108       }
  109 
  110       client.log.verbose('adduser', 'back', [error, data, json])
  111       if (!error) {
  112         error = new Error(
  113           (response && response.statusCode || '') + ' ' +
  114           'Could not create user\n' + JSON.stringify(data)
  115         )
  116       }
  117 
  118       if (response && (response.statusCode === 401 || response.statusCode === 403)) {
  119         client.log.warn('adduser', 'Incorrect username or password\n' +
  120                                    'You can reset your account by visiting:\n' +
  121                                    '\n' +
  122                                    '    https://npmjs.org/forgot\n')
  123       }
  124 
  125       return cb(error)
  126     }
  127   }
  128 }