XAMPPRocky / octocrab

A modern, extensible GitHub API Client for Rust.
Other
1.06k stars 258 forks source link

Fix personal token auth for pagination #602

Closed pnevyk closed 5 months ago

pnevyk commented 6 months ago

Previously the AuthHeaderLayer added the auth header only when the authority was empty (i.e., base URI for GitHub was supposed to be used). However, the page URIs (e.g., next page) returned by GitHub API contains the absolute URL including the hostname. Because of this, the following code didn't work for resources that needed authentication (e.g., private repos)

use octocrab::Octocrab;

#[tokio::main]
async fn main() -> octocrab::Result<()> {
    let token = std::env::var("GITHUB_TOKEN").expect("GITHUB_TOKEN env variable is required");

    let octocrab = Octocrab::builder().personal_token(token).build()?;

    let page = octocrab
        .repos("owner", "private-repo")
        .list_commits()
        .send()
        .await?;

    octocrab
        .get_page::<octocrab::models::repos::RepoCommit>(&page.next)
        .await?;

    Ok(())
}

This fix adds the base URI to the AuthHeaderLayer so that it can check whether a request is destined for GitHub (either the hostname is empty or it is equal to the base URI).

marcusirgens commented 5 months ago

This also breaks paginators for installations and apps, would be great to get this merged.

XAMPPRocky commented 5 months ago

Thank you for your PR, and congrats on your first contribution! 🎉