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

Enable client reuse when authenticating with app user access tokens #740

Merged
Merged
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
23 changes: 23 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,11 @@ pub enum AuthState {
/// The cached access token, if any
token: CachedToken,
},
/// Access token based authentication.
AccessToken {
/// The access token
token: SecretString,
},
}

pub type OctocrabService = Buffer<
Expand Down Expand Up @@ -1106,6 +1111,19 @@ impl Octocrab {
let token = crab.request_installation_auth_token().await?;
Ok((crab, token))
}

/// Returns a new `Octocrab` based on the current builder but
/// authorizing via an access token.
///
/// See also https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-user-access-token-for-a-github-app
pub fn user_access_token<S: Into<SecretString>>(&self, token: S) -> Result<Self> {
Ok(Octocrab {
client: self.client.clone(),
auth_state: AuthState::AccessToken {
token: token.into(),
},
})
}
}

/// # GitHub API Methods
Expand Down Expand Up @@ -1681,6 +1699,11 @@ impl Octocrab {
.context(HttpSnafu)?,
)
}
AuthState::AccessToken { ref token } => Some(
HeaderValue::from_str(format!("Bearer {}", token.expose_secret()).as_str())
.map_err(http::Error::from)
.context(HttpSnafu)?,
),
};

if let Some(mut auth_header) = auth_header {
Expand Down
Loading