Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix personal token auth for pagination #602

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,9 +741,9 @@ impl OctocrabBuilder<NoSvc, DefaultOctocrabBuilderConfig, NoAuth, NotLayerReady>
.clone()
.unwrap_or_else(|| Uri::from_str(GITHUB_BASE_URI).unwrap());

let client = BaseUriLayer::new(uri).layer(client);
let client = BaseUriLayer::new(uri.clone()).layer(client);

let client = AuthHeaderLayer::new(auth_header).layer(client);
let client = AuthHeaderLayer::new(auth_header, uri).layer(client);

Ok(Octocrab::new(client, auth_state))
}
Expand Down
15 changes: 10 additions & 5 deletions src/service/middleware/auth_header.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
use std::sync::Arc;

use http::{header::AUTHORIZATION, request::Request, HeaderValue};
use http::{header::AUTHORIZATION, request::Request, HeaderValue, Uri};
use tower::{Layer, Service};

#[derive(Clone)]
/// Layer that adds the authentication header to github-bound requests
pub struct AuthHeaderLayer {
pub(crate) auth_header: Arc<Option<HeaderValue>>,
base_uri: Uri,
}

impl AuthHeaderLayer {
pub fn new(auth_header: Option<HeaderValue>) -> Self {
pub fn new(auth_header: Option<HeaderValue>, base_uri: Uri) -> Self {
AuthHeaderLayer {
auth_header: Arc::new(auth_header),
base_uri,
}
}
}
Expand All @@ -24,6 +26,7 @@ impl<S> Layer<S> for AuthHeaderLayer {
AuthHeader {
inner,
auth_header: self.auth_header.clone(),
base_uri: self.base_uri.clone(),
}
}
}
Expand All @@ -33,6 +36,7 @@ impl<S> Layer<S> for AuthHeaderLayer {
pub struct AuthHeader<S> {
inner: S,
pub(crate) auth_header: Arc<Option<HeaderValue>>,
base_uri: Uri,
}

impl<S, ReqBody> Service<Request<ReqBody>> for AuthHeader<S>
Expand All @@ -51,11 +55,12 @@ where
}

fn call(&mut self, mut req: Request<ReqBody>) -> Self::Future {
// Only set the auth_header if the authority (host) is empty (destined for
// GitHub). Otherwise, leave it off as we could have been redirected
// Only set the auth_header if the authority (host) is destined for
// GitHub. Otherwise, leave it off as we could have been redirected
// away from GitHub (via follow_location_to_data()), and we don't
// want to give our credentials to third-party services.
if req.uri().authority().is_none() {
let authority = req.uri().authority();
if authority.is_none() || authority == self.base_uri.authority() {
if let Some(auth_header) = &*self.auth_header {
req.headers_mut().append(AUTHORIZATION, auth_header.clone());
}
Expand Down
Loading