about summary refs log tree commit diff
path: root/src/lib.rs
blob: f1a563e8b049c25bc386da2199fbf761d502f789 (plain) (blame)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#![forbid(unsafe_code)]
#![warn(clippy::todo)]

use std::sync::Arc;

use axum::{extract::{FromRef, FromRequestParts}, response::IntoResponse};
use axum_extra::extract::{cookie::Key, SignedCookieJar};
use database::{FileStorage, PostgresStorage, Storage};
use indieauth::backend::{AuthBackend, FileBackend as FileAuthBackend};
use kittybox_util::queue::JobQueue;
use media::storage::{MediaStore, file::FileStore as FileMediaStore};
use tokio::{sync::{Mutex, RwLock}, task::JoinSet};
use webmentions::queue::PostgresJobQueue;

/// Database abstraction layer for Kittybox, allowing the CMS to work with any kind of database.
pub mod database;
pub mod frontend;
pub mod media;
pub mod micropub;
pub mod indieauth;
pub mod webmentions;
pub mod login;
//pub mod admin;

const OAUTH2_SOFTWARE_ID: &str = "6f2eee84-c22c-4c9e-b900-10d4e97273c8";

#[derive(Clone)]
pub struct AppState<A, S, M, Q>
where
A: AuthBackend + Sized + 'static,
S: Storage + Sized + 'static,
M: MediaStore + Sized + 'static,
Q: JobQueue<webmentions::Webmention> + Sized
{
    pub auth_backend: A,
    pub storage: S,
    pub media_store: M,
    pub job_queue: Q,
    pub http: reqwest::Client,
    pub background_jobs: Arc<Mutex<JoinSet<()>>>,
    pub cookie_key: Key,
    pub session_store: SessionStore
}

pub type SessionStore = Arc<RwLock<std::collections::HashMap<uuid::Uuid, Session>>>;

#[derive(Debug, Clone)]
#[repr(transparent)]
pub struct Session(kittybox_indieauth::ProfileUrl);

impl std::ops::Deref for Session {
    type Target = kittybox_indieauth::ProfileUrl;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

pub struct NoSessionError;
impl axum::response::IntoResponse for NoSessionError {
    fn into_response(self) -> axum::response::Response {
        // TODO: prettier error message
        (axum::http::StatusCode::UNAUTHORIZED, "You are not logged in, but this page requires a session.").into_response()
    }
}

#[async_trait::async_trait]
impl<S> FromRequestParts<S> for Session
where
    SessionStore: FromRef<S>,
    Key: FromRef<S>,
    S: Send + Sync,
{
    type Rejection = NoSessionError;

    async fn from_request_parts(parts: &mut axum::http::request::Parts, state: &S) ->  Result<Self, Self::Rejection> {
        let jar = SignedCookieJar::<Key>::from_request_parts(parts, state).await.unwrap();
        let session_store = SessionStore::from_ref(state).read_owned().await;

        tracing::debug!("Cookie jar: {:#?}", jar);
        let cookie = match jar.get("session_id") {
            Some(cookie) => {
                tracing::debug!("Session ID cookie: {}", cookie);
                cookie
            },
            None => { return Err(NoSessionError) }
        };

        session_store.get(
            &dbg!(cookie.value_trimmed())
                .parse()
                .map_err(|err| {
                    tracing::error!("Error parsing cookie: {}", err);
                    NoSessionError
                })?
        ).cloned().ok_or(NoSessionError)
    }
}

// This is really regrettable, but I can't write:
//
// ```compile-error
// impl <A, S, M> FromRef<AppState<A, S, M>> for A
// where A: AuthBackend, S: Storage, M: MediaStore {
//     fn from_ref(input: &AppState<A, S, M>) -> A {
//         input.auth_backend.clone()
//     }
// }
// ```
//
// ...because of the orphan rule.
//
// I wonder if this would stifle external implementations. I think it
// shouldn't, because my AppState type is generic, and since the
// target type is local, the orphan rule will not kick in. You just
// have to repeat this magic invocation.

impl<S, M, Q> FromRef<AppState<Self, S, M, Q>> for FileAuthBackend
where S: Storage, M: MediaStore, Q: JobQueue<webmentions::Webmention>
{
    fn from_ref(input: &AppState<Self, S, M, Q>) -> Self {
        input.auth_backend.clone()
    }
}

impl<A, M, Q> FromRef<AppState<A, Self, M, Q>> for PostgresStorage
where A: AuthBackend, M: MediaStore, Q: JobQueue<webmentions::Webmention>
{
    fn from_ref(input: &AppState<A, Self, M, Q>) -> Self {
        input.storage.clone()
    }
}

impl<A, M, Q> FromRef<AppState<A, Self, M, Q>> for FileStorage
where A: AuthBackend, M: MediaStore, Q: JobQueue<webmentions::Webmention>
{
    fn from_ref(input: &AppState<A, Self, M, Q>) -> Self {
        input.storage.clone()
    }
}

impl<A, S, Q> FromRef<AppState<A, S, Self, Q>> for FileMediaStore
// where A: AuthBackend, S: Storage
where A: AuthBackend, S: Storage, Q: JobQueue<webmentions::Webmention>
{
    fn from_ref(input: &AppState<A, S, Self, Q>) -> Self {
        input.media_store.clone()
    }
}

impl<A, S, M, Q> FromRef<AppState<A, S, M, Q>> for Key
where A: AuthBackend, S: Storage, M: MediaStore, Q: JobQueue<webmentions::Webmention>
{
    fn from_ref(input: &AppState<A, S, M, Q>) -> Self {
        input.cookie_key.clone()
    }
}

impl<A, S, M, Q> FromRef<AppState<A, S, M, Q>> for reqwest::Client
where A: AuthBackend, S: Storage, M: MediaStore, Q: JobQueue<webmentions::Webmention>
{
    fn from_ref(input: &AppState<A, S, M, Q>) -> Self {
        input.http.clone()
    }
}

impl<A, S, M, Q> FromRef<AppState<A, S, M, Q>> for Arc<Mutex<JoinSet<()>>>
where A: AuthBackend, S: Storage, M: MediaStore, Q: JobQueue<webmentions::Webmention>
{
    fn from_ref(input: &AppState<A, S, M, Q>) -> Self {
        input.background_jobs.clone()
    }
}

#[cfg(feature = "sqlx")]
impl<A, S, M> FromRef<AppState<A, S, M, Self>> for PostgresJobQueue<webmentions::Webmention>
where A: AuthBackend, S: Storage, M: MediaStore
{
    fn from_ref(input: &AppState<A, S, M, Self>) -> Self {
        input.job_queue.clone()
    }
}

impl<A, S, M, Q> FromRef<AppState<A, S, M, Q>> for SessionStore
where A: AuthBackend, S: Storage, M: MediaStore, Q: JobQueue<webmentions::Webmention>
{
    fn from_ref(input: &AppState<A, S, M, Q>) -> Self {
        input.session_store.clone()
    }
}

pub mod companion {
    use std::{collections::HashMap, sync::Arc};
    use axum::{
        extract::{Extension, Path},
        response::{IntoResponse, Response}
    };

    #[derive(Debug, Clone, Copy)]
    struct Resource {
        data: &'static [u8],
        mime: &'static str
    }

    impl IntoResponse for &Resource {
        fn into_response(self) -> Response {
            (axum::http::StatusCode::OK,
             [("Content-Type", self.mime)],
             self.data).into_response()
        }
    }

    // TODO replace with the "phf" crate someday
    type ResourceTable = Arc<HashMap<&'static str, Resource>>;

    #[tracing::instrument]
    async fn map_to_static(
        Path(name): Path<String>,
        Extension(resources): Extension<ResourceTable>
    ) -> Response {
        tracing::debug!("Searching for {} in the resource table...", name);
        match resources.get(name.as_str()) {
            Some(res) => res.into_response(),
            None => {
                #[cfg(debug_assertions)] tracing::error!("Not found");

                (axum::http::StatusCode::NOT_FOUND,
                 [("Content-Type", "text/plain")],
                 "Not found. Sorry.".as_bytes()).into_response()
            }
        }
    }

    pub fn router<St: Clone + Send + Sync + 'static>() -> axum::Router<St> {
        let resources: ResourceTable = {
            let mut map = HashMap::new();

            macro_rules! register_resource {
                ($map:ident, $prefix:expr, ($filename:literal, $mime:literal)) => {{
                    $map.insert($filename, Resource {
                        data: include_bytes!(concat!($prefix, $filename)),
                        mime: $mime
                    })
                }};
                ($map:ident, $prefix:expr, ($filename:literal, $mime:literal), $( ($f:literal, $m:literal) ),+) => {{
                    register_resource!($map, $prefix, ($filename, $mime));
                    register_resource!($map, $prefix, $(($f, $m)),+);
                }};
            }

            register_resource! {
                map,
                concat!(env!("OUT_DIR"), "/", "companion", "/"),
                ("index.html", "text/html; charset=\"utf-8\""),
                ("main.js", "text/javascript"),
                ("micropub_api.js", "text/javascript"),
                ("indieauth.js", "text/javascript"),
                ("base64.js", "text/javascript"),
                ("style.css", "text/css")
            };

            Arc::new(map)
        };

        axum::Router::new()
            .route(
                "/:filename",
                axum::routing::get(map_to_static)
                    .layer(Extension(resources))
            )
    }
}

async fn teapot_route() -> impl axum::response::IntoResponse {
    use axum::http::{header, StatusCode};
    (StatusCode::IM_A_TEAPOT, [(header::CONTENT_TYPE, "text/plain")], "Sorry, can't brew coffee yet!")
}

async fn health_check<D>(
    axum::extract::State(data): axum::extract::State<D>,
) -> impl axum::response::IntoResponse
where
    D: crate::database::Storage
{
    (axum::http::StatusCode::OK, std::borrow::Cow::Borrowed("OK"))
}

pub async fn compose_kittybox<St, A, S, M, Q>() -> axum::Router<St>
where
A: AuthBackend + 'static + FromRef<St>,
S: Storage + 'static + FromRef<St>,
M: MediaStore + 'static + FromRef<St>,
Q: kittybox_util::queue::JobQueue<crate::webmentions::Webmention> + FromRef<St>,
reqwest::Client: FromRef<St>,
Arc<Mutex<JoinSet<()>>>: FromRef<St>,
crate::SessionStore: FromRef<St>,
axum_extra::extract::cookie::Key: FromRef<St>,
St: Clone + Send + Sync + 'static
{
    use axum::routing::get;
    axum::Router::new()
        .route("/", get(crate::frontend::homepage::<S>))
        .fallback(get(crate::frontend::catchall::<S>))
        .route("/.kittybox/micropub", crate::micropub::router::<A, S, St>())
        .route("/.kittybox/onboarding", crate::frontend::onboarding::router::<St, S>())
        .nest("/.kittybox/media", crate::media::router::<St, A, M>())
        .merge(crate::indieauth::router::<St, A, S>())
        .merge(crate::webmentions::router::<St, Q>())
        .route("/.kittybox/health", get(health_check::<S>))
        .nest("/.kittybox/login", crate::login::router::<St, S>())
        .route(
            "/.kittybox/static/:path",
            axum::routing::get(crate::frontend::statics)
        )
        .route("/.kittybox/coffee", get(teapot_route))
        .nest("/.kittybox/micropub/client", crate::companion::router::<St>())
        .layer(tower_http::trace::TraceLayer::new_for_http())
        .layer(tower_http::catch_panic::CatchPanicLayer::new())
        .layer(tower_http::sensitive_headers::SetSensitiveHeadersLayer::new([
            axum::http::header::AUTHORIZATION,
            axum::http::header::COOKIE,
            axum::http::header::SET_COOKIE,
        ]))
}