mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-22 13:38:43 +00:00
Verify syntax of the github token provided in GhApiClient::new
(#870)
Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
174b6c8144
commit
ecd599bfa8
1 changed files with 14 additions and 0 deletions
|
@ -7,6 +7,7 @@ use std::{
|
|||
|
||||
use compact_str::{CompactString, ToCompactString};
|
||||
use tokio::sync::OnceCell;
|
||||
use tracing::warn;
|
||||
|
||||
use crate::remote;
|
||||
|
||||
|
@ -96,8 +97,21 @@ struct Inner {
|
|||
#[derive(Clone, Debug)]
|
||||
pub struct GhApiClient(Arc<Inner>);
|
||||
|
||||
fn gh_prefixed(token: &str) -> bool {
|
||||
matches!((token.get(0..2), token.get(3..4)), (Some("gh"), Some("_")))
|
||||
}
|
||||
|
||||
impl GhApiClient {
|
||||
pub fn new(client: remote::Client, auth_token: Option<CompactString>) -> Self {
|
||||
let auth_token = auth_token.and_then(|auth_token| {
|
||||
if gh_prefixed(&auth_token) {
|
||||
Some(auth_token)
|
||||
} else {
|
||||
warn!("Invalid auth_token, expected 'gh*_', fallback to unauthorized mode");
|
||||
None
|
||||
}
|
||||
});
|
||||
|
||||
Self(Arc::new(Inner {
|
||||
client,
|
||||
auth_token,
|
||||
|
|
Loading…
Add table
Reference in a new issue