Expand description
Security primitives used by Crabka brokers, clients, and tooling.
The crate owns the protocol-independent pieces of TLS, SASL, SCRAM,
OAuth/OIDC, mTLS principal extraction, delegation-token HMACs, Kerberos
exchange state, and principal modelling. Network I/O remains in the caller:
OAuth introspection is represented by the async IntrospectionClient trait,
and the broker/client crates provide concrete transports and wire the
resulting validators into listener or connection handshakes.
§SASL/PLAIN verification
use crabka_security::{AuthMethod, verify_plain};
use std::collections::HashMap;
let mut users = HashMap::new();
users.insert("alice".to_string(), "wonderland".to_string());
let principal = verify_plain(&users, "alice", b"wonderland").unwrap();
assert_eq!(principal.name, "alice");
assert_eq!(principal.auth_method, AuthMethod::SaslPlain);§Storing SCRAM credentials
use crabka_security::{SaslMechanism, hash_scram_password};
let credential = hash_scram_password(
b"correct horse battery staple",
SaslMechanism::ScramSha512,
4096,
);
assert_eq!(credential.iterations, 4096);Re-exports§
pub use delegation_token::SecretBytes;pub use delegation_token::compute_token_hmac;pub use scram::ScramClientExchange;pub use scram::ScramCredential;pub use scram::ScramServerExchange;pub use scram::StepResult;pub use scram::derive_keys_from_salted;pub use scram::hash_scram_password;pub use scram::pbkdf2_salted;pub use scram::scram_hash_len;
Modules§
- ca
- Pure X.509 CA + leaf-cert generation for the operator’s clients-CA bootstrap. Reusable by inter-broker mTLS and cert hot-reload tests.
- delegation_
token - KIP-48 delegation token primitives — HMAC and secret-key wrapper that keeps the bytes out of Debug.
- gssapi
- SASL/GSSAPI (Kerberos) support. See SASL/GSSAPI Kerberos support.
- scram
- SCRAM (RFC 5802) — supports SHA-256 and SHA-512.
Structs§
- Auth
Outcome - Outcome of an OAUTHBEARER validation: the authenticated principal plus the
token’s expiry. The expiry populates
SaslAuthenticateResponse.session_lifetime_msand what the dispatch loop uses to schedule per-connection re-auth deadlines (KIP-368). - Client
Initial Response - Parsed RFC 7628 client initial response.
- Dynamic
Server Config - Atomically swappable wrapper around a
rustls::ServerConfig. Cheap to clone (oneArcbump); cheap to read (lock-free); the only expensive operation isreload_from, which re-parses cert files. - Introspection
Validator - RFC 7662 opaque-token introspection validator. Calls the introspection endpoint per token (no caching — RFC 7662 §4 discourages caching without explicit lifetime info; SASL is once per connection so the cost is acceptable). Optionally calls OIDC userinfo after a successful introspection and merges the profile claims over the introspection claims.
- Jwks
- A parsed JWK key set. Lookups are by
kid; keys without akidare stored under the empty string and used when a token header omitskidand the set holds exactly one key. - Jwks
Handle - A cheaply-clonable, atomically-swappable holder for the live
Jwks. - Kafka
Principal - KIP-48: Kafka wire-level principal — the
(principalType, name)pair carried in delegation-token records, ACL entries, andKafkaPrincipal-shaped fields across the Kafka protocol. Distinct fromPrincipalwhich models the runtime session identity (auth method + OAuth groups). Format-stable:Display/FromStrround-trip the canonicalType:Nameform. - Principal
- Signed
JwsValidator - Validates a signed JWS bearer token (
RS256/ES256) against a JWKS key set fetched from the identity provider, then checks the standard JWT claims and derives the connection principal. - TlsConfig
- Unsecured
JwsValidator - Validates an unsecured JWS bearer token (
alg: none) and derives the connection principal. Mirrors Kafka’sOAuthBearerUnsecuredValidatorCallbackHandler.
Enums§
- Auth
Error - Auth
Method - How a
Principalwas authenticated. A strict superset ofSaslMechanismthat also covers mTLS client-cert authentication and the implicit ANONYMOUS path on PLAINTEXT / SSL-no-mTLS listeners. - Client
Auth Mode - Whether the server requests and verifies a client certificate during the TLS handshake (RFC 5246 §7.4.6 — Kafka’s mTLS path).
- Introspection
Error - Transport-layer failures surfaced by
IntrospectionClient. The validator maps these ontoAuthError::IntrospectionTransportfor the SASL handler. - Jwks
Trust Error - Listener
Protocol - OAuth
Bearer Validator - The broker’s configured OAUTHBEARER token validator: the development-only unsecured-JWS path, production signed-JWT validation against a JWKS endpoint, or RFC 7662 opaque-token introspection. Defaults to unsecured.
- Sasl
Mechanism - TlsError
Traits§
- Introspection
Client - HTTP transport contract for RFC 7662 introspection + OIDC userinfo.
Lives in this crate to keep
crates/securityas the validator surface; the concrete reqwest-backed impl lives incrates/broker(oauth_introspection.rs) so this crate stays I/O-free.
Functions§
- build_
client_ config_ from_ pem - Read a PEM bundle of one or more CA certificates and produce a
rustls::ClientConfigthat trusts exactly those certificates. The returned config has no client auth (the broker does not present a client cert when fetching the JWKS endpoint). - extract_
principal_ from_ cert - Parse
cert_derand return the Subject DN in RFC 2253 format. ReturnsNoneif the bytes don’t parse as a valid X.509 cert. - invalid_
token_ json - The RFC 7628 server error response body for a rejected token. The JVM
OAuthBearerSaslClienttreats any non-empty first server message as an error and replies with a single\x01kvsep, after which the broker completes the failure handshake. - parse_
client_ initial_ response - Parse the SASL/OAUTHBEARER client initial response (RFC 7628 §3.1).
- verify_
plain - Verifies a SASL/PLAIN auth attempt against a static credential map.