xref: /third_party/node/deps/npm/lib/utils/get-identity.js
  • Home
  • History
  • Annotate Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
  • only in /third_party/node/deps/npm/lib/utils/
11cb0ef41Sopenharmony_ciconst npmFetch = require('npm-registry-fetch')
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_cimodule.exports = async (npm, opts) => {
41cb0ef41Sopenharmony_ci  const { registry } = opts
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci  // First, check if we have a user/pass-based auth
71cb0ef41Sopenharmony_ci  const creds = npm.config.getCredentialsByURI(registry)
81cb0ef41Sopenharmony_ci  if (creds.username) {
91cb0ef41Sopenharmony_ci    return creds.username
101cb0ef41Sopenharmony_ci  }
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci  // No username, but we have other credentials; fetch the username from registry
131cb0ef41Sopenharmony_ci  if (creds.token || creds.certfile && creds.keyfile) {
141cb0ef41Sopenharmony_ci    const registryData = await npmFetch.json('/-/whoami', { ...opts })
151cb0ef41Sopenharmony_ci    if (typeof registryData?.username === 'string') {
161cb0ef41Sopenharmony_ci      return registryData.username
171cb0ef41Sopenharmony_ci    }
181cb0ef41Sopenharmony_ci  }
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci  // At this point, even if they have a credentials object, it doesn't have a
211cb0ef41Sopenharmony_ci  // valid token.
221cb0ef41Sopenharmony_ci  throw Object.assign(
231cb0ef41Sopenharmony_ci    new Error('This command requires you to be logged in.'),
241cb0ef41Sopenharmony_ci    { code: 'ENEEDAUTH' }
251cb0ef41Sopenharmony_ci  )
261cb0ef41Sopenharmony_ci}
27

Indexes created Thu Nov 07 10:32:03 CST 2024