2020-10-02 17:52:19 -04:00
|
|
|
const npmFetch = require('npm-registry-fetch')
|
|
|
|
|
2022-05-25 21:26:36 +00:00
|
|
|
module.exports = async (npm, opts) => {
|
2020-10-02 17:52:19 -04:00
|
|
|
const { registry } = opts
|
|
|
|
|
|
|
|
// First, check if we have a user/pass-based auth
|
|
|
|
const creds = npm.config.getCredentialsByURI(registry)
|
2022-05-25 21:26:36 +00:00
|
|
|
if (creds.username) {
|
|
|
|
return creds.username
|
|
|
|
}
|
2020-10-02 17:52:19 -04:00
|
|
|
|
2022-07-21 04:24:12 -07:00
|
|
|
// No username, but we have other credentials; fetch the username from registry
|
|
|
|
if (creds.token || creds.certfile && creds.keyfile) {
|
2022-05-25 21:26:36 +00:00
|
|
|
const registryData = await npmFetch.json('/-/whoami', { ...opts })
|
2022-12-06 22:18:33 -05:00
|
|
|
if (typeof registryData?.username === 'string') {
|
|
|
|
return registryData.username
|
|
|
|
}
|
2020-10-02 17:52:19 -04:00
|
|
|
}
|
2022-05-25 21:26:36 +00:00
|
|
|
|
|
|
|
// At this point, even if they have a credentials object, it doesn't have a
|
|
|
|
// valid token.
|
|
|
|
throw Object.assign(
|
|
|
|
new Error('This command requires you to be logged in.'),
|
|
|
|
{ code: 'ENEEDAUTH' }
|
|
|
|
)
|
2020-10-02 17:52:19 -04:00
|
|
|
}
|