riley-cms-api 0.1.0

HTTP API server for riley_cms - Axum-based REST endpoints
Documentation
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
//! HTTP request handlers for riley-cms-api

use crate::AppState;
use crate::middleware::AuthStatus;
use axum::{
    Extension, Json,
    extract::{Path, Query, State},
    http::{StatusCode, header},
    response::{IntoResponse, Response},
};
use futures_util::StreamExt;
use riley_cms_core::ListOptions;
use serde::{Deserialize, Serialize};
use std::sync::Arc;

/// Query parameters for list endpoints
#[derive(Debug, Clone, Deserialize)]
pub struct ListQuery {
    #[serde(default)]
    pub include_drafts: bool,
    #[serde(default)]
    pub include_scheduled: bool,
    pub limit: Option<usize>,
    pub offset: Option<usize>,
}

impl From<ListQuery> for ListOptions {
    fn from(q: ListQuery) -> Self {
        Self {
            include_drafts: q.include_drafts,
            include_scheduled: q.include_scheduled,
            limit: q.limit,
            offset: q.offset,
        }
    }
}

/// Error response format
#[derive(Serialize)]
struct ErrorResponse {
    error: String,
}

/// Convert internal errors to HTTP responses.
///
/// Logs the actual error server-side but returns a generic message to clients
/// to avoid leaking internal details (file paths, S3 errors, etc.).
fn internal_error(err: impl std::fmt::Display) -> Response {
    tracing::error!("Internal error: {}", err);
    let body = Json(ErrorResponse {
        error: "Internal server error".to_string(),
    });
    (StatusCode::INTERNAL_SERVER_ERROR, body).into_response()
}

/// Add caching headers to response
fn with_cache_headers(
    response: impl IntoResponse,
    state: &AppState,
    etag: &str,
    is_authenticated: bool,
) -> Response {
    let mut response = response.into_response();
    let headers = response.headers_mut();

    if is_authenticated {
        headers.insert(
            header::CACHE_CONTROL,
            "private, no-store".parse().expect("valid static header"),
        );
    } else {
        let server = state.config.server.as_ref();
        let max_age = server.map(|s| s.cache_max_age).unwrap_or(60);
        let swr = server
            .map(|s| s.cache_stale_while_revalidate)
            .unwrap_or(300);

        headers.insert(
            header::CACHE_CONTROL,
            format!(
                "public, max-age={}, stale-while-revalidate={}",
                max_age, swr
            )
            .parse()
            .expect("valid cache-control header"),
        );
        headers.insert(header::ETAG, etag.parse().expect("valid etag header"));
    }

    response
}

/// Check if request requires authentication (has include_drafts or include_scheduled)
fn is_authenticated_request(query: &ListQuery) -> bool {
    query.include_drafts || query.include_scheduled
}

/// Check if content is visible based on goes_live_at and auth status.
/// Live content (goes_live_at in the past) is always visible.
/// Drafts (None) and scheduled (future) require admin auth.
fn is_content_visible(
    goes_live_at: Option<chrono::DateTime<chrono::Utc>>,
    auth_status: AuthStatus,
) -> bool {
    if auth_status == AuthStatus::Admin {
        return true;
    }
    match goes_live_at {
        None => false,                                    // Draft
        Some(date) if date > chrono::Utc::now() => false, // Scheduled
        Some(_) => true,                                  // Live
    }
}

/// Generate a standard not-found response
fn not_found_response(slug: &str, kind: &str) -> Response {
    let body = Json(ErrorResponse {
        error: format!("{} not found: {}", kind, slug),
    });
    (StatusCode::NOT_FOUND, body).into_response()
}

// === Handlers ===

/// GET /posts - List all posts
pub async fn list_posts(
    State(state): State<Arc<AppState>>,
    Extension(auth_status): Extension<AuthStatus>,
    Query(query): Query<ListQuery>,
) -> Response {
    let is_auth_required = is_authenticated_request(&query);

    // Security check: require authentication for drafts/scheduled content
    if is_auth_required && auth_status != AuthStatus::Admin {
        return (
            StatusCode::UNAUTHORIZED,
            Json(ErrorResponse {
                error: "Authentication required for drafts/scheduled content".to_string(),
            }),
        )
            .into_response();
    }

    let opts: ListOptions = query.into();

    match state.riley_cms.list_posts(&opts).await {
        Ok(result) => {
            let etag = state.riley_cms.content_etag().await;

            #[derive(Serialize)]
            struct PostsResponse {
                posts: Vec<riley_cms_core::PostSummary>,
                total: usize,
                limit: usize,
                offset: usize,
            }

            let response = Json(PostsResponse {
                posts: result.items,
                total: result.total,
                limit: result.limit,
                offset: result.offset,
            });

            with_cache_headers(response, &state, &etag, is_auth_required)
        }
        Err(e) => internal_error(e),
    }
}

/// GET /posts/:slug - Get a single post
pub async fn get_post(
    State(state): State<Arc<AppState>>,
    Extension(auth_status): Extension<AuthStatus>,
    Path(slug): Path<String>,
) -> Response {
    match state.riley_cms.get_post(&slug).await {
        Ok(Some(post)) => {
            // Visibility check: drafts/scheduled posts require admin auth
            if !is_content_visible(post.goes_live_at, auth_status) {
                return not_found_response(&slug, "Post");
            }
            let etag = state.riley_cms.content_etag().await;
            with_cache_headers(Json(post), &state, &etag, auth_status == AuthStatus::Admin)
        }
        Ok(None) => not_found_response(&slug, "Post"),
        Err(e) => internal_error(e),
    }
}

/// GET /posts/:slug/raw - Get raw MDX content only
pub async fn get_post_raw(
    State(state): State<Arc<AppState>>,
    Extension(auth_status): Extension<AuthStatus>,
    Path(slug): Path<String>,
) -> Response {
    match state.riley_cms.get_post(&slug).await {
        Ok(Some(post)) => {
            // Visibility check: drafts/scheduled posts require admin auth
            if !is_content_visible(post.goes_live_at, auth_status) {
                return not_found_response(&slug, "Post");
            }
            let is_admin = auth_status == AuthStatus::Admin;
            let etag = state.riley_cms.content_etag().await;
            let mut response = post.content.into_response();
            let headers = response.headers_mut();

            headers.insert(
                header::CONTENT_TYPE,
                "text/plain; charset=utf-8"
                    .parse()
                    .expect("valid static header"),
            );

            if is_admin {
                headers.insert(
                    header::CACHE_CONTROL,
                    "private, no-store".parse().expect("valid static header"),
                );
            } else {
                let server = state.config.server.as_ref();
                let max_age = server.map(|s| s.cache_max_age).unwrap_or(60);
                let swr = server
                    .map(|s| s.cache_stale_while_revalidate)
                    .unwrap_or(300);

                headers.insert(
                    header::CACHE_CONTROL,
                    format!(
                        "public, max-age={}, stale-while-revalidate={}",
                        max_age, swr
                    )
                    .parse()
                    .expect("valid cache-control header"),
                );
                headers.insert(header::ETAG, etag.parse().expect("valid etag header"));
            }

            response
        }
        Ok(None) => not_found_response(&slug, "Post"),
        Err(e) => internal_error(e),
    }
}

/// GET /series - List all series
pub async fn list_series(
    State(state): State<Arc<AppState>>,
    Extension(auth_status): Extension<AuthStatus>,
    Query(query): Query<ListQuery>,
) -> Response {
    let is_auth_required = is_authenticated_request(&query);

    // Security check: require authentication for drafts/scheduled content
    if is_auth_required && auth_status != AuthStatus::Admin {
        return (
            StatusCode::UNAUTHORIZED,
            Json(ErrorResponse {
                error: "Authentication required for drafts/scheduled content".to_string(),
            }),
        )
            .into_response();
    }

    let opts: ListOptions = query.into();

    match state.riley_cms.list_series(&opts).await {
        Ok(result) => {
            let etag = state.riley_cms.content_etag().await;

            #[derive(Serialize)]
            struct SeriesResponse {
                series: Vec<riley_cms_core::SeriesSummary>,
                total: usize,
                limit: usize,
                offset: usize,
            }

            let response = Json(SeriesResponse {
                series: result.items,
                total: result.total,
                limit: result.limit,
                offset: result.offset,
            });

            with_cache_headers(response, &state, &etag, is_auth_required)
        }
        Err(e) => internal_error(e),
    }
}

/// GET /series/:slug - Get a single series with posts
pub async fn get_series(
    State(state): State<Arc<AppState>>,
    Extension(auth_status): Extension<AuthStatus>,
    Path(slug): Path<String>,
) -> Response {
    match state.riley_cms.get_series(&slug).await {
        Ok(Some(series)) => {
            // Visibility check: drafts/scheduled series require admin auth
            if !is_content_visible(series.goes_live_at, auth_status) {
                return not_found_response(&slug, "Series");
            }
            let etag = state.riley_cms.content_etag().await;
            with_cache_headers(
                Json(series),
                &state,
                &etag,
                auth_status == AuthStatus::Admin,
            )
        }
        Ok(None) => not_found_response(&slug, "Series"),
        Err(e) => internal_error(e),
    }
}

/// Query parameters for asset list endpoint
#[derive(Debug, Clone, Deserialize)]
pub struct AssetListQuery {
    pub limit: Option<usize>,
    pub continuation_token: Option<String>,
}

/// GET /assets - List assets in storage with pagination (admin only)
pub async fn list_assets(
    State(state): State<Arc<AppState>>,
    Extension(auth_status): Extension<AuthStatus>,
    Query(query): Query<AssetListQuery>,
) -> Response {
    if auth_status != AuthStatus::Admin {
        return (
            StatusCode::UNAUTHORIZED,
            Json(ErrorResponse {
                error: "Authentication required to list assets".to_string(),
            }),
        )
            .into_response();
    }

    let opts = riley_cms_core::AssetListOptions {
        limit: query.limit,
        continuation_token: query.continuation_token,
    };

    match state.riley_cms.list_assets(&opts).await {
        Ok(result) => {
            let mut response = Json(result).into_response();
            response.headers_mut().insert(
                header::CACHE_CONTROL,
                "private, no-store".parse().expect("valid static header"),
            );
            response
        }
        Err(e) => internal_error(e),
    }
}

/// GET /health - Health check
pub async fn health() -> Response {
    #[derive(Serialize)]
    struct HealthResponse {
        status: &'static str,
    }

    Json(HealthResponse { status: "ok" }).into_response()
}

// === Git Smart HTTP Handlers ===

use axum::http::HeaderMap;
use base64::Engine;
use riley_cms_core::GitBackend;
use sha2::{Digest, Sha256};
use subtle::ConstantTimeEq;

/// Check Basic Auth for Git operations
///
/// Git clients typically use Basic Auth (not Bearer tokens).
/// Returns true if authentication is valid.
fn check_git_basic_auth(headers: &HeaderMap, state: &AppState) -> bool {
    // Get the configured git_token
    let expected_token = match &state.config.auth {
        Some(auth) => match &auth.git_token {
            Some(token_config) => match token_config.resolve() {
                Ok(token) => {
                    if token.is_empty() {
                        tracing::warn!("Git token resolves to empty string. Git auth disabled.");
                        return false;
                    }
                    token
                }
                Err(e) => {
                    tracing::warn!("Failed to resolve git token: {}. Git auth disabled.", e);
                    return false;
                }
            },
            None => return false, // No git_token configured, deny access
        },
        None => return false, // No auth configured, deny access
    };

    // Parse Basic Auth header
    let auth_header = match headers.get(header::AUTHORIZATION) {
        Some(h) => h,
        None => return false,
    };

    let auth_str = match auth_header.to_str() {
        Ok(s) => s,
        Err(_) => return false,
    };

    // Format: "Basic base64(username:password)"
    let encoded = match auth_str.strip_prefix("Basic ") {
        Some(e) => e,
        None => return false,
    };

    let decoded = match base64::engine::general_purpose::STANDARD.decode(encoded) {
        Ok(d) => d,
        Err(_) => return false,
    };

    let credentials = match String::from_utf8(decoded) {
        Ok(c) => c,
        Err(_) => return false,
    };

    // Format: "username:password" - we only check password (the token)
    // Username can be anything (commonly "git" or the actual username)
    if let Some((_username, password)) = credentials.split_once(':') {
        // Hash both tokens before comparing to prevent leaking
        // token length via timing side-channel.
        let provided_hash = Sha256::digest(password.as_bytes());
        let expected_hash = Sha256::digest(expected_token.as_bytes());
        provided_hash.ct_eq(&expected_hash).into()
    } else {
        false
    }
}

/// Default maximum allowed body size for git operations (100 MB)
const DEFAULT_GIT_MAX_BODY_SIZE: u64 = 100 * 1024 * 1024;

/// Validate that a git path is safe (no traversal, no injection)
fn is_valid_git_path(path: &str) -> bool {
    // Reject path traversal
    if path.contains("..") {
        return false;
    }
    // Only allow alphanumeric, hyphens, underscores, dots, forward slashes, and query-safe chars
    path.chars()
        .all(|c| c.is_ascii_alphanumeric() || "-_./=?&+".contains(c))
}

/// Git Smart HTTP handler
///
/// Handles all Git HTTP protocol requests by proxying to git-http-backend.
/// Supports both read (fetch/clone) and write (push) operations.
/// Request bodies and CGI responses are streamed to avoid buffering large payloads.
pub async fn git_handler(
    State(state): State<Arc<AppState>>,
    Path(path): Path<String>,
    request: axum::http::Request<axum::body::Body>,
) -> Response {
    // Validate path to prevent traversal and injection
    if !is_valid_git_path(&path) {
        tracing::warn!("Rejected invalid git path: {:?}", path);
        return (
            StatusCode::BAD_REQUEST,
            Json(ErrorResponse {
                error: "Invalid path".to_string(),
            }),
        )
            .into_response();
    }

    let method = request.method().clone();
    let headers = request.headers().clone();
    let uri = request.uri().clone();

    // Check Basic Auth before consuming the body
    if !check_git_basic_auth(&headers, &state) {
        return (
            StatusCode::UNAUTHORIZED,
            [(header::WWW_AUTHENTICATE, "Basic realm=\"Git\"")],
            "Authentication required",
        )
            .into_response();
    }

    // Get the content repository path and git config
    let repo_path = &state.config.content.repo_path;
    let git_config = state.config.git.as_ref();
    let backend_path = git_config.and_then(|g| g.backend_path.clone());
    let max_body_size = git_config
        .map(|g| g.max_body_size)
        .unwrap_or(DEFAULT_GIT_MAX_BODY_SIZE);
    let cgi_timeout =
        std::time::Duration::from_secs(git_config.map(|g| g.cgi_timeout_secs).unwrap_or(300));
    let backend = GitBackend::with_backend_path(repo_path, backend_path);

    // Check if repo exists
    if !backend.is_valid_repo() {
        return (
            StatusCode::NOT_FOUND,
            Json(ErrorResponse {
                error: "Git repository not found".to_string(),
            }),
        )
            .into_response();
    }

    // Build path_info (the path after /git/)
    let path_info = format!("/{}", path);

    // Extract query string from URI
    let query_string = uri.query().map(String::from);

    // Get content type and content length
    let content_type = headers
        .get(header::CONTENT_TYPE)
        .and_then(|h| h.to_str().ok())
        .map(String::from);

    let content_length = headers
        .get(header::CONTENT_LENGTH)
        .and_then(|h| h.to_str().ok())
        .and_then(|s| s.parse::<u64>().ok());

    // Determine if this is a write operation (push)
    let is_write_operation = path.contains("git-receive-pack");

    // Convert request body to a stream for incremental processing
    let body_stream = request
        .into_body()
        .into_data_stream()
        .map(|result| result.map_err(std::io::Error::other));

    // Run the Git CGI backend with streaming
    match backend
        .run_cgi(
            method.as_str(),
            &path_info,
            query_string.as_deref(),
            content_type.as_deref(),
            content_length,
            body_stream,
            max_body_size,
        )
        .await
    {
        Ok(cgi_response) => {
            let status =
                StatusCode::from_u16(cgi_response.headers.status).unwrap_or(StatusCode::OK);

            let mut response_builder = Response::builder().status(status);

            // Copy headers from CGI response
            for (key, value) in &cgi_response.headers.headers {
                if let Ok(header_name) = key.parse::<axum::http::header::HeaderName>()
                    && let Ok(header_value) = value.parse::<axum::http::header::HeaderValue>()
                {
                    response_builder = response_builder.header(header_name, header_value);
                }
            }

            // Build streaming response body
            let response = match response_builder
                .body(axum::body::Body::from_stream(cgi_response.body_stream))
            {
                Ok(r) => r,
                Err(e) => {
                    tracing::error!("Failed to build response from CGI output: {}", e);
                    return (
                        StatusCode::INTERNAL_SERVER_ERROR,
                        Json(ErrorResponse {
                            error: "Git operation failed".to_string(),
                        }),
                    )
                        .into_response();
                }
            };

            // Always spawn a task to reap the child process (prevents zombies).
            // Only trigger refresh/webhooks for successful write operations.
            let state_clone = state.clone();
            tokio::spawn(async move {
                match cgi_response.completion.wait(cgi_timeout).await {
                    Ok(exit_status) => {
                        if is_write_operation && exit_status.success() {
                            // Update working tree to match pushed content.
                            // git-http-backend updates the git database but not the
                            // working tree, so we must checkout explicitly before
                            // refreshing the content cache.
                            let repo_path = &state_clone.config.content.repo_path;
                            match tokio::process::Command::new("git")
                                .args(["-C", &repo_path.to_string_lossy(), "checkout", "-f", "HEAD"])
                                .output()
                                .await
                            {
                                Ok(output) if !output.status.success() => {
                                    tracing::error!(
                                        "git checkout failed after push: {}",
                                        String::from_utf8_lossy(&output.stderr)
                                    );
                                }
                                Err(e) => {
                                    tracing::error!(
                                        "Failed to run git checkout after push: {}",
                                        e
                                    );
                                }
                                _ => {}
                            }

                            if let Err(e) = state_clone.riley_cms.refresh().await {
                                tracing::error!("Failed to refresh content after git push: {}", e);
                            }
                            state_clone.riley_cms.fire_webhooks().await;
                        }
                    }
                    Err(e) => {
                        tracing::error!("Git CGI completion error: {}", e);
                    }
                }
            });

            response
        }
        Err(e) => {
            tracing::error!("Git CGI error: {}", e);

            // Check if this was a body-too-large error
            let error_msg = e.to_string();
            if error_msg.contains("exceeds maximum") {
                return (
                    StatusCode::PAYLOAD_TOO_LARGE,
                    Json(ErrorResponse { error: error_msg }),
                )
                    .into_response();
            }

            (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(ErrorResponse {
                    error: "Git operation failed".to_string(),
                }),
            )
                .into_response()
        }
    }
}