@@ -8,8 +8,11 @@ use std::time::{SystemTime, UNIX_EPOCH};
88#[ derive( Debug , thiserror:: Error ) ]
99pub enum WebhookError {
1010 /// Invalid webhook signature or signature verification failed
11- #[ error( "invalid webhook signature: {0}" ) ]
12- InvalidSignature ( String ) ,
11+ #[ error( "invalid webhook signature" ) ]
12+ InvalidSignature ,
13+ /// Invalid input (timestamp or secret key)
14+ #[ error( "invalid input " ) ]
15+ Invalid ( String ) ,
1316 /// Failed to deserialize webhook payload
1417 #[ error( "failed to deserialize webhook payload: error:{0} content:{1}" ) ]
1518 Deserialization ( serde_json:: Error , String ) ,
@@ -92,21 +95,21 @@ impl Webhooks {
9295 // Validate timestamp to prevent replay attacks
9396 let timestamp_seconds = timestamp
9497 . parse :: < i64 > ( )
95- . map_err ( |_| WebhookError :: InvalidSignature ( "invalid timestamp format" . to_string ( ) ) ) ?;
98+ . map_err ( |_| WebhookError :: Invalid ( "invalid timestamp format" . to_string ( ) ) ) ?;
9699
97100 let now = SystemTime :: now ( )
98101 . duration_since ( UNIX_EPOCH )
99102 . unwrap ( )
100103 . as_secs ( ) as i64 ;
101104
102105 if now - timestamp_seconds > tolerance_seconds {
103- return Err ( WebhookError :: InvalidSignature (
106+ return Err ( WebhookError :: Invalid (
104107 "webhook timestamp is too old" . to_string ( ) ,
105108 ) ) ;
106109 }
107110
108111 if timestamp_seconds > now + tolerance_seconds {
109- return Err ( WebhookError :: InvalidSignature (
112+ return Err ( WebhookError :: Invalid (
110113 "webhook timestamp is too new" . to_string ( ) ,
111114 ) ) ;
112115 }
@@ -119,12 +122,12 @@ impl Webhooks {
119122
120123 // Decode the secret from base64 (Standard Webhooks uses base64-encoded secrets)
121124 let secret_bytes = BASE64 . decode ( secret_key) . map_err ( |_| {
122- WebhookError :: InvalidSignature ( "failed to decode secret from base64" . to_string ( ) )
125+ WebhookError :: Invalid ( "failed to decode secret from base64" . to_string ( ) )
123126 } ) ?;
124127
125128 // Compute HMAC-SHA256
126129 let mut mac = HmacSha256 :: new_from_slice ( & secret_bytes)
127- . map_err ( |_| WebhookError :: InvalidSignature ( "invalid secret key length" . to_string ( ) ) ) ?;
130+ . map_err ( |_| WebhookError :: Invalid ( "invalid secret key length" . to_string ( ) ) ) ?;
128131 mac. update ( signed_payload. as_bytes ( ) ) ;
129132
130133 // Get the expected signature in base64
@@ -156,9 +159,7 @@ impl Webhooks {
156159 }
157160 }
158161
159- Err ( WebhookError :: InvalidSignature (
160- "signature mismatch" . to_string ( ) ,
161- ) )
162+ Err ( WebhookError :: InvalidSignature )
162163 }
163164}
164165
0 commit comments