Skip to content
New issue

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

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

Already on GitHub? # to your account

Add User Access Authentication #375

Merged
merged 2 commits into from
Jun 3, 2023
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
2 changes: 2 additions & 0 deletions src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ pub enum Auth {
App(AppAuth),
/// Authenticate as a Github OAuth App
OAuth(OAuth),
/// Authenticate using a User Access Token
UserAccessToken(SecretString),
}

impl Default for Auth {
Expand Down
13 changes: 13 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,12 @@ impl OctocrabBuilder<NoSvc, DefaultOctocrabBuilderConfig, NoAuth, NotLayerReady>
self
}

/// Authenticate with a user access token.
pub fn user_access_token(mut self, token: String) -> Self {
self.config.auth = Auth::UserAccessToken(SecretString::new(token));
self
}

/// Set the base url for `Octocrab`.
pub fn base_uri(mut self, base_uri: impl TryInto<Uri>) -> Result<Self> {
self.config.base_uri = Some(
Expand Down Expand Up @@ -624,6 +630,13 @@ impl OctocrabBuilder<NoSvc, DefaultOctocrabBuilderConfig, NoAuth, NotLayerReady>
));
AuthState::None
}
Auth::UserAccessToken(token) => {
hmap.push((
http::header::AUTHORIZATION,
format!("Bearer {}", token.expose_secret()).parse().unwrap(),
));
AuthState::None
}
Auth::App(app_auth) => AuthState::App(app_auth),
Auth::OAuth(device) => {
hmap.push((
Expand Down