Skip to main content

atmos/os_lib/
cloud.rs

1//! Phase 7 外部連携: OAuth2 + Google Drive / GitHub の REST クライアント。
2//!
3//! ヘッドレス OS のため OAuth2 は **デバイスフロー**(ブラウザ無しで user_code を表示し、
4//! 別端末で認可 → token をポーリング取得)を用いる。リクエスト構築と JSON レスポンス解析は
5//! 純関数として `cloud::selftest()` で検証する。実際の HTTPS 呼び出しは `tls::https_request`
6//! 経由で行う(実機/ネットワーク必須のため QEMU 自己テストでは解析部のみを確認)。
7
8#![allow(dead_code)]
9
10use crate::os_lib::json::{self, JsonValue};
11use alloc::format;
12use alloc::string::{String, ToString};
13use alloc::vec::Vec;
14
15// ============ JSON アクセスヘルパ ============
16
17fn obj_get<'a>(v: &'a JsonValue, key: &str) -> Option<&'a JsonValue> {
18    if let JsonValue::Object(m) = v {
19        m.get(key)
20    } else {
21        None
22    }
23}
24
25fn val_to_string(v: &JsonValue) -> Option<String> {
26    match v {
27        JsonValue::String(s) => Some(s.clone()),
28        JsonValue::Number(n) => Some(crate::os_lib::ecma::value::fmt_number(*n)),
29        JsonValue::Bool(b) => Some(b.to_string()),
30        _ => None,
31    }
32}
33
34fn field_str(v: &JsonValue, key: &str) -> String {
35    obj_get(v, key).and_then(val_to_string).unwrap_or_default()
36}
37
38/// 複数キー候補のうち最初に見つかった文字列。
39fn field_str_any(v: &JsonValue, keys: &[&str]) -> String {
40    for k in keys {
41        if let Some(found) = obj_get(v, k).and_then(val_to_string) {
42            return found;
43        }
44    }
45    String::new()
46}
47
48fn field_i64(v: &JsonValue, key: &str) -> i64 {
49    match obj_get(v, key) {
50        Some(JsonValue::Number(n)) => *n as i64,
51        Some(JsonValue::String(s)) => s.parse().unwrap_or(0),
52        _ => 0,
53    }
54}
55
56fn field_bool(v: &JsonValue, key: &str) -> bool {
57    matches!(obj_get(v, key), Some(JsonValue::Bool(true)))
58}
59
60// ============ URL / フォームエンコード ============
61
62/// パーセントエンコード(unreserved 以外を %XX に)。
63pub fn url_encode(s: &str) -> String {
64    let mut out = String::new();
65    for b in s.bytes() {
66        let c = b as char;
67        if c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.' | '~') {
68            out.push(c);
69        } else {
70            out.push('%');
71            out.push(hex_digit(b >> 4));
72            out.push(hex_digit(b & 0xF));
73        }
74    }
75    out
76}
77
78fn hex_digit(n: u8) -> char {
79    match n {
80        0..=9 => (b'0' + n) as char,
81        _ => (b'A' + (n - 10)) as char,
82    }
83}
84
85/// `application/x-www-form-urlencoded` ボディを組み立てる。
86pub fn form_encode(params: &[(&str, &str)]) -> String {
87    let mut parts: Vec<String> = Vec::new();
88    for (k, v) in params {
89        parts.push(format!("{}={}", url_encode(k), url_encode(v)));
90    }
91    parts.join("&")
92}
93
94// ============ OAuth2 ============
95
96#[derive(Default, Debug)]
97pub struct TokenResponse {
98    pub access_token: String,
99    pub refresh_token: String,
100    pub token_type: String,
101    pub expires_in: i64,
102    pub scope: String,
103    /// `authorization_pending` 等のエラーコード(成功時は空)。
104    pub error: String,
105}
106
107pub fn parse_token_response(body: &str) -> TokenResponse {
108    let v = match json::parse(body) {
109        Ok(v) => v,
110        Err(_) => return TokenResponse::default(),
111    };
112    TokenResponse {
113        access_token: field_str(&v, "access_token"),
114        refresh_token: field_str(&v, "refresh_token"),
115        token_type: field_str(&v, "token_type"),
116        expires_in: field_i64(&v, "expires_in"),
117        scope: field_str(&v, "scope"),
118        error: field_str(&v, "error"),
119    }
120}
121
122#[derive(Default, Debug)]
123pub struct DeviceCode {
124    pub device_code: String,
125    pub user_code: String,
126    pub verification_url: String,
127    pub expires_in: i64,
128    pub interval: i64,
129}
130
131pub fn parse_device_code(body: &str) -> DeviceCode {
132    let v = match json::parse(body) {
133        Ok(v) => v,
134        Err(_) => return DeviceCode::default(),
135    };
136    DeviceCode {
137        device_code: field_str(&v, "device_code"),
138        user_code: field_str(&v, "user_code"),
139        // Google は verification_url、GitHub は verification_uri。
140        verification_url: field_str_any(&v, &["verification_url", "verification_uri"]),
141        expires_in: field_i64(&v, "expires_in"),
142        interval: field_i64(&v, "interval"),
143    }
144}
145
146// ---- リクエストボディ構築 ----
147
148pub fn google_device_code_body(client_id: &str, scope: &str) -> String {
149    form_encode(&[("client_id", client_id), ("scope", scope)])
150}
151
152pub fn google_token_body(client_id: &str, client_secret: &str, device_code: &str) -> String {
153    form_encode(&[
154        ("client_id", client_id),
155        ("client_secret", client_secret),
156        ("device_code", device_code),
157        ("grant_type", "urn:ietf:params:oauth:grant-type:device_code"),
158    ])
159}
160
161pub fn google_refresh_body(client_id: &str, client_secret: &str, refresh_token: &str) -> String {
162    form_encode(&[
163        ("client_id", client_id),
164        ("client_secret", client_secret),
165        ("refresh_token", refresh_token),
166        ("grant_type", "refresh_token"),
167    ])
168}
169
170pub fn github_device_code_body(client_id: &str, scope: &str) -> String {
171    form_encode(&[("client_id", client_id), ("scope", scope)])
172}
173
174pub fn github_token_body(client_id: &str, device_code: &str) -> String {
175    form_encode(&[
176        ("client_id", client_id),
177        ("device_code", device_code),
178        ("grant_type", "urn:ietf:params:oauth:grant-type:device_code"),
179    ])
180}
181
182// ============ HTTPS 実呼び出し(ネットワーク必須) ============
183
184/// OAuth2 トークン/デバイスコードエンドポイントへ form POST し、レスポンス本文を返す。
185pub fn oauth_post(host: &str, path: &str, form: &str) -> Result<String, &'static str> {
186    let res = crate::kernel::tls::https_request(
187        "POST",
188        host,
189        path,
190        &[("Accept", "application/json")],
191        "application/x-www-form-urlencoded",
192        Some(form.as_bytes()),
193    )?;
194    Ok(res.body)
195}
196
197/// Bearer トークン付き API GET。
198pub fn api_get(host: &str, path: &str, token: &str) -> Result<String, &'static str> {
199    let auth = format!("Bearer {}", token);
200    let res = crate::kernel::tls::https_request(
201        "GET",
202        host,
203        path,
204        &[("Authorization", &auth), ("Accept", "application/json")],
205        "",
206        None,
207    )?;
208    Ok(res.body)
209}
210
211// ============ Google Drive ============
212
213#[derive(Default, Debug, Clone)]
214pub struct DriveFile {
215    pub id: String,
216    pub name: String,
217    pub mime_type: String,
218}
219
220pub fn parse_drive_file_list(body: &str) -> Vec<DriveFile> {
221    let v = match json::parse(body) {
222        Ok(v) => v,
223        Err(_) => return Vec::new(),
224    };
225    let mut out = Vec::new();
226    if let Some(JsonValue::Array(files)) = obj_get(&v, "files") {
227        for f in files {
228            out.push(DriveFile {
229                id: field_str(f, "id"),
230                name: field_str(f, "name"),
231                mime_type: field_str(f, "mimeType"),
232            });
233        }
234    }
235    out
236}
237
238/// Google Drive のファイル一覧(live)。
239pub fn drive_list_files(token: &str) -> Result<Vec<DriveFile>, &'static str> {
240    let body = api_get(
241        "www.googleapis.com",
242        "/drive/v3/files?pageSize=100&fields=files(id,name,mimeType)",
243        token,
244    )?;
245    Ok(parse_drive_file_list(&body))
246}
247
248/// 指定フォルダ直下のファイル一覧。`folder_id` が None なら Drive ルート直下。
249/// フォルダ階層ナビゲーション用(drive アプリの Enter でフォルダに入る操作)。
250pub fn drive_list_files_in_folder(
251    token: &str,
252    folder_id: Option<&str>,
253) -> Result<Vec<DriveFile>, &'static str> {
254    let parent = folder_id.unwrap_or("root");
255    let q = format!("'{}' in parents and trashed = false", parent);
256    let path = format!(
257        "/drive/v3/files?pageSize=100&fields=files(id,name,mimeType)&q={}",
258        url_encode(&q)
259    );
260    let body = api_get("www.googleapis.com", &path, token)?;
261    Ok(parse_drive_file_list(&body))
262}
263
264// ---- 読み書き REST 操作 ----
265
266const GOOGLE_API_HOST: &str = "www.googleapis.com";
267/// Google ネイティブ Docs の MIME(本文取得は export が必要)。
268pub const MIME_GOOGLE_DOC: &str = "application/vnd.google-apps.document";
269/// Google Drive のフォルダ MIME。
270pub const MIME_FOLDER: &str = "application/vnd.google-apps.folder";
271
272/// Bearer トークン付き API 送信(GET 以外の任意メソッド + ボディ)。
273fn api_send(
274    method: &str,
275    host: &str,
276    path: &str,
277    token: &str,
278    content_type: &str,
279    body: Option<&[u8]>,
280) -> Result<crate::kernel::net::HttpsGetResult, &'static str> {
281    let auth = format!("Bearer {}", token);
282    crate::kernel::tls::https_request(
283        method,
284        host,
285        path,
286        &[("Authorization", &auth), ("Accept", "application/json")],
287        content_type,
288        body,
289    )
290}
291
292/// HTTP ステータスが 2xx かを判定。失敗時はステータスを含むエラーにしたいが
293/// `&'static str` 制約のため、代表的なケースを分岐する。
294fn ok_or_status(res: &crate::kernel::net::HttpsGetResult) -> Result<(), &'static str> {
295    match res.status_code {
296        200..=299 => Ok(()),
297        401 => Err("unauthorized (token expired?)"),
298        403 => Err("forbidden (scope/quota)"),
299        404 => Err("not found"),
300        _ => Err("drive API error"),
301    }
302}
303
304/// 通常ファイル(text/plain 等)の本文をダウンロード(`alt=media`)。
305/// バイナリは https_request が UTF-8 lossy になるため、テキスト用途のみ想定。
306pub fn drive_download_text(token: &str, file_id: &str) -> Result<String, &'static str> {
307    let path = format!("/drive/v3/files/{}?alt=media", url_encode(file_id));
308    let res = api_send("GET", GOOGLE_API_HOST, &path, token, "", None)?;
309    ok_or_status(&res)?;
310    Ok(res.body)
311}
312
313/// ファイル本文を生バイト列でダウンロード(`alt=media`)。画像/zip 等のバイナリ用。
314/// `HttpsGetResult.body_bytes` を使うため lossy UTF-8 変換によるデータ破損が起きない。
315pub fn drive_download_binary(token: &str, file_id: &str) -> Result<Vec<u8>, &'static str> {
316    let path = format!("/drive/v3/files/{}?alt=media", url_encode(file_id));
317    let res = api_send("GET", GOOGLE_API_HOST, &path, token, "", None)?;
318    ok_or_status(&res)?;
319    Ok(res.body_bytes)
320}
321
322/// Google ネイティブ Docs をプレーンテキストとして export して取得。
323pub fn drive_export_doc_text(token: &str, file_id: &str) -> Result<String, &'static str> {
324    let path = format!(
325        "/drive/v3/files/{}/export?mimeType=text%2Fplain",
326        url_encode(file_id)
327    );
328    let res = api_send("GET", GOOGLE_API_HOST, &path, token, "", None)?;
329    ok_or_status(&res)?;
330    Ok(res.body)
331}
332
333/// MIME に応じて Docs は export、それ以外は alt=media でダウンロード。
334pub fn drive_get_text(token: &str, file_id: &str, mime_type: &str) -> Result<String, &'static str> {
335    if mime_type == MIME_GOOGLE_DOC {
336        drive_export_doc_text(token, file_id)
337    } else {
338        drive_download_text(token, file_id)
339    }
340}
341
342/// ファイルの mimeType のみを取得(CUI の `drive cat <id>` のように mime_type を
343/// 事前に知らない呼び出し元向け。mime に応じた export/download 分岐に使う)。
344pub fn drive_get_mime_type(token: &str, file_id: &str) -> Result<String, &'static str> {
345    let path = format!(
346        "/drive/v3/files/{}?fields=mimeType",
347        url_encode(file_id)
348    );
349    let body = api_get(GOOGLE_API_HOST, &path, token)?;
350    let v = json::parse(&body).unwrap_or(JsonValue::Null);
351    Ok(field_str(&v, "mimeType"))
352}
353
354/// file_id だけから本文を取得する(mime_type をまず問い合わせてから分岐)。
355/// CUI コマンド(`drive cat <id>`)向けの一括ヘルパー。
356pub fn drive_cat(token: &str, file_id: &str) -> Result<String, &'static str> {
357    let mime_type = drive_get_mime_type(token, file_id)?;
358    drive_get_text(token, file_id, &mime_type)
359}
360
361/// JSON 文字列値のエスケープ(メタデータ部のファイル名用、最小限)。
362fn json_escape(s: &str) -> String {
363    let mut out = String::new();
364    for c in s.chars() {
365        match c {
366            '"' => out.push_str("\\\""),
367            '\\' => out.push_str("\\\\"),
368            '\n' => out.push_str("\\n"),
369            '\r' => out.push_str("\\r"),
370            '\t' => out.push_str("\\t"),
371            c if (c as u32) < 0x20 => out.push_str(&format!("\\u{:04x}", c as u32)),
372            c => out.push(c),
373        }
374    }
375    out
376}
377
378/// multipart/related ボディを組み立てる(メタデータ JSON + メディア本文)。
379/// 返り値は (boundary, body_bytes)。Content-Type に boundary を載せる必要がある。
380pub fn drive_multipart_body(name: &str, mime_type: &str, content: &[u8]) -> (String, Vec<u8>) {
381    // 衝突しにくい固定 boundary(本文に出現しない前提の十分長い文字列)。
382    let boundary = String::from("atmos_drive_boundary_7f3a9c1e2b");
383    let meta = format!("{{\"name\":\"{}\"}}", json_escape(name));
384
385    let mut body: Vec<u8> = Vec::new();
386    let push = |s: &str, b: &mut Vec<u8>| b.extend_from_slice(s.as_bytes());
387
388    push(&format!("--{}\r\n", boundary), &mut body);
389    push(
390        "Content-Type: application/json; charset=UTF-8\r\n\r\n",
391        &mut body,
392    );
393    push(&meta, &mut body);
394    push("\r\n", &mut body);
395    push(&format!("--{}\r\n", boundary), &mut body);
396    push(&format!("Content-Type: {}\r\n\r\n", mime_type), &mut body);
397    body.extend_from_slice(content);
398    push(&format!("\r\n--{}--\r\n", boundary), &mut body);
399
400    (boundary, body)
401}
402
403/// 新規ファイルをアップロード(multipart:メタデータ + 本文)。成功時は作成された file id。
404pub fn drive_create_text(token: &str, name: &str, content: &str) -> Result<String, &'static str> {
405    let (boundary, body) = drive_multipart_body(name, "text/plain", content.as_bytes());
406    let ctype = format!("multipart/related; boundary={}", boundary);
407    let res = api_send(
408        "POST",
409        GOOGLE_API_HOST,
410        "/upload/drive/v3/files?uploadType=multipart&fields=id",
411        token,
412        &ctype,
413        Some(&body),
414    )?;
415    ok_or_status(&res)?;
416    Ok(field_str(
417        &json::parse(&res.body).unwrap_or(JsonValue::Null),
418        "id",
419    ))
420}
421
422/// 既存ファイルの本文を上書き(メディアのみ PATCH)。
423pub fn drive_update_text(token: &str, file_id: &str, content: &str) -> Result<(), &'static str> {
424    let path = format!(
425        "/upload/drive/v3/files/{}?uploadType=media",
426        url_encode(file_id)
427    );
428    let res = api_send(
429        "PATCH",
430        GOOGLE_API_HOST,
431        &path,
432        token,
433        "text/plain",
434        Some(content.as_bytes()),
435    )?;
436    ok_or_status(&res)
437}
438
439/// ファイルを削除。
440pub fn drive_delete(token: &str, file_id: &str) -> Result<(), &'static str> {
441    let path = format!("/drive/v3/files/{}", url_encode(file_id));
442    let res = api_send("DELETE", GOOGLE_API_HOST, &path, token, "", None)?;
443    // 削除成功は 204 No Content。
444    ok_or_status(&res)
445}
446
447// ============ Google Drive 認証情報の永続化 + デバイスフロー ============
448
449const AUTH_FILE: &str = "gdrive_auth.json";
450const GOOGLE_OAUTH_HOST: &str = "oauth2.googleapis.com";
451/// Drive 読み書きフルスコープ。
452pub const DRIVE_SCOPE: &str = "https://www.googleapis.com/auth/drive";
453
454/// ディスクに保存する Drive 認証状態。client_id/secret はユーザーが Google Cloud で
455/// 発行したものを事前に書き込んでおく(このアプリからは編集しない)。token 類は
456/// デバイスフロー成功後に書き戻す。
457#[derive(Default, Debug, Clone)]
458pub struct GdriveAuth {
459    pub client_id: String,
460    pub client_secret: String,
461    pub access_token: String,
462    pub refresh_token: String,
463}
464
465impl GdriveAuth {
466    pub fn has_client(&self) -> bool {
467        !self.client_id.is_empty() && !self.client_secret.is_empty()
468    }
469    pub fn is_logged_in(&self) -> bool {
470        !self.access_token.is_empty()
471    }
472}
473
474pub fn auth_to_json(a: &GdriveAuth) -> String {
475    format!(
476        "{{\"client_id\":\"{}\",\"client_secret\":\"{}\",\"access_token\":\"{}\",\"refresh_token\":\"{}\"}}",
477        json_escape(&a.client_id), json_escape(&a.client_secret),
478        json_escape(&a.access_token), json_escape(&a.refresh_token)
479    )
480}
481
482pub fn parse_auth(body: &str) -> GdriveAuth {
483    let v = json::parse(body).unwrap_or(JsonValue::Null);
484    GdriveAuth {
485        client_id: field_str(&v, "client_id"),
486        client_secret: field_str(&v, "client_secret"),
487        access_token: field_str(&v, "access_token"),
488        refresh_token: field_str(&v, "refresh_token"),
489    }
490}
491
492/// `gdrive_auth.json` を読む(無ければ default)。
493pub fn load_auth() -> GdriveAuth {
494    let fs = crate::kernel::fs::get_fs();
495    let auth_data = match fs.read_file_checked(AUTH_FILE) {
496        Ok(Some((_m, bytes))) => Some(bytes),
497        Ok(None) => None,
498        Err(e) => {
499            crate::error!("[CLOUD] load_auth failed to read {}: {}", AUTH_FILE, e);
500            None
501        }
502    };
503    if let Some(bytes) = auth_data {
504        if let Ok(s) = core::str::from_utf8(&bytes) {
505            return parse_auth(s);
506        }
507    }
508    GdriveAuth::default()
509}
510
511/// `gdrive_auth.json` に書き戻す。
512pub fn save_auth(a: &GdriveAuth) -> Result<(), &'static str> {
513    let json = auth_to_json(a);
514    let fs = crate::kernel::fs::get_fs();
515    fs.save_file(AUTH_FILE, json.as_bytes(), "system,auth")
516}
517
518/// デバイスコードを要求(ユーザーに user_code と verification_url を提示するため)。
519pub fn drive_request_device_code(client_id: &str) -> Result<DeviceCode, &'static str> {
520    let body = google_device_code_body(client_id, DRIVE_SCOPE);
521    let resp = oauth_post(GOOGLE_OAUTH_HOST, "/device/code", &body)?;
522    Ok(parse_device_code(&resp))
523}
524
525/// device_code でトークンを1回ポーリングする。`authorization_pending` の間は
526/// TokenResponse.error にコードが入る(成功時は access_token が埋まる)。
527pub fn drive_poll_token(
528    client_id: &str,
529    client_secret: &str,
530    device_code: &str,
531) -> Result<TokenResponse, &'static str> {
532    let body = google_token_body(client_id, client_secret, device_code);
533    let resp = oauth_post(GOOGLE_OAUTH_HOST, "/token", &body)?;
534    Ok(parse_token_response(&resp))
535}
536
537/// refresh_token から access_token を更新する。
538pub fn drive_refresh_token(
539    client_id: &str,
540    client_secret: &str,
541    refresh_token: &str,
542) -> Result<TokenResponse, &'static str> {
543    let body = google_refresh_body(client_id, client_secret, refresh_token);
544    let resp = oauth_post(GOOGLE_OAUTH_HOST, "/token", &body)?;
545    Ok(parse_token_response(&resp))
546}
547
548// ============ Microsoft OneDrive (Graph API) ============
549//
550// Google Drive と同じデバイスフロー方式。Microsoft の device flow はパブリック
551// クライアント向けのため client_secret を要求しない点が Google と異なる(よりシンプル)。
552
553pub fn ms_device_code_body(client_id: &str, scope: &str) -> String {
554    form_encode(&[("client_id", client_id), ("scope", scope)])
555}
556
557pub fn ms_token_body(client_id: &str, device_code: &str) -> String {
558    form_encode(&[
559        ("client_id", client_id),
560        ("device_code", device_code),
561        ("grant_type", "urn:ietf:params:oauth:grant-type:device_code"),
562    ])
563}
564
565pub fn ms_refresh_body(client_id: &str, refresh_token: &str, scope: &str) -> String {
566    form_encode(&[
567        ("client_id", client_id),
568        ("refresh_token", refresh_token),
569        ("grant_type", "refresh_token"),
570        ("scope", scope),
571    ])
572}
573
574const MS_OAUTH_HOST: &str = "login.microsoftonline.com";
575const GRAPH_API_HOST: &str = "graph.microsoft.com";
576/// OneDrive 読み書き + オフラインアクセス(refresh_token 発行)に必要な最小スコープ。
577pub const ONEDRIVE_SCOPE: &str = "Files.ReadWrite offline_access";
578
579pub fn onedrive_request_device_code(client_id: &str) -> Result<DeviceCode, &'static str> {
580    let body = ms_device_code_body(client_id, ONEDRIVE_SCOPE);
581    let resp = oauth_post(MS_OAUTH_HOST, "/common/oauth2/v2.0/devicecode", &body)?;
582    Ok(parse_device_code(&resp))
583}
584
585/// device_code でトークンを1回ポーリングする。Google 版と同じ挙動(pending 中は
586/// TokenResponse.error にコードが入る)。
587pub fn onedrive_poll_token(client_id: &str, device_code: &str) -> Result<TokenResponse, &'static str> {
588    let body = ms_token_body(client_id, device_code);
589    let resp = oauth_post(MS_OAUTH_HOST, "/common/oauth2/v2.0/token", &body)?;
590    Ok(parse_token_response(&resp))
591}
592
593pub fn onedrive_refresh_token(
594    client_id: &str,
595    refresh_token: &str,
596) -> Result<TokenResponse, &'static str> {
597    let body = ms_refresh_body(client_id, refresh_token, ONEDRIVE_SCOPE);
598    let resp = oauth_post(MS_OAUTH_HOST, "/common/oauth2/v2.0/token", &body)?;
599    Ok(parse_token_response(&resp))
600}
601
602#[derive(Default, Debug, Clone)]
603pub struct OneDriveFile {
604    pub id: String,
605    pub name: String,
606    pub is_folder: bool,
607}
608
609fn parse_onedrive_file_list(body: &str) -> Vec<OneDriveFile> {
610    let v = match json::parse(body) {
611        Ok(v) => v,
612        Err(_) => return Vec::new(),
613    };
614    let mut out = Vec::new();
615    if let Some(JsonValue::Array(items)) = obj_get(&v, "value") {
616        for it in items {
617            out.push(OneDriveFile {
618                id: field_str(it, "id"),
619                name: field_str(it, "name"),
620                // フォルダかどうかは "folder" プロパティの有無で判定(Graph API の慣例)。
621                is_folder: obj_get(it, "folder").is_some(),
622            });
623        }
624    }
625    out
626}
627
628/// 指定フォルダ直下のアイテム一覧。`folder_id` が None なら OneDrive ルート直下。
629pub fn onedrive_list_files_in_folder(
630    token: &str,
631    folder_id: Option<&str>,
632) -> Result<Vec<OneDriveFile>, &'static str> {
633    let path = match folder_id {
634        Some(id) => format!("/v1.0/me/drive/items/{}/children", url_encode(id)),
635        None => String::from("/v1.0/me/drive/root/children"),
636    };
637    let body = api_get(GRAPH_API_HOST, &path, token)?;
638    Ok(parse_onedrive_file_list(&body))
639}
640
641/// `https://host/path` 形式の絶対URLを (host, path) に分解する(リダイレクト追従用の簡易パーサ)。
642fn split_https_url(url: &str) -> Option<(String, String)> {
643    let rest = url.strip_prefix("https://")?;
644    #[allow(clippy::string_slice)] // find() で ASCII バイト位置を特定済み
645    match rest.find('/') {
646        Some(idx) => Some((String::from(&rest[..idx]), String::from(&rest[idx..]))),
647        None => Some((String::from(rest), String::from("/"))),
648    }
649}
650
651/// ファイル本文を生バイト列でダウンロード。Graph API はしばしば 302 で
652/// 署名付きCDN URLへリダイレクトするため、その場合は Authorization ヘッダを付けずに
653/// リダイレクト先へ再取得する(署名済みURLは通常トークン不要かつ拒否されることがある)。
654pub fn onedrive_download_binary(token: &str, file_id: &str) -> Result<Vec<u8>, &'static str> {
655    let path = format!("/v1.0/me/drive/items/{}/content", url_encode(file_id));
656    let res = api_send("GET", GRAPH_API_HOST, &path, token, "", None)?;
657    if matches!(res.status_code, 301 | 302 | 303 | 307 | 308) {
658        let location = res
659            .headers
660            .get("location")
661            .cloned()
662            .unwrap_or_default();
663        let (host, redir_path) =
664            split_https_url(&location).ok_or("onedrive: invalid redirect location")?;
665        let res2 = crate::kernel::tls::https_request("GET", &host, &redir_path, &[], "", None)?;
666        ok_or_status(&res2)?;
667        return Ok(res2.body_bytes);
668    }
669    ok_or_status(&res)?;
670    Ok(res.body_bytes)
671}
672
673/// 新規ファイルをアップロード(4MB未満の単純 PUT。folder_id=None ならルート直下)。
674/// 同名ファイルが既にあれば上書きされる(Graph API のデフォルト挙動)。成功時は item id。
675pub fn onedrive_upload_binary(
676    token: &str,
677    folder_id: Option<&str>,
678    name: &str,
679    content: &[u8],
680) -> Result<String, &'static str> {
681    let path = match folder_id {
682        Some(id) => format!(
683            "/v1.0/me/drive/items/{}:/{}:/content",
684            url_encode(id),
685            url_encode(name)
686        ),
687        None => format!("/v1.0/me/drive/root:/{}:/content", url_encode(name)),
688    };
689    let res = api_send(
690        "PUT",
691        GRAPH_API_HOST,
692        &path,
693        token,
694        "application/octet-stream",
695        Some(content),
696    )?;
697    ok_or_status(&res)?;
698    Ok(field_str(
699        &json::parse(&res.body).unwrap_or(JsonValue::Null),
700        "id",
701    ))
702}
703
704/// ファイルを削除。
705pub fn onedrive_delete(token: &str, file_id: &str) -> Result<(), &'static str> {
706    let path = format!("/v1.0/me/drive/items/{}", url_encode(file_id));
707    let res = api_send("DELETE", GRAPH_API_HOST, &path, token, "", None)?;
708    ok_or_status(&res)
709}
710
711// ============ Microsoft OneDrive 認証情報の永続化 ============
712
713const MS_AUTH_FILE: &str = "onedrive_auth.json";
714
715/// ディスクに保存する OneDrive 認証状態。Microsoft の device flow はパブリック
716/// クライアント向けで client_secret 不要のため GdriveAuth より単純。
717#[derive(Default, Debug, Clone)]
718pub struct MsAuth {
719    pub client_id: String,
720    pub access_token: String,
721    pub refresh_token: String,
722}
723
724impl MsAuth {
725    pub fn has_client(&self) -> bool {
726        !self.client_id.is_empty()
727    }
728    pub fn is_logged_in(&self) -> bool {
729        !self.access_token.is_empty()
730    }
731}
732
733pub fn ms_auth_to_json(a: &MsAuth) -> String {
734    format!(
735        "{{\"client_id\":\"{}\",\"access_token\":\"{}\",\"refresh_token\":\"{}\"}}",
736        json_escape(&a.client_id),
737        json_escape(&a.access_token),
738        json_escape(&a.refresh_token)
739    )
740}
741
742pub fn parse_ms_auth(body: &str) -> MsAuth {
743    let v = json::parse(body).unwrap_or(JsonValue::Null);
744    MsAuth {
745        client_id: field_str(&v, "client_id"),
746        access_token: field_str(&v, "access_token"),
747        refresh_token: field_str(&v, "refresh_token"),
748    }
749}
750
751/// `onedrive_auth.json` を読む(無ければ default)。
752pub fn load_ms_auth() -> MsAuth {
753    let fs = crate::kernel::fs::get_fs();
754    let auth_data = match fs.read_file_checked(MS_AUTH_FILE) {
755        Ok(Some((_m, bytes))) => Some(bytes),
756        Ok(None) => None,
757        Err(e) => {
758            crate::error!("[CLOUD] load_ms_auth failed to read {}: {}", MS_AUTH_FILE, e);
759            None
760        }
761    };
762    if let Some(bytes) = auth_data {
763        if let Ok(s) = core::str::from_utf8(&bytes) {
764            return parse_ms_auth(s);
765        }
766    }
767    MsAuth::default()
768}
769
770/// `onedrive_auth.json` に書き戻す。
771pub fn save_ms_auth(a: &MsAuth) -> Result<(), &'static str> {
772    let json = ms_auth_to_json(a);
773    let fs = crate::kernel::fs::get_fs();
774    fs.save_file(MS_AUTH_FILE, json.as_bytes(), "system,auth")
775}
776
777// ============ GitHub ============
778
779#[derive(Default, Debug)]
780pub struct GitHubUser {
781    pub login: String,
782    pub name: String,
783    pub id: i64,
784}
785
786pub fn parse_github_user(body: &str) -> GitHubUser {
787    let v = match json::parse(body) {
788        Ok(v) => v,
789        Err(_) => return GitHubUser::default(),
790    };
791    GitHubUser {
792        login: field_str(&v, "login"),
793        name: field_str(&v, "name"),
794        id: field_i64(&v, "id"),
795    }
796}
797
798#[derive(Default, Debug, Clone)]
799pub struct Repo {
800    pub name: String,
801    pub full_name: String,
802    pub private: bool,
803}
804
805pub fn parse_github_repos(body: &str) -> Vec<Repo> {
806    let v = match json::parse(body) {
807        Ok(v) => v,
808        Err(_) => return Vec::new(),
809    };
810    let mut out = Vec::new();
811    if let JsonValue::Array(repos) = v {
812        for r in &repos {
813            out.push(Repo {
814                name: field_str(r, "name"),
815                full_name: field_str(r, "full_name"),
816                private: field_bool(r, "private"),
817            });
818        }
819    }
820    out
821}
822
823/// GitHub の認証ユーザ情報(live)。GitHub API は User-Agent 必須(https_request が付与)。
824pub fn github_get_user(token: &str) -> Result<GitHubUser, &'static str> {
825    let body = api_get("api.github.com", "/user", token)?;
826    Ok(parse_github_user(&body))
827}
828
829pub fn github_list_repos(token: &str) -> Result<Vec<Repo>, &'static str> {
830    let body = api_get("api.github.com", "/user/repos?per_page=100", token)?;
831    Ok(parse_github_repos(&body))
832}
833
834// ============ GitHub 認証情報の永続化 + デバイスフロー ============
835
836const GH_AUTH_FILE: &str = "gh_auth.json";
837const GITHUB_HOST: &str = "github.com";
838/// User情報 + リポジトリ一覧に必要な最小スコープ。
839pub const GITHUB_DEFAULT_SCOPE: &str = "repo read:user";
840
841/// ディスクに保存する GitHub 認証状態。GitHub のデバイスフローは client_secret を
842/// 要求しないため GdriveAuth と異なり client_id + token のみ。device_code は
843/// login → poll の間、CUI の別呼び出しをまたいで引き継ぐために保存する。
844#[derive(Default, Debug, Clone)]
845pub struct GithubAuth {
846    pub client_id: String,
847    pub device_code: String,
848    pub access_token: String,
849}
850
851impl GithubAuth {
852    pub fn has_client(&self) -> bool {
853        !self.client_id.is_empty()
854    }
855    pub fn is_logged_in(&self) -> bool {
856        !self.access_token.is_empty()
857    }
858}
859
860pub fn gh_auth_to_json(a: &GithubAuth) -> String {
861    format!(
862        "{{\"client_id\":\"{}\",\"device_code\":\"{}\",\"access_token\":\"{}\"}}",
863        json_escape(&a.client_id),
864        json_escape(&a.device_code),
865        json_escape(&a.access_token)
866    )
867}
868
869pub fn parse_gh_auth(body: &str) -> GithubAuth {
870    let v = json::parse(body).unwrap_or(JsonValue::Null);
871    GithubAuth {
872        client_id: field_str(&v, "client_id"),
873        device_code: field_str(&v, "device_code"),
874        access_token: field_str(&v, "access_token"),
875    }
876}
877
878/// `gh_auth.json` を読む(無ければ default)。
879pub fn load_gh_auth() -> GithubAuth {
880    let fs = crate::kernel::fs::get_fs();
881    let auth_data = match fs.read_file_checked(GH_AUTH_FILE) {
882        Ok(Some((_m, bytes))) => Some(bytes),
883        Ok(None) => None,
884        Err(e) => {
885            crate::error!("[CLOUD] load_gh_auth failed to read {}: {}", GH_AUTH_FILE, e);
886            None
887        }
888    };
889    if let Some(bytes) = auth_data {
890        if let Ok(s) = core::str::from_utf8(&bytes) {
891            return parse_gh_auth(s);
892        }
893    }
894    GithubAuth::default()
895}
896
897/// `gh_auth.json` に書き戻す。
898pub fn save_gh_auth(a: &GithubAuth) -> Result<(), &'static str> {
899    let json = gh_auth_to_json(a);
900    let fs = crate::kernel::fs::get_fs();
901    fs.save_file(GH_AUTH_FILE, json.as_bytes(), "system,auth")
902}
903
904/// デバイスコードを要求(ユーザーに user_code と verification_url を提示するため)。
905pub fn github_request_device_code(client_id: &str) -> Result<DeviceCode, &'static str> {
906    let body = github_device_code_body(client_id, GITHUB_DEFAULT_SCOPE);
907    let resp = oauth_post(GITHUB_HOST, "/login/device/code", &body)?;
908    Ok(parse_device_code(&resp))
909}
910
911/// device_code でトークンを1回ポーリングする。`authorization_pending` の間は
912/// TokenResponse.error にコードが入る(成功時は access_token が埋まる)。
913pub fn github_poll_token(
914    client_id: &str,
915    device_code: &str,
916) -> Result<TokenResponse, &'static str> {
917    let body = github_token_body(client_id, device_code);
918    let resp = oauth_post(GITHUB_HOST, "/login/oauth/access_token", &body)?;
919    Ok(parse_token_response(&resp))
920}
921
922// ============ 自己テスト(解析・構築の純関数部) ============
923
924pub fn selftest() -> (usize, usize) {
925    let mut passed = 0usize;
926    let mut total = 0usize;
927    let mut check = |cond: bool, label: &str| {
928        total += 1;
929        if cond {
930            passed += 1;
931        } else {
932            crate::println!("CLOUD_SELFTEST FAIL: {}", label);
933        }
934    };
935
936    // URL / フォームエンコード
937    check(url_encode("a b&c=d") == "a%20b%26c%3Dd", "url_encode");
938    check(
939        url_encode("safe-_.~") == "safe-_.~",
940        "url_encode unreserved",
941    );
942    check(
943        form_encode(&[("grant_type", "refresh_token"), ("code", "x y")])
944            == "grant_type=refresh_token&code=x%20y",
945        "form_encode",
946    );
947
948    // OAuth2 トークンレスポンス
949    let tok = parse_token_response(
950        r#"{"access_token":"ya29.AbC","expires_in":3599,"refresh_token":"1//rt","scope":"drive","token_type":"Bearer"}"#,
951    );
952    check(tok.access_token == "ya29.AbC", "token access_token");
953    check(tok.expires_in == 3599, "token expires_in");
954    check(tok.refresh_token == "1//rt", "token refresh_token");
955    let pend = parse_token_response(r#"{"error":"authorization_pending"}"#);
956    check(
957        pend.error == "authorization_pending" && pend.access_token.is_empty(),
958        "token pending",
959    );
960
961    // デバイスコード(Google / GitHub のキー差を吸収)
962    let g = parse_device_code(
963        r#"{"device_code":"DC","user_code":"WDJB-MJHT","verification_url":"https://www.google.com/device","expires_in":1800,"interval":5}"#,
964    );
965    check(
966        g.user_code == "WDJB-MJHT"
967            && g.verification_url == "https://www.google.com/device"
968            && g.interval == 5,
969        "google device code",
970    );
971    let gh = parse_device_code(
972        r#"{"device_code":"DC2","user_code":"ABCD-1234","verification_uri":"https://github.com/login/device","expires_in":900,"interval":5}"#,
973    );
974    check(
975        gh.user_code == "ABCD-1234" && gh.verification_url == "https://github.com/login/device",
976        "github device code",
977    );
978
979    // ボディ構築
980    check(
981        google_token_body("cid", "sec", "dc")
982            .contains("grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Adevice_code"),
983        "google token body",
984    );
985    check(
986        google_refresh_body("cid", "sec", "rt").contains("grant_type=refresh_token"),
987        "google refresh body",
988    );
989
990    // Google Drive ファイル一覧
991    let files = parse_drive_file_list(
992        r#"{"files":[{"id":"1","name":"memo.txt","mimeType":"text/plain"},{"id":"2","name":"work","mimeType":"application/vnd.google-apps.folder"}]}"#,
993    );
994    check(files.len() == 2, "drive file count");
995    check(
996        files.first().map(|f| f.name == "memo.txt").unwrap_or(false),
997        "drive file name",
998    );
999    check(
1000        files
1001            .get(1)
1002            .map(|f| f.mime_type.contains("folder"))
1003            .unwrap_or(false),
1004        "drive folder mime",
1005    );
1006
1007    // Drive 書き込み系の純粋ロジック
1008    check(json_escape(r#"a"b\c"#) == r#"a\"b\\c"#, "json_escape");
1009    let (boundary, body) = drive_multipart_body("memo.txt", "text/plain", b"hello");
1010    let body_str = alloc::string::String::from_utf8_lossy(&body);
1011    check(
1012        body_str.contains("--atmos_drive_boundary_7f3a9c1e2b"),
1013        "multipart boundary",
1014    );
1015    check(
1016        body_str.contains(r#"{"name":"memo.txt"}"#),
1017        "multipart metadata name",
1018    );
1019    check(
1020        body_str.contains("Content-Type: text/plain"),
1021        "multipart media type",
1022    );
1023    check(
1024        body_str.contains("hello") && body_str.ends_with("--atmos_drive_boundary_7f3a9c1e2b--\r\n"),
1025        "multipart closing",
1026    );
1027    check(
1028        boundary == "atmos_drive_boundary_7f3a9c1e2b",
1029        "multipart boundary value",
1030    );
1031
1032    // Drive 認証情報の round-trip
1033    let a = GdriveAuth {
1034        client_id: String::from("cid.apps.googleusercontent.com"),
1035        client_secret: String::from("sec"),
1036        access_token: String::from("ya29.tok"),
1037        refresh_token: String::from("1//rt"),
1038    };
1039    let round = parse_auth(&auth_to_json(&a));
1040    check(
1041        round.client_id == a.client_id
1042            && round.access_token == "ya29.tok"
1043            && round.refresh_token == "1//rt",
1044        "auth round-trip",
1045    );
1046    check(a.has_client() && a.is_logged_in(), "auth flags");
1047    check(
1048        !GdriveAuth::default().is_logged_in(),
1049        "auth default logged-out",
1050    );
1051
1052    // ---- Microsoft OneDrive (Graph API) ----
1053
1054    // デバイスコード(Microsoft は verification_uri を使う。GitHub と同じキーを
1055    // parse_device_code が既に吸収しているため専用分岐は不要だが念のため確認)。
1056    let ms_dc = parse_device_code(
1057        r#"{"device_code":"MSDC","user_code":"ABC-DEF-123","verification_uri":"https://microsoft.com/devicelogin","expires_in":900,"interval":5,"message":"..."}"#,
1058    );
1059    check(
1060        ms_dc.user_code == "ABC-DEF-123"
1061            && ms_dc.verification_url == "https://microsoft.com/devicelogin",
1062        "ms device code",
1063    );
1064
1065    // ボディ構築(client_secret 不要。google_token_body と違う点)
1066    check(
1067        ms_token_body("cid", "dc")
1068            .contains("grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Adevice_code"),
1069        "ms token body",
1070    );
1071    check(
1072        !ms_token_body("cid", "dc").contains("client_secret"),
1073        "ms token body no secret",
1074    );
1075    check(
1076        ms_refresh_body("cid", "rt", ONEDRIVE_SCOPE).contains("grant_type=refresh_token"),
1077        "ms refresh body",
1078    );
1079
1080    // Graph API のファイル/フォルダ一覧("folder" プロパティの有無で判定)
1081    let od_files = parse_onedrive_file_list(
1082        r#"{"value":[{"id":"1","name":"memo.txt"},{"id":"2","name":"work","folder":{"childCount":3}}]}"#,
1083    );
1084    check(od_files.len() == 2, "onedrive file count");
1085    check(
1086        od_files.first().map(|f| !f.is_folder).unwrap_or(false),
1087        "onedrive file is not folder",
1088    );
1089    check(
1090        od_files.get(1).map(|f| f.is_folder).unwrap_or(false),
1091        "onedrive folder detection",
1092    );
1093
1094    // リダイレクト先URLの分解(Graph API の 302 追従用)
1095    check(
1096        split_https_url("https://cdn.example.com/blob/abc?sig=xyz")
1097            == Some((
1098                String::from("cdn.example.com"),
1099                String::from("/blob/abc?sig=xyz"),
1100            )),
1101        "split_https_url",
1102    );
1103    check(split_https_url("http://insecure.example.com/x").is_none(), "split_https_url rejects non-https");
1104
1105    // OneDrive 認証情報の round-trip(client_secret フィールドが無い点が Google と異なる)
1106    let ms_a = MsAuth {
1107        client_id: String::from("11111111-2222-3333-4444-555555555555"),
1108        access_token: String::from("eyJ0eXAi.tok"),
1109        refresh_token: String::from("M.R3_BAY.rt"),
1110    };
1111    let ms_round = parse_ms_auth(&ms_auth_to_json(&ms_a));
1112    check(
1113        ms_round.client_id == ms_a.client_id
1114            && ms_round.access_token == "eyJ0eXAi.tok"
1115            && ms_round.refresh_token == "M.R3_BAY.rt",
1116        "ms auth round-trip",
1117    );
1118    check(ms_a.has_client() && ms_a.is_logged_in(), "ms auth flags");
1119    check(!MsAuth::default().is_logged_in(), "ms auth default logged-out");
1120
1121    // GitHub ユーザ / リポジトリ
1122    let user = parse_github_user(r#"{"login":"octocat","name":"The Octocat","id":583231}"#);
1123    check(user.login == "octocat" && user.id == 583231, "github user");
1124    let repos = parse_github_repos(
1125        r#"[{"name":"r1","full_name":"u/r1","private":false},{"name":"r2","full_name":"u/r2","private":true}]"#,
1126    );
1127    check(
1128        repos.len() == 2 && repos[1].private && repos[0].full_name == "u/r1",
1129        "github repos",
1130    );
1131
1132    // GitHub 認証情報の round-trip
1133    let gh = GithubAuth {
1134        client_id: String::from("Iv1.abc123"),
1135        device_code: String::from("dc-xyz"),
1136        access_token: String::from("gho_tok"),
1137    };
1138    let gh_round = parse_gh_auth(&gh_auth_to_json(&gh));
1139    check(
1140        gh_round.client_id == gh.client_id
1141            && gh_round.device_code == "dc-xyz"
1142            && gh_round.access_token == "gho_tok",
1143        "gh auth round-trip",
1144    );
1145    check(gh.has_client() && gh.is_logged_in(), "gh auth flags");
1146    check(
1147        !GithubAuth::default().is_logged_in(),
1148        "gh auth default logged-out",
1149    );
1150
1151    (passed, total)
1152}