forked from d-cloud/d-wiki
Compare commits
No commits in common. "feature/auth-integration" and "main" have entirely different histories.
feature/au
...
main
@ -2,7 +2,6 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import json
|
import json
|
||||||
import subprocess
|
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any
|
from typing import Any
|
||||||
@ -18,17 +17,6 @@ def main(argv: list[str] | None = None) -> int:
|
|||||||
parser.add_argument("--config", type=Path, default=None)
|
parser.add_argument("--config", type=Path, default=None)
|
||||||
subparsers = parser.add_subparsers(dest="command", required=True)
|
subparsers = parser.add_subparsers(dest="command", required=True)
|
||||||
|
|
||||||
# auth 子命令 - 转发给 d-auth CLI
|
|
||||||
auth_parser = subparsers.add_parser("auth")
|
|
||||||
auth_sub = auth_parser.add_subparsers(dest="auth_action", required=True)
|
|
||||||
|
|
||||||
auth_login = auth_sub.add_parser("login")
|
|
||||||
auth_login.add_argument("--timeout", type=int, default=120)
|
|
||||||
|
|
||||||
auth_logout = auth_sub.add_parser("logout")
|
|
||||||
|
|
||||||
auth_status = auth_sub.add_parser("status")
|
|
||||||
|
|
||||||
status_parser = subparsers.add_parser("status")
|
status_parser = subparsers.add_parser("status")
|
||||||
status_parser.add_argument("--format", choices=["json"], default="json")
|
status_parser.add_argument("--format", choices=["json"], default="json")
|
||||||
|
|
||||||
@ -70,48 +58,7 @@ def main(argv: list[str] | None = None) -> int:
|
|||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
# 需要登录才能使用的命令
|
|
||||||
REQUIRES_AUTH = {"index", "compile", "dream", "search", "get"}
|
|
||||||
|
|
||||||
|
|
||||||
def call_d_auth(subcommand: str, extra_args: list[str] = None) -> dict[str, Any]:
|
|
||||||
"""调用 d-auth CLI 并返回结果"""
|
|
||||||
cmd = ["d-auth", subcommand]
|
|
||||||
if extra_args:
|
|
||||||
cmd.extend(extra_args)
|
|
||||||
|
|
||||||
result = subprocess.run(cmd, capture_output=True, text=True)
|
|
||||||
if result.returncode != 0:
|
|
||||||
try:
|
|
||||||
return json.loads(result.stderr)
|
|
||||||
except json.JSONDecodeError:
|
|
||||||
return {"status": "error", "error": result.stderr.strip()}
|
|
||||||
return json.loads(result.stdout)
|
|
||||||
|
|
||||||
|
|
||||||
def check_auth_or_exit(command: str) -> None:
|
|
||||||
"""检查登录状态,未登录则打印错误并退出 - 通过调用 d-auth CLI"""
|
|
||||||
if command not in REQUIRES_AUTH:
|
|
||||||
return
|
|
||||||
|
|
||||||
# 调用 d-auth check 检查登录状态
|
|
||||||
result = call_d_auth("check")
|
|
||||||
|
|
||||||
if not result.get("logged_in", False):
|
|
||||||
print(json.dumps({
|
|
||||||
"status": "error",
|
|
||||||
"error": "D_WIKI_NOT_LOGGED_IN: 请先运行 'd-auth login' 登录",
|
|
||||||
"hint": "d-auth login"
|
|
||||||
}, ensure_ascii=False), file=sys.stderr)
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
|
|
||||||
def dispatch(args: argparse.Namespace, config: DWikiConfig) -> dict[str, Any]:
|
def dispatch(args: argparse.Namespace, config: DWikiConfig) -> dict[str, Any]:
|
||||||
# 先检查登录状态(通过 d-auth CLI)
|
|
||||||
check_auth_or_exit(args.command)
|
|
||||||
|
|
||||||
if args.command == "auth":
|
|
||||||
return handle_auth(args)
|
|
||||||
if args.command == "status":
|
if args.command == "status":
|
||||||
return status_payload(config.path, config.knowledge_bases)
|
return status_payload(config.path, config.knowledge_bases)
|
||||||
kb = config.knowledge_bases.get(args.kb)
|
kb = config.knowledge_bases.get(args.kb)
|
||||||
@ -128,15 +75,3 @@ def dispatch(args: argparse.Namespace, config: DWikiConfig) -> dict[str, Any]:
|
|||||||
if args.command == "get":
|
if args.command == "get":
|
||||||
return get_page(kb, args.id)
|
return get_page(kb, args.id)
|
||||||
raise ValueError(f"D_WIKI_UNKNOWN_COMMAND: {args.command}")
|
raise ValueError(f"D_WIKI_UNKNOWN_COMMAND: {args.command}")
|
||||||
|
|
||||||
|
|
||||||
def handle_auth(args: argparse.Namespace) -> dict[str, Any]:
|
|
||||||
"""处理 auth 子命令 - 转发给 d-auth CLI"""
|
|
||||||
if args.auth_action == "login":
|
|
||||||
return call_d_auth("login", ["--timeout", str(args.timeout)])
|
|
||||||
elif args.auth_action == "logout":
|
|
||||||
return call_d_auth("logout")
|
|
||||||
elif args.auth_action == "status":
|
|
||||||
return call_d_auth("status")
|
|
||||||
|
|
||||||
raise ValueError(f"D_WIKI_UNKNOWN_AUTH_ACTION: {args.auth_action}")
|
|
||||||
133
docs/AUTH.md
133
docs/AUTH.md
@ -1,133 +0,0 @@
|
|||||||
# d-wiki 认证集成说明
|
|
||||||
|
|
||||||
## 概述
|
|
||||||
|
|
||||||
d-wiki 现已集成 d-auth 认证系统,用户需要先登录才能使用知识库相关功能。
|
|
||||||
|
|
||||||
## 认证架构
|
|
||||||
|
|
||||||
```
|
|
||||||
d-wiki CLI ──调用──> d-auth CLI ──存储──> macOS Keychain
|
|
||||||
│ │
|
|
||||||
│ └──验证──> JWKS (cloud.d-robotics.cc)
|
|
||||||
│
|
|
||||||
└──拦截未登录请求──> 提示用户登录
|
|
||||||
```
|
|
||||||
|
|
||||||
## 依赖关系
|
|
||||||
|
|
||||||
d-wiki 依赖 d-auth 包,安装 d-wiki 时会自动安装 d-auth:
|
|
||||||
|
|
||||||
```toml
|
|
||||||
# pyproject.toml
|
|
||||||
dependencies = [
|
|
||||||
"d-auth>=0.1.0",
|
|
||||||
]
|
|
||||||
```
|
|
||||||
|
|
||||||
## 受认证保护的命令
|
|
||||||
|
|
||||||
以下命令需要登录才能使用:
|
|
||||||
|
|
||||||
- `d-wiki index`
|
|
||||||
- `d-wiki compile`
|
|
||||||
- `d-wiki dream`
|
|
||||||
- `d-wiki search`
|
|
||||||
- `d-wiki get`
|
|
||||||
|
|
||||||
以下命令无需登录:
|
|
||||||
|
|
||||||
- `d-wiki status`
|
|
||||||
- `d-wiki auth login` / `logout` / `status`
|
|
||||||
|
|
||||||
## 使用方式
|
|
||||||
|
|
||||||
### 安装
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# 安装 d-wiki(会自动安装 d-auth)
|
|
||||||
pip install git+https://gitea.d-robotics.cc/d-cloud/d-wiki.git
|
|
||||||
|
|
||||||
# 或单独安装 d-auth
|
|
||||||
pip install git+https://gitea.d-robotics.cc/d-cloud/d-auth.git
|
|
||||||
```
|
|
||||||
|
|
||||||
### 登录
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# 方式一:通过 d-wiki 登录
|
|
||||||
d-wiki auth login
|
|
||||||
|
|
||||||
# 方式二:直接使用 d-auth 登录
|
|
||||||
d-auth login
|
|
||||||
```
|
|
||||||
|
|
||||||
登录流程:
|
|
||||||
1. CLI 启动本地 HTTP 服务器(随机端口)
|
|
||||||
2. 打开浏览器访问 SSO 登录页
|
|
||||||
3. 用户在浏览器中完成登录
|
|
||||||
4. SSO 跳转回本地回调地址,传递 token
|
|
||||||
5. CLI 将 token 存入 macOS Keychain
|
|
||||||
|
|
||||||
### 查看登录状态
|
|
||||||
|
|
||||||
```bash
|
|
||||||
d-wiki auth status
|
|
||||||
# 或
|
|
||||||
d-auth status
|
|
||||||
```
|
|
||||||
|
|
||||||
输出示例:
|
|
||||||
```json
|
|
||||||
{
|
|
||||||
"logged_in": true,
|
|
||||||
"username": "admin&dcloud",
|
|
||||||
"user_id": "e0111193-da12-45c2-94b3-e5854ea69192",
|
|
||||||
"expires_at": "2026-06-09T13:51:41+00:00"
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### 登出
|
|
||||||
|
|
||||||
```bash
|
|
||||||
d-wiki auth logout
|
|
||||||
# 或
|
|
||||||
d-auth logout
|
|
||||||
```
|
|
||||||
|
|
||||||
### 使用知识库功能
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# 登录后可正常使用
|
|
||||||
d-wiki search --kb mykb --query "测试查询"
|
|
||||||
|
|
||||||
# 未登录时会被拦截
|
|
||||||
d-wiki search --kb mykb --query "测试查询"
|
|
||||||
# 输出: {"status": "error", "error": "D_WIKI_NOT_LOGGED_IN: 请先运行 'd-auth login' 登录"}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Token 存储
|
|
||||||
|
|
||||||
Token 存储在 macOS Keychain 中:
|
|
||||||
- Service: `d-auth`
|
|
||||||
- Account: `user-center-jwt`
|
|
||||||
|
|
||||||
## Token 验证
|
|
||||||
|
|
||||||
登录状态通过 JWKS 验证:
|
|
||||||
- JWKS URL: `https://cloud.d-robotics.cc/.well-known/jwks.json`
|
|
||||||
- 算法: RS256
|
|
||||||
- Key ID: `yx-yyds-key`
|
|
||||||
|
|
||||||
## Token 过期处理
|
|
||||||
|
|
||||||
当 token 过期或无效时:
|
|
||||||
1. `d-auth status` 自动检测并清除过期 token
|
|
||||||
2. 返回 `logged_in: false`
|
|
||||||
3. 用户需重新登录
|
|
||||||
|
|
||||||
## 相关项目
|
|
||||||
|
|
||||||
- **d-auth**: https://gitea.d-robotics.cc/d-cloud/d-auth
|
|
||||||
- **d-wiki**: https://gitea.d-robotics.cc/d-cloud/d-wiki
|
|
||||||
- **SSO**: https://sso.d-robotics.cc
|
|
||||||
@ -7,9 +7,7 @@ name = "d-wiki"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
description = "Lightweight local knowledge retrieval with BM25 and metadata context packs."
|
description = "Lightweight local knowledge retrieval with BM25 and metadata context packs."
|
||||||
requires-python = ">=3.10"
|
requires-python = ">=3.10"
|
||||||
dependencies = [
|
dependencies = []
|
||||||
"d-auth>=0.1.0",
|
|
||||||
]
|
|
||||||
|
|
||||||
[project.scripts]
|
[project.scripts]
|
||||||
d-wiki = "d_wiki.cli:main"
|
d-wiki = "d_wiki.cli:main"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user