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
|
use crate::web_client::authentication::{IsReadOnly, SessionTokenHash};
use crate::web_client::types::{AppState, CreateClientIdResponse, LoginRequest, LoginResponse};
use crate::web_client::utils::{get_mime_type, parse_cookies};
use axum::{
extract::{Path as AxumPath, Request, State},
http::{header, StatusCode},
response::{Html, IntoResponse},
Json,
};
use axum_extra::extract::cookie::{Cookie, SameSite};
use include_dir;
use uuid::Uuid;
use zellij_utils::{consts::VERSION, web_authentication_tokens::create_session_token};
fn html_escape(s: &str) -> String {
s.replace('&', "&")
.replace('<', "<")
.replace('>', ">")
.replace('"', """)
.replace('\'', "'")
}
const WEB_CLIENT_PAGE: &str = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/",
"assets/index.html"
));
const ASSETS_DIR: include_dir::Dir<'_> = include_dir::include_dir!("$CARGO_MANIFEST_DIR/assets");
pub async fn serve_html(State(state): State<AppState>, request: Request) -> Html<String> {
let cookies = parse_cookies(&request);
let is_authenticated = cookies.get("session_token").is_some();
let auth_value = if is_authenticated { "true" } else { "false" };
let base_url = html_escape(
&state
.config
.lock()
.unwrap()
.web_client
.base_url
.clone()
.unwrap_or("/".to_string()),
);
let html = Html(
WEB_CLIENT_PAGE
.replace("IS_AUTHENTICATED", &format!("{}", auth_value))
.replace("BASE_URL", &base_url),
);
html
}
pub async fn login_handler(
State(state): State<AppState>,
Json(login_request): Json<LoginRequest>,
) -> impl IntoResponse {
match create_session_token(
&login_request.auth_token,
login_request.remember_me.unwrap_or(false),
) {
Ok(session_token) => {
let is_https = state.is_https;
let cookie = if login_request.remember_me.unwrap_or(false) {
// Persistent cookie for remember_me
Cookie::build(("session_token", session_token))
.http_only(true)
.secure(is_https)
.same_site(SameSite::Strict)
.path("/")
.max_age(time::Duration::weeks(4))
.build()
} else {
// Session cookie - NO max_age means it expires when browser closes/refreshes
Cookie::build(("session_token", session_token))
.http_only(true)
.secure(is_https)
.same_site(SameSite::Strict)
.path("/")
.build()
};
let mut response = Json(LoginResponse {
success: true,
message: "Login successful".to_string(),
})
.into_response();
if let Ok(cookie_header) = axum::http::HeaderValue::from_str(&cookie.to_string()) {
response.headers_mut().insert("set-cookie", cookie_header);
}
response
},
Err(_) => (
StatusCode::UNAUTHORIZED,
Json(LoginResponse {
success: false,
message: "Invalid authentication token".to_string(),
}),
)
.into_response(),
}
}
pub async fn create_new_client(
State(state): State<AppState>,
request: axum::extract::Request,
) -> Result<Json<CreateClientIdResponse>, (StatusCode, impl IntoResponse)> {
// Extract is_read_only from request extensions (set by auth middleware)
let is_read_only = request
.extensions()
.get::<IsReadOnly>()
.copied()
.unwrap_or(IsReadOnly(true))
.0;
let session_token_hash = request
.extensions()
.get::<SessionTokenHash>()
.cloned()
.ok_or((
StatusCode::INTERNAL_SERVER_ERROR,
Json("Missing session info".to_string()),
))?;
let web_client_id = String::from(Uuid::new_v4());
let os_input = state
.client_os_api_factory
.create_client_os_api()
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, Json(e.to_string())))?;
state.connection_table.lock().unwrap().add_new_client(
web_client_id.to_owned(),
os_input,
is_read_only,
session_token_hash.0,
);
Ok(Json(CreateClientIdResponse {
web_client_id,
is_read_only,
}))
}
pub async fn get_static_asset(AxumPath(path): AxumPath<String>) -> impl IntoResponse {
let path = path.trim_start_matches('/');
match ASSETS_DIR.get_file(path) {
None => (
[(header::CONTENT_TYPE, "text/html")],
"Not Found".as_bytes(),
),
Some(file) => {
let ext = file.path().extension().and_then(|ext| ext.to_str());
let mime_type = get_mime_type(ext);
([(header::CONTENT_TYPE, mime_type)], file.contents())
},
}
}
pub async fn version_handler() -> &'static str {
VERSION
}
|