mirror of
https://github.com/denoland/deno.git
synced 2024-11-24 15:19:26 -05:00
feat(node): support username
and _password
in .npmrc
file (#24793)
Closes #23950
This commit is contained in:
parent
1f2d48cd97
commit
f89b531149
17 changed files with 135 additions and 8 deletions
|
@ -1,25 +1,54 @@
|
|||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use base64::prelude::BASE64_STANDARD;
|
||||
use base64::Engine;
|
||||
use deno_core::anyhow::bail;
|
||||
use deno_core::error::AnyError;
|
||||
use deno_npm::npm_rc::RegistryConfig;
|
||||
use http::header;
|
||||
|
||||
// TODO(bartlomieju): support more auth methods besides token and basic auth
|
||||
pub fn maybe_auth_header_for_npm_registry(
|
||||
registry_config: &RegistryConfig,
|
||||
) -> Option<(header::HeaderName, header::HeaderValue)> {
|
||||
) -> Result<Option<(header::HeaderName, header::HeaderValue)>, AnyError> {
|
||||
if let Some(token) = registry_config.auth_token.as_ref() {
|
||||
return Some((
|
||||
return Ok(Some((
|
||||
header::AUTHORIZATION,
|
||||
header::HeaderValue::from_str(&format!("Bearer {}", token)).unwrap(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
|
||||
if let Some(auth) = registry_config.auth.as_ref() {
|
||||
return Some((
|
||||
return Ok(Some((
|
||||
header::AUTHORIZATION,
|
||||
header::HeaderValue::from_str(&format!("Basic {}", auth)).unwrap(),
|
||||
));
|
||||
)));
|
||||
}
|
||||
|
||||
None
|
||||
let (username, password) = (
|
||||
registry_config.username.as_ref(),
|
||||
registry_config.password.as_ref(),
|
||||
);
|
||||
if (username.is_some() && password.is_none())
|
||||
|| (username.is_none() && password.is_some())
|
||||
{
|
||||
bail!("Both the username and password must be provided for basic auth")
|
||||
}
|
||||
|
||||
if username.is_some() && password.is_some() {
|
||||
return Ok(Some((
|
||||
header::AUTHORIZATION,
|
||||
header::HeaderValue::from_str(&format!(
|
||||
"Basic {}",
|
||||
BASE64_STANDARD.encode(&format!(
|
||||
"{}:{}",
|
||||
username.unwrap(),
|
||||
password.unwrap()
|
||||
))
|
||||
))
|
||||
.unwrap(),
|
||||
)));
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
|
|
8
cli/npm/managed/cache/registry_info.rs
vendored
8
cli/npm/managed/cache/registry_info.rs
vendored
|
@ -192,7 +192,13 @@ impl RegistryInfoDownloader {
|
|||
let downloader = self.clone();
|
||||
let package_url = self.get_package_url(name);
|
||||
let registry_config = self.npmrc.get_registry_config(name);
|
||||
let maybe_auth_header = maybe_auth_header_for_npm_registry(registry_config);
|
||||
let maybe_auth_header =
|
||||
match maybe_auth_header_for_npm_registry(registry_config) {
|
||||
Ok(maybe_auth_header) => maybe_auth_header,
|
||||
Err(err) => {
|
||||
return std::future::ready(Err(Arc::new(err))).boxed_local()
|
||||
}
|
||||
};
|
||||
let guard = self.progress_bar.update(package_url.as_str());
|
||||
let name = name.to_string();
|
||||
async move {
|
||||
|
|
2
cli/npm/managed/cache/tarball.rs
vendored
2
cli/npm/managed/cache/tarball.rs
vendored
|
@ -167,7 +167,7 @@ impl TarballCache {
|
|||
let tarball_uri = Url::parse(&dist.tarball)?;
|
||||
let maybe_registry_config =
|
||||
tarball_cache.npmrc.tarball_config(&tarball_uri);
|
||||
let maybe_auth_header = maybe_registry_config.and_then(|c| maybe_auth_header_for_npm_registry(c));
|
||||
let maybe_auth_header = maybe_registry_config.and_then(|c| maybe_auth_header_for_npm_registry(c).ok()?);
|
||||
|
||||
let guard = tarball_cache.progress_bar.update(&dist.tarball);
|
||||
let result = tarball_cache.http_client_provider
|
||||
|
|
2
tests/specs/npm/npmrc_password_no_username/.npmrc
Normal file
2
tests/specs/npm/npmrc_password_no_username/.npmrc
Normal file
|
@ -0,0 +1,2 @@
|
|||
@denotest:registry=http://localhost:4261/
|
||||
//localhost:4261/:_password=land
|
11
tests/specs/npm/npmrc_password_no_username/__test__.jsonc
Normal file
11
tests/specs/npm/npmrc_password_no_username/__test__.jsonc
Normal file
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"envs": {
|
||||
"DENO_FUTURE": "1"
|
||||
},
|
||||
"tempDir": true,
|
||||
"steps": [{
|
||||
"args": "install",
|
||||
"output": "install.out",
|
||||
"exitCode": 1
|
||||
}]
|
||||
}
|
3
tests/specs/npm/npmrc_password_no_username/install.out
Normal file
3
tests/specs/npm/npmrc_password_no_username/install.out
Normal file
|
@ -0,0 +1,3 @@
|
|||
[UNORDERED_START]
|
||||
error: Error getting response at http://localhost:4261/@denotest/basic for package "@denotest/basic": Both the username and password must be provided for basic auth
|
||||
[UNORDERED_END]
|
7
tests/specs/npm/npmrc_password_no_username/package.json
Normal file
7
tests/specs/npm/npmrc_password_no_username/package.json
Normal file
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"name": "npmrc_test",
|
||||
"version": "0.0.1",
|
||||
"dependencies": {
|
||||
"@denotest/basic": "^=1.0.0"
|
||||
}
|
||||
}
|
2
tests/specs/npm/npmrc_username_no_password/.npmrc
Normal file
2
tests/specs/npm/npmrc_username_no_password/.npmrc
Normal file
|
@ -0,0 +1,2 @@
|
|||
@denotest:registry=http://localhost:4261/
|
||||
//localhost:4261/:username=deno
|
11
tests/specs/npm/npmrc_username_no_password/__test__.jsonc
Normal file
11
tests/specs/npm/npmrc_username_no_password/__test__.jsonc
Normal file
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"envs": {
|
||||
"DENO_FUTURE": "1"
|
||||
},
|
||||
"tempDir": true,
|
||||
"steps": [{
|
||||
"args": "install",
|
||||
"output": "install.out",
|
||||
"exitCode": 1
|
||||
}]
|
||||
}
|
3
tests/specs/npm/npmrc_username_no_password/install.out
Normal file
3
tests/specs/npm/npmrc_username_no_password/install.out
Normal file
|
@ -0,0 +1,3 @@
|
|||
[UNORDERED_START]
|
||||
error: Error getting response at http://localhost:4261/@denotest/basic for package "@denotest/basic": Both the username and password must be provided for basic auth
|
||||
[UNORDERED_END]
|
7
tests/specs/npm/npmrc_username_no_password/package.json
Normal file
7
tests/specs/npm/npmrc_username_no_password/package.json
Normal file
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"name": "npmrc_test",
|
||||
"version": "0.0.1",
|
||||
"dependencies": {
|
||||
"@denotest/basic": "^=1.0.0"
|
||||
}
|
||||
}
|
6
tests/specs/npm/npmrc_username_password/.npmrc
Normal file
6
tests/specs/npm/npmrc_username_password/.npmrc
Normal file
|
@ -0,0 +1,6 @@
|
|||
@denotest:registry=http://localhost:4261/
|
||||
//localhost:4261/:username=deno
|
||||
//localhost:4261/:_password=land
|
||||
@denotest2:registry=http://localhost:4262/
|
||||
//localhost:4262/:username=deno
|
||||
//localhost:4262/:_password=land2
|
13
tests/specs/npm/npmrc_username_password/__test__.jsonc
Normal file
13
tests/specs/npm/npmrc_username_password/__test__.jsonc
Normal file
|
@ -0,0 +1,13 @@
|
|||
{
|
||||
"envs": {
|
||||
"DENO_FUTURE": "1"
|
||||
},
|
||||
"tempDir": true,
|
||||
"steps": [{
|
||||
"args": "install",
|
||||
"output": "install.out"
|
||||
}, {
|
||||
"args": "run -A main.js",
|
||||
"output": "main.out"
|
||||
}]
|
||||
}
|
8
tests/specs/npm/npmrc_username_password/install.out
Normal file
8
tests/specs/npm/npmrc_username_password/install.out
Normal file
|
@ -0,0 +1,8 @@
|
|||
[UNORDERED_START]
|
||||
Download http://localhost:4261/@denotest/basic
|
||||
Download http://localhost:4262/@denotest2/basic
|
||||
Download http://localhost:4261/@denotest/basic/1.0.0.tgz
|
||||
Download http://localhost:4262/@denotest2/basic/1.0.0.tgz
|
||||
Initialize @denotest2/basic@1.0.0
|
||||
Initialize @denotest/basic@1.0.0
|
||||
[UNORDERED_END]
|
8
tests/specs/npm/npmrc_username_password/main.js
Normal file
8
tests/specs/npm/npmrc_username_password/main.js
Normal file
|
@ -0,0 +1,8 @@
|
|||
import { getValue, setValue } from "@denotest/basic";
|
||||
import * as test from "@denotest2/basic";
|
||||
|
||||
console.log(getValue());
|
||||
setValue(42);
|
||||
console.log(getValue());
|
||||
|
||||
console.log(test.getValue());
|
3
tests/specs/npm/npmrc_username_password/main.out
Normal file
3
tests/specs/npm/npmrc_username_password/main.out
Normal file
|
@ -0,0 +1,3 @@
|
|||
0
|
||||
42
|
||||
0
|
8
tests/specs/npm/npmrc_username_password/package.json
Normal file
8
tests/specs/npm/npmrc_username_password/package.json
Normal file
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"name": "npmrc_test",
|
||||
"version": "0.0.1",
|
||||
"dependencies": {
|
||||
"@denotest/basic": "^=1.0.0",
|
||||
"@denotest2/basic": "1.0.0"
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue