use crate::detectors::additional_providers::TypedKnownFormatProviderDetector;
use crate::detectors::pattern::{self, PrefixShape};
const BODY_LEN: usize = 43;
const SIGNALS: [&str; 2] = ["polar-generator-prefix", "polar-generator-length"];
const ORGANIZATION: &str = "polar_organization_access_token";
const API: &str = "polar_api_credential";
pub(super) const POLAR: TypedKnownFormatProviderDetector = TypedKnownFormatProviderDetector::new(
"polar-token",
&[
PrefixShape::exact("polar_oat_", BODY_LEN, pattern::is_alnum, &SIGNALS),
PrefixShape::exact("polar_pat_", BODY_LEN, pattern::is_alnum_dash, &SIGNALS),
PrefixShape::exact("polar_at_u_", BODY_LEN, pattern::is_alnum_dash, &SIGNALS),
PrefixShape::exact("polar_at_o_", BODY_LEN, pattern::is_alnum_dash, &SIGNALS),
PrefixShape::exact("polar_rt_u_", BODY_LEN, pattern::is_alnum_dash, &SIGNALS),
PrefixShape::exact("polar_rt_o_", BODY_LEN, pattern::is_alnum_dash, &SIGNALS),
PrefixShape::exact("polar_cs_", BODY_LEN, pattern::is_alnum_dash, &SIGNALS),
PrefixShape::exact("polar_crt_", BODY_LEN, pattern::is_alnum_dash, &SIGNALS),
],
&[ORGANIZATION, API, API, API, API, API, API, API],
pattern::is_alnum_dash,
);
#[cfg(test)]
mod tests {
use super::*;
use crate::types::{ByteRange, Candidate, Confidence, Detector, DetectorContext, Specificity};
const ALNUM: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const URL_SAFE: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
const API_PREFIXES: [&str; 7] = [
"polar_pat_",
"polar_at_u_",
"polar_at_o_",
"polar_rt_u_",
"polar_rt_o_",
"polar_cs_",
"polar_crt_",
];
fn filler(alphabet: &[u8], len: usize, seed: usize) -> String {
(0..len)
.map(|i| char::from(alphabet[(i * 7 + seed * 13 + i / 5) % alphabet.len()]))
.collect()
}
fn detect(input: &str) -> Vec<Candidate> {
POLAR
.detect(input, &DetectorContext::new(input.len()))
.unwrap()
}
fn assert_single(input: &str, token: &str, type_name: &str) {
let candidates = detect(input);
assert_eq!(candidates.len(), 1, "{input}");
let start = input.find(token).unwrap();
assert_eq!(candidates[0].type_name(), type_name, "{input}");
assert_eq!(candidates[0].confidence(), Confidence::High);
assert_eq!(candidates[0].effective_specificity(), Specificity::Provider);
assert_eq!(
candidates[0].range(),
ByteRange::new(start, start + token.len()).unwrap(),
"{input}"
);
}
fn contexts(token: &str) -> Vec<String> {
vec![
token.to_owned(),
format!("POLAR_ACCESS_TOKEN={token}"),
format!("export POLAR_ACCESS_TOKEN=\"{token}\""),
format!("Authorization: Bearer {token}"),
format!("{{\"token\": \"{token}\"}}"),
format!("polar = Polar(access_token=\"{token}\")"),
format!("Here is my token {token} can you debug this?"),
format!("The token is {token}."),
]
}
#[test]
fn every_role_is_detected_in_every_context() {
let oat = format!("polar_oat_{}", filler(ALNUM, BODY_LEN, 1));
for input in contexts(&oat) {
assert_single(&input, &oat, ORGANIZATION);
}
for (seed, prefix) in API_PREFIXES.iter().enumerate() {
for body in [
filler(URL_SAFE, BODY_LEN, seed),
filler(ALNUM, BODY_LEN, seed),
] {
let token = format!("{prefix}{body}");
for input in contexts(&token) {
assert_single(&input, &token, API);
}
}
}
}
#[test]
fn near_miss_twins_are_rejected() {
let body = filler(ALNUM, BODY_LEN, 2);
let mut dashed = body.clone();
dashed.replace_range(10..11, "-");
let mut underscored = body.clone();
underscored.replace_range(10..11, "_");
let oat = format!("polar_oat_{body}");
for input in [
format!("polar_oat_{}", &body[..42]),
format!("polar_oat_{body}A"),
format!("polar_pat_{}", &body[..42]),
format!("polar_pat_{body}A"),
format!("polar_oat_{dashed}"),
format!("polar_oat_{underscored}"),
format!("polar_at_{body}"),
format!("POLAR_OAT_{body}"),
format!("x{oat}"),
format!("_{oat}"),
format!("{oat}_"),
format!("{oat}-x"),
] {
assert!(detect(&input).is_empty(), "{input}");
}
}
#[test]
fn public_and_out_of_contract_siblings_are_not_claimed() {
let body = filler(ALNUM, BODY_LEN, 3);
for prefix in [
"polar_ci_",
"polar_c_",
"polar_cl_",
"polar_us_",
"polar_cst_",
"polar_ac_",
"whsec_",
] {
let input = format!("{prefix}{body}");
assert!(detect(&input).is_empty(), "{input}");
}
for input in [
"POLAR_ACCESS_TOKEN=polar_oat_xxxxxxxx",
"POLAR_ACCESS_TOKEN=${POLAR_ACCESS_TOKEN}",
"polar_access_token_id = 42",
] {
assert!(detect(input).is_empty(), "{input}");
}
}
#[test]
fn repeated_tokens_are_reported_once_each_and_a_glued_run_never() {
let oat = format!("polar_oat_{}", filler(ALNUM, BODY_LEN, 4));
assert_eq!(detect(&format!("{oat} {oat}")).len(), 2);
assert!(detect(&oat.repeat(200)).is_empty());
assert!(detect(&"polar_oat_".repeat(2_000)).is_empty());
}
}