From ca0757652e4120d6493f12a1b32f54c9e8f88cdf Mon Sep 17 00:00:00 2001 From: Warren Lo Date: Mon, 9 Nov 2026 05:03:27 +0800 Subject: [PATCH] fix: seed api_keys on fresh install (X-API-Key auth) + momentry-proxy web-server plist + test.sh SPA auth model MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - install.sh: seed api_keys row (key_hash = SHA-256 of demo key) — missing row caused 401 on every X-API-Key request after clean-install - launchd/com.momentry.proxy.plist: serve via momentry-proxy binary (MOMENTRY_WEB_SERVER=true + TAURI_DIST_DIR) instead of serve_frontend.py - test.sh: remote probes use X-API-Key (SPA auth model) instead of Bearer (proxy login returns UUID session token, core expects JWT/X-API-Key) --- install.sh | 13 +++++++++++++ launchd/com.momentry.proxy.plist | 23 +++++++++++++++++++++++ test.sh | 7 ++++--- 3 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 launchd/com.momentry.proxy.plist diff --git a/install.sh b/install.sh index b9d7667..0a28c19 100755 --- a/install.sh +++ b/install.sh @@ -248,6 +248,19 @@ ON CONFLICT (username) DO NOTHING; " 2>/dev/null || true pass "demo 和 admin 用戶已建立" +# 建立預設 API Key(X-API-Key 認證用;key_hash = SHA-256 of key) +# 未種入此列會導致所有 X-API-Key 請求 401 +$PG_BIN -U "$(whoami)" -d momentry -c " +INSERT INTO api_keys (key_id, key_hash, key_prefix, name, key_type, user_id, permissions, status) +SELECT 'muser_demo_key_32chars_abcdef1234567890', + '188601ba06ecda64873bedfd1176b4359fbbc0a54ac1b3650d73159efaf3a8ab', + 'muser_de', 'Demo API Key', 'user', + (SELECT id FROM users WHERE username='demo'), + '{\"read\": true, \"write\": true}', 'active' +WHERE NOT EXISTS (SELECT 1 FROM api_keys WHERE key_id='muser_demo_key_32chars_abcdef1234567890'); +" 2>/dev/null || true +pass "預設 API Key 已建立 (X-API-Key 認證)" + # ── Step 5: 部署二進制 ── step "[5/10] 部署二進制" diff --git a/launchd/com.momentry.proxy.plist b/launchd/com.momentry.proxy.plist new file mode 100644 index 0000000..a0a68d6 --- /dev/null +++ b/launchd/com.momentry.proxy.plist @@ -0,0 +1,23 @@ + + + + + Labelcom.momentry.proxy + ProgramArguments + + /Users/accusys/momentry/bin/momentry-proxy + + WorkingDirectory/Users/accusys/momentry + EnvironmentVariables + + MOMENTRY_API_KEYmuser_demo_key_32chars_abcdef1234567890 + MOMENTRY_DATA_DIR/Users/accusys/data/users + MOMENTRY_WEB_SERVERtrue + TAURI_DIST_DIR/Users/accusys/momentry/dist + + RunAtLoad + KeepAlive + StandardOutPath/Users/accusys/momentry/logs/momentry_proxy.log + StandardErrorPath/Users/accusys/momentry/logs/momentry_proxy.error.log + + diff --git a/test.sh b/test.sh index 8568d76..6274dd7 100644 --- a/test.sh +++ b/test.sh @@ -175,7 +175,8 @@ else local name=$2 local pattern=${3:-"."} echo -n " $name: " - RESP=$(curl -s --max-time 5 -H "Authorization: Bearer $TOKEN" "$url" 2>/dev/null | head -c 300) + # SPA 認證模型: X-API-Key (Bearer 僅用於 admin token 端點) + RESP=$(curl -s --max-time 5 -H "X-API-Key: muser_demo_key_32chars_abcdef1234567890" "$url" 2>/dev/null | head -c 300) if echo "$RESP" | grep -qE "$pattern"; then pass "HTTP 可及" else @@ -184,7 +185,7 @@ else } probe_url "$BASE_URL/" "frontend proxy (:8888)" "DOCTYPE|html" - probe_url "$BASE_URL/api/v1/health" "API health (透過 proxy)" "status" + probe_url "$BASE_URL/api/v1/health" "API health (透過 proxy)" "ok|status" probe_url "$BASE_URL/api/v1/files?page_size=1" "API files" "success|total|files" if [ "$QUICK" = false ] && [ -n "$SSH_CMD" ]; then @@ -259,7 +260,7 @@ if [ -n "$TOKEN" ]; then echo -n " 檔案列表: " RESP=$(curl -s --max-time 5 "$BASE_URL/api/v1/files?page_size=1" \ - -H "Authorization: Bearer $TOKEN" 2>/dev/null) + -H "X-API-Key: muser_demo_key_32chars_abcdef1234567890" 2>/dev/null) if echo "$RESP" | grep -q '"success":true\|total'; then TOTAL=$(echo "$RESP" | python3 -c "import sys,json; d=json.load(sys.stdin); print(d.get('total', d.get('data') and len(d.get('data')) or 0))" 2>/dev/null) pass "$TOTAL 個檔案"