Skip to content

Commit

Permalink
Remove oauth token
Browse files Browse the repository at this point in the history
Turns out app tokens are temporal tokens, so we shouldn't need this code for now.
  • Loading branch information
sondrelg committed Jun 24, 2024
1 parent 851b141 commit 79a0b31
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 27 deletions.
4 changes: 0 additions & 4 deletions src/cli/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,6 @@ mod tests {
Token::try_from_str("ghp_sSIL4kMdtzfbfDdm1MC1OU2q5DbRqA3eSszT").unwrap(),
Token::ClassicPersonalAccess(Secret::new("ghp_sSIL4kMdtzfbfDdm1MC1OU2q5DbRqA3eSszT".to_string()))
);
assert_eq!(
Token::try_from_str("gho_sSIL4kMdtzfbfDdm1MC1OU2q5DbRqA3eSszT").unwrap(),
Token::Oauth(Secret::new("gho_sSIL4kMdtzfbfDdm1MC1OU2q5DbRqA3eSszT".to_string()))
);
}

#[test]
Expand Down
20 changes: 3 additions & 17 deletions src/cli/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ pub enum TagSelection {
#[derive(Debug, Clone)]
pub enum Token {
ClassicPersonalAccess(Secret<String>),
Oauth(Secret<String>),
Temporal(Secret<String>),
}

Expand All @@ -45,13 +44,6 @@ impl PartialEq for Token {
false
}
}
Self::Oauth(a) => {
if let Self::Oauth(b) = other {
a.expose_secret() == b.expose_secret()
} else {
false
}
}
}
}
}
Expand All @@ -63,22 +55,16 @@ impl Token {

// Classic PAT
if Regex::new(r"ghp_[a-zA-Z0-9]{36}$").unwrap().is_match(trimmed_value) {
debug!("Recognized tokens as personal access token");
debug!("Recognized token as personal access token");
return Ok(Self::ClassicPersonalAccess(secret));
};

// Temporal token - i.e., $GITHUB_TOKEN
// Temporal token - i.e., $GITHUB_TOKEN or token acquired for a GitHub app
if Regex::new(r"ghs_[a-zA-Z0-9]{36}$").unwrap().is_match(trimmed_value) {
debug!("Recognized tokens as temporal token");
debug!("Recognized token as temporal token");
return Ok(Self::Temporal(secret));
};

// GitHub oauth token
// TODO: Verify whether a Github app token is an oauth token or not.
if Regex::new(r"gho_[a-zA-Z0-9]{36}$").unwrap().is_match(trimmed_value) {
debug!("Recognized tokens as oauth token");
return Ok(Self::Oauth(secret));
};
Err(
"The `token` value is not valid. Must be $GITHUB_TOKEN, a classic personal access token (prefixed by 'ghp') or oauth token (prefixed by 'gho').".to_string()
)
Expand Down
3 changes: 1 addition & 2 deletions src/client/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ impl PackagesClientBuilder {
let auth_header_value = format!(
"Bearer {}",
match &token {
Token::Temporal(token) | Token::Oauth(token) | Token::ClassicPersonalAccess(token) =>
token.expose_secret(),
Token::Temporal(token) | Token::ClassicPersonalAccess(token) => token.expose_secret(),
}
);
let mut headers = HeaderMap::new();
Expand Down
4 changes: 1 addition & 3 deletions src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,16 +454,14 @@ impl PackagesClient {
// auth check: Make sure we have the correct scopes
match self.token {
Token::Temporal(_) => (),
Token::Oauth(_) | Token::ClassicPersonalAccess(_) => {
Token::ClassicPersonalAccess(_) => {
if response_headers.x_oauth_scopes.is_none()
|| !response_headers
.x_oauth_scopes
.clone()
.unwrap()
.contains("write:packages")
{
/// Check that the headers of a GitHub request indicate that the token used has the correct scopes for deleting packages.
/// See documentation at: https://docs.github.com/en/rest/packages/packages?apiVersion=2022-11-28#delete-a-package-for-an-organization
eprintln!("The token does not have the scopes needed. Tokens need `write:packages`. The scopes found were {}.", response_headers.x_oauth_scopes.unwrap_or("none".to_string()));
exit(1);
}
Expand Down
1 change: 0 additions & 1 deletion src/client/headers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::cli::models::Token;
use chrono::{DateTime, Utc};
use color_eyre::Result;
use reqwest::header::HeaderMap;
Expand Down

0 comments on commit 79a0b31

Please sign in to comment.