Skip to content

Commit

Permalink
fix: trim space around DENO_AUTH_TOKENS (#25147)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacasonato authored Aug 22, 2024
1 parent 9bc7de9 commit e7026c5
Showing 1 changed file with 42 additions and 8 deletions.
50 changes: 42 additions & 8 deletions cli/auth_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,19 @@ impl AuthTokens {
pub fn new(maybe_tokens_str: Option<String>) -> 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,
Expand Down Expand Up @@ -211,6 +211,40 @@ mod tests {
);
}

#[test]
fn test_auth_tokens_space() {
let auth_tokens = AuthTokens::new(Some(
" [email protected];[email protected]\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(
"\n[email protected];[email protected]\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 =
Expand Down

0 comments on commit e7026c5

Please sign in to comment.