use crate::detectors::additional_providers::TypedKnownFormatProviderDetector;
use crate::detectors::pattern::{self, PrefixShape};
const ACCESS_TOKEN: &str = "square_access_token";
const APPLICATION_SECRET: &str = "square_oauth_application_secret";
const SIGNALS: [&str; 2] = ["square-docs-prefix", "square-docs-shape"];
const SECRET_WIDTHS: &[usize] = &[43, 44];
pub(super) const SQUARE: TypedKnownFormatProviderDetector = TypedKnownFormatProviderDetector::new(
"square-token",
&[
PrefixShape::exact("EAAA", 60, pattern::is_alnum_dash, &SIGNALS),
PrefixShape::one_of("sq0csp-", SECRET_WIDTHS, pattern::is_alnum_dash, &SIGNALS),
PrefixShape::exact("sandbox-sq0csb-", 43, pattern::is_alnum_dash, &SIGNALS),
],
&[ACCESS_TOKEN, APPLICATION_SECRET, APPLICATION_SECRET],
pattern::is_alnum_dash,
);
#[cfg(test)]
mod tests {
use super::*;
use crate::types::{ByteRange, Candidate, Confidence, Detector, DetectorContext, Specificity};
const B64URL: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";
fn filler(len: usize, seed: usize) -> String {
(0..len)
.map(|i| char::from(B64URL[(i * 7 + seed * 13 + i / 5) % B64URL.len()]))
.collect()
}
fn key(prefix: &str, len: usize) -> String {
format!("{prefix}{}", filler(len, len))
}
fn detect(input: &str) -> Vec<Candidate> {
SQUARE
.detect(input, &DetectorContext::new(input.len()))
.unwrap()
}
fn assert_single(input: &str, key: &str, type_name: &str) {
let candidates = detect(input);
assert_eq!(candidates.len(), 1, "{input}");
let start = input.find(key).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 + key.len()).unwrap(),
"{input}"
);
}
#[test]
fn stable_widths_are_claimed_with_exact_spans() {
for (key, type_name) in [
(key("EAAA", 60), ACCESS_TOKEN),
(key("sq0csp-", 43), APPLICATION_SECRET),
(key("sq0csp-", 44), APPLICATION_SECRET),
(key("sandbox-sq0csb-", 43), APPLICATION_SECRET),
] {
for input in [
key.clone(),
format!("SQUARE_ACCESS_TOKEN={key}"),
format!("Authorization: Bearer {key}"),
format!("\u{d0a4}\u{1f511} \"{key}\"."),
] {
assert_single(&input, &key, type_name);
}
}
}
#[test]
fn conflicting_widths_and_shapes_are_unclaimed() {
for input in [
key("EAAA", 59),
key("EAAA", 61),
key("EAAl", 59),
key("EQAA", 60),
key("EAAB", 60),
key("eaaa", 60),
key("sq0csp-", 42),
key("sq0csp-", 45),
key("sandbox-sq0csb-", 42),
key("sandbox-sq0csb-", 44),
key("sq0idp-", 22),
key("sq0atp-", 22),
format!("xEAAA{}", filler(60, 1)),
format!("{}_", key("EAAA", 60)),
format!("{}-x", key("sq0csp-", 43)),
format!("{}0", key("sq0csp-", 44)),
format!("EAAA{}+{}", filler(30, 2), filler(29, 3)),
format!("EAAA{}={}", filler(30, 2), filler(29, 3)),
] {
assert!(detect(&input).is_empty(), "{input}");
}
}
#[test]
fn repetition_stays_linear_and_exact() {
assert!(detect(&"EAAA".repeat(10_000)).is_empty());
assert!(detect(&"sq0csp-".repeat(10_000)).is_empty());
let token = key("EAAA", 60);
assert_eq!(detect(&format!("{token} ").repeat(200)).len(), 200);
assert!(detect(&token.repeat(200)).is_empty());
}
}