Skip to content

Instantly share code, notes, and snippets.

@jastisriradheshyam
Last active August 23, 2021 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jastisriradheshyam/296707910bbcf4eda96752b449fd040f to your computer and use it in GitHub Desktop.
Save jastisriradheshyam/296707910bbcf4eda96752b449fd040f to your computer and use it in GitHub Desktop.
NPM Login with Verdaccio having "max_users: -1" and solves error "user registration disabled"
#!/bin/bash
## Author : Jasti Sri Radhe Shyam (https://jastisriradheshyam.github.io)
function printHelp() {
cat <<EOL
Usage:
npm_login.sh [Flags]
Flags:
-h print help
--domain set the domain, (mandatory); usage: --domain DOMAIN_NAME
--user set the username (mandatory); usage: --username REPOSITORY_USERNAME
--pass set the password (mandatory if not used --askpass); usage: --password REPOSITORY_PASSWORD
will overwritten if --askpass is used
--askpass asks the password from STDIN (recommended) (mandatory if not used --pass)
--ssl_enabled (default : true), (optional) possibile values : true, false
--set_registry_default will set the registry (domain) as default registry
EOL
}
ssl_enabled=true
http_protocol=https
set_registry_default=false
# parse flags
while [[ $# -ge 1 ]]; do
key="$1"
case $key in
-h)
printHelp
exit 0
;;
--domain)
domain="$2"
shift
;;
--user)
user="$2"
shift
;;
--pass)
pass="$2"
shift
;;
--askpass)
askpass="true"
;;
--ssl_enabled)
ssl_enabled="$2"
shift
;;
--set_registry_default)
set_registry_default="true"
;;
*)
echo
echo "Unknown flag: $key"
echo
printHelp
exit 1
;;
esac
shift
done
if [ "${ssl_enabled}" != "true" -a "${ssl_enabled}" != "false" ]; then
echo "ssl_enabled can only have values \"true\" or \"false\""
exit 1
elif [ "${ssl_enabled}" == "false" ]; then
http_protocol=http
fi
if [ -z "${user}" ]; then
echo "Username not provided"
exit 1
fi
if [ -z "${domain}" ]; then
echo "Domain not provided"
exit 1
fi
if [ -n "${askpass}" ]; then
read -sp "Enter password: " pass
echo -e ""
fi
if [ -z "${pass}" ]; then
echo "Password not provided"
exit 1
fi
request_time=$(date -u +"%Y-%m-%dT%H:%M:%S.0Z");
echo //$domain/:_authToken=$( curl --silent $http_protocol://$domain/-/user/org.couchdb.user:$user/-rev/undefined -u $user:$pass -X PUT \
-d '{"_id":"org.couchdb.user:'$user'","name":"'$user'","password":"'$pass'","type":"user","roles":[],"date":"'$request_time'","ok":"you are authenticated as 'undefined'"}' \
-H content-type:application/json |jq --raw-output .token ) > ~/.npmrc
# Useful when used with yarn
# Set in ~/.npmrc
# always-auth = true
npm config set always-auth true
# Add default registry
if [ "${set_registry_default}" == "true" ]; then
npm config set registry ${http_protocol}://${domain}
fi
echo -e "\nDone with Authentication"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment