From e7026c5ee8ac6780391b2169e338ca5fb5bbac70 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Thu, 22 Aug 2024 15:14:50 +0200 Subject: [PATCH] fix: trim space around DENO_AUTH_TOKENS (#25147) --- cli/auth_tokens.rs | 50 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/cli/auth_tokens.rs b/cli/auth_tokens.rs index 42009ef27be539..ef9f9d0746727c 100644 --- a/cli/auth_tokens.rs +++ b/cli/auth_tokens.rs @@ -123,19 +123,19 @@ impl AuthTokens { pub fn new(maybe_tokens_str: Option) -> Self { let mut tokens = Vec::new(); if let Some(tokens_str) = maybe_tokens_str { - for token_str in tokens_str.split(';') { + for token_str in tokens_str.trim().split(';') { if token_str.contains('@') { - let pair: Vec<&str> = token_str.rsplitn(2, '@').collect(); - let token = pair[1]; - let host = AuthDomain::from(pair[0]); + let mut iter = token_str.rsplitn(2, '@'); + let host = AuthDomain::from(iter.next().unwrap()); + let token = iter.next().unwrap(); if token.contains(':') { - let pair: Vec<&str> = token.rsplitn(2, ':').collect(); - let username = pair[1].to_string(); - let password = pair[0].to_string(); + let mut iter = token.rsplitn(2, ':'); + let password = iter.next().unwrap().to_owned(); + let username = iter.next().unwrap().to_owned(); tokens.push(AuthToken { host, token: AuthTokenData::Basic { username, password }, - }) + }); } else { tokens.push(AuthToken { host, @@ -211,6 +211,40 @@ mod tests { ); } + #[test] + fn test_auth_tokens_space() { + let auth_tokens = AuthTokens::new(Some( + " abc123@deno.land;def456@example.com\t".to_string(), + )); + let fixture = resolve_url("https://deno.land/x/mod.ts").unwrap(); + assert_eq!( + auth_tokens.get(&fixture).unwrap().to_string(), + "Bearer abc123".to_string() + ); + let fixture = resolve_url("http://example.com/a/file.ts").unwrap(); + assert_eq!( + auth_tokens.get(&fixture).unwrap().to_string(), + "Bearer def456".to_string() + ); + } + + #[test] + fn test_auth_tokens_newline() { + let auth_tokens = AuthTokens::new(Some( + "\nabc123@deno.land;def456@example.com\n".to_string(), + )); + let fixture = resolve_url("https://deno.land/x/mod.ts").unwrap(); + assert_eq!( + auth_tokens.get(&fixture).unwrap().to_string(), + "Bearer abc123".to_string() + ); + let fixture = resolve_url("http://example.com/a/file.ts").unwrap(); + assert_eq!( + auth_tokens.get(&fixture).unwrap().to_string(), + "Bearer def456".to_string() + ); + } + #[test] fn test_auth_tokens_port() { let auth_tokens =