use super::{ChecksumResult, ChecksumValidator};
pub struct GitlabTokenValidator;
const GITLAB_BODY_MIN: usize = 20;
const GITLAB_BODY_MAX: usize = 64;
fn gitlab_body_charset_ok(payload: &str) -> bool {
payload
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == '.')
}
impl ChecksumValidator for GitlabTokenValidator {
fn validator_id(&self) -> &str {
"gitlab-token"
}
fn validate(&self, credential: &str) -> ChecksumResult {
if let Some(payload) = credential.strip_prefix("glpat-") {
if !gitlab_body_charset_ok(payload) {
return ChecksumResult::Invalid;
}
return match payload.len() {
GITLAB_BODY_MIN..=GITLAB_BODY_MAX => ChecksumResult::Valid,
n if n < GITLAB_BODY_MIN => ChecksumResult::Invalid,
_ => ChecksumResult::NotApplicable,
};
}
if let Some(payload) = credential
.strip_prefix("glcbt-")
.or_else(|| credential.strip_prefix("glrt-"))
{
if !gitlab_body_charset_ok(payload) {
return ChecksumResult::Invalid;
}
return match payload.len() {
16..=GITLAB_BODY_MAX => ChecksumResult::Valid,
n if n < 16 => ChecksumResult::Invalid,
_ => ChecksumResult::NotApplicable,
};
}
ChecksumResult::NotApplicable
}
}