1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

fix(npm): match npm bearer token generation (#26544)

Spend some time stepping through the npm client code and noticed that
the bearer token was different from ours. They do some double encoding
and @dsherret helped me in matching the encoding behavior.

Fixes https://github.com/denoland/deno/issues/26033
This commit is contained in:
Marvin Hagemeister 2024-10-28 12:08:51 +01:00 committed by GitHub
parent 3a306c450c
commit 5389972ba5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 11 deletions

View file

@ -3,6 +3,7 @@
use base64::prelude::BASE64_STANDARD; use base64::prelude::BASE64_STANDARD;
use base64::Engine; use base64::Engine;
use deno_core::anyhow::bail; use deno_core::anyhow::bail;
use deno_core::anyhow::Context;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_npm::npm_rc::RegistryConfig; use deno_npm::npm_rc::RegistryConfig;
use http::header; use http::header;
@ -36,17 +37,21 @@ pub fn maybe_auth_header_for_npm_registry(
} }
if username.is_some() && password.is_some() { if username.is_some() && password.is_some() {
return Ok(Some(( // The npm client does some double encoding when generating the
header::AUTHORIZATION, // bearer token value, see
header::HeaderValue::from_str(&format!( // https://github.com/npm/cli/blob/780afc50e3a345feb1871a28e33fa48235bc3bd5/workspaces/config/lib/index.js#L846-L851
"Basic {}", let pw_base64 = BASE64_STANDARD
BASE64_STANDARD.encode(format!( .decode(password.unwrap())
.with_context(|| "The password in npmrc is an invalid base64 string")?;
let bearer = BASE64_STANDARD.encode(format!(
"{}:{}", "{}:{}",
username.unwrap(), username.unwrap(),
password.unwrap() String::from_utf8_lossy(&pw_base64)
)) ));
))
.unwrap(), return Ok(Some((
header::AUTHORIZATION,
header::HeaderValue::from_str(&format!("Basic {}", bearer)).unwrap(),
))); )));
} }

View file

@ -1,6 +1,8 @@
@denotest:registry=http://localhost:4261/ @denotest:registry=http://localhost:4261/
//localhost:4261/:username=deno //localhost:4261/:username=deno
//localhost:4261/:_password=land # base64 of land
//localhost:4261/:_password=bGFuZA==
@denotest2:registry=http://localhost:4262/ @denotest2:registry=http://localhost:4262/
//localhost:4262/:username=deno //localhost:4262/:username=deno
//localhost:4262/:_password=land2 # base64 of land2
//localhost:4262/:_password=bGFuZDI=