pub struct SignatureValidator { /* private fields */ }Expand description
Validates GitHub webhook signatures using HMAC-SHA256.
This validator ensures webhook payloads are authentic by verifying
the X-Hub-Signature-256 header against the payload using the
webhook secret.
§Security
- Uses constant-time comparison to prevent timing attacks
- Never logs secrets or signature values
- Validates signature format before HMAC computation
- Completes validation in under 100ms
§Examples
use github_bot_sdk::webhook::SignatureValidator;
use github_bot_sdk::auth::SecretProvider;
use std::sync::Arc;
let validator = SignatureValidator::new(secret_provider);
let payload = b"{\"action\":\"opened\",\"number\":1}";
let signature = "sha256=a1b2c3d4..."; // From X-Hub-Signature-256 header
if validator.validate(payload, signature).await? {
println!("Valid webhook");
} else {
println!("Invalid signature - rejecting webhook");
}Implementations§
Source§impl SignatureValidator
impl SignatureValidator
Sourcepub fn new(secrets: Arc<dyn SecretProvider>) -> Self
pub fn new(secrets: Arc<dyn SecretProvider>) -> Self
Sourcepub async fn validate(
&self,
payload: &[u8],
signature: &str,
) -> Result<bool, ValidationError>
pub async fn validate( &self, payload: &[u8], signature: &str, ) -> Result<bool, ValidationError>
Validate a webhook signature.
Verifies that the signature matches the HMAC-SHA256 of the payload using the webhook secret. Uses constant-time comparison to prevent timing attacks.
§Arguments
payload- The raw webhook payload bytessignature- The signature from X-Hub-Signature-256 header (format: “sha256=<hex>”)
§Returns
Ok(true)- Signature is validOk(false)- Signature is invalid (tampered payload or wrong secret)Err- Validation error (malformed signature, secret retrieval failure)
§Examples
let payload = b"{\"action\":\"opened\"}";
let signature = "sha256=5c4a...";
match validator.validate(payload, signature).await {
Ok(true) => println!("Valid webhook"),
Ok(false) => println!("Invalid signature"),
Err(e) => println!("Validation error: {}", e),
}Trait Implementations§
Source§impl Clone for SignatureValidator
impl Clone for SignatureValidator
Source§fn clone(&self) -> SignatureValidator
fn clone(&self) -> SignatureValidator
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for SignatureValidator
impl !RefUnwindSafe for SignatureValidator
impl Send for SignatureValidator
impl Sync for SignatureValidator
impl Unpin for SignatureValidator
impl UnsafeUnpin for SignatureValidator
impl !UnwindSafe for SignatureValidator
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more