sbol-db-server 0.1.8

HTTP server for sbol-db.
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
//! Classic SynBioHub auth routes: `/login`, `/logout`, `/register`,
//! `/profile`, `/resetPassword`, `/setNewPassword`.
//!
//! These reproduce classic SynBioHub's request and response shapes so its
//! clients (and pySBOL2's `PartShop`) authenticate unchanged. Bodies arrive as
//! `application/x-www-form-urlencoded` (classic) or JSON (API clients); the
//! response is Accept-negotiated, mirroring classic's `res.plainOrHtml`: an API
//! client (no `text/html`) gets a machine-readable body, a browser gets a
//! session cookie and a redirect.

use axum::body::{Body, Bytes};
use axum::extract::{Extension, State};
use axum::http::header::{ACCEPT, CONTENT_TYPE, LOCATION, SET_COOKIE};
use axum::http::{HeaderMap, StatusCode};
use axum::response::{IntoResponse, Response};
use axum::Json;
use chrono::{DateTime, SecondsFormat, Utc};
use sbol_db_app::{PasswordReset, Registration};
use sbol_db_core::{DomainError, User};
use sbol_db_storage::NewJob;
use serde::de::DeserializeOwned;
use serde::Deserialize;
use serde_json::json;

use super::CurrentUser;
use crate::error::ApiError;
use crate::session::{login_cookie, logout_cookie, token_from_cookie};
use crate::AppState;

#[derive(Deserialize, Default)]
struct LoginBody {
    /// Matches either the account email or its username, as classic's login
    /// form does.
    #[serde(default)]
    email: Option<String>,
    #[serde(default)]
    password: Option<String>,
    #[serde(default)]
    next: Option<String>,
}

/// `POST /login` — verify credentials and mint an API token. API clients get
/// the bare token as a `text/plain` body (classic's `res.plainOrHtml`
/// message); browsers get a session cookie and a 302 to `next`.
pub async fn login(
    State(state): State<AppState>,
    headers: HeaderMap,
    body: Bytes,
) -> Result<Response, ApiError> {
    let form: LoginBody = parse_body(&headers, &body)?;
    let (Some(email), Some(password)) = (form.email, form.password) else {
        return Err(ApiError::BadRequest(
            "email and password are required".to_owned(),
        ));
    };
    let user = match state
        .app
        .auth
        .authenticate(&email, &password, &state.config.password_salt)
        .await
    {
        Ok(user) => user,
        Err(DomainError::Validation(_)) => return Ok(unauthorized("invalid credentials")),
        Err(err) => return Err(err.into()),
    };
    let token = state.app.auth.issue_token(user.id).await?;

    if wants_html(&headers) {
        let next = form
            .next
            .filter(|n| !n.is_empty())
            .unwrap_or_else(|| "/".to_owned());
        Ok(browser_login(
            &token,
            &next,
            state.config.session_cookie_secure,
        ))
    } else {
        Ok(([(CONTENT_TYPE, "text/plain")], token).into_response())
    }
}

/// `POST /logout` — revoke the presented token (classic deletes it from its
/// in-memory map). Browsers additionally get their session cookie cleared and a
/// redirect home.
pub async fn logout(
    State(state): State<AppState>,
    headers: HeaderMap,
) -> Result<Response, ApiError> {
    if let Some(token) = headers
        .get("x-authorization")
        .and_then(|v| v.to_str().ok())
        .filter(|t| !t.is_empty())
        .or_else(|| token_from_cookie(&headers))
    {
        state.app.auth.revoke_token(token).await?;
    }
    if wants_html(&headers) {
        Ok(clear_session_redirect(
            "/",
            state.config.session_cookie_secure,
        ))
    } else {
        Ok(StatusCode::OK.into_response())
    }
}

#[derive(Deserialize, Default)]
struct RegisterBody {
    #[serde(default)]
    name: Option<String>,
    #[serde(default)]
    username: Option<String>,
    #[serde(default)]
    email: Option<String>,
    #[serde(default)]
    affiliation: Option<String>,
    /// Accept classic's `password1`/`password2` pair as well as a plain
    /// `password` field.
    #[serde(default)]
    password: Option<String>,
    #[serde(default)]
    password1: Option<String>,
    #[serde(default)]
    password2: Option<String>,
    #[serde(default)]
    next: Option<String>,
}

/// `POST /register` — create an account when public signup is enabled,
/// otherwise `403`. New accounts are members, not admins or curators. API
/// clients get a `text/plain` confirmation; browsers redirect to `next`.
pub async fn register(
    State(state): State<AppState>,
    headers: HeaderMap,
    body: Bytes,
) -> Result<Response, ApiError> {
    if !crate::instance::public_signup_allowed(&state).await? {
        return Ok(forbidden("public signup is disabled"));
    }
    let form: RegisterBody = parse_body(&headers, &body)?;
    let name = required(form.name, "name")?;
    let username = required(form.username, "username")?;
    // Classic validates the username with `validator.isAlphanumeric`: non-empty
    // and ASCII letters/digits only (no underscores, spaces, or punctuation).
    // A drop-in must reject the same inputs with the same 400.
    if username.is_empty() || !username.chars().all(|c| c.is_ascii_alphanumeric()) {
        return Err(ApiError::BadRequest(
            "Please enter a valid username".to_owned(),
        ));
    }
    let email = required(form.email, "email")?;
    let password = form
        .password1
        .or(form.password)
        .filter(|p| !p.is_empty())
        .ok_or_else(|| ApiError::BadRequest("password is required".to_owned()))?;
    if let Some(confirm) = form.password2.filter(|p| !p.is_empty()) {
        if confirm != password {
            return Err(ApiError::BadRequest("passwords do not match".to_owned()));
        }
    }
    let registration = Registration {
        username,
        name,
        email,
        affiliation: form.affiliation.filter(|a| !a.is_empty()),
        password,
        is_admin: false,
        is_curator: false,
        is_member: true,
    };
    state.app.auth.register(registration).await?;

    if wants_html(&headers) {
        let next = form
            .next
            .filter(|n| !n.is_empty())
            .unwrap_or_else(|| "/".to_owned());
        Ok(redirect(&next))
    } else {
        Ok((
            [(CONTENT_TYPE, "text/plain")],
            "User registered successfully",
        )
            .into_response())
    }
}

/// `GET /profile` — the authenticated caller's account as JSON. Anonymous
/// callers get `401`.
pub async fn get_profile(Extension(CurrentUser(user)): Extension<CurrentUser>) -> Response {
    match user {
        Some(user) => Json(profile_json(&user)).into_response(),
        None => unauthorized("authentication required"),
    }
}

#[derive(Deserialize, Default)]
struct ProfileUpdateBody {
    #[serde(default)]
    name: Option<String>,
    #[serde(default)]
    affiliation: Option<String>,
}

/// `POST /profile` — update the caller's display name and affiliation.
/// Anonymous callers get `401`.
pub async fn update_profile(
    State(state): State<AppState>,
    Extension(CurrentUser(user)): Extension<CurrentUser>,
    headers: HeaderMap,
    body: Bytes,
) -> Result<Response, ApiError> {
    let Some(mut user) = user else {
        return Ok(unauthorized("authentication required"));
    };
    let patch: ProfileUpdateBody = parse_body(&headers, &body)?;
    if let Some(name) = patch.name.filter(|n| !n.is_empty()) {
        user.name = name;
    }
    if let Some(affiliation) = patch.affiliation {
        user.affiliation = Some(affiliation).filter(|a| !a.is_empty());
    }
    state.app.users.update_user(&user).await?;

    if wants_html(&headers) {
        Ok(redirect("/profile"))
    } else {
        Ok((
            [(CONTENT_TYPE, "text/plain")],
            "Profile updated successfully",
        )
            .into_response())
    }
}

#[derive(Deserialize, Default)]
struct ResetPasswordBody {
    /// Matches account email or username, as the reset form does.
    #[serde(default)]
    email: Option<String>,
}

/// `POST /resetPassword` — mint a single-use reset link and enqueue a
/// `send_email` job to deliver it (the mail handler is a later phase; the job
/// is a durable no-op until then). The response is identical whether or not the
/// address is registered, so it does not leak account existence.
pub async fn reset_password(
    State(state): State<AppState>,
    headers: HeaderMap,
    body: Bytes,
) -> Result<Response, ApiError> {
    let form: ResetPasswordBody = parse_body(&headers, &body)?;
    let identifier = required(form.email, "email")?;
    if let Some(reset) = state.app.auth.reset_password(&identifier).await? {
        enqueue_reset_email(&state, &reset).await?;
    }
    Ok((
        [(CONTENT_TYPE, "text/plain")],
        "If that account exists, a password reset link has been sent.",
    )
        .into_response())
}

#[derive(Deserialize, Default)]
struct SetNewPasswordBody {
    /// The reset link delivered by `/resetPassword`. Accept the classic field
    /// name alongside the shorter aliases.
    #[serde(default)]
    token: Option<String>,
    #[serde(default)]
    link: Option<String>,
    #[serde(default, rename = "resetPasswordLink")]
    reset_password_link: Option<String>,
    #[serde(default)]
    password: Option<String>,
    #[serde(default)]
    password1: Option<String>,
    #[serde(default)]
    password2: Option<String>,
}

/// `POST /setNewPassword` — consume a reset link and set the new argon2 hash.
/// An unrecognized or already-used link is a `400`.
pub async fn set_new_password(
    State(state): State<AppState>,
    headers: HeaderMap,
    body: Bytes,
) -> Result<Response, ApiError> {
    let form: SetNewPasswordBody = parse_body(&headers, &body)?;
    let link = form
        .token
        .or(form.link)
        .or(form.reset_password_link)
        .filter(|l| !l.is_empty())
        .ok_or_else(|| ApiError::BadRequest("a reset token is required".to_owned()))?;
    let password = form
        .password1
        .or(form.password)
        .filter(|p| !p.is_empty())
        .ok_or_else(|| ApiError::BadRequest("a new password is required".to_owned()))?;
    if let Some(confirm) = form.password2.filter(|p| !p.is_empty()) {
        if confirm != password {
            return Err(ApiError::BadRequest("passwords do not match".to_owned()));
        }
    }
    match state.app.auth.set_new_password(&link, &password).await {
        Ok(_) => Ok((
            [(CONTENT_TYPE, "text/plain")],
            "Password updated successfully.",
        )
            .into_response()),
        Err(DomainError::Validation(message)) => Ok((
            StatusCode::BAD_REQUEST,
            [(CONTENT_TYPE, "text/plain")],
            message,
        )
            .into_response()),
        Err(err) => Err(err.into()),
    }
}

/// Enqueue the `send_email` job carrying a password-reset link. The handler is
/// added in the mail phase; until then the job persists as a no-op record.
async fn enqueue_reset_email(state: &AppState, reset: &PasswordReset) -> Result<(), ApiError> {
    let payload = json!({
        "template": "reset_password",
        "to": reset.email,
        "username": reset.username,
        "reset_link": reset.reset_link,
    });
    state
        .app
        .jobs
        .enqueue(NewJob {
            kind: "send_email".to_owned(),
            payload,
            queue: None,
            priority: None,
            max_attempts: None,
            idempotency_key: None,
            available_at: None,
            parent_job_id: None,
            correlation_id: None,
        })
        .await?;
    Ok(())
}

/// The profile JSON classic returns to an API client, keyed with classic's
/// camelCase field names. `password` and `resetPasswordLink` are always blank,
/// matching classic: the account's credentials never travel in the profile
/// response even when a reset is outstanding. `createdAt`/`updatedAt` are the
/// account's real timestamps in classic's Sequelize wire format (RFC3339 with
/// millisecond precision and a `Z` suffix). `user_external_profiles` is the set
/// of linked external-identity profiles, empty because this instance has no
/// external-auth providers, exactly as classic returns for an unlinked account.
fn profile_json(user: &User) -> serde_json::Value {
    json!({
        "id": user.id,
        "username": user.username,
        "name": user.name,
        "email": user.email,
        "affiliation": user.affiliation.clone().unwrap_or_default(),
        "password": "",
        "graphUri": user.graph_uri,
        "isAdmin": user.is_admin,
        "isCurator": user.is_curator,
        "isMember": user.is_member,
        "resetPasswordLink": "",
        "createdAt": iso8601_millis(&user.created_at),
        "updatedAt": iso8601_millis(&user.updated_at),
        "user_external_profiles": [],
    })
}

/// A UTC timestamp in classic's Sequelize wire format: RFC3339 with millisecond
/// precision and a `Z` zone suffix (`2026-07-17T21:12:27.022Z`).
fn iso8601_millis(at: &DateTime<Utc>) -> String {
    at.to_rfc3339_opts(SecondsFormat::Millis, true)
}

/// Whether the client prefers HTML (a browser), selecting the cookie+redirect
/// variant of an Accept-negotiated route. API clients that omit `text/html` get
/// the machine-readable variant, matching classic's `res.plainOrHtml`.
fn wants_html(headers: &HeaderMap) -> bool {
    headers
        .get(ACCEPT)
        .and_then(|v| v.to_str().ok())
        .map(|accept| {
            accept
                .split(',')
                .any(|media| media.split(';').next().unwrap_or("").trim() == "text/html")
        })
        .unwrap_or(false)
}

/// The lowercased base media type of the request body.
fn content_type(headers: &HeaderMap) -> String {
    headers
        .get(CONTENT_TYPE)
        .and_then(|v| v.to_str().ok())
        .map(|s| {
            s.split(';')
                .next()
                .unwrap_or("")
                .trim()
                .to_ascii_lowercase()
        })
        .unwrap_or_default()
}

/// Deserialize a request body as JSON when the `Content-Type` says so, else as
/// form-encoded. Classic posts `application/x-www-form-urlencoded`; API clients
/// often send JSON.
pub(super) fn parse_body<T: DeserializeOwned>(
    headers: &HeaderMap,
    body: &[u8],
) -> Result<T, ApiError> {
    if content_type(headers) == "application/json" {
        serde_json::from_slice(body)
            .map_err(|e| ApiError::BadRequest(format!("invalid JSON body: {e}")))
    } else {
        serde_urlencoded::from_bytes(body)
            .map_err(|e| ApiError::BadRequest(format!("invalid form body: {e}")))
    }
}

/// A required string field, rejecting an absent or blank value.
fn required(value: Option<String>, field: &str) -> Result<String, ApiError> {
    match value {
        Some(v) if !v.trim().is_empty() => Ok(v),
        _ => Err(ApiError::BadRequest(format!("{field} is required"))),
    }
}

fn unauthorized(message: &str) -> Response {
    (
        StatusCode::UNAUTHORIZED,
        [(CONTENT_TYPE, "text/plain")],
        message.to_owned(),
    )
        .into_response()
}

fn forbidden(message: &str) -> Response {
    (
        StatusCode::FORBIDDEN,
        [(CONTENT_TYPE, "text/plain")],
        message.to_owned(),
    )
        .into_response()
}

/// A 302 redirect with no body.
fn redirect(location: &str) -> Response {
    Response::builder()
        .status(StatusCode::FOUND)
        .header(LOCATION, location)
        .body(Body::empty())
        .expect("valid redirect response")
}

/// A browser login: set the session cookie and 302 to `next`.
fn browser_login(token: &str, next: &str, secure: bool) -> Response {
    Response::builder()
        .status(StatusCode::FOUND)
        .header(SET_COOKIE, login_cookie(token, secure))
        .header(LOCATION, next)
        .body(Body::empty())
        .expect("valid login redirect response")
}

/// A browser logout: expire the session cookie and 302 to `next`.
fn clear_session_redirect(next: &str, secure: bool) -> Response {
    Response::builder()
        .status(StatusCode::FOUND)
        .header(SET_COOKIE, logout_cookie(secure))
        .header(LOCATION, next)
        .body(Body::empty())
        .expect("valid logout redirect response")
}

#[cfg(test)]
mod tests {
    use super::*;

    fn headers_with(name: &str, value: &str) -> HeaderMap {
        let mut headers = HeaderMap::new();
        headers.insert(
            axum::http::HeaderName::from_bytes(name.as_bytes()).unwrap(),
            value.parse().unwrap(),
        );
        headers
    }

    #[test]
    fn wants_html_detects_browser_accept() {
        assert!(wants_html(&headers_with(
            "accept",
            "text/html,application/xhtml+xml"
        )));
        assert!(!wants_html(&headers_with("accept", "text/plain")));
        assert!(!wants_html(&headers_with("accept", "application/json")));
        assert!(!wants_html(&HeaderMap::new()));
    }

    #[test]
    fn parse_body_reads_form_and_json() {
        let form = headers_with("content-type", "application/x-www-form-urlencoded");
        let parsed: LoginBody = parse_body(&form, b"email=a@b.c&password=pw").unwrap();
        assert_eq!(parsed.email.as_deref(), Some("a@b.c"));
        assert_eq!(parsed.password.as_deref(), Some("pw"));

        let json = headers_with("content-type", "application/json");
        let parsed: LoginBody = parse_body(&json, br#"{"email":"a@b.c","password":"pw"}"#).unwrap();
        assert_eq!(parsed.email.as_deref(), Some("a@b.c"));
    }

    #[test]
    fn parse_body_rejects_malformed_json() {
        let json = headers_with("content-type", "application/json");
        let result: Result<LoginBody, _> = parse_body(&json, b"{not json");
        assert!(result.is_err());
    }

    #[test]
    fn required_rejects_blank() {
        assert!(required(Some("  ".to_owned()), "name").is_err());
        assert!(required(None, "name").is_err());
        assert_eq!(required(Some("x".to_owned()), "name").unwrap(), "x");
    }
}