Skip to content

detect

Recognizes a well-known credential format: the whole of token must look like one.

The check is by prefix, alphabet and length (GitHub, AWS, Slack, Stripe, Google, npm, JWT, and the header of a PEM private key): no network call, so it says a string looks like a token, not that it is valid, and it will miss formats it does not know. Use scan to search a whole text.

use helpers4::secret::detect;

Cargo feature secret (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features secret

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.6", default-features = false, features = ["secret"] }
pub fn detect(token: &str) -> Option<TokenKind>
ParameterTypeDescription
token&strThe string to check, a single word without surrounding quotes.

Option<TokenKind> — The TokenKind, or None when the string matches no known format.

use helpers4::secret::{detect, TokenKind};

assert_eq!(detect("ghp_0123456789abcdefghijklmnopqrstuvwxyz"), Some(TokenKind::GitHub));
assert_eq!(detect("just-a-word"), None);

src/secret/detect.rs