[New] Add support for `NVM_AUTH_HEADER` env var
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Closes #3366
authored by David Welch · committed by Jordan Harband · parent 4c7d899 · Browse files · 3 files·+38 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: David Welch <david@davidwelch.co>
- Co-authored-by: Andre Kradolfer <narfdre@gmail.com>
Diff stats
3 files+38 added−4 removed
- tests:+11 added−1 removed
- other:+27 added−3 removed
Provenance
- Commit
bd090ef- Tree
edc9f01- Author
David Welch <david@davidwelch.co>- Committer
Jordan Harband <ljharb@gmail.com>- Parent
4c7d899
3 files changed+38 added−4 removed
modified:test/fast/Unit tests/nvm_download+11 added-1 removed
1 #!/bin/sh2 3 cleanup () {4- unset -f die cleanup4+ unset -f die cleanup NVM_AUTH_HEADER5+ docker stop httpbin && docker rm httpbin6 }7 die () { echo "$@" ; cleanup ; exit 1; }8 9 \. ../../../nvm.sh5 unmodified lines15 16 # nvm_download should fail to download wrong_install.sh17 ! nvm_download "https://raw.githubusercontent.com/nvm-sh/nvm/HEAD/wrong_install.sh" >/dev/null || die "nvm_download should fail to download no existing file"18 19+# nvm_download should pass when calling with auth header20+docker pull kennethreitz/httpbin && docker run --shell=bash -d --name httpbin -p 80:80 kennethreitz/httpbin21+sleep 1 # wait for httpbin to start22+NVM_AUTH_HEADER="Bearer test-token" nvm_download "http://127.0.0.1/bearer" > /dev/null || die 'nvm_download with auth header should send correctly'23+24+# nvm_download should fail when calling without auth header25+nvm_download "http://127.0.0.1/bearer" > /dev/null && die 'nvm_download with no auth header should not send the header and should fail'26+docker stop httpbin && docker rm httpbin27+28 cleanupmodified:nvm.sh+19 added-3 removed
114 unmodified lines115 nvm_echo "${NVM_LATEST_URL##*/}"116 }117 118 nvm_download() {119- local CURL_COMPRESSED_FLAG119 if nvm_has "curl"; then120+ local CURL_COMPRESSED_FLAG=""121+ local CURL_HEADER_FLAG=""122+123+ if [ -n "${NVM_AUTH_HEADER:-}" ]; then124+ sanitized_header=$(nvm_sanitize_auth_header "${NVM_AUTH_HEADER}")125+ CURL_HEADER_FLAG="--header \"Authorization: ${sanitized_header}\""126+ fi127+128 if nvm_curl_use_compression; then129 CURL_COMPRESSED_FLAG="--compressed"130 fi124- curl --fail ${CURL_COMPRESSED_FLAG:-} -q "$@"131+ eval "curl -q --fail ${CURL_COMPRESSED_FLAG:-} ${CURL_HEADER_FLAG:-} $*"132 elif nvm_has "wget"; then133 # Emulate curl with wget134 ARGS=$(nvm_echo "$@" | command sed -e 's/--progress-bar /--progress=bar /' \135 -e 's/--compressed //' \3 unmodified lines139 -e 's/-s /-q /' \140 -e 's/-sS /-nv /' \141 -e 's/-o /-O /' \142 -e 's/-C - /-c /')143+144+ if [ -n "${NVM_AUTH_HEADER:-}" ]; then145+ ARGS="${ARGS} --header \"${NVM_AUTH_HEADER}\""146+ fi147 # shellcheck disable=SC2086148 eval wget $ARGS149 fi150 }151 152+nvm_sanitize_auth_header() {153+ # Remove potentially dangerous characters154+ nvm_echo "$1" | command sed 's/[^a-zA-Z0-9:;_. -]//g'155+}156+157 nvm_has_system_node() {158 [ "$(nvm deactivate >/dev/null 2>&1 && command -v node)" != '' ]159 }160 4212 unmodified lines4373 nvm_print_default_alias nvm_print_formatted_alias nvm_resolve_local_alias \4374 nvm_sanitize_path nvm_has_colors nvm_process_parameters \4375 nvm_node_version_has_solaris_binary nvm_iojs_version_has_solaris_binary \4376 nvm_curl_libz_support nvm_command_info nvm_is_zsh nvm_stdout_is_terminal \4361- nvm_npmrc_bad_news_bears \4377+ nvm_npmrc_bad_news_bears nvm_sanitize_auth_header \4378 nvm_get_colors nvm_set_colors nvm_print_color_code nvm_wrap_with_color_code nvm_format_help_message_colors \4379 nvm_echo_with_colors nvm_err_with_colors \4380 nvm_get_artifact_compression nvm_install_binary_extract nvm_extract_tarball \4381 nvm_process_nvmrc nvm_nvmrc_invalid_msg \175 unmodified linesmodified:README.md+8 added
37 unmodified lines38 - [Suppressing colorized output](#suppressing-colorized-output)39 - [Restoring PATH](#restoring-path)40 - [Set default node version](#set-default-node-version)41 - [Use a mirror of node binaries](#use-a-mirror-of-node-binaries)42+ - [Pass Authorization header to mirror](#pass-authorization-header-to-mirror)43 - [.nvmrc](#nvmrc)44 - [Deeper Shell Integration](#deeper-shell-integration)45 - [Calling `nvm use` automatically in a directory with a `.nvmrc` file](#calling-nvm-use-automatically-in-a-directory-with-a-nvmrc-file)46 - [bash](#bash)483 unmodified lines530 ```531 532 `nvm use` will not, by default, create a "current" symlink. Set `$NVM_SYMLINK_CURRENT` to "true" to enable this behavior, which is sometimes useful for IDEs. Note that using `nvm` in multiple shell tabs with this environment variable enabled can cause race conditions.533 534+#### Pass Authorization header to mirror535+To pass an Authorization header through to the mirror url, set `$NVM_AUTH_HEADER`536+537+```sh538+NVM_AUTH_HEADER="Bearer secret-token" nvm install node539+```540+541 ### .nvmrc542 543 You can create a `.nvmrc` file containing a node version number (or any other string that `nvm` understands; see `nvm --help` for details) in the project root directory (or any parent directory).544 Afterwards, `nvm use`, `nvm install`, `nvm exec`, `nvm run`, and `nvm which` will use the version specified in the `.nvmrc` file if no version is supplied on the command line.537 unmodified lines