-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
collaboration.rs
59 lines (52 loc) 路 1.42 KB
/
collaboration.rs
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
use super::*;
use crate::infrastructure::auth::get_claim_from_token;
use axum::{
extract::{ws::WebSocketUpgrade, Path},
http::StatusCode,
response::Response,
};
use futures_util::FutureExt;
use jwst_rpc::{axum_socket_connector, handle_connector};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
#[derive(Serialize)]
pub struct WebSocketAuthentication {
protocol: String,
}
pub fn make_ws_route() -> Router {
Router::new().route("/:id", get(ws_handler))
}
#[derive(Deserialize)]
struct Param {
token: String,
}
#[instrument(skip(ctx, token, ws))]
async fn ws_handler(
Extension(ctx): Extension<Arc<Context>>,
Path(workspace): Path<String>,
Query(Param { token }): Query<Param>,
ws: WebSocketUpgrade,
) -> Response {
let user = match get_claim_from_token(&token, &ctx.key.jwt_decode) {
Some(claims) => Some(claims.user.id),
None => None,
};
let Some(user_id) = user else {
return StatusCode::UNAUTHORIZED.into_response();
};
if !ctx
.db
.can_read_workspace(user_id.clone(), workspace.clone())
.await
.ok()
.unwrap_or(false)
{
return StatusCode::UNAUTHORIZED.into_response();
}
ws.protocols(["AFFiNE"]).on_upgrade(move |socket| {
handle_connector(ctx.clone(), workspace.clone(), user_id, move || {
axum_socket_connector(socket, &workspace)
})
.map(|_| ())
})
}