From a5d76cacec3f42ee22dee02933c2132ea3e37baf Mon Sep 17 00:00:00 2001 From: Luke Swithenbank Date: Sat, 3 Jun 2023 20:56:14 +1000 Subject: [PATCH] Add User Access Authentication (#375) * add useraccess authentication * add a user_access_token method --- src/auth.rs | 2 ++ src/lib.rs | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/auth.rs b/src/auth.rs index 41a6e774..bcbfb1f8 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -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 { diff --git a/src/lib.rs b/src/lib.rs index eac25d4d..5660ff38 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -489,6 +489,12 @@ impl OctocrabBuilder 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) -> Result { self.config.base_uri = Some( @@ -624,6 +630,13 @@ impl OctocrabBuilder )); 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((