diff options
author | Asher Gomez <ashersaupingomez@gmail.com> | 2024-08-14 18:53:15 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-14 18:53:15 +0200 |
commit | f89b5311492377a3ac18d756dc8c8a309e2c9e8a (patch) | |
tree | 68fc92eb556eb72cf75d4f3dd8ff424e283853c2 /cli/npm/common.rs | |
parent | 1f2d48cd975b719f0248e471f3b503cb01398dfb (diff) |
feat(node): support `username` and `_password` in `.npmrc` file (#24793)
Closes #23950
Diffstat (limited to 'cli/npm/common.rs')
-rw-r--r-- | cli/npm/common.rs | 41 |
1 files changed, 35 insertions, 6 deletions
diff --git a/cli/npm/common.rs b/cli/npm/common.rs index 34835216c..a3a828e74 100644 --- a/cli/npm/common.rs +++ b/cli/npm/common.rs @@ -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) } |