crabmate 0.4.0

Rust AI agent: OpenAI-compatible chat/completions, function calling, HTTP serve, ops CLI
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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
//! GitHub OAuth Device Flow:客户端提供 `client_id`,服务端代要码与轮询;成功后经 Cookie / JSON 交给客户端。
//!
//! `device_code` 仅留在本模块内存;服务端不把 user token 写入钥匙串或磁盘。

use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::{Duration, Instant};

use axum::Json;
use axum::extract::State;
use axum::http::{HeaderMap, HeaderValue, StatusCode, header};
use axum::response::{IntoResponse, Response};
use serde::{Deserialize, Serialize};
use serde_json::json;
use tokio::sync::Mutex;
use tracing::{debug, warn};

use crate::web::app_state::AppStateHttpCore;
use crate::web::github_token_request::{
    GITHUB_TOKEN_COOKIE_NAME, wants_github_token_body_delivery,
};

const GITHUB_DEVICE_CODE_URL: &str = "https://github.com/login/device/code";
const GITHUB_ACCESS_TOKEN_URL: &str = "https://github.com/login/oauth/access_token";

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub(crate) enum DeviceFlowState {
    Pending,
    SlowDown,
    Success,
    Denied,
    Expired,
    Cancelled,
    Error,
}

struct DeviceSession {
    device_code: String,
    client_id: String,
    interval: Duration,
    expires_at: Instant,
    cancel: Arc<AtomicBool>,
    state: DeviceFlowState,
    login: Option<String>,
    scopes: Option<String>,
    error: Option<String>,
    /// 成功后暂存;status 投递(Cookie / 可选 body)后清空。
    access_token: Option<String>,
}

static DEVICE_SESSION: Mutex<Option<DeviceSession>> = Mutex::const_new(None);

/// OAuth App 可用 `repo` 等;GitHub App Device Flow 通常**省略** scope(靠 App 权限)。
/// 环境变量 **`CM_GITHUB_OAUTH_SCOPES`**:未设置或空 → 不传 `scope`;非空则原样提交(空格分隔)。
fn oauth_scopes_from_env() -> Option<String> {
    std::env::var("CM_GITHUB_OAUTH_SCOPES")
        .ok()
        .map(|s| s.trim().to_string())
        .filter(|s| !s.is_empty())
}

fn ensure_verification_uri_complete(verification_uri: &str, user_code: &str) -> String {
    let base = verification_uri.trim();
    let code = user_code.trim();
    if base.is_empty() {
        return format!("https://github.com/login/device?user_code={code}");
    }
    if base.contains("user_code=") {
        return base.to_string();
    }
    let sep = if base.contains('?') { '&' } else { '?' };
    format!("{base}{sep}user_code={code}")
}

fn validate_oauth_client_id(raw: &str) -> Option<String> {
    let id = raw.trim();
    if id.is_empty() || id.len() > 128 {
        return None;
    }
    if !id
        .bytes()
        .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'.' | b'_' | b'-'))
    {
        return None;
    }
    Some(id.to_string())
}

#[derive(Debug, Deserialize)]
pub(crate) struct DeviceStartRequest {
    pub client_id: String,
}

#[derive(Debug, Serialize)]
pub(crate) struct DeviceStartResponse {
    pub user_code: String,
    pub verification_uri: String,
    pub verification_uri_complete: String,
    pub expires_in: u64,
    pub interval: u64,
}

#[derive(Debug, Serialize)]
pub(crate) struct DeviceStatusResponse {
    pub state: DeviceFlowState,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub login: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scopes: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub access_token: Option<String>,
}

fn err_json(
    status: StatusCode,
    code: &str,
    message: &str,
) -> (StatusCode, Json<serde_json::Value>) {
    (
        status,
        Json(json!({
            "ok": false,
            "code": code,
            "error": message,
        })),
    )
}

fn form_encode(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for b in s.bytes() {
        match b {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
                out.push(b as char);
            }
            _ => out.push_str(&format!("%{b:02X}")),
        }
    }
    out
}

fn form_body(pairs: &[(&str, &str)]) -> String {
    pairs
        .iter()
        .map(|(k, v)| format!("{}={}", form_encode(k), form_encode(v)))
        .collect::<Vec<_>>()
        .join("&")
}

fn origin_matches_host(origin: &str, host: &str) -> bool {
    let origin = origin.trim().trim_end_matches('/');
    let host = host.trim();
    if let Some(rest) = origin.strip_prefix("https://") {
        return rest == host || rest == format!("www.{host}") || host == format!("www.{rest}");
    }
    if let Some(rest) = origin.strip_prefix("http://") {
        return rest == host;
    }
    false
}

fn request_is_https(headers: &HeaderMap) -> bool {
    headers
        .get("x-forwarded-proto")
        .and_then(|v| v.to_str().ok())
        .is_some_and(|p| {
            p.split(',')
                .next()
                .is_some_and(|s| s.trim().eq_ignore_ascii_case("https"))
        })
        || headers
            .get(header::ORIGIN)
            .and_then(|v| v.to_str().ok())
            .is_some_and(|o| o.trim().starts_with("https://"))
}

fn cookie_secure_flag(headers: &HeaderMap) -> bool {
    if request_is_https(headers) {
        return true;
    }
    let host = headers
        .get(header::HOST)
        .and_then(|v| v.to_str().ok())
        .unwrap_or("")
        .to_ascii_lowercase();
    let host_only = host.split(':').next().unwrap_or("");
    matches!(host_only, "localhost" | "127.0.0.1" | "::1")
}

fn cookie_samesite(headers: &HeaderMap) -> &'static str {
    let origin = headers
        .get(header::ORIGIN)
        .and_then(|v| v.to_str().ok())
        .map(str::trim)
        .filter(|s| !s.is_empty());
    let host = headers
        .get(header::HOST)
        .and_then(|v| v.to_str().ok())
        .map(str::trim)
        .filter(|s| !s.is_empty());
    match (origin, host) {
        (Some(o), Some(h)) if origin_matches_host(o, h) => "Strict",
        (Some(_), _) => "None",
        _ => "Strict",
    }
}

fn set_github_token_cookie_header(token: &str, headers: &HeaderMap) -> Option<HeaderValue> {
    let mut parts = vec![
        format!("{GITHUB_TOKEN_COOKIE_NAME}={}", form_encode(token)),
        "HttpOnly".into(),
        "Path=/".into(),
        format!("SameSite={}", cookie_samesite(headers)),
    ];
    if cookie_secure_flag(headers) || cookie_samesite(headers) == "None" {
        parts.push("Secure".into());
    }
    HeaderValue::from_str(&parts.join("; ")).ok()
}

fn clear_github_token_cookie_header(headers: &HeaderMap) -> Option<HeaderValue> {
    let mut parts = vec![
        format!("{GITHUB_TOKEN_COOKIE_NAME}="),
        "HttpOnly".into(),
        "Path=/".into(),
        "Max-Age=0".into(),
        format!("SameSite={}", cookie_samesite(headers)),
    ];
    if cookie_secure_flag(headers) || cookie_samesite(headers) == "None" {
        parts.push("Secure".into());
    }
    HeaderValue::from_str(&parts.join("; ")).ok()
}

pub(crate) async fn github_oauth_device_start_handler(
    State(http): State<AppStateHttpCore>,
    Json(body): Json<DeviceStartRequest>,
) -> Result<Json<DeviceStartResponse>, (StatusCode, Json<serde_json::Value>)> {
    let Some(client_id) = validate_oauth_client_id(&body.client_id) else {
        return Err(err_json(
            StatusCode::BAD_REQUEST,
            "GITHUB_OAUTH_CLIENT_ID_REQUIRED",
            "请在请求体提供非空的 client_id(GitHub OAuth / App Client ID)",
        ));
    };

    let scopes_opt = oauth_scopes_from_env();
    let mut form_pairs: Vec<(&str, &str)> = vec![("client_id", client_id.as_str())];
    if let Some(ref sc) = scopes_opt {
        form_pairs.push(("scope", sc.as_str()));
    }
    let form = form_body(&form_pairs);
    let resp = http
        .client
        .post(GITHUB_DEVICE_CODE_URL)
        .header("Accept", "application/json")
        .header("Content-Type", "application/x-www-form-urlencoded")
        .body(form)
        .send()
        .await
        .map_err(|e| {
            err_json(
                StatusCode::BAD_GATEWAY,
                "GITHUB_DEVICE_START_FAILED",
                &format!("请求 GitHub device/code 失败:{e}"),
            )
        })?;
    if !resp.status().is_success() {
        let status = resp.status();
        let body = resp.text().await.unwrap_or_default();
        warn!(
            target: "crabmate",
            %status,
            body_len = body.len(),
            "GitHub device/code 非成功响应"
        );
        return Err(err_json(
            StatusCode::BAD_GATEWAY,
            "GITHUB_DEVICE_START_FAILED",
            &format!("GitHub device/code 返回 HTTP {status}"),
        ));
    }
    let v: serde_json::Value = resp.json().await.map_err(|e| {
        err_json(
            StatusCode::BAD_GATEWAY,
            "GITHUB_DEVICE_START_FAILED",
            &format!("解析 device/code 响应失败:{e}"),
        )
    })?;

    let user_code = v
        .get("user_code")
        .and_then(|x| x.as_str())
        .unwrap_or("")
        .trim()
        .to_string();
    let device_code = v
        .get("device_code")
        .and_then(|x| x.as_str())
        .unwrap_or("")
        .trim()
        .to_string();
    let verification_uri = v
        .get("verification_uri")
        .and_then(|x| x.as_str())
        .unwrap_or("https://github.com/login/device")
        .trim()
        .to_string();
    let verification_uri_complete = v
        .get("verification_uri_complete")
        .and_then(|x| x.as_str())
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .map(str::to_string)
        .unwrap_or_else(|| ensure_verification_uri_complete(&verification_uri, &user_code));
    let expires_in = v
        .get("expires_in")
        .and_then(|x| x.as_u64())
        .unwrap_or(900)
        .max(60);
    let interval_secs = v
        .get("interval")
        .and_then(|x| x.as_u64())
        .unwrap_or(5)
        .max(1);

    if user_code.is_empty() || device_code.is_empty() {
        return Err(err_json(
            StatusCode::BAD_GATEWAY,
            "GITHUB_DEVICE_START_FAILED",
            "GitHub device/code 响应缺少 user_code 或 device_code",
        ));
    }

    let cancel = Arc::new(AtomicBool::new(false));
    {
        let mut guard = DEVICE_SESSION.lock().await;
        if let Some(prev) = guard.as_ref() {
            prev.cancel.store(true, Ordering::SeqCst);
        }
        *guard = Some(DeviceSession {
            device_code: device_code.clone(),
            client_id: client_id.clone(),
            interval: Duration::from_secs(interval_secs),
            expires_at: Instant::now() + Duration::from_secs(expires_in),
            cancel: Arc::clone(&cancel),
            state: DeviceFlowState::Pending,
            login: None,
            scopes: None,
            error: None,
            access_token: None,
        });
    }

    let client = http.client.clone();
    tokio::spawn(async move {
        poll_device_token(client, cancel).await;
    });

    Ok(Json(DeviceStartResponse {
        user_code,
        verification_uri,
        verification_uri_complete,
        expires_in,
        interval: interval_secs,
    }))
}

pub(crate) async fn github_oauth_device_status_handler(headers: HeaderMap) -> Response {
    let deliver_body = wants_github_token_body_delivery(&headers);
    let mut guard = DEVICE_SESSION.lock().await;
    let (body, set_cookie) = match guard.as_mut() {
        None => (
            DeviceStatusResponse {
                state: DeviceFlowState::Cancelled,
                login: None,
                scopes: None,
                error: Some("无进行中的 Device Flow 会话".into()),
                access_token: None,
            },
            None,
        ),
        Some(s) => {
            let mut access_token = None;
            let mut set_cookie = None;
            if s.state == DeviceFlowState::Success
                && let Some(token) = s.access_token.take()
            {
                set_cookie = set_github_token_cookie_header(&token, &headers);
                if deliver_body {
                    access_token = Some(token);
                } else if set_cookie.is_none() {
                    // Cookie 头构造失败时把 token 放回会话,避免静默丢凭据。
                    s.access_token = Some(token);
                }
            }
            (
                DeviceStatusResponse {
                    state: s.state,
                    login: s.login.clone(),
                    scopes: s.scopes.clone(),
                    error: s.error.clone(),
                    access_token,
                },
                set_cookie,
            )
        }
    };
    drop(guard);
    let mut res = Json(body).into_response();
    if let Some(cookie) = set_cookie {
        res.headers_mut().insert(header::SET_COOKIE, cookie);
    }
    res
}

pub(crate) async fn github_oauth_device_cancel_handler() -> StatusCode {
    let mut guard = DEVICE_SESSION.lock().await;
    if let Some(s) = guard.as_mut() {
        s.cancel.store(true, Ordering::SeqCst);
        s.state = DeviceFlowState::Cancelled;
        s.access_token = None;
    }
    *guard = None;
    StatusCode::NO_CONTENT
}

/// 清除浏览器 HttpOnly GitHub token Cookie(壳另清本机钥匙串)。
pub(crate) async fn github_oauth_device_logout_handler(headers: HeaderMap) -> Response {
    let mut res = StatusCode::NO_CONTENT.into_response();
    if let Some(cookie) = clear_github_token_cookie_header(&headers) {
        res.headers_mut().insert(header::SET_COOKIE, cookie);
    }
    res
}

fn session_is_ours(s: &DeviceSession, cancel: &Arc<AtomicBool>) -> bool {
    Arc::ptr_eq(&s.cancel, cancel)
}

async fn mark_session(
    cancel: &Arc<AtomicBool>,
    state: DeviceFlowState,
    error: Option<String>,
    clear_device_code: bool,
    login: Option<String>,
    scopes: Option<String>,
    access_token: Option<String>,
) {
    let mut guard = DEVICE_SESSION.lock().await;
    let Some(s) = guard.as_mut() else {
        return;
    };
    if !session_is_ours(s, cancel) {
        return;
    }
    s.state = state;
    s.error = error;
    if clear_device_code {
        s.device_code.clear();
    }
    if login.is_some() {
        s.login = login;
    }
    if scopes.is_some() {
        s.scopes = scopes;
    }
    if access_token.is_some() {
        s.access_token = access_token;
    }
}

async fn read_poll_snapshot(
    cancel: &Arc<AtomicBool>,
) -> Option<(String, String, Duration, Instant)> {
    let guard = DEVICE_SESSION.lock().await;
    let s = guard.as_ref()?;
    if !session_is_ours(s, cancel) {
        return None;
    }
    if Instant::now() >= s.expires_at {
        return None;
    }
    Some((
        s.device_code.clone(),
        s.client_id.clone(),
        s.interval,
        s.expires_at,
    ))
}

async fn apply_access_token(
    client: &reqwest::Client,
    cancel: &Arc<AtomicBool>,
    token: &str,
    scopes: Option<String>,
) {
    let login = fetch_github_login(client, token).await;
    mark_session(
        cancel,
        DeviceFlowState::Success,
        None,
        true,
        login,
        scopes,
        Some(token.to_string()),
    )
    .await;
}

/// 处理非 access_token 的 OAuth 错误;返回是否应结束轮询。
async fn handle_oauth_error(cancel: &Arc<AtomicBool>, err: &str, interval: Duration) -> bool {
    match err {
        "authorization_pending" => {
            mark_session(
                cancel,
                DeviceFlowState::Pending,
                None,
                false,
                None,
                None,
                None,
            )
            .await;
            tokio::time::sleep(interval).await;
            false
        }
        "slow_down" => {
            let sleep_for = {
                let mut guard = DEVICE_SESSION.lock().await;
                if let Some(s) = guard.as_mut()
                    && session_is_ours(s, cancel)
                {
                    s.state = DeviceFlowState::SlowDown;
                    s.interval += Duration::from_secs(5);
                    s.interval
                } else {
                    interval + Duration::from_secs(5)
                }
            };
            tokio::time::sleep(sleep_for).await;
            false
        }
        "access_denied" => {
            mark_session(
                cancel,
                DeviceFlowState::Denied,
                Some("用户拒绝了授权".into()),
                false,
                None,
                None,
                None,
            )
            .await;
            true
        }
        "expired_token" => {
            mark_session(
                cancel,
                DeviceFlowState::Expired,
                Some("授权码已过期,请重新连接".into()),
                false,
                None,
                None,
                None,
            )
            .await;
            true
        }
        other => {
            let msg = if other.is_empty() {
                "换取 access_token 失败".into()
            } else {
                format!("GitHub 错误:{other}")
            };
            mark_session(
                cancel,
                DeviceFlowState::Error,
                Some(msg),
                false,
                None,
                None,
                None,
            )
            .await;
            true
        }
    }
}

async fn poll_device_token(client: reqwest::Client, cancel: Arc<AtomicBool>) {
    loop {
        if cancel.load(Ordering::SeqCst) {
            return;
        }
        let Some((device_code, client_id, interval, expires_at)) =
            read_poll_snapshot(&cancel).await
        else {
            let mut guard = DEVICE_SESSION.lock().await;
            if let Some(s) = guard.as_mut()
                && session_is_ours(s, &cancel)
                && Instant::now() >= s.expires_at
            {
                s.state = DeviceFlowState::Expired;
                s.error = Some("授权码已过期,请重新连接".into());
            }
            return;
        };

        if Instant::now() >= expires_at {
            mark_session(
                &cancel,
                DeviceFlowState::Expired,
                Some("授权码已过期,请重新连接".into()),
                false,
                None,
                None,
                None,
            )
            .await;
            return;
        }

        let form = form_body(&[
            ("client_id", client_id.as_str()),
            ("device_code", device_code.as_str()),
            ("grant_type", "urn:ietf:params:oauth:grant-type:device_code"),
        ]);
        let resp = match client
            .post(GITHUB_ACCESS_TOKEN_URL)
            .header("Accept", "application/json")
            .header("Content-Type", "application/x-www-form-urlencoded")
            .body(form)
            .send()
            .await
        {
            Ok(r) => r,
            Err(e) => {
                debug!(target: "crabmate", error = %e, "GitHub access_token 请求失败,将重试");
                tokio::time::sleep(interval).await;
                continue;
            }
        };
        let v: serde_json::Value = match resp.json().await {
            Ok(v) => v,
            Err(_) => {
                tokio::time::sleep(interval).await;
                continue;
            }
        };

        if let Some(token) = v
            .get("access_token")
            .and_then(|x| x.as_str())
            .map(str::trim)
            .filter(|s| !s.is_empty())
        {
            let scopes = v
                .get("scope")
                .and_then(|x| x.as_str())
                .map(|s| s.to_string());
            apply_access_token(&client, &cancel, token, scopes).await;
            return;
        }

        let err = v.get("error").and_then(|x| x.as_str()).unwrap_or("");
        if handle_oauth_error(&cancel, err, interval).await {
            return;
        }
    }
}

async fn fetch_github_login(client: &reqwest::Client, token: &str) -> Option<String> {
    let resp = client
        .get("https://api.github.com/user")
        .header("Accept", "application/vnd.github+json")
        .header("User-Agent", "crabmate")
        .bearer_auth(token)
        .send()
        .await
        .ok()?;
    if !resp.status().is_success() {
        return None;
    }
    let v: serde_json::Value = resp.json().await.ok()?;
    v.get("login")
        .and_then(|x| x.as_str())
        .map(|s| s.to_string())
}

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

    #[test]
    fn complete_uri_appends_user_code() {
        assert_eq!(
            ensure_verification_uri_complete("https://github.com/login/device", "ABCD-1234"),
            "https://github.com/login/device?user_code=ABCD-1234"
        );
        assert!(
            ensure_verification_uri_complete(
                "https://github.com/login/device?user_code=ABCD-1234",
                "ZZZZ"
            )
            .contains("ABCD-1234")
        );
    }

    #[test]
    fn validate_client_id_accepts_github_shapes() {
        assert_eq!(
            validate_oauth_client_id("  Iv1.abcDEF12  ").as_deref(),
            Some("Iv1.abcDEF12")
        );
        assert!(validate_oauth_client_id("").is_none());
        assert!(validate_oauth_client_id("bad id").is_none());
    }

    #[test]
    fn origin_host_match_detects_same_site() {
        assert!(origin_matches_host(
            "http://127.0.0.1:8080",
            "127.0.0.1:8080"
        ));
        assert!(!origin_matches_host(
            "https://ui.example.com",
            "api.example.com"
        ));
    }
}