"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/npm/lib/adduser.js" (7 Feb 2017, 4079 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 log = require('npmlog')
    4 var npm = require('./npm.js')
    5 var read = require('read')
    6 var userValidate = require('npm-user-validate')
    7 var output = require('./utils/output')
    8 var usage = require('./utils/usage')
    9 var chain = require('slide').chain
   10 var crypto
   11 
   12 try {
   13   crypto = require('crypto')
   14 } catch (ex) {}
   15 
   16 adduser.usage = usage(
   17   'adduser',
   18   'npm adduser [--registry=url] [--scope=@orgname] [--always-auth]'
   19 )
   20 
   21 function adduser (args, cb) {
   22   if (!crypto) {
   23     return cb(new Error(
   24     'You must compile node with ssl support to use the adduser feature'
   25     ))
   26   }
   27 
   28   var creds = npm.config.getCredentialsByURI(npm.config.get('registry'))
   29   var c = {
   30     u: creds.username || '',
   31     p: creds.password || '',
   32     e: creds.email || ''
   33   }
   34   var u = {}
   35 
   36   log.disableProgress()
   37   chain([
   38     [readUsername, c, u],
   39     [readPassword, c, u],
   40     [readEmail, c, u],
   41     [save, c, u]
   42   ], cb)
   43 }
   44 
   45 function readUsername (c, u, cb) {
   46   var v = userValidate.username
   47   read({prompt: 'Username: ', default: c.u || ''}, function (er, un) {
   48     if (er) {
   49       return cb(er.message === 'cancelled' ? er.message : er)
   50     }
   51 
   52     // make sure it's valid.  we have to do this here, because
   53     // couchdb will only ever say "bad password" with a 401 when
   54     // you try to PUT a _users record that the validate_doc_update
   55     // rejects for *any* reason.
   56 
   57     if (!un) {
   58       return readUsername(c, u, cb)
   59     }
   60 
   61     var error = v(un)
   62     if (error) {
   63       log.warn(error.message)
   64       return readUsername(c, u, cb)
   65     }
   66 
   67     c.changed = c.u !== un
   68     u.u = un
   69     cb(er)
   70   })
   71 }
   72 
   73 function readPassword (c, u, cb) {
   74   var v = userValidate.pw
   75 
   76   var prompt
   77   if (c.p && !c.changed) {
   78     prompt = 'Password: (or leave unchanged) '
   79   } else {
   80     prompt = 'Password: '
   81   }
   82 
   83   read({prompt: prompt, silent: true}, function (er, pw) {
   84     if (er) {
   85       return cb(er.message === 'cancelled' ? er.message : er)
   86     }
   87 
   88     if (!c.changed && pw === '') {
   89       // when the username was not changed,
   90       // empty response means "use the old value"
   91       pw = c.p
   92     }
   93 
   94     if (!pw) {
   95       return readPassword(c, u, cb)
   96     }
   97 
   98     var error = v(pw)
   99     if (error) {
  100       log.warn(error.message)
  101       return readPassword(c, u, cb)
  102     }
  103 
  104     c.changed = c.changed || c.p !== pw
  105     u.p = pw
  106     cb(er)
  107   })
  108 }
  109 
  110 function readEmail (c, u, cb) {
  111   var v = userValidate.email
  112   var r = { prompt: 'Email: (this IS public) ', default: c.e || '' }
  113   read(r, function (er, em) {
  114     if (er) {
  115       return cb(er.message === 'cancelled' ? er.message : er)
  116     }
  117 
  118     if (!em) {
  119       return readEmail(c, u, cb)
  120     }
  121 
  122     var error = v(em)
  123     if (error) {
  124       log.warn(error.message)
  125       return readEmail(c, u, cb)
  126     }
  127 
  128     u.e = em
  129     cb(er)
  130   })
  131 }
  132 
  133 function save (c, u, cb) {
  134   // save existing configs, but yank off for this PUT
  135   var uri = npm.config.get('registry')
  136   var scope = npm.config.get('scope')
  137 
  138   // there may be a saved scope and no --registry (for login)
  139   if (scope) {
  140     if (scope.charAt(0) !== '@') scope = '@' + scope
  141 
  142     var scopedRegistry = npm.config.get(scope + ':registry')
  143     var cliRegistry = npm.config.get('registry', 'cli')
  144     if (scopedRegistry && !cliRegistry) uri = scopedRegistry
  145   }
  146 
  147   var params = {
  148     auth: {
  149       username: u.u,
  150       password: u.p,
  151       email: u.e
  152     }
  153   }
  154   npm.registry.adduser(uri, params, function (er, doc) {
  155     if (er) return cb(er)
  156 
  157     // don't want this polluting the configuration
  158     npm.config.del('_token', 'user')
  159 
  160     if (scope) npm.config.set(scope + ':registry', uri, 'user')
  161 
  162     if (doc && doc.token) {
  163       npm.config.setCredentialsByURI(uri, {
  164         token: doc.token
  165       })
  166     } else {
  167       npm.config.setCredentialsByURI(uri, {
  168         username: u.u,
  169         password: u.p,
  170         email: u.e,
  171         alwaysAuth: npm.config.get('always-auth')
  172       })
  173     }
  174 
  175     log.info('adduser', 'Authorized user %s', u.u)
  176     var scopeMessage = scope ? ' to scope ' + scope : ''
  177     output('Logged in as %s%s on %s.', u.u, scopeMessage, uri)
  178     npm.config.save('user', cb)
  179   })
  180 }