summaryrefslogtreecommitdiff
path: root/exes/webhook/src/handler/handler.rs
blob: b2ef44cf4f04806ee49285e60094182552b5b9df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use super::error::WebhookError;
use super::signature::validate_signature;
use crate::config::Config;
use ed25519_dalek::PublicKey;
use hyper::{
    body::{to_bytes, Bytes},
    service::Service,
    Body, Method, Request, Response, StatusCode,
};
use serde::{Deserialize, Serialize};
use shared::nats_crate::Client;
use shared::{
    log::{debug, error},
    payloads::{CachePayload, DispatchEventTagged, Tracing},
};
use std::{
    future::Future,
    pin::Pin,
    str::from_utf8,
    sync::Arc,
    task::{Context, Poll},
};
use twilight_model::gateway::event::{DispatchEvent};
use twilight_model::{
    application::interaction::{Interaction, InteractionType},
    gateway::payload::incoming::InteractionCreate,
};

/// Hyper service used to handle the discord webhooks
#[derive(Clone)]
pub struct HandlerService {
    pub config: Arc<Config>,
    pub nats: Arc<Client>,
    pub public_key: Arc<PublicKey>,
}

impl HandlerService {
    async fn check_request(&self, req: Request<Body>) -> Result<Bytes, WebhookError> {
        if req.method() == Method::POST {
            let signature = if let Some(sig) = req.headers().get("X-Signature-Ed25519") {
                sig.to_owned()
            } else {
                return Err(WebhookError::new(
                    StatusCode::BAD_REQUEST,
                    "missing signature header",
                ));
            };

            let timestamp = if let Some(timestamp) = req.headers().get("X-Signature-Timestamp") {
                timestamp.to_owned()
            } else {
                return Err(WebhookError::new(
                    StatusCode::BAD_REQUEST,
                    "missing timestamp header",
                ));
            };
            let data = to_bytes(req.into_body()).await?;

            if validate_signature(
                &self.public_key,
                &[timestamp.as_bytes().to_vec(), data.to_vec()].concat(),
                signature.to_str()?,
            ) {
                Ok(data)
            } else {
                Err(WebhookError::new(
                    StatusCode::UNAUTHORIZED,
                    "invalid signature",
                ))
            }
        } else {
            Err(WebhookError::new(StatusCode::NOT_FOUND, "not found"))
        }
    }

    async fn process_request(
        &mut self,
        req: Request<Body>,
    ) -> Result<Response<Body>, WebhookError> {
        match self.check_request(req).await {
            Ok(data) => {
                let utf8 = from_utf8(&data);
                match utf8 {
                    Ok(data) => match serde_json::from_str::<Interaction>(data) {
                        Ok(value) => {
                            match value.kind {
                                InteractionType::Ping => Ok(Response::builder()
                                    .header("Content-Type", "application/json")
                                    .body(serde_json::to_string(&Ping { t: 1 }).unwrap().into())
                                    .unwrap()),
                                _ => {
                                    debug!("calling nats");
                                    // this should hopefully not fail ?

                                    let data = CachePayload {
                                        tracing: Tracing {
                                            node_id: "".to_string(),
                                            span: None,
                                        },
                                        data: DispatchEventTagged {
                                            data: DispatchEvent::InteractionCreate(Box::new(
                                                InteractionCreate(value),
                                            )),
                                        },
                                    };

                                    let payload = serde_json::to_string(&data).unwrap();

                                    match self.nats.request(
                                        "nova.cache.dispatch.INTERACTION_CREATE".to_string(),
                                        Bytes::from(payload),
                                    ).await {
                                        Ok(response) => Ok(Response::builder()
                                            .header("Content-Type", "application/json")
                                            .body(Body::from(response.reply.unwrap()))
                                            .unwrap()),

                                        Err(error) => {
                                            error!("failed to request nats: {}", error);
                                            Err(WebhookError::new(
                                                StatusCode::INTERNAL_SERVER_ERROR,
                                                "failed to request nats",
                                            ))
                                        }
                                    }
                                }
                            }
                        }

                        Err(error) => {
                            error!("invalid json body: {}", error);
                            Err(WebhookError::new(
                                StatusCode::BAD_REQUEST,
                                "invalid json body",
                            ))
                        }
                    },

                    Err(_) => Err(WebhookError::new(StatusCode::BAD_REQUEST, "not utf-8 body")),
                }
            }
            Err(error) => Err(error),
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Ping {
    #[serde(rename = "type")]
    t: i32,
}

/// Implementation of the service
impl Service<Request<Body>> for HandlerService {
    type Response = Response<Body>;
    type Error = hyper::Error;
    type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;

    fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: Request<Body>) -> Self::Future {
        let mut clone = self.clone();
        Box::pin(async move {
            let response = clone.process_request(req).await;

            match response {
                Ok(r) => Ok(r),
                Err(e) => Ok(e.into()),
            }
        })
    }
}