我们如何验证 "npm login" 成功并且令牌尚未过期?

How do we verify "npm login" succeeded and the token has not expired?

我有一个脚本可以通过 greping 来验证用户是否已登录私有 npm 注册表(通过 "npm login"):

//registry-sub-url:_authToken=

...在:

~/.npmrc

但是,随着用户凭据过期,这种情况会随着时间的推移而崩溃 (由于标准密码过期规则)。

此外,我创建的帮助脚本无法区分 successful/failed npm login 调用,因为脚本总是以 0 状态退出。

问:(1)我们如何验证npm login成功了? (2) 如何识别npm token过期?

我正在发布我想出的解决方法,但我想要一个更好的解决方案。

我有 Jenkins 运行 这个 bash 脚本定期 test/verify 我的 npm login 针对私有注册表:

#/bin/bash

# Suppress commands (Jenkins turns this on)
set +x

# Suppress color codes from NPM output (for proper grepping)
export TERM=dumb

# Stop on any error
set -e

NPM_USERNAME=...
NPM_PASSWORD=...
NPM_URL=...
NPM_EMAIL=...
WORKSPACE=... (in my case, set by Jenkins)

echo "========"
echo "Looking for previous failed login (cached credentials)..."
echo ""
# NOTE: A previous failed login can result in an ".npmrc" containing
# a username/password in lieu of an auth token. We look for this and
# remove it (logout) if it exists so that the cached credentials are
# not applied when we run "expect" to login
# (which would see different prompts from cached credentials).

# Chop off "http:"/"https:" prefix from URL
NPM_REPO_PREFIX=`sed -e 's~https\{0,1\}:\(.*\)~~' <<< "$NPM_URL"`

# NOTE: piping to /dev/null so the password isn't printed
set +e
grep -F "${NPM_REPO_PREFIX}:_password=" ~/.npmrc > /dev/null
GREP_EXIT="$?"
set -e

if [[ "$GREP_EXIT" == "0" ]]; then
    echo "========"
    echo "Logging out of repo..."
    echo ""


    npm logout --registry "$NPM_URL"
fi

echo "========"
echo "Logging into repo..."
echo ""

(/usr/bin/expect <<EOF
set timeout 10
spawn npm login --verbose --registry "$NPM_URL"
match_max 100000

expect "Username"
send "$NPM_USERNAME\r"

expect "Password"
send "$NPM_PASSWORD\r"

expect "Email"
send "$NPM_EMAIL\r"

expect {
   timeout      exit 1
   expect eof
}

EOF
) | tee "$WORKSPACE/npm-login.out"

echo "========"
echo "Verifying output of login..."
echo ""
# NOTE: If the login fails, the npm command still exits with status "0",
# so we read the verbose output to see that the http server confirms
# successful with "http 201".

set +e
grep "npm http 201" "$WORKSPACE/npm-login.out"
GREP_EXIT="$?"
set -e

if [[ "$GREP_EXIT" != "0" ]]; then
  >&2 echo "========"
  >&2 echo "ERROR: Failed to login to repo [$NPM_REPO]"
  exit 1
else
  echo "========"
  echo "SUCCESS: Logged into [$NPM_REPO]"
fi

您可能会使用 npm whoami 命令。

$> npm whoami
${username}
$>  npm logout
 npm whoami
npm ERR! code ENEEDAUTH
npm ERR! need auth This command requires you to be logged in.
npm ERR! need auth You need to authorize this machine using `npm adduser`

npm ERR! A complete log of this run can be found in:
npm ERR!     /xxxxx/.npm/_logs/2019-02-06T10_21_10_780Z-debug.logged

在 GitHub 包的上下文中:如果您使用 npm login --registry=https://npm.pkg.github.com 登录,您可以使用 npm whoami --registry=https://npm.pkg.github.com(相同的注册表)来验证当前登录的用户。

$ npm login --registry=https://npm.pkg.github.com
Username: <your-user>
Password: <your-password-or-your-personal-access-token>
Email: (this IS public) <your@email.com>
Logged in as <your-user> on https://npm.pkg.github.com/.

$ npm whoami --registry=https://npm.pkg.github.com
<your-user>

如果我只 运行 npm whoami 我得到问题中提到的错误。

这个问题可能是因为npm whoami调用文件中有一个.npmrc文件。如果该文件中的令牌已损坏,则会出现此错误。您可以更正或删除文件中的令牌值并使用全局令牌值。