summaryrefslogtreecommitdiff
path: root/deps/npm/lib/whoami.js
blob: 5145b447de4c6b107bdf83078bff620685b20ad2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
'use strict'

const BB = require('bluebird')

const npmConfig = require('./config/figgy-config.js')
const fetch = require('libnpm/fetch')
const figgyPudding = require('figgy-pudding')
const npm = require('./npm.js')
const output = require('./utils/output.js')

const WhoamiConfig = figgyPudding({
  json: {},
  registry: {}
})

module.exports = whoami

whoami.usage = 'npm whoami [--registry <registry>]\n(just prints username according to given registry)'

function whoami ([spec], silent, cb) {
  // FIXME: need tighter checking on this, but is a breaking change
  if (typeof cb !== 'function') {
    cb = silent
    silent = false
  }
  const opts = WhoamiConfig(npmConfig())
  return BB.try(() => {
    // First, check if we have a user/pass-based auth
    const registry = opts.registry
    if (!registry) throw new Error('no default registry set')
    return npm.config.getCredentialsByURI(registry)
  }).then(({username, token}) => {
    if (username) {
      return username
    } else if (token) {
      return fetch.json('/-/whoami', opts.concat({
        spec
      })).then(({username}) => {
        if (username) {
          return username
        } else {
          throw Object.assign(new Error(
            'Your auth token is no longer valid. Please log in again.'
          ), {code: 'ENEEDAUTH'})
        }
      })
    } else {
      // At this point, if they have a credentials object, it doesn't have a
      // token or auth in it.  Probably just the default registry.
      throw Object.assign(new Error(
        'This command requires you to be logged in.'
      ), {code: 'ENEEDAUTH'})
    }
  }).then(username => {
    if (silent) {
    } else if (opts.json) {
      output(JSON.stringify(username))
    } else {
      output(username)
    }
    return username
  }).nodeify(cb)
}