Skip to content

Commit ce33ac1

Browse files
authored
GitHub: Use basic/bearer_auth() fns from reqwest (#11014)
Turns out we were not applying base64 encoding to our basic auth requests before. Using `.basic_auth()` fixes the issue, which means that we now properly authenticate, while previously we were accidentally running these requests completely unauthenticated, making us subject to the 60 requests per hour rate limit...
1 parent 146eecb commit ce33ac1

File tree

1 file changed

+9
-11
lines changed
  • crates/crates_io_github/src

1 file changed

+9
-11
lines changed

Diff for: crates/crates_io_github/src/lib.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
extern crate tracing;
55

66
use oauth2::AccessToken;
7-
use reqwest::{self, header};
7+
use reqwest::{self, header, RequestBuilder};
88

99
use serde::de::DeserializeOwned;
1010

@@ -54,22 +54,21 @@ impl RealGitHubClient {
5454
}
5555

5656
/// Does all the nonsense for sending a GET to Github.
57-
async fn _request<T>(&self, url: &str, auth: &str) -> Result<T>
57+
async fn _request<T, A>(&self, url: &str, apply_auth: A) -> Result<T>
5858
where
5959
T: DeserializeOwned,
60+
A: Fn(RequestBuilder) -> RequestBuilder,
6061
{
6162
let url = format!("https://api.github.com{url}");
6263
info!("GitHub request: GET {url}");
6364

64-
let response = self
65+
let request = self
6566
.client
6667
.get(&url)
6768
.header(header::ACCEPT, "application/vnd.github.v3+json")
68-
.header(header::AUTHORIZATION, auth)
69-
.header(header::USER_AGENT, "crates.io (https://crates.io)")
70-
.send()
71-
.await?
72-
.error_for_status()?;
69+
.header(header::USER_AGENT, "crates.io (https://crates.io)");
70+
71+
let response = apply_auth(request).send().await?.error_for_status()?;
7372

7473
let headers = response.headers();
7574
let remaining = headers.get("x-ratelimit-remaining");
@@ -84,16 +83,15 @@ impl RealGitHubClient {
8483
where
8584
T: DeserializeOwned,
8685
{
87-
self._request(url, &format!("Bearer {}", auth.secret()))
88-
.await
86+
self._request(url, |r| r.bearer_auth(auth.secret())).await
8987
}
9088

9189
/// Sends a GET to GitHub using basic authentication
9290
pub async fn request_basic<T>(&self, url: &str, username: &str, password: &str) -> Result<T>
9391
where
9492
T: DeserializeOwned,
9593
{
96-
self._request(url, &format!("basic {username}:{password}"))
94+
self._request(url, |r| r.basic_auth(username, Some(password)))
9795
.await
9896
}
9997
}

0 commit comments

Comments
 (0)