PHP 8.2.30
Preview: auth.js Size: 2.80 KB
//opt/alt/alt-nodejs19/root/lib/node_modules/npm/lib/utils/auth.js

const profile = require('npm-profile')
const log = require('../utils/log-shim')
const openUrlPrompt = require('../utils/open-url-prompt.js')
const read = require('../utils/read-user-info.js')
const otplease = require('../utils/otplease.js')

const adduser = async (npm, { creds, ...opts }) => {
  const authType = npm.config.get('auth-type')
  let res
  if (authType === 'web') {
    try {
      res = await profile.adduserWeb((url, emitter) => {
        openUrlPrompt(
          npm,
          url,
          'Create your account at',
          'Press ENTER to open in the browser...',
          emitter
        )
      }, opts)
    } catch (err) {
      if (err.code === 'ENYI') {
        log.verbose('web add user not supported, trying couch')
      } else {
        throw err
      }
    }
  }

  // auth type !== web or ENYI error w/ web adduser
  if (!res) {
    const username = await read.username('Username:', creds.username)
    const password = await read.password('Password:', creds.password)
    const email = await read.email('Email: (this IS public) ', creds.email)
    // npm registry quirk: If you "add" an existing user with their current
    // password, it's effectively a login, and if that account has otp you'll
    // be prompted for it.
    res = await otplease(npm, opts, (reqOpts) =>
      profile.adduserCouch(username, email, password, opts)
    )
  }

  // We don't know the username if it was a web login, all we can reliably log is scope and registry
  const message = `Logged in${opts.scope ? ` to scope ${opts.scope}` : ''} on ${opts.registry}.`

  log.info('adduser', message)

  return {
    message,
    newCreds: { token: res.token },
  }
}

const login = async (npm, { creds, ...opts }) => {
  const authType = npm.config.get('auth-type')
  let res
  if (authType === 'web') {
    try {
      res = await profile.loginWeb((url, emitter) => {
        openUrlPrompt(
          npm,
          url,
          'Login at',
          'Press ENTER to open in the browser...',
          emitter
        )
      }, opts)
    } catch (err) {
      if (err.code === 'ENYI') {
        log.verbose('web login not supported, trying couch')
      } else {
        throw err
      }
    }
  }

  // auth type !== web or ENYI error w/ web login
  if (!res) {
    const username = await read.username('Username:', creds.username)
    const password = await read.password('Password:', creds.password)
    res = await otplease(npm, opts, (reqOpts) =>
      profile.loginCouch(username, password, reqOpts)
    )
  }

  // We don't know the username if it was a web login, all we can reliably log is scope and registry
  const message = `Logged in${opts.scope ? ` to scope ${opts.scope}` : ''} on ${opts.registry}.`

  log.info('login', message)

  return {
    message,
    newCreds: { token: res.token },
  }
}

module.exports = {
  adduser,
  login,
}

Directory Contents

Dirs: 2 × Files: 34

Name Size Perms Modified Actions
- drwxr-xr-x 2024-03-03 22:54:11
Edit Download
config DIR
- drwxr-xr-x 2024-03-03 22:54:11
Edit Download
173 B lrw-r--r-- 2023-12-06 18:07:50
Edit Download
1.02 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
2.80 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
2.16 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
1.56 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
1.85 KB lrwxr-xr-x 2023-12-06 18:07:50
Edit Download
1.46 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
3.43 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
13.53 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
6.74 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
3.50 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
2.55 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
629 B lrw-r--r-- 2023-12-06 18:07:50
Edit Download
3.93 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
802 B lrw-r--r-- 2023-12-06 18:07:50
Edit Download
233 B lrw-r--r-- 2023-12-06 18:07:50
Edit Download
7.03 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
1.68 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
2.04 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
1.44 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
1.09 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
1.23 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
252 B lrw-r--r-- 2023-12-06 18:07:50
Edit Download
412 B lrw-r--r-- 2023-12-06 18:07:50
Edit Download
9.56 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
2.00 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
876 B lrw-r--r-- 2023-12-06 18:07:50
Edit Download
4.97 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
807 B lrw-r--r-- 2023-12-06 18:07:50
Edit Download
3.90 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
2.73 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
4.19 KB lrw-r--r-- 2023-12-06 18:07:50
Edit Download
1023 B lrw-r--r-- 2023-12-06 18:07:50
Edit Download
558 B lrw-r--r-- 2023-12-06 18:07:50
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).