kyma-server 0.0.1

HTTP + gRPC query API, auth stub, health, observability.
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
//! HTTP handlers for the login / refresh / me / logout auth surface.
//!
//! Sessions use short-lived **access** tokens + long-lived **refresh** tokens,
//! both grouped by a `session_id`. The client authenticates API calls with the
//! access token and silently exchanges the refresh token for a new pair when it
//! nears expiry (refresh-token rotation). Logout revokes the whole session.
//! Refresh tokens are barred from authenticating ordinary API requests.
//!
//! # Routes
//!
//! ## Unauthenticated (mount via [`auth_login_router`])
//! - `POST /v1/auth/login` — username + password → access+refresh pair.
//! - `POST /v1/auth/refresh` — refresh token → rotated access+refresh pair.
//!
//! ## Authenticated (mount via [`auth_session_router`], wrapped with
//!   `require_role_middleware` at `Role::Read`)
//! - `GET /v1/auth/me` — return the current principal.
//! - `POST /v1/auth/logout` — revoke the whole session.

use crate::auth::{hash_token, Principal};
use axum::{
    extract::{Request, State},
    http::{header, StatusCode},
    response::{IntoResponse, Response},
    routing::{get, post},
    Extension, Json, Router,
};
use chrono::{Duration, Utc};
use kyma_core::catalog::Catalog;
use rand::RngCore;
use serde::{Deserialize, Serialize};
use std::sync::Arc;

// -------------------------------------------------------------------------
// State
// -------------------------------------------------------------------------

/// Shared handler state — just the catalog.
#[derive(Clone)]
pub struct AuthState {
    pub catalog: Arc<dyn Catalog>,
}

// -------------------------------------------------------------------------
// Request / response shapes
// -------------------------------------------------------------------------

#[derive(Debug, Deserialize)]
pub struct LoginRequest {
    pub username: String,
    pub password: String,
}

#[derive(Debug, Deserialize)]
pub struct RefreshRequest {
    pub refresh_token: String,
}

/// Response for both login and refresh: a fresh access+refresh token pair.
#[derive(Debug, Serialize)]
pub struct TokenPairResponse {
    pub access_token: String,
    pub refresh_token: String,
    pub access_expires_at: chrono::DateTime<Utc>,
    pub refresh_expires_at: chrono::DateTime<Utc>,
    pub user: UserInfo,
}

/// Access-token lifetime. Short by design — the client silently refreshes.
/// Overridable via `KYMA_ACCESS_TTL_SECS` (default 3600 = 1h).
fn access_ttl() -> Duration {
    std::env::var("KYMA_ACCESS_TTL_SECS")
        .ok()
        .and_then(|s| s.parse::<i64>().ok())
        .map(Duration::seconds)
        .unwrap_or_else(|| Duration::hours(1))
}

/// Refresh-token lifetime (the effective max session age before re-login).
/// Overridable via `KYMA_REFRESH_TTL_SECS` (default 2592000 = 30d).
fn refresh_ttl() -> Duration {
    std::env::var("KYMA_REFRESH_TTL_SECS")
        .ok()
        .and_then(|s| s.parse::<i64>().ok())
        .map(Duration::seconds)
        .unwrap_or_else(|| Duration::days(30))
}

/// Mint a random URL-safe token; returns `(raw_token, sha256_hash)`.
fn mint_token() -> (String, Vec<u8>) {
    let mut raw_bytes = [0u8; 32];
    rand::thread_rng().fill_bytes(&mut raw_bytes);
    use base64::Engine as _;
    let raw = base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(raw_bytes);
    let hash = hash_token(&raw);
    (raw, hash)
}

/// Issue + persist an access+refresh pair for a login session.
async fn issue_token_pair(
    catalog: &dyn Catalog,
    session_id: uuid::Uuid,
    role: &str,
    subject: &str,
) -> std::result::Result<TokenPairResponse, String> {
    let now = Utc::now();
    let access_expires_at = now + access_ttl();
    let refresh_expires_at = now + refresh_ttl();

    let (access_raw, access_hash) = mint_token();
    let (refresh_raw, refresh_hash) = mint_token();

    catalog
        .insert_session_token(&access_hash, role, Some(subject), "access", access_expires_at, session_id)
        .await
        .map_err(|e| e.to_string())?;
    catalog
        .insert_session_token(&refresh_hash, role, Some(subject), "refresh", refresh_expires_at, session_id)
        .await
        .map_err(|e| e.to_string())?;

    Ok(TokenPairResponse {
        access_token: access_raw,
        refresh_token: refresh_raw,
        access_expires_at,
        refresh_expires_at,
        user: UserInfo { username: subject.to_string(), role: role.to_string() },
    })
}

#[derive(Debug, Serialize)]
pub struct MeResponse {
    pub username: Option<String>,
    pub role: String,
}

#[derive(Debug, Serialize)]
pub struct UserInfo {
    pub username: String,
    pub role: String,
}

// -------------------------------------------------------------------------
// Error helpers
// -------------------------------------------------------------------------

fn unauthorized_response() -> Response {
    (
        StatusCode::UNAUTHORIZED,
        Json(serde_json::json!({
            "error": {
                "code": "unauthorized",
                "message": "invalid credentials"
            }
        })),
    )
        .into_response()
}

fn internal_error_response(msg: &str) -> Response {
    (
        StatusCode::INTERNAL_SERVER_ERROR,
        Json(serde_json::json!({
            "error": {
                "code": "internal",
                "message": msg
            }
        })),
    )
        .into_response()
}

// -------------------------------------------------------------------------
// Handlers
// -------------------------------------------------------------------------

/// `POST /v1/auth/login` — exchange credentials for a session token.
async fn login_handler(
    State(state): State<AuthState>,
    Json(body): Json<LoginRequest>,
) -> Response {
    // Look up the user (returns hash for verification).
    let result = state
        .catalog
        .get_user_with_hash(&body.username)
        .await;

    let (user, stored_hash) = match result {
        Ok(Some(pair)) => pair,
        Ok(None) => return unauthorized_response(),
        Err(e) => return internal_error_response(&e.to_string()),
    };

    // Verify password with argon2.
    if !crate::auth::passwords::verify_password(&body.password, &stored_hash) {
        return unauthorized_response();
    }

    // Mint a fresh login session: an access + refresh token pair grouped by a
    // new session_id so logout/refresh can act on the whole session.
    let session_id = uuid::Uuid::new_v4();
    match issue_token_pair(state.catalog.as_ref(), session_id, &user.role, &user.username).await {
        Ok(pair) => (StatusCode::OK, Json(pair)).into_response(),
        Err(e) => internal_error_response(&e),
    }
}

/// `POST /v1/auth/refresh` — exchange a valid refresh token for a rotated pair.
///
/// Unauthenticated route: the refresh token itself is the credential. On
/// success the presented refresh token is revoked (rotation) and a new
/// access+refresh pair is issued within the same session.
async fn refresh_handler(
    State(state): State<AuthState>,
    Json(body): Json<RefreshRequest>,
) -> Response {
    let presented_hash = hash_token(body.refresh_token.trim());

    let claim = match state.catalog.lookup_refresh_token(&presented_hash).await {
        Ok(Some(c)) => c,
        Ok(None) => return unauthorized_response(),
        Err(e) => return internal_error_response(&e.to_string()),
    };

    // Rotate: revoke the presented refresh token so it can't be replayed.
    if let Err(e) = state.catalog.revoke_api_token(&presented_hash).await {
        return internal_error_response(&e.to_string());
    }

    let subject = claim.subject.clone().unwrap_or_default();
    match issue_token_pair(state.catalog.as_ref(), claim.session_id, &claim.role, &subject).await {
        Ok(pair) => (StatusCode::OK, Json(pair)).into_response(),
        Err(e) => internal_error_response(&e),
    }
}

/// `GET /v1/auth/me` — return the current principal (injected by middleware).
async fn me_handler(Extension(principal): Extension<Principal>) -> Response {
    let role = format!("{:?}", principal.role).to_lowercase();
    (
        StatusCode::OK,
        Json(MeResponse {
            username: principal.subject,
            role,
        }),
    )
        .into_response()
}

/// `POST /v1/auth/logout` — revoke the presented session token.
async fn logout_handler(State(state): State<AuthState>, req: Request) -> Response {
    let raw_token = req
        .headers()
        .get(header::AUTHORIZATION)
        .and_then(|v| v.to_str().ok())
        .and_then(|s| s.strip_prefix("Bearer "))
        .map(str::trim);

    let Some(token) = raw_token else {
        return (StatusCode::BAD_REQUEST, "missing Authorization header").into_response();
    };

    // Revoke the whole session (this access token + its paired refresh token),
    // so a stolen refresh token can't outlive an explicit logout.
    let hash = hash_token(token);
    match state.catalog.revoke_session_by_token(&hash).await {
        Ok(_) => StatusCode::NO_CONTENT.into_response(),
        Err(e) => internal_error_response(&e.to_string()),
    }
}

// -------------------------------------------------------------------------
// First-run setup: signup + status + environment probe
// -------------------------------------------------------------------------

#[derive(Debug, Deserialize)]
pub struct SignupRequest {
    pub username: String,
    pub password: String,
}

/// `POST /v1/auth/signup` — create the FIRST admin user (first-run setup).
///
/// Unauthenticated, but only succeeds while no users exist; once setup is
/// complete it returns 409 so this can't be used to mint extra admins. On
/// success it issues a token pair (auto-login) so the wizard proceeds signed in.
async fn signup_handler(State(state): State<AuthState>, Json(body): Json<SignupRequest>) -> Response {
    let username = body.username.trim().to_string();
    if username.is_empty() || body.password.len() < 8 {
        return (
            StatusCode::BAD_REQUEST,
            Json(serde_json::json!({"error": {"code": "invalid",
                "message": "username is required and password must be at least 8 characters"}})),
        )
            .into_response();
    }
    match state.catalog.count_users().await {
        Ok(0) => {}
        Ok(_) => {
            return (
                StatusCode::CONFLICT,
                Json(serde_json::json!({"error": {"code": "already_setup",
                    "message": "setup is already complete — sign in instead"}})),
            )
                .into_response();
        }
        Err(e) => return internal_error_response(&e.to_string()),
    }
    let phc = match crate::auth::passwords::hash_password(&body.password) {
        Ok(h) => h,
        Err(e) => return internal_error_response(&format!("hashing password: {e}")),
    };
    if let Err(e) = state.catalog.create_user(&username, &phc, "admin").await {
        return internal_error_response(&e.to_string());
    }
    let session_id = uuid::Uuid::new_v4();
    match issue_token_pair(state.catalog.as_ref(), session_id, "admin", &username).await {
        Ok(pair) => (StatusCode::CREATED, Json(pair)).into_response(),
        Err(e) => internal_error_response(&e),
    }
}

/// `GET /v1/auth/status` — unauthenticated. Lets the web app decide between
/// showing the first-run setup wizard vs. the login screen on load.
async fn auth_status_handler(State(state): State<AuthState>) -> Response {
    let users_exist = matches!(state.catalog.count_users().await, Ok(n) if n > 0);
    (
        StatusCode::OK,
        Json(serde_json::json!({
            "users_exist": users_exist,
            "setup_required": !users_exist,
        })),
    )
        .into_response()
}

/// `GET /v1/setup/probe` — unauthenticated host capability probe for the setup
/// wizard's AI-engine step: which engines/keys are available, whether Ollama is
/// running and `gemma4:latest` is installed, plus a recommended default.
async fn env_probe_handler() -> Response {
    let anthropic_key_present =
        std::env::var("ANTHROPIC_API_KEY").map(|v| !v.is_empty()).unwrap_or(false);
    let openai_key_present =
        std::env::var("OPENAI_API_KEY").map(|v| !v.is_empty()).unwrap_or(false);
    let claude_binary_found = crate::agent::engine::claude_cli::locate_binary().is_some();
    let (ollama_reachable, ollama_models) = probe_ollama().await;
    let gemma4_present = ollama_models.iter().any(|m| m.starts_with("gemma4"));

    // On-device gemma4 is the preferred default (private, no key). Fall back to
    // whatever cloud capability is actually available.
    let recommend = if gemma4_present {
        serde_json::json!({"kind": "ollama", "model": "gemma4:latest"})
    } else if anthropic_key_present {
        serde_json::json!({"kind": "anthropic", "model": "claude-sonnet-4-6"})
    } else if claude_binary_found {
        serde_json::json!({"kind": "claude_cli", "model": "sonnet"})
    } else if openai_key_present {
        serde_json::json!({"kind": "openai", "model": "gpt-4o"})
    } else if ollama_reachable && !ollama_models.is_empty() {
        serde_json::json!({"kind": "ollama", "model": ollama_models[0]})
    } else {
        // Nothing detected — still default to on-device gemma4 and hint a pull.
        serde_json::json!({"kind": "ollama", "model": "gemma4:latest", "needs_pull": true})
    };

    (
        StatusCode::OK,
        Json(serde_json::json!({
            "ollama_reachable": ollama_reachable,
            "ollama_models": ollama_models,
            "gemma4_present": gemma4_present,
            "anthropic_key_present": anthropic_key_present,
            "openai_key_present": openai_key_present,
            "claude_binary_found": claude_binary_found,
            "recommend": recommend,
        })),
    )
        .into_response()
}

/// Probe the local Ollama daemon for installed models. Returns
/// `(reachable, model_names)`. Short timeout so the wizard stays snappy.
async fn probe_ollama() -> (bool, Vec<String>) {
    let host = std::env::var("KYMA_OLLAMA_HOST")
        .ok()
        .filter(|s| !s.is_empty())
        .unwrap_or_else(|| "http://localhost:11434".to_string());
    let url = format!("{}/api/tags", host.trim_end_matches('/'));
    let client = match reqwest::Client::builder()
        .timeout(std::time::Duration::from_millis(1500))
        .build()
    {
        Ok(c) => c,
        Err(_) => return (false, Vec::new()),
    };
    match client.get(&url).send().await {
        Ok(resp) if resp.status().is_success() => {
            let models = resp
                .json::<serde_json::Value>()
                .await
                .ok()
                .and_then(|v| v.get("models").cloned())
                .and_then(|m| m.as_array().cloned())
                .map(|arr| {
                    arr.iter()
                        .filter_map(|m| m.get("name").and_then(|n| n.as_str()).map(String::from))
                        .collect::<Vec<_>>()
                })
                .unwrap_or_default();
            (true, models)
        }
        _ => (false, Vec::new()),
    }
}

// -------------------------------------------------------------------------
// Router builders
// -------------------------------------------------------------------------

/// Build the **unauthenticated** login router.
///
/// Mount this with `.merge(auth_login_router(...))` BEFORE the auth layer —
/// it must NOT be wrapped with `require_role_middleware`.
pub fn auth_login_router(catalog: Arc<dyn Catalog>) -> Router {
    let state = AuthState { catalog };
    Router::new()
        .route("/v1/auth/login", post(login_handler))
        .route("/v1/auth/refresh", post(refresh_handler))
        .route("/v1/auth/signup", post(signup_handler))
        .route("/v1/auth/status", get(auth_status_handler))
        .route("/v1/setup/probe", get(env_probe_handler))
        .with_state(state)
}

/// Build the **authenticated** session router (me + logout).
///
/// The caller MUST wrap this router with
/// `require_role_middleware(Role::Read)` so the bearer token is validated and
/// a [`Principal`] is inserted into request extensions before these handlers
/// are reached.
pub fn auth_session_router(catalog: Arc<dyn Catalog>) -> Router {
    let state = AuthState { catalog };
    Router::new()
        .route("/v1/auth/me", get(me_handler))
        .route("/v1/auth/logout", post(logout_handler))
        .with_state(state)
}