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:
Jiahao XU 2023-03-07 18:48:01 +11:00 committed by GitHub
parent 174b6c8144
commit ecd599bfa8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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,