lific 1.2.0

Local-first, lightweight issue tracker. Single binary, SQLite-backed, MCP-native.
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
mod api;
mod auth;
mod backup;
mod cli;
mod config;
mod db;
mod error;
mod export;
mod mcp;
mod oauth;
mod ratelimit;

use std::sync::Arc;

use axum::{
    body::Body,
    extract::Request,
    http::{StatusCode, header},
    middleware,
    response::IntoResponse,
    routing::{any, get},
};
use clap::Parser;
use cli::{Cli, Command, KeyAction, UserAction};
use config::Config;

// Commands that operate directly on the database (no server required)
fn is_crud_command(cmd: &Command) -> bool {
    matches!(cmd,
        Command::Issue { .. } | Command::Project { .. } | Command::Page { .. } |
        Command::Export { .. } |
        Command::Search { .. } | Command::Comment { .. } | Command::Module { .. } |
        Command::Label { .. } | Command::Folder { .. }
    )
}
use rmcp::{
    ServiceExt,
    transport::streamable_http_server::{
        session::local::LocalSessionManager,
        tower::{StreamableHttpServerConfig, StreamableHttpService},
    },
};
use rust_embed::Embed;
use tracing::info;

/// Embedded frontend assets compiled from web/dist/.
/// Falls back gracefully if dist/ doesn't exist (e.g. dev builds without frontend).
#[derive(Embed)]
#[folder = "web/dist/"]
#[allow(dead_code)]
struct WebAssets;

/// Serve an embedded static file, or fall back to index.html for SPA routing.
async fn serve_frontend(uri: axum::http::Uri) -> impl IntoResponse {
    let path = uri.path().trim_start_matches('/');

    // Try the exact path first (e.g. assets/index-abc.js)
    if let Some(file) = WebAssets::get(path) {
        let mime = mime_guess::from_path(path)
            .first_or_octet_stream()
            .to_string();
        return (
            StatusCode::OK,
            [(header::CONTENT_TYPE, mime)],
            file.data.to_vec(),
        )
            .into_response();
    }

    // SPA fallback: serve index.html for all unmatched routes
    match WebAssets::get("index.html") {
        Some(file) => (
            StatusCode::OK,
            [(header::CONTENT_TYPE, "text/html".to_string())],
            file.data.to_vec(),
        )
            .into_response(),
        None => (
            StatusCode::NOT_FOUND,
            "Frontend not built. Run: cd web && bun run build",
        )
            .into_response(),
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let cli = Cli::parse();

    // Load config (CLI flags override config values)
    let mut cfg = Config::load(cli.config.as_deref());

    // CLI overrides
    if let Some(ref db) = cli.db {
        cfg.database.path = db.clone();
    }

    // Handle CRUD commands (direct database access, no server needed)
    if is_crud_command(&cli.command) {
        let pool = db::open(&cfg.database.path)?;
        return cli::exec::run(&pool, &cli.command, cli.json).map_err(Into::into);
    }

    match cli.command {
        Command::Init => {
            let config_path = std::path::Path::new("lific.toml");
            if config_path.exists() {
                eprintln!("lific.toml already exists in current directory");
                std::process::exit(1);
            }
            std::fs::write(config_path, Config::default_toml())?;
            println!("Created lific.toml with default settings");
            return Ok(());
        }

        Command::Key { action } => {
            let pool = db::open(&cfg.database.path)?;
            let manager =
                auth::create_key_manager().map_err(|e| format!("key manager init failed: {e}"))?;

            match action {
                KeyAction::Create { name, user } => {
                    let key = auth::create_api_key(&pool, &manager, &name)?;

                    // If --user was provided, assign the key to that user
                    if let Some(ref username) = user {
                        let conn = pool.read()?;
                        let u = db::queries::users::get_user_by_username(&conn, username)?;
                        drop(conn);
                        let conn = pool.write()?;
                        db::queries::users::assign_key_to_user(&conn, &name, u.id)?;
                        println!();
                        println!("  API Key created: {name} (assigned to {username})");
                    } else {
                        println!();
                        println!("  API Key created: {name}");
                    }
                    println!();
                    println!("  {key}");
                    println!();
                    println!("  Save this key now. It will never be shown again.");
                    println!("  Use as: Authorization: Bearer {key}");
                    println!();
                }
                KeyAction::List => {
                    let keys = auth::list_api_keys(&pool)?;
                    if keys.is_empty() {
                        println!("No API keys configured.");
                    } else {
                        println!("{} API key(s):", keys.len());
                        for k in &keys {
                            let status = if k.revoked { "REVOKED" } else { "active" };
                            let expiry = k.expires_at.as_deref().unwrap_or("never");
                            println!(
                                "  {} | {} | created {} | expires {}",
                                k.name, status, k.created_at, expiry
                            );
                        }
                    }
                }
                KeyAction::Revoke { name } => {
                    auth::revoke_api_key(&pool, &name)?;
                    println!("Revoked key: {name}");
                }
                KeyAction::Rotate { name } => {
                    let key = auth::rotate_api_key(&pool, &manager, &name)?;
                    println!();
                    println!("  Key rotated: {name}");
                    println!();
                    println!("  {key}");
                    println!();
                    println!("  Save this key now. It will never be shown again.");
                    println!();
                }
                KeyAction::Assign { name, user } => {
                    let conn = pool.read()?;
                    let u = db::queries::users::get_user_by_username(&conn, &user)?;
                    drop(conn);
                    let conn = pool.write()?;
                    db::queries::users::assign_key_to_user(&conn, &name, u.id)?;
                    println!("Assigned key '{name}' to user '{user}'");
                }
            }
            return Ok(());
        }

        Command::User { action } => {
            let pool = db::open(&cfg.database.path)?;

            match action {
                UserAction::Create {
                    username,
                    email,
                    password,
                    admin,
                    bot,
                } => {
                    // Prompt for password if not provided
                    let pw = match password {
                        Some(p) => p,
                        None => {
                            eprint!("Password: ");
                            let mut buf = String::new();
                            std::io::stdin().read_line(&mut buf)?;
                            buf.trim().to_string()
                        }
                    };

                    let conn = pool.write()?;
                    let user = db::queries::users::create_user(
                        &conn,
                        &db::models::CreateUser {
                            username: username.clone(),
                            email: email.clone(),
                            password: pw,
                            display_name: None,
                            is_admin: admin,
                            is_bot: bot,
                        },
                    )?;

                    let role = if user.is_admin { " (admin)" } else { "" };
                    println!("User created: {}{role}", user.username);
                    println!("  email: {}", user.email);
                    println!("  display_name: {}", user.display_name);
                }
                UserAction::List => {
                    let conn = pool.read()?;
                    let users = db::queries::users::list_users(&conn)?;

                    if users.is_empty() {
                        println!("No users.");
                    } else {
                        println!("{} user(s):", users.len());
                        for u in &users {
                            let flags = match (u.is_admin, u.is_bot) {
                                (true, true) => " [admin, bot]",
                                (true, false) => " [admin]",
                                (false, true) => " [bot]",
                                (false, false) => "",
                            };
                            println!(
                                "  {} | {} | {}{} | created {}",
                                u.id, u.username, u.email, flags, u.created_at
                            );
                        }
                    }
                }
                UserAction::Promote { username } => {
                    let conn = pool.write()?;
                    db::queries::users::set_admin(&conn, &username, true)?;
                    println!("Promoted '{username}' to admin.");
                }
                UserAction::Demote { username } => {
                    let conn = pool.write()?;
                    db::queries::users::set_admin(&conn, &username, false)?;
                    println!("Demoted '{username}' from admin.");
                }
            }
            return Ok(());
        }

        Command::Start { port, host } => {
            if let Some(p) = port {
                cfg.server.port = p;
            }
            if let Some(h) = host {
                cfg.server.host = h;
            }

            tracing_subscriber::fmt()
                .with_env_filter(
                    tracing_subscriber::EnvFilter::try_from_default_env()
                        .unwrap_or_else(|_| format!("lific={}", cfg.log.level).into()),
                )
                .init();

            let pool = db::open(&cfg.database.path)?;
            info!(path = %cfg.database.path.display(), "database ready");

            // Key manager for auth
            let manager =
                auth::create_key_manager().map_err(|e| format!("key manager init failed: {e}"))?;

            // Auto-generate a key if none exist
            if !auth::has_any_keys(&pool) {
                let key = auth::create_api_key(&pool, &manager, "default")?;
                info!("no API keys found, auto-generated initial key");
                println!();
                println!("  ┌─────────────────────────────────────────────────────┐");
                println!("  │  No API keys found. Generated initial key:          │");
                println!("  │                                                     │");
                println!("{key}");
                println!("  │                                                     │");
                println!("  │  Save this key now. It will never be shown again.   │");
                println!("  │  Use as: Authorization: Bearer <key>                │");
                println!("  └─────────────────────────────────────────────────────┘");
                println!();
            } else {
                let count = auth::list_api_keys(&pool)?
                    .iter()
                    .filter(|k| !k.revoked)
                    .count();
                info!(active_keys = count, "API key auth enabled");
            }

            // Auth state for middleware
            let issuer = cfg
                .server
                .public_url
                .clone()
                .unwrap_or_else(|| format!("http://{}:{}", cfg.server.host, cfg.server.port));

            let manager_ext = Arc::new(manager.clone());

            let auth_state = auth::AuthState {
                db: pool.clone(),
                manager,
                public_url: issuer.clone(),
            };

            // Start backup task
            if cfg.backup.enabled {
                let pool_arc = Arc::new(pool.clone());
                backup::start_backup_task(pool_arc, cfg.database.path.clone(), cfg.backup.clone());
                info!(
                    dir = %cfg.backup_dir().display(),
                    interval = %format!("{}m", cfg.backup.interval_minutes),
                    retain = cfg.backup.retain,
                    "automatic backups enabled"
                );
            }

            // MCP StreamableHTTP service
            let db_for_mcp = pool.clone();
            let mut mcp_allowed_hosts: Vec<String> =
                vec!["localhost".into(), "127.0.0.1".into(), "::1".into()];

            // If public_url is set, allow its hostname through the DNS rebinding check
            // so reverse proxies (Tailscale funnel, nginx, etc.) can forward requests.
            if let Some(ref url) = cfg.server.public_url {
                if let Ok(parsed) = url.parse::<axum::http::Uri>() {
                    if let Some(authority) = parsed.authority() {
                        let host: String = authority.host().to_string();
                        mcp_allowed_hosts.push(host);
                    }
                }
            }

            let mcp_config = StreamableHttpServerConfig::default()
                .with_stateful_mode(false)
                .with_json_response(true)
                .with_allowed_hosts(mcp_allowed_hosts);

            let mcp_service = StreamableHttpService::new(
                move || Ok(mcp::LificMcp::new(db_for_mcp.clone())),
                Arc::new(LocalSessionManager::default()),
                mcp_config,
            );

            // Login rate limiter: 5 attempts per 15 minutes per identity
            let login_limiter = Arc::new(ratelimit::RateLimiter::new(
                5,
                std::time::Duration::from_secs(15 * 60),
            ));

            // Routes behind auth: REST API + MCP
            let authed_routes = api::router(pool.clone(), &cfg.server.cors_origins)
                .route(
                    "/mcp",
                    any(move |request: Request<Body>| async move {
                        // Extract the authenticated user (set by auth middleware)
                        // and store it for MCP tools to read. Serialized to prevent
                        // concurrent requests from overwriting each other's identity.
                        let auth_user = request
                            .extensions()
                            .get::<Option<db::models::AuthUser>>()
                            .cloned()
                            .flatten();

                        mcp::with_request_user(auth_user, || async {
                            mcp_service.handle(request).await.into_response()
                        })
                        .await
                    }),
                )
                .layer(axum::Extension(login_limiter))
                .layer(axum::Extension(cfg.auth.clone()))
                .layer(axum::Extension(manager_ext))
                .layer(middleware::from_fn_with_state(
                    auth_state,
                    auth_middleware_wrapper,
                ));

            // OAuth client registration rate limiter: 10 clients per IP per hour.
            // /oauth/register is unauthenticated per RFC 7591; without this anyone
            // can flood the server with throwaway clients (LIF-64).
            let oauth_register_limiter = Arc::new(ratelimit::RateLimiter::new(
                10,
                std::time::Duration::from_secs(60 * 60),
            ));

            let oauth_state = oauth::OAuthState {
                db: pool.clone(),
                issuer,
                register_limiter: oauth_register_limiter,
            };

            let app = authed_routes
                .merge(oauth::router(oauth_state))
                .fallback(get(serve_frontend))
                .layer(axum::extract::DefaultBodyLimit::max(2 * 1024 * 1024)); // 2 MB

            let addr = format!("{}:{}", cfg.server.host, cfg.server.port);
            let listener = tokio::net::TcpListener::bind(&addr).await?;
            info!(addr = %addr, "lific server started (REST + MCP + OAuth at /mcp)");

            let shutdown_pool = pool.clone();
            let server =
                axum::serve(listener, app).with_graceful_shutdown(shutdown_signal(shutdown_pool));
            server.await?;
        }

        Command::Mcp => {
            tracing_subscriber::fmt()
                .with_env_filter(
                    tracing_subscriber::EnvFilter::try_from_default_env()
                        .unwrap_or_else(|_| format!("lific={}", cfg.log.level).into()),
                )
                .with_writer(std::io::stderr)
                .init();

            let pool = db::open(&cfg.database.path)?;
            info!(path = %cfg.database.path.display(), "database ready");

            let server = mcp::LificMcp::new(pool);
            let transport = rmcp::transport::io::stdio();

            info!("lific MCP server started (stdio)");
            let handle = server.serve(transport).await?;
            handle.waiting().await?;
        }

        // CRUD commands are handled before this match
        Command::Issue { .. } | Command::Project { .. } | Command::Page { .. } |
        Command::Export { .. } |
        Command::Search { .. } | Command::Comment { .. } | Command::Module { .. } |
        Command::Label { .. } | Command::Folder { .. } => unreachable!(),
    }

    Ok(())
}

/// Wrapper that skips auth for /api/health
async fn auth_middleware_wrapper(
    state: axum::extract::State<auth::AuthState>,
    request: Request<Body>,
    next: middleware::Next,
) -> axum::response::Response {
    let path = request.uri().path();
    if path == "/api/health"
        || path == "/api/auth/signup"
        || path == "/api/auth/login"
        || path.starts_with("/.well-known/")
        || path.starts_with("/oauth/")
        || path == "/register"
        || path == "/authorize"
        || path == "/token"
        || path == "/revoke"
    {
        return next.run(request).await;
    }
    auth::require_api_key(state, request, next).await
}

/// Wait for SIGINT/SIGTERM, then checkpoint WAL before shutting down.
async fn shutdown_signal(pool: db::DbPool) {
    let ctrl_c = async {
        tokio::signal::ctrl_c()
            .await
            .expect("failed to install Ctrl+C handler");
    };

    #[cfg(unix)]
    let terminate = async {
        tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
            .expect("failed to install SIGTERM handler")
            .recv()
            .await;
    };

    #[cfg(not(unix))]
    let terminate = std::future::pending::<()>();

    tokio::select! {
        _ = ctrl_c => {},
        _ = terminate => {},
    }

    info!("shutdown signal received, checkpointing WAL...");
    backup::checkpoint_wal(&pool);
    info!("shutdown complete");
}