tower_github_webhook/
layer.rs

1use crate::ValidateGitHubWebhook;
2use tower_layer::Layer;
3
4/// Layer that applies the [ValidateGitHubWebhook] middleware which authorizes all requests using
5/// the `X-Hub-Signature-256` header.
6#[derive(Clone)]
7pub struct ValidateGitHubWebhookLayer<Secret> {
8    webhook_secret: Secret,
9}
10
11impl<Secret> ValidateGitHubWebhookLayer<Secret> {
12    /// Authorize requests using the `X-Hub-Signature-256` header. If the signature specified in
13    /// that header is not signed using the `webhook_secret` secret, the request will fail.
14    ///
15    /// The `webhook_secret` parameter can be any type that implements `AsRef<[u8]>` such as
16    /// `String`. However, using `secrecy::SecretString` is recommended to prevent the secret from
17    /// being printed in any logs.
18    pub fn new(webhook_secret: Secret) -> Self {
19        Self { webhook_secret }
20    }
21}
22
23impl<S, Secret> Layer<S> for ValidateGitHubWebhookLayer<Secret>
24where
25    Secret: AsRef<[u8]> + Clone,
26{
27    type Service = ValidateGitHubWebhook<S>;
28
29    fn layer(&self, inner: S) -> Self::Service {
30        ValidateGitHubWebhook::new(self.webhook_secret.clone(), inner)
31    }
32}