Skip to content

Commit

Permalink
Add User Access Authentication (#375)
Browse files Browse the repository at this point in the history
* add useraccess authentication

* add a user_access_token method
  • Loading branch information
lswith authored Jun 3, 2023
1 parent 9d1e959 commit a5d76ca
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
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

0 comments on commit a5d76ca

Please sign in to comment.