Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

Allow copying active API tokens

tianmaba8b5b531afc
diff --git a/.env.local.example b/.env.local.example
index 767c77fc..ec0ce544 100644
--- a/.env.local.example
+++ b/.env.local.example
@@ -37,8 +37,7 @@ EDGE_EVER_AUTH_USERNAME=admin
# 登录密码的 PBKDF2-SHA256 hash。
# 生成命令:`bun run auth:hash -- <你的密码>`
-# 也可以用 `EDGE_EVER_TIANMA_AUTH_PASSWORD_HASH` / `EDGE_EVER_DEMO_AUTH_PASSWORD_HASH`
-# 给多实例配置不同密码。
+# 也可以用 `EDGE_EVER_<INSTANCE>_AUTH_PASSWORD_HASH` 给多实例配置不同密码。
# 线上部署时建议把对应 hash 配成 Cloudflare Worker Secret。
EDGE_EVER_AUTH_PASSWORD_HASH=
@@ -49,24 +48,8 @@ EDGE_EVER_SESSION_TTL_DAYS=30
# 本地开发默认指向 Wrangler dev:`bun run dev:api`
VITE_API_BASE_URL=http://127.0.0.1:8787
-# 多实例部署模板。
-# 使用 `EDGE_EVER_INSTANCE=tianma bun run deploy` 或 `bun run deploy:tianma` 时,
-# 脚本会优先读取 `EDGE_EVER_TIANMA_*`,再回退到上面的通用 `EDGE_EVER_*`。
-# `demo` 实例同理。真实 D1 ID 只填到本地 `.env.local` 或 Cloudflare Build Variables。
-
-# 私人实例:tianma.edgeever.org
-EDGE_EVER_TIANMA_WORKER_NAME=edgeever-tianma
-EDGE_EVER_TIANMA_CUSTOM_DOMAIN=tianma.edgeever.org
-EDGE_EVER_TIANMA_D1_DATABASE_NAME=edgeever-tianma
-EDGE_EVER_TIANMA_D1_DATABASE_ID=00000000-0000-0000-0000-000000000000
-EDGE_EVER_TIANMA_R2_BUCKET_NAME=edgeever-tianma-resources
-EDGE_EVER_TIANMA_R2_PREVIEW_BUCKET_NAME=edgeever-tianma-resources-preview
-
-# 公开演示实例:demo.edgeever.org
-# 如果 Workers Custom Domain 暂未稳定生效,可以用 Route Pattern + DNS CNAME。
-EDGE_EVER_DEMO_WORKER_NAME=edgeever-demo
-EDGE_EVER_DEMO_ROUTE_PATTERN=demo.edgeever.org/*
-EDGE_EVER_DEMO_D1_DATABASE_NAME=edgeever-demo
-EDGE_EVER_DEMO_D1_DATABASE_ID=00000000-0000-0000-0000-000000000000
-EDGE_EVER_DEMO_R2_BUCKET_NAME=edgeever-demo-resources
-EDGE_EVER_DEMO_R2_PREVIEW_BUCKET_NAME=edgeever-demo-resources-preview
+# 可选:多实例部署。
+# 在本机 `.env.local` 或 Cloudflare Build Variables 中设置 `EDGE_EVER_INSTANCE=<name>` 后,
+# 脚本会优先读取 `EDGE_EVER_<NAME>_*`,再回退到上面的通用 `EDGE_EVER_*`。
+# 例如 `EDGE_EVER_INSTANCE=prod` 会优先读取 `EDGE_EVER_PROD_WORKER_NAME`、
+# `EDGE_EVER_PROD_D1_DATABASE_ID`、`EDGE_EVER_PROD_R2_BUCKET_NAME` 等变量。
diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts
index 9031df71..51d39684 100644
--- a/apps/api/src/index.ts
+++ b/apps/api/src/index.ts
@@ -133,6 +133,7 @@ type SessionRow = {
type ApiTokenRow = {
id: string;
name: string;
+ token_value: string | null;
scopes_json: string;
last_used_at: string | null;
expires_at: string | null;
@@ -363,7 +364,7 @@ app.get("/api/v1/api-tokens", async (c) => {
}
const rows = await c.env.DB.prepare(
- `SELECT id, name, scopes_json, last_used_at, expires_at, is_revoked, created_at
+ `SELECT id, name, token_value, scopes_json, last_used_at, expires_at, is_revoked, created_at
FROM api_tokens
ORDER BY is_revoked ASC, created_at DESC
LIMIT 200`
@@ -396,9 +397,9 @@ app.post("/api/v1/api-tokens", zValidator("json", ApiTokenCreateSchema), async (
await c.env.DB.batch([
c.env.DB.prepare(
- `INSERT INTO api_tokens (id, name, token_hash, scopes_json, expires_at, created_at)
- VALUES (?, ?, ?, ?, ?, ?)`
- ).bind(id, input.name, await sha256(token), JSON.stringify(scopes), input.expiresAt ?? null, now),
+ `INSERT INTO api_tokens (id, name, token_hash, token_value, scopes_json, expires_at, created_at)
+ VALUES (?, ?, ?, ?, ?, ?, ?)`
+ ).bind(id, input.name, await sha256(token), token, JSON.stringify(scopes), input.expiresAt ?? null, now),
auditStatement(c.env.DB, actor.actorType, actor.actorId, "api_token.create", "api_token", id, {
name: input.name,
scopes,
@@ -2145,7 +2146,7 @@ const authenticateBearerToken = async (c: AppContext, touch: boolean): Promise<A
}
const row = await c.env.DB.prepare(
- `SELECT id, name, scopes_json, last_used_at, expires_at, is_revoked, created_at
+ `SELECT id, name, token_value, scopes_json, last_used_at, expires_at, is_revoked, created_at
FROM api_tokens
WHERE token_hash = ?
AND is_revoked = 0
@@ -2489,6 +2490,7 @@ const mapResourceStorageSummary = (row: ResourceStatsRow | null): ResourceStorag
const mapApiToken = (row: ApiTokenRow): ApiToken => ({
id: row.id,
name: row.name,
+ token: row.token_value,
scopes: parseJsonArray(row.scopes_json),
lastUsedAt: row.last_used_at,
expiresAt: row.expires_at,
@@ -2505,7 +2507,7 @@ const mapTagSummary = (row: TagSummaryRow): TagSummary => ({
const getApiTokenRow = async (db: D1Database, id: string): Promise<ApiTokenRow | null> =>
db
.prepare(
- `SELECT id, name, scopes_json, last_used_at, expires_at, is_revoked, created_at
+ `SELECT id, name, token_value, scopes_json, last_used_at, expires_at, is_revoked, created_at
FROM api_tokens
WHERE id = ?`
)
diff --git a/apps/web/src/components/SettingsPane.tsx b/apps/web/src/components/SettingsPane.tsx
index 679adb2d..a03f4501 100644
--- a/apps/web/src/components/SettingsPane.tsx
+++ b/apps/web/src/components/SettingsPane.tsx
@@ -165,7 +165,7 @@ const CreatedTokenNotice = ({ token }: CreatedTokenNoticeProps) => {
{copied ? "已复制" : "复制 Token"}
</Button>
</div>
- <p className="mt-2 text-xs font-medium leading-4 text-emerald-800">安全提醒:明文 Token 仅展示一次,关闭后无法再次找回。</p>
+ <p className="mt-2 text-xs font-medium leading-4 text-emerald-800">安全提醒:此 Token 后续仍可在列表中复制,请仅授予需要的权限。</p>
</div>
);
};
@@ -337,6 +337,19 @@ interface TokenListProps {
}
const TokenList = ({ tokens, isLoading, isRevoking, onRevoke }: TokenListProps) => {
+ const [copiedTokenId, setCopiedTokenId] = useState<string | null>(null);
+
+ const handleCopy = async (token: ApiToken) => {
+ if (!token.token || !(await copyTextToClipboard(token.token))) {
+ return;
+ }
+
+ setCopiedTokenId(token.id);
+ window.setTimeout(() => {
+ setCopiedTokenId((current) => (current === token.id ? null : current));
+ }, 1600);
+ };
+
if (isLoading) {
return (
<div className="rounded-lg border border-dashed border-slate-200 px-4 py-8 text-center text-sm text-slate-400">
@@ -373,8 +386,20 @@ const TokenList = ({ tokens, isLoading, isRevoking, onRevoke }: TokenListProps)
</span>
<span className="mt-2 block text-[11px] font-medium text-slate-400">
{token.lastUsedAt ? `上次调用时间:${formatDateTime(token.lastUsedAt)}` : "从未被调用"}
+ {!token.token ? " · 旧 Token 无法找回明文,请重新生成" : ""}
</span>
</span>
+ <Button
+ size="icon"
+ variant="outline"
+ className="h-9 w-9 shrink-0 bg-white"
+ title={token.token ? "复制 Token" : "旧 Token 无法复制"}
+ aria-label={token.token ? "复制 Token" : "旧 Token 无法复制"}
+ disabled={token.isRevoked || !token.token}
+ onClick={() => void handleCopy(token)}
+ >
+ {copiedTokenId === token.id ? <ShieldCheck className="h-4 w-4" /> : <Copy className="h-4 w-4" />}
+ </Button>
<Button
size="icon"
variant="danger"
diff --git a/docs/openapi.json b/docs/openapi.json
index d6e8effc..66551e80 100644
--- a/docs/openapi.json
+++ b/docs/openapi.json
@@ -1368,6 +1368,9 @@
"name": {
"type": "string"
},
+ "token": {
+ "type": ["string", "null"]
+ },
"scopes": {
"type": "array",
"items": {
@@ -1390,7 +1393,7 @@
"format": "date-time"
}
},
- "required": ["id", "name", "scopes", "lastUsedAt", "expiresAt", "isRevoked", "createdAt"]
+ "required": ["id", "name", "token", "scopes", "lastUsedAt", "expiresAt", "isRevoked", "createdAt"]
},
"ApiTokenCreateInput": {
"type": "object",
diff --git a/migrations/0007_api_token_value.sql b/migrations/0007_api_token_value.sql
new file mode 100644
index 00000000..3076df34
--- /dev/null
+++ b/migrations/0007_api_token_value.sql
@@ -0,0 +1,4 @@
+PRAGMA foreign_keys = ON;
+
+ALTER TABLE api_tokens
+ ADD COLUMN token_value TEXT;
diff --git a/package.json b/package.json
index 2dfb69d5..f587e9cc 100644
--- a/package.json
+++ b/package.json
@@ -9,11 +9,9 @@
],
"scripts": {
"dev": "concurrently -k -n api,web -c cyan,magenta \"bun run dev:api\" \"bun run dev:web\"",
- "dev:api": "bun run dev:api:tianma",
+ "dev:api": "bun scripts/run-wrangler.mjs dev --remote --port 8787",
"dev:local": "concurrently -k -n api,web -c cyan,magenta \"bun run dev:api:local\" \"bun run dev:web\"",
"dev:api:local": "bun scripts/run-wrangler.mjs dev --local --port 8787",
- "dev:tianma": "concurrently -k -n api,web -c cyan,magenta \"bun run dev:api:tianma\" \"bun run dev:web\"",
- "dev:api:tianma": "EDGE_EVER_INSTANCE=tianma bun scripts/run-wrangler.mjs dev --remote --port 8787",
"dev:web": "vite --config apps/web/vite.config.ts --host 127.0.0.1 --port 5173",
"build": "bun run build:web",
"build:web": "vite build --config apps/web/vite.config.ts",
@@ -25,10 +23,7 @@
"cli": "bun scripts/edgeever.mjs",
"import:evernote": "bun scripts/import-evernote-enex-via-mcp.mjs",
"mcp:stdio": "bun scripts/edgeever-mcp-stdio.mjs",
- "deploy": "bun run build && bun run db:migrate:remote && bun scripts/run-wrangler.mjs deploy",
- "deploy:tianma": "EDGE_EVER_INSTANCE=tianma bun run deploy",
- "deploy:demo": "EDGE_EVER_INSTANCE=demo bun run deploy",
- "deploy:all": "bun run deploy:tianma && bun run deploy:demo"
+ "deploy": "bun run build && bun run db:migrate:remote && bun scripts/run-wrangler.mjs deploy"
},
"cloudflare": {
"bindings": {
diff --git a/packages/shared/src/types.ts b/packages/shared/src/types.ts
index 168224e8..b0cac089 100644
--- a/packages/shared/src/types.ts
+++ b/packages/shared/src/types.ts
@@ -85,6 +85,7 @@ export type ResourceStorageSummary = {
export type ApiToken = {
id: string;
name: string;
+ token: string | null;
scopes: string[];
lastUsedAt: string | null;
expiresAt: string | null;