|
| 1 | +use crate::utils::environment::fetch_var; |
| 2 | +use reqwest::RequestBuilder; |
| 3 | + |
| 4 | +const THUNDERSTORE_USERNAME_VAR: &str = "THUNDERSTORE_USERNAME"; |
| 5 | +const THUNDERSTORE_PASSWORD_VAR: &str = "THUNDERSTORE_PASSWORD"; |
| 6 | +const THUNDERSTORE_HOST: &str = "thunderstore.io"; |
| 7 | + |
| 8 | +/// Returns `THUNDERSTORE_USERNAME`/`THUNDERSTORE_PASSWORD` when both are set, for |
| 9 | +/// authenticating against Thunderstore's API (see https://thunderstore.io/api/docs/). |
| 10 | +fn thunderstore_credentials() -> Option<(String, String)> { |
| 11 | + let username = fetch_var(THUNDERSTORE_USERNAME_VAR, ""); |
| 12 | + let password = fetch_var(THUNDERSTORE_PASSWORD_VAR, ""); |
| 13 | + if username.is_empty() || password.is_empty() { |
| 14 | + return None; |
| 15 | + } |
| 16 | + Some((username, password)) |
| 17 | +} |
| 18 | + |
| 19 | +/// Attaches HTTP Basic Auth to `builder` when `url` targets thunderstore.io and |
| 20 | +/// `THUNDERSTORE_USERNAME`/`THUNDERSTORE_PASSWORD` are both configured. Requests to |
| 21 | +/// any other host are returned unmodified. |
| 22 | +pub fn with_thunderstore_auth(builder: RequestBuilder, url: &str) -> RequestBuilder { |
| 23 | + let is_thunderstore = reqwest::Url::parse(url) |
| 24 | + .ok() |
| 25 | + .and_then(|u| { |
| 26 | + u.host_str() |
| 27 | + .map(|h| h.eq_ignore_ascii_case(THUNDERSTORE_HOST)) |
| 28 | + }) |
| 29 | + .unwrap_or(false); |
| 30 | + |
| 31 | + if !is_thunderstore { |
| 32 | + return builder; |
| 33 | + } |
| 34 | + |
| 35 | + match thunderstore_credentials() { |
| 36 | + Some((username, password)) => builder.basic_auth(username, Some(password)), |
| 37 | + None => builder, |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +#[cfg(test)] |
| 42 | +mod tests { |
| 43 | + use super::*; |
| 44 | + use serial_test::serial; |
| 45 | + use std::env::{remove_var, set_var}; |
| 46 | + |
| 47 | + #[test] |
| 48 | + #[serial] |
| 49 | + fn no_auth_when_credentials_missing() { |
| 50 | + remove_var(THUNDERSTORE_USERNAME_VAR); |
| 51 | + remove_var(THUNDERSTORE_PASSWORD_VAR); |
| 52 | + assert!(thunderstore_credentials().is_none()); |
| 53 | + } |
| 54 | + |
| 55 | + #[test] |
| 56 | + #[serial] |
| 57 | + fn no_auth_when_only_username_set() { |
| 58 | + set_var(THUNDERSTORE_USERNAME_VAR, "user"); |
| 59 | + remove_var(THUNDERSTORE_PASSWORD_VAR); |
| 60 | + assert!(thunderstore_credentials().is_none()); |
| 61 | + remove_var(THUNDERSTORE_USERNAME_VAR); |
| 62 | + } |
| 63 | + |
| 64 | + #[test] |
| 65 | + #[serial] |
| 66 | + fn auth_present_when_both_set() { |
| 67 | + set_var(THUNDERSTORE_USERNAME_VAR, "user"); |
| 68 | + set_var(THUNDERSTORE_PASSWORD_VAR, "pass"); |
| 69 | + assert_eq!( |
| 70 | + thunderstore_credentials(), |
| 71 | + Some(("user".to_string(), "pass".to_string())) |
| 72 | + ); |
| 73 | + remove_var(THUNDERSTORE_USERNAME_VAR); |
| 74 | + remove_var(THUNDERSTORE_PASSWORD_VAR); |
| 75 | + } |
| 76 | + |
| 77 | + #[test] |
| 78 | + #[serial] |
| 79 | + fn non_thunderstore_host_untouched() { |
| 80 | + set_var(THUNDERSTORE_USERNAME_VAR, "user"); |
| 81 | + set_var(THUNDERSTORE_PASSWORD_VAR, "pass"); |
| 82 | + let client = reqwest::Client::new(); |
| 83 | + let builder = with_thunderstore_auth( |
| 84 | + client.get("https://example.com/file.zip"), |
| 85 | + "https://example.com/file.zip", |
| 86 | + ); |
| 87 | + let req = builder.build().unwrap(); |
| 88 | + assert!(req.headers().get("authorization").is_none()); |
| 89 | + remove_var(THUNDERSTORE_USERNAME_VAR); |
| 90 | + remove_var(THUNDERSTORE_PASSWORD_VAR); |
| 91 | + } |
| 92 | + |
| 93 | + #[test] |
| 94 | + #[serial] |
| 95 | + fn thunderstore_host_gets_basic_auth() { |
| 96 | + set_var(THUNDERSTORE_USERNAME_VAR, "user"); |
| 97 | + set_var(THUNDERSTORE_PASSWORD_VAR, "pass"); |
| 98 | + let client = reqwest::Client::new(); |
| 99 | + let url = "https://thunderstore.io/package/download/Author/Mod/1.0.0/"; |
| 100 | + let builder = with_thunderstore_auth(client.get(url), url); |
| 101 | + let req = builder.build().unwrap(); |
| 102 | + assert!(req.headers().get("authorization").is_some()); |
| 103 | + remove_var(THUNDERSTORE_USERNAME_VAR); |
| 104 | + remove_var(THUNDERSTORE_PASSWORD_VAR); |
| 105 | + } |
| 106 | +} |
0 commit comments