|
1 | | -var npm = require('./npm.js') |
2 | | -var output = require('./utils/output.js') |
| 1 | +'use strict' |
| 2 | + |
| 3 | +const BB = require('bluebird') |
| 4 | + |
| 5 | +const npmConfig = require('./config/figgy-config.js') |
| 6 | +const fetch = require('libnpm/fetch') |
| 7 | +const figgyPudding = require('figgy-pudding') |
| 8 | +const npm = require('./npm.js') |
| 9 | +const output = require('./utils/output.js') |
| 10 | + |
| 11 | +const WhoamiConfig = figgyPudding({ |
| 12 | + json: {}, |
| 13 | + registry: {} |
| 14 | +}) |
3 | 15 |
|
4 | 16 | module.exports = whoami |
5 | 17 |
|
6 | 18 | whoami.usage = 'npm whoami [--registry <registry>]\n(just prints username according to given registry)' |
7 | 19 |
|
8 | | -function whoami (args, silent, cb) { |
| 20 | +function whoami ([spec], silent, cb) { |
9 | 21 | // FIXME: need tighter checking on this, but is a breaking change |
10 | 22 | if (typeof cb !== 'function') { |
11 | 23 | cb = silent |
12 | 24 | silent = false |
13 | 25 | } |
14 | | - |
15 | | - var registry = npm.config.get('registry') |
16 | | - if (!registry) return cb(new Error('no default registry set')) |
17 | | - |
18 | | - var auth = npm.config.getCredentialsByURI(registry) |
19 | | - if (auth) { |
20 | | - if (auth.username) { |
21 | | - if (!silent) output(auth.username) |
22 | | - return process.nextTick(cb.bind(this, null, auth.username)) |
23 | | - } else if (auth.token) { |
24 | | - return npm.registry.whoami(registry, { auth: auth }, function (er, username) { |
25 | | - if (er) return cb(er) |
26 | | - if (!username) { |
27 | | - var needNewSession = new Error( |
| 26 | + const opts = WhoamiConfig(npmConfig()) |
| 27 | + return BB.try(() => { |
| 28 | + // First, check if we have a user/pass-based auth |
| 29 | + const registry = opts.registry |
| 30 | + if (!registry) throw new Error('no default registry set') |
| 31 | + return npm.config.getCredentialsByURI(registry) |
| 32 | + }).then(({username, token}) => { |
| 33 | + if (username) { |
| 34 | + return username |
| 35 | + } else if (token) { |
| 36 | + return fetch.json('/-/whoami', opts.concat({ |
| 37 | + spec |
| 38 | + })).then(({username}) => { |
| 39 | + if (username) { |
| 40 | + return username |
| 41 | + } else { |
| 42 | + throw Object.assign(new Error( |
28 | 43 | 'Your auth token is no longer valid. Please log in again.' |
29 | | - ) |
30 | | - needNewSession.code = 'ENEEDAUTH' |
31 | | - return cb(needNewSession) |
| 44 | + ), {code: 'ENEEDAUTH'}) |
32 | 45 | } |
33 | | - |
34 | | - if (!silent) output(username) |
35 | | - cb(null, username) |
36 | 46 | }) |
| 47 | + } else { |
| 48 | + // At this point, if they have a credentials object, it doesn't have a |
| 49 | + // token or auth in it. Probably just the default registry. |
| 50 | + throw Object.assign(new Error( |
| 51 | + 'This command requires you to be logged in.' |
| 52 | + ), {code: 'ENEEDAUTH'}) |
37 | 53 | } |
38 | | - } |
39 | | - |
40 | | - // At this point, if they have a credentials object, it doesn't have a token |
41 | | - // or auth in it. Probably just the default registry. |
42 | | - var needAuth = new Error( |
43 | | - 'this command requires you to be logged in.' |
44 | | - ) |
45 | | - needAuth.code = 'ENEEDAUTH' |
46 | | - process.nextTick(cb.bind(this, needAuth)) |
| 54 | + }).then(username => { |
| 55 | + if (silent) { |
| 56 | + } else if (opts.json) { |
| 57 | + output(JSON.stringify(username)) |
| 58 | + } else { |
| 59 | + output(username) |
| 60 | + } |
| 61 | + return username |
| 62 | + }).nodeify(cb) |
47 | 63 | } |
0 commit comments