Skip to content

Instantly share code, notes, and snippets.

@jonathanvanschenck
Created April 20, 2024 03:23
Show Gist options
  • Save jonathanvanschenck/9e41abe5fae6d7441dcb31c6bc8821c5 to your computer and use it in GitHub Desktop.
Save jonathanvanschenck/9e41abe5fae6d7441dcb31c6bc8821c5 to your computer and use it in GitHub Desktop.
Scriptably log into a private verdaccio npm registry
#!/bin/sh
usage() {
echo ""
echo "Log into a private verdaccio npm registry"
echo ""
echo " Usage: npm_login.sh [OPTIONS] [REQUIRED]"
echo ""
echo "REQUIRED"
echo " --username : Your username"
echo " --registry : The registry url, e.g. 'https://example.com' (notice no trailing space)"
echo ""
echo "OPTIONS"
echo " -h,--help : Display this menu"
echo " -v,--version : Display the version of this script"
echo " --password : Your password"
echo " --password-stdin : Pass in your password as stdin"
echo " --scope : The scope of your registry, e.g. '@myorg'"
}
VERSION="1.0.0"
_username=""
registry=""
password=""
password_stdin=""
scope=""
while [ "$#" -gt 0 ]; do
case "$1" in
--username)
shift
if [ -z "$1" ]; then
echo "Bad username" >&2
exit 1
fi
_username="$1"
;;
--registry)
shift
if [ -z "$1" ]; then
echo "Bad registry" >&2
exit 1
fi
registry="$1"
;;
--scope)
shift
if [ -z "$1" ]; then
echo "Bad scope" >&2
exit 1
fi
scope="$1"
;;
--password)
shift
if [ -z "$1" ]; then
echo "Bad password" >&2
exit 1
fi
password="$1"
;;
--password-stdin)
password_stdin="true"
;;
-v|--version)
echo "npm_login: v$VERSION"
exit 0
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unrecognized argument '$1'" >&2
exit 1
;;
esac
shift
done
if [ -z "$_username" ]; then
echo "Missing required parameter --username" >&2
exit 1
fi
if [ -z "$registry" ]; then
echo "Missing required parameter --registry" >&2
exit 1
fi
cleaned_registry="${registry%/}/"
if [ ! -z "$password_stdin" ]; then
password="$(cat -)"
elif [ -z "$password" ]; then
cleanup() {
stty echo
exit 1
}
trap cleanup INT TERM
printf "Password: "
stty -echo
read password
stty echo
printf "\n"
fi
if [ -z "$password" ]; then
echo "Missing password" >&2
exit 1
fi
# many thanks to: https://gist.github.com/jastisriradheshyam/296707910bbcf4eda96752b449fd040f
BODY=$(curl -s \
-X PUT \
-u $_username:$password \
-H "Content-Type:application/json" \
-d "{\"name\": \"$_username\", \"password\": \"$password\"}" \
"${cleaned_registry}-/user/org.couchdb.user:${_username}/-rev/undefined" 2>&1)
TOKEN=$(echo "$BODY" | grep -Po '(?<="token": ")[^"]*')
if [ -z "$TOKEN" ]; then
echo "Failed to login" >&2
echo "$BODY" >&2
exit 2
fi
domain="${cleaned_registry#http://}"
domain="${domain#https://}"
domain="${domain%/}"
npm config set "//$domain/:_authToken=$TOKEN"
if [ -z "$scope" ]; then
npm config set "registry=${cleaned_registry}"
echo "You are new authenticated as '$_username' on $cleaned_registry"
else
npm config set "${scope}:registry=${cleaned_registry}"
echo "You are new authenticated as '$_username' on $cleaned_registry under $scope"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment