@downcity/agent 1.1.7 → 1.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/agent/AgentContext.d.ts.map +1 -1
- package/bin/agent/AgentContext.js +1 -0
- package/bin/agent/AgentContext.js.map +1 -1
- package/bin/agent/AgentContextTypes.d.ts +5 -1
- package/bin/agent/AgentContextTypes.d.ts.map +1 -1
- package/bin/agent/AgentRuntime.d.ts +6 -1
- package/bin/agent/AgentRuntime.d.ts.map +1 -1
- package/bin/agent/AgentRuntime.js +22 -3
- package/bin/agent/AgentRuntime.js.map +1 -1
- package/bin/agent/AgentRuntimeState.d.ts.map +1 -1
- package/bin/agent/AgentRuntimeState.js +58 -2
- package/bin/agent/AgentRuntimeState.js.map +1 -1
- package/bin/agent/AgentRuntimeTypes.d.ts +5 -1
- package/bin/agent/AgentRuntimeTypes.d.ts.map +1 -1
- package/bin/agent/project/AgentInitializer.d.ts +3 -2
- package/bin/agent/project/AgentInitializer.d.ts.map +1 -1
- package/bin/agent/project/AgentInitializer.js +34 -44
- package/bin/agent/project/AgentInitializer.js.map +1 -1
- package/bin/config/Config.d.ts.map +1 -1
- package/bin/config/Config.js +2 -21
- package/bin/config/Config.js.map +1 -1
- package/bin/config/Paths.d.ts +1 -5
- package/bin/config/Paths.d.ts.map +1 -1
- package/bin/config/Paths.js +2 -8
- package/bin/config/Paths.js.map +1 -1
- package/bin/host/daemon/ProjectSetup.d.ts +2 -1
- package/bin/host/daemon/ProjectSetup.d.ts.map +1 -1
- package/bin/host/daemon/ProjectSetup.js +14 -21
- package/bin/host/daemon/ProjectSetup.js.map +1 -1
- package/bin/host/runtime/AgentHostRuntime.d.ts.map +1 -1
- package/bin/host/runtime/AgentHostRuntime.js +1 -2
- package/bin/host/runtime/AgentHostRuntime.js.map +1 -1
- package/bin/host/runtime/CityPaths.d.ts +0 -3
- package/bin/host/runtime/CityPaths.d.ts.map +1 -1
- package/bin/host/runtime/CityPaths.js +0 -3
- package/bin/host/runtime/CityPaths.js.map +1 -1
- package/bin/host/sdk/Agent.d.ts +1 -0
- package/bin/host/sdk/Agent.d.ts.map +1 -1
- package/bin/host/sdk/Agent.js +24 -2
- package/bin/host/sdk/Agent.js.map +1 -1
- package/bin/host/sdk/AgentSdkTypes.d.ts +9 -0
- package/bin/host/sdk/AgentSdkTypes.d.ts.map +1 -1
- package/bin/http/Server.d.ts.map +1 -1
- package/bin/http/Server.js +1 -11
- package/bin/http/Server.js.map +1 -1
- package/bin/http/auth/AuthEnv.d.ts +0 -9
- package/bin/http/auth/AuthEnv.d.ts.map +1 -1
- package/bin/http/auth/AuthEnv.js +0 -9
- package/bin/http/auth/AuthEnv.js.map +1 -1
- package/bin/http/auth/CliAuthStateStore.d.ts +0 -4
- package/bin/http/auth/CliAuthStateStore.d.ts.map +1 -1
- package/bin/http/auth/CliAuthStateStore.js +0 -4
- package/bin/http/auth/CliAuthStateStore.js.map +1 -1
- package/bin/http/control/ModelRoutes.d.ts.map +1 -1
- package/bin/http/control/ModelRoutes.js +3 -8
- package/bin/http/control/ModelRoutes.js.map +1 -1
- package/bin/index.d.ts +2 -6
- package/bin/index.d.ts.map +1 -1
- package/bin/index.js +2 -6
- package/bin/index.js.map +1 -1
- package/bin/model/CreateModel.d.ts +2 -2
- package/bin/model/CreateModel.d.ts.map +1 -1
- package/bin/model/CreateModel.js +13 -12
- package/bin/model/CreateModel.js.map +1 -1
- package/bin/plugin/Activation.d.ts +4 -0
- package/bin/plugin/Activation.d.ts.map +1 -1
- package/bin/plugin/Activation.js +2 -2
- package/bin/plugin/Activation.js.map +1 -1
- package/bin/plugin/LocalExecution.d.ts.map +1 -1
- package/bin/plugin/LocalExecution.js +23 -2
- package/bin/plugin/LocalExecution.js.map +1 -1
- package/bin/plugin/PluginRegistry.js +2 -2
- package/bin/plugin/PluginRegistry.js.map +1 -1
- package/bin/plugins/asr/Plugin.d.ts.map +1 -1
- package/bin/plugins/asr/Plugin.js +4 -5
- package/bin/plugins/asr/Plugin.js.map +1 -1
- package/bin/plugins/auth/Plugin.d.ts.map +1 -1
- package/bin/plugins/auth/Plugin.js +1 -0
- package/bin/plugins/auth/Plugin.js.map +1 -1
- package/bin/plugins/auth/runtime/AuthorizationConfig.d.ts +4 -4
- package/bin/plugins/auth/runtime/AuthorizationConfig.d.ts.map +1 -1
- package/bin/plugins/auth/runtime/AuthorizationConfig.js +28 -26
- package/bin/plugins/auth/runtime/AuthorizationConfig.js.map +1 -1
- package/bin/plugins/auth/runtime/AuthorizationPolicy.d.ts +2 -0
- package/bin/plugins/auth/runtime/AuthorizationPolicy.d.ts.map +1 -1
- package/bin/plugins/auth/runtime/AuthorizationPolicy.js +3 -2
- package/bin/plugins/auth/runtime/AuthorizationPolicy.js.map +1 -1
- package/bin/plugins/auth/runtime/AuthorizationStore.d.ts +1 -1
- package/bin/plugins/auth/runtime/AuthorizationStore.d.ts.map +1 -1
- package/bin/plugins/auth/runtime/AuthorizationStore.js +3 -4
- package/bin/plugins/auth/runtime/AuthorizationStore.js.map +1 -1
- package/bin/plugins/skill/Plugin.js +2 -2
- package/bin/plugins/skill/Plugin.js.map +1 -1
- package/bin/plugins/tts/Plugin.d.ts.map +1 -1
- package/bin/plugins/tts/Plugin.js +4 -5
- package/bin/plugins/tts/Plugin.js.map +1 -1
- package/bin/plugins/web/Plugin.d.ts.map +1 -1
- package/bin/plugins/web/Plugin.js +4 -5
- package/bin/plugins/web/Plugin.js.map +1 -1
- package/bin/plugins/workboard/Plugin.js +2 -2
- package/bin/plugins/workboard/Plugin.js.map +1 -1
- package/bin/service/builtins/chat/accounts/ChannelAccountService.d.ts +4 -1
- package/bin/service/builtins/chat/accounts/ChannelAccountService.d.ts.map +1 -1
- package/bin/service/builtins/chat/accounts/ChannelAccountService.js +64 -91
- package/bin/service/builtins/chat/accounts/ChannelAccountService.js.map +1 -1
- package/bin/service/builtins/chat/runtime/ChatChannelActions.d.ts.map +1 -1
- package/bin/service/builtins/chat/runtime/ChatChannelActions.js +11 -18
- package/bin/service/builtins/chat/runtime/ChatChannelActions.js.map +1 -1
- package/bin/service/builtins/chat/runtime/ChatChannelCore.d.ts +1 -1
- package/bin/service/builtins/chat/runtime/ChatChannelCore.d.ts.map +1 -1
- package/bin/service/builtins/chat/runtime/ChatChannelCore.js +9 -17
- package/bin/service/builtins/chat/runtime/ChatChannelCore.js.map +1 -1
- package/bin/service/builtins/memory/Action.d.ts +1 -5
- package/bin/service/builtins/memory/Action.d.ts.map +1 -1
- package/bin/service/builtins/memory/Action.js +4 -42
- package/bin/service/builtins/memory/Action.js.map +1 -1
- package/bin/service/builtins/memory/MemoryService.d.ts.map +1 -1
- package/bin/service/builtins/memory/MemoryService.js +2 -32
- package/bin/service/builtins/memory/MemoryService.js.map +1 -1
- package/bin/service/builtins/memory/runtime/Search.d.ts +7 -3
- package/bin/service/builtins/memory/runtime/Search.d.ts.map +1 -1
- package/bin/service/builtins/memory/runtime/Search.js +220 -16
- package/bin/service/builtins/memory/runtime/Search.js.map +1 -1
- package/bin/service/builtins/memory/runtime/Store.d.ts +9 -50
- package/bin/service/builtins/memory/runtime/Store.d.ts.map +1 -1
- package/bin/service/builtins/memory/runtime/Store.js +10 -130
- package/bin/service/builtins/memory/runtime/Store.js.map +1 -1
- package/bin/service/builtins/memory/runtime/Writer.d.ts.map +1 -1
- package/bin/service/builtins/memory/runtime/Writer.js +1 -2
- package/bin/service/builtins/memory/runtime/Writer.js.map +1 -1
- package/bin/service/builtins/memory/types/Memory.d.ts +3 -57
- package/bin/service/builtins/memory/types/Memory.d.ts.map +1 -1
- package/bin/service/schedule/Store.d.ts +22 -25
- package/bin/service/schedule/Store.d.ts.map +1 -1
- package/bin/service/schedule/Store.js +172 -154
- package/bin/service/schedule/Store.js.map +1 -1
- package/bin/session/composer/system/default/SystemDomain.d.ts.map +1 -1
- package/bin/session/composer/system/default/SystemDomain.js +1 -0
- package/bin/session/composer/system/default/SystemDomain.js.map +1 -1
- package/bin/shared/types/AgentHost.d.ts +120 -4
- package/bin/shared/types/AgentHost.d.ts.map +1 -1
- package/bin/shared/types/Plugin.d.ts +5 -1
- package/bin/shared/types/Plugin.d.ts.map +1 -1
- package/package.json +1 -4
- package/src/agent/AgentContext.ts +1 -0
- package/src/agent/AgentContextTypes.ts +5 -0
- package/src/agent/AgentRuntime.ts +32 -3
- package/src/agent/AgentRuntimeState.ts +66 -2
- package/src/agent/AgentRuntimeTypes.ts +5 -0
- package/src/agent/project/AgentInitializer.ts +40 -42
- package/src/config/Config.ts +2 -17
- package/src/config/Paths.ts +2 -9
- package/src/host/daemon/ProjectSetup.ts +19 -21
- package/src/host/runtime/AgentHostRuntime.ts +0 -2
- package/src/host/runtime/CityPaths.ts +0 -3
- package/src/host/sdk/Agent.ts +26 -2
- package/src/host/sdk/AgentSdkTypes.ts +10 -0
- package/src/http/Server.ts +0 -13
- package/src/http/auth/AuthEnv.ts +0 -9
- package/src/http/auth/CliAuthStateStore.ts +0 -4
- package/src/http/control/ModelRoutes.ts +3 -9
- package/src/index.ts +2 -12
- package/src/model/CreateModel.ts +15 -13
- package/src/plugin/Activation.ts +6 -2
- package/src/plugin/LocalExecution.ts +24 -2
- package/src/plugin/PluginRegistry.ts +2 -2
- package/src/plugins/asr/Plugin.ts +4 -5
- package/src/plugins/auth/Plugin.ts +1 -0
- package/src/plugins/auth/runtime/AuthorizationConfig.ts +47 -37
- package/src/plugins/auth/runtime/AuthorizationPolicy.ts +5 -2
- package/src/plugins/auth/runtime/AuthorizationStore.ts +6 -5
- package/src/plugins/skill/Plugin.ts +2 -2
- package/src/plugins/tts/Plugin.ts +4 -5
- package/src/plugins/web/Plugin.ts +4 -5
- package/src/plugins/workboard/Plugin.ts +2 -2
- package/src/service/builtins/chat/accounts/ChannelAccountService.ts +42 -62
- package/src/service/builtins/chat/runtime/ChatChannelActions.ts +12 -18
- package/src/service/builtins/chat/runtime/ChatChannelCore.ts +9 -14
- package/src/service/builtins/memory/Action.ts +6 -47
- package/src/service/builtins/memory/MemoryService.ts +1 -33
- package/src/service/builtins/memory/runtime/Search.ts +256 -16
- package/src/service/builtins/memory/runtime/Store.ts +13 -185
- package/src/service/builtins/memory/runtime/Writer.ts +1 -2
- package/src/service/builtins/memory/types/Memory.ts +2 -59
- package/src/service/schedule/Store.ts +215 -175
- package/src/session/composer/system/default/SystemDomain.ts +1 -0
- package/src/shared/types/AgentHost.ts +138 -4
- package/src/shared/types/Plugin.ts +5 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/bin/http/auth/AuthMiddleware.d.ts +0 -36
- package/bin/http/auth/AuthMiddleware.d.ts.map +0 -1
- package/bin/http/auth/AuthMiddleware.js +0 -37
- package/bin/http/auth/AuthMiddleware.js.map +0 -1
- package/bin/http/auth/AuthRoutes.d.ts +0 -17
- package/bin/http/auth/AuthRoutes.d.ts.map +0 -1
- package/bin/http/auth/AuthRoutes.js +0 -78
- package/bin/http/auth/AuthRoutes.js.map +0 -1
- package/bin/http/auth/AuthService.d.ts +0 -119
- package/bin/http/auth/AuthService.d.ts.map +0 -1
- package/bin/http/auth/AuthService.js +0 -307
- package/bin/http/auth/AuthService.js.map +0 -1
- package/bin/http/auth/AuthStore.d.ts +0 -165
- package/bin/http/auth/AuthStore.d.ts.map +0 -1
- package/bin/http/auth/AuthStore.js +0 -442
- package/bin/http/auth/AuthStore.js.map +0 -1
- package/bin/http/auth/RoutePolicy.d.ts +0 -30
- package/bin/http/auth/RoutePolicy.d.ts.map +0 -1
- package/bin/http/auth/RoutePolicy.js +0 -229
- package/bin/http/auth/RoutePolicy.js.map +0 -1
- package/bin/plugin/Lifecycle.d.ts +0 -33
- package/bin/plugin/Lifecycle.d.ts.map +0 -1
- package/bin/plugin/Lifecycle.js +0 -102
- package/bin/plugin/Lifecycle.js.map +0 -1
- package/bin/service/builtins/memory/runtime/Indexer.d.ts +0 -71
- package/bin/service/builtins/memory/runtime/Indexer.d.ts.map +0 -1
- package/bin/service/builtins/memory/runtime/Indexer.js +0 -345
- package/bin/service/builtins/memory/runtime/Indexer.js.map +0 -1
- package/bin/service/schedule/Schema.d.ts +0 -171
- package/bin/service/schedule/Schema.d.ts.map +0 -1
- package/bin/service/schedule/Schema.js +0 -26
- package/bin/service/schedule/Schema.js.map +0 -1
- package/bin/shared/utils/store/StoreChannelAccountRepository.d.ts +0 -34
- package/bin/shared/utils/store/StoreChannelAccountRepository.d.ts.map +0 -1
- package/bin/shared/utils/store/StoreChannelAccountRepository.js +0 -198
- package/bin/shared/utils/store/StoreChannelAccountRepository.js.map +0 -1
- package/bin/shared/utils/store/StoreEnvRepository.d.ts +0 -98
- package/bin/shared/utils/store/StoreEnvRepository.d.ts.map +0 -1
- package/bin/shared/utils/store/StoreEnvRepository.js +0 -334
- package/bin/shared/utils/store/StoreEnvRepository.js.map +0 -1
- package/bin/shared/utils/store/StoreModelRepository.d.ts +0 -61
- package/bin/shared/utils/store/StoreModelRepository.d.ts.map +0 -1
- package/bin/shared/utils/store/StoreModelRepository.js +0 -278
- package/bin/shared/utils/store/StoreModelRepository.js.map +0 -1
- package/bin/shared/utils/store/StoreSchema.d.ts +0 -13
- package/bin/shared/utils/store/StoreSchema.d.ts.map +0 -1
- package/bin/shared/utils/store/StoreSchema.js +0 -319
- package/bin/shared/utils/store/StoreSchema.js.map +0 -1
- package/bin/shared/utils/store/StoreSecureSettings.d.ts +0 -33
- package/bin/shared/utils/store/StoreSecureSettings.d.ts.map +0 -1
- package/bin/shared/utils/store/StoreSecureSettings.js +0 -91
- package/bin/shared/utils/store/StoreSecureSettings.js.map +0 -1
- package/bin/shared/utils/store/StoreShared.d.ts +0 -44
- package/bin/shared/utils/store/StoreShared.d.ts.map +0 -1
- package/bin/shared/utils/store/StoreShared.js +0 -40
- package/bin/shared/utils/store/StoreShared.js.map +0 -1
- package/bin/shared/utils/store/crypto.d.ts +0 -24
- package/bin/shared/utils/store/crypto.d.ts.map +0 -1
- package/bin/shared/utils/store/crypto.js +0 -101
- package/bin/shared/utils/store/crypto.js.map +0 -1
- package/bin/shared/utils/store/index.d.ts +0 -230
- package/bin/shared/utils/store/index.d.ts.map +0 -1
- package/bin/shared/utils/store/index.js +0 -360
- package/bin/shared/utils/store/index.js.map +0 -1
- package/bin/shared/utils/store/schema.d.ts +0 -690
- package/bin/shared/utils/store/schema.d.ts.map +0 -1
- package/bin/shared/utils/store/schema.js +0 -81
- package/bin/shared/utils/store/schema.js.map +0 -1
- package/src/http/auth/AuthMiddleware.ts +0 -61
- package/src/http/auth/AuthRoutes.ts +0 -100
- package/src/http/auth/AuthService.ts +0 -367
- package/src/http/auth/AuthStore.ts +0 -572
- package/src/http/auth/RoutePolicy.ts +0 -255
- package/src/plugin/Lifecycle.ts +0 -116
- package/src/service/builtins/memory/runtime/Indexer.ts +0 -466
- package/src/service/schedule/Schema.ts +0 -34
- package/src/shared/utils/store/StoreChannelAccountRepository.ts +0 -269
- package/src/shared/utils/store/StoreEnvRepository.ts +0 -452
- package/src/shared/utils/store/StoreModelRepository.ts +0 -324
- package/src/shared/utils/store/StoreSchema.ts +0 -344
- package/src/shared/utils/store/StoreSecureSettings.ts +0 -126
- package/src/shared/utils/store/StoreShared.ts +0 -67
- package/src/shared/utils/store/crypto.ts +0 -112
- package/src/shared/utils/store/index.ts +0 -497
- package/src/shared/utils/store/schema.ts +0 -103
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 模型存储表结构(drizzle schema)。
|
|
3
|
-
*
|
|
4
|
-
* 关键点(中文)
|
|
5
|
-
* - providers 与 models 分表,使用 providerId 做关联。
|
|
6
|
-
* - API Key 以密文字段存储(`apiKeyEncrypted`)。
|
|
7
|
-
*/
|
|
8
|
-
import { index, integer, primaryKey, real, sqliteTable, text, } from "drizzle-orm/sqlite-core";
|
|
9
|
-
export const modelProvidersTable = sqliteTable("model_providers", {
|
|
10
|
-
id: text("id").primaryKey(),
|
|
11
|
-
type: text("type").notNull(),
|
|
12
|
-
baseUrl: text("base_url"),
|
|
13
|
-
apiKeyEncrypted: text("api_key_encrypted"),
|
|
14
|
-
createdAt: text("created_at").notNull(),
|
|
15
|
-
updatedAt: text("updated_at").notNull(),
|
|
16
|
-
});
|
|
17
|
-
export const modelsTable = sqliteTable("models", {
|
|
18
|
-
id: text("id").primaryKey(),
|
|
19
|
-
providerId: text("provider_id").notNull(),
|
|
20
|
-
name: text("name").notNull(),
|
|
21
|
-
temperature: real("temperature"),
|
|
22
|
-
maxTokens: integer("max_tokens"),
|
|
23
|
-
topP: real("top_p"),
|
|
24
|
-
frequencyPenalty: real("frequency_penalty"),
|
|
25
|
-
presencePenalty: real("presence_penalty"),
|
|
26
|
-
anthropicVersion: text("anthropic_version"),
|
|
27
|
-
isPaused: integer("is_paused").notNull().default(0),
|
|
28
|
-
createdAt: text("created_at").notNull(),
|
|
29
|
-
updatedAt: text("updated_at").notNull(),
|
|
30
|
-
}, (table) => ({
|
|
31
|
-
providerIdIdx: index("models_provider_id_idx").on(table.providerId),
|
|
32
|
-
}));
|
|
33
|
-
/**
|
|
34
|
-
* 平台 Env 统一存储表。
|
|
35
|
-
*
|
|
36
|
-
* 关键点(中文)
|
|
37
|
-
* - 全局 env 与 agent env 共用一张表,通过 `scope` + `agentId` 区分。
|
|
38
|
-
* - `agentId` 在 `scope=global` 时固定为空字符串,避免 SQLite 复合主键中的 NULL 语义问题。
|
|
39
|
-
* - value 采用密文存储,解密仅在运行时内存中进行。
|
|
40
|
-
*/
|
|
41
|
-
export const envEntriesTable = sqliteTable("env_entries", {
|
|
42
|
-
scope: text("scope").notNull(),
|
|
43
|
-
agentId: text("agent_id").notNull().default(""),
|
|
44
|
-
key: text("key").notNull(),
|
|
45
|
-
description: text("description"),
|
|
46
|
-
valueEncrypted: text("value_encrypted").notNull(),
|
|
47
|
-
createdAt: text("created_at").notNull(),
|
|
48
|
-
updatedAt: text("updated_at").notNull(),
|
|
49
|
-
}, (table) => ({
|
|
50
|
-
pk: primaryKey({
|
|
51
|
-
columns: [table.scope, table.agentId, table.key],
|
|
52
|
-
name: "env_entries_scope_agent_key_pk",
|
|
53
|
-
}),
|
|
54
|
-
scopeIdx: index("env_entries_scope_idx").on(table.scope),
|
|
55
|
-
agentIdIdx: index("env_entries_agent_id_idx").on(table.agentId),
|
|
56
|
-
}));
|
|
57
|
-
/**
|
|
58
|
-
* Channel Account 表。
|
|
59
|
-
*
|
|
60
|
-
* 关键点(中文)
|
|
61
|
-
* - 各渠道敏感字段独立密文列存储。
|
|
62
|
-
* - `downcity.json` 仅保存 channelAccountId 绑定,不直接保存密钥。
|
|
63
|
-
*/
|
|
64
|
-
export const channelAccountsTable = sqliteTable("channel_accounts", {
|
|
65
|
-
id: text("id").primaryKey(),
|
|
66
|
-
channel: text("channel").notNull(),
|
|
67
|
-
name: text("name").notNull(),
|
|
68
|
-
identity: text("identity"),
|
|
69
|
-
owner: text("owner"),
|
|
70
|
-
creator: text("creator"),
|
|
71
|
-
botTokenEncrypted: text("bot_token_encrypted"),
|
|
72
|
-
appIdEncrypted: text("app_id_encrypted"),
|
|
73
|
-
appSecretEncrypted: text("app_secret_encrypted"),
|
|
74
|
-
domain: text("domain"),
|
|
75
|
-
sandbox: integer("sandbox"),
|
|
76
|
-
createdAt: text("created_at").notNull(),
|
|
77
|
-
updatedAt: text("updated_at").notNull(),
|
|
78
|
-
}, (table) => ({
|
|
79
|
-
channelIdx: index("channel_accounts_channel_idx").on(table.channel),
|
|
80
|
-
}));
|
|
81
|
-
//# sourceMappingURL=schema.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../../../src/shared/utils/store/schema.ts"],"names":[],"mappings":"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"}
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Auth 中间件。
|
|
3
|
-
*
|
|
4
|
-
* 关键点(中文)
|
|
5
|
-
* - 当前先服务 `/api/auth/*` 的受保护子路由。
|
|
6
|
-
* - 后续把更多控制面路由切到 Bearer 鉴权时,可直接复用这一层。
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import type { Context, MiddlewareHandler } from "hono";
|
|
10
|
-
import type { AuthPrincipal } from "@/shared/types/auth/AuthTypes.js";
|
|
11
|
-
import { isAuthError } from "./AuthError.js";
|
|
12
|
-
import type { AuthService } from "./AuthService.js";
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Hono Context 中保存 principal 的 key。
|
|
16
|
-
*/
|
|
17
|
-
export const AUTH_PRINCIPAL_CONTEXT_KEY = "authPrincipal";
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Auth 中间件变量映射。
|
|
21
|
-
*/
|
|
22
|
-
export interface AuthMiddlewareVariables {
|
|
23
|
-
/**
|
|
24
|
-
* 当前请求的认证主体。
|
|
25
|
-
*/
|
|
26
|
-
authPrincipal: AuthPrincipal;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* 生成 Bearer 鉴权中间件。
|
|
31
|
-
*/
|
|
32
|
-
export function createRequireAuthMiddleware(
|
|
33
|
-
authService: AuthService,
|
|
34
|
-
): MiddlewareHandler<{ Variables: AuthMiddlewareVariables }> {
|
|
35
|
-
return async (c, next) => {
|
|
36
|
-
try {
|
|
37
|
-
const principal = authService.authenticateBearerHeader(
|
|
38
|
-
c.req.header("authorization"),
|
|
39
|
-
);
|
|
40
|
-
c.set(AUTH_PRINCIPAL_CONTEXT_KEY, principal);
|
|
41
|
-
await next();
|
|
42
|
-
} catch (error) {
|
|
43
|
-
if (isAuthError(error)) {
|
|
44
|
-
return c.json(
|
|
45
|
-
{ success: false, error: error.message },
|
|
46
|
-
error.status as 200,
|
|
47
|
-
);
|
|
48
|
-
}
|
|
49
|
-
return c.json({ success: false, error: String(error) }, 500);
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* 从 Context 中读取 principal。
|
|
56
|
-
*/
|
|
57
|
-
export function getAuthPrincipal(
|
|
58
|
-
context: Context<{ Variables: AuthMiddlewareVariables }>,
|
|
59
|
-
): AuthPrincipal {
|
|
60
|
-
return context.get(AUTH_PRINCIPAL_CONTEXT_KEY);
|
|
61
|
-
}
|
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Auth API 路由。
|
|
3
|
-
*
|
|
4
|
-
* 关键点(中文)
|
|
5
|
-
* - 本模块只承接 Bearer Token 模型下的最小认证接口。
|
|
6
|
-
* - 路由层不做领域判断,所有业务逻辑统一委托给 `AuthService`。
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { Hono, type Context } from "hono";
|
|
10
|
-
import type { AuthService } from "./AuthService.js";
|
|
11
|
-
import { AuthService as DefaultAuthService } from "./AuthService.js";
|
|
12
|
-
import { isAuthError } from "./AuthError.js";
|
|
13
|
-
import {
|
|
14
|
-
createRequireAuthMiddleware,
|
|
15
|
-
getAuthPrincipal,
|
|
16
|
-
type AuthMiddlewareVariables,
|
|
17
|
-
} from "./AuthMiddleware.js";
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* 注册 auth 路由。
|
|
21
|
-
*/
|
|
22
|
-
export function registerAuthRoutes(params: {
|
|
23
|
-
app: Hono;
|
|
24
|
-
authService?: AuthService;
|
|
25
|
-
}): void {
|
|
26
|
-
const authService = params.authService || new DefaultAuthService();
|
|
27
|
-
const router = new Hono();
|
|
28
|
-
const protectedRouter = new Hono<{ Variables: AuthMiddlewareVariables }>();
|
|
29
|
-
const requireAuth = createRequireAuthMiddleware(authService);
|
|
30
|
-
|
|
31
|
-
router.get("/status", (c) => {
|
|
32
|
-
const initialized = authService.hasLocalCliAccess();
|
|
33
|
-
return c.json({
|
|
34
|
-
success: true,
|
|
35
|
-
initialized,
|
|
36
|
-
requireToken: initialized,
|
|
37
|
-
});
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
protectedRouter.get("/me", requireAuth, (c) => {
|
|
41
|
-
const principal = getAuthPrincipal(c);
|
|
42
|
-
return c.json({
|
|
43
|
-
success: true,
|
|
44
|
-
user: authService.getCurrentUser(principal),
|
|
45
|
-
});
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
protectedRouter.get("/token/list", requireAuth, (c) => {
|
|
49
|
-
const principal = getAuthPrincipal(c);
|
|
50
|
-
return c.json({
|
|
51
|
-
success: true,
|
|
52
|
-
tokens: authService.listTokens(principal),
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
protectedRouter.post("/token/create", requireAuth, async (c) => {
|
|
57
|
-
try {
|
|
58
|
-
const principal = getAuthPrincipal(c);
|
|
59
|
-
const body = (await c.req.json().catch(() => ({}))) as {
|
|
60
|
-
name?: string;
|
|
61
|
-
expiresAt?: string;
|
|
62
|
-
};
|
|
63
|
-
return c.json({
|
|
64
|
-
success: true,
|
|
65
|
-
token: authService.createToken(principal, {
|
|
66
|
-
name: String(body.name || ""),
|
|
67
|
-
expiresAt: typeof body.expiresAt === "string" ? body.expiresAt : undefined,
|
|
68
|
-
}),
|
|
69
|
-
});
|
|
70
|
-
} catch (error) {
|
|
71
|
-
return toErrorResponse(c, error);
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
protectedRouter.post("/token/delete", requireAuth, async (c) => {
|
|
76
|
-
try {
|
|
77
|
-
const principal = getAuthPrincipal(c);
|
|
78
|
-
const body = (await c.req.json().catch(() => ({}))) as {
|
|
79
|
-
tokenId?: string;
|
|
80
|
-
};
|
|
81
|
-
authService.deleteToken(principal, String(body.tokenId || ""));
|
|
82
|
-
return c.json({ success: true });
|
|
83
|
-
} catch (error) {
|
|
84
|
-
return toErrorResponse(c, error);
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
router.route("/", protectedRouter);
|
|
89
|
-
params.app.route("/api/auth", router);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function toErrorResponse(c: Context, error: unknown) {
|
|
93
|
-
if (isAuthError(error)) {
|
|
94
|
-
return c.json(
|
|
95
|
-
{ success: false, error: error.message },
|
|
96
|
-
error.status as 200,
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
return c.json({ success: false, error: String(error) }, 500);
|
|
100
|
-
}
|
|
@@ -1,367 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 统一账户服务层。
|
|
3
|
-
*
|
|
4
|
-
* 关键点(中文)
|
|
5
|
-
* - 该模块承接本机 token 初始化、token 校验与 token 管理等业务语义。
|
|
6
|
-
* - 路由层只调用这里,不直接碰数据库与密码哈希细节。
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import type { AuthIssuedToken, AuthTokenSummary } from "@/shared/types/auth/AuthToken.js";
|
|
10
|
-
import type { AuthPrincipal, AuthTokenRecord, AuthUser } from "@/shared/types/auth/AuthTypes.js";
|
|
11
|
-
import { optionalTrimmedText } from "@/shared/utils/store/StoreShared.js";
|
|
12
|
-
import { AuthError } from "./AuthError.js";
|
|
13
|
-
import { AuthStore, type AuthStoreOptions } from "./AuthStore.js";
|
|
14
|
-
import { extractBearerToken, generateAccessToken, hashAccessToken } from "./TokenService.js";
|
|
15
|
-
|
|
16
|
-
const LOCAL_CLI_USERNAME = "local-cli";
|
|
17
|
-
const LOCAL_CLI_DISPLAY_NAME = "Local CLI";
|
|
18
|
-
const LOCAL_CLI_PASSWORD_HASH = "[token-only-local-cli]";
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* AuthService 构造参数。
|
|
22
|
-
*/
|
|
23
|
-
export interface AuthServiceOptions extends AuthStoreOptions {
|
|
24
|
-
/**
|
|
25
|
-
* 复用外部传入的 store。
|
|
26
|
-
*/
|
|
27
|
-
store?: AuthStore;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* 登录/初始化后返回的用户摘要。
|
|
32
|
-
*/
|
|
33
|
-
export interface AuthCurrentUserPayload {
|
|
34
|
-
/**
|
|
35
|
-
* 用户 ID。
|
|
36
|
-
*/
|
|
37
|
-
id: string;
|
|
38
|
-
/**
|
|
39
|
-
* 用户名。
|
|
40
|
-
*/
|
|
41
|
-
username: string;
|
|
42
|
-
/**
|
|
43
|
-
* 展示名。
|
|
44
|
-
*/
|
|
45
|
-
displayName?: string;
|
|
46
|
-
/**
|
|
47
|
-
* 角色列表。
|
|
48
|
-
*/
|
|
49
|
-
roles: string[];
|
|
50
|
-
/**
|
|
51
|
-
* 权限列表。
|
|
52
|
-
*/
|
|
53
|
-
permissions: string[];
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* AuthService 门面。
|
|
58
|
-
*/
|
|
59
|
-
export class AuthService {
|
|
60
|
-
private readonly store: AuthStore;
|
|
61
|
-
private readonly ownsStore: boolean;
|
|
62
|
-
|
|
63
|
-
constructor(options: AuthServiceOptions = {}) {
|
|
64
|
-
if (options.store) {
|
|
65
|
-
this.store = options.store;
|
|
66
|
-
this.ownsStore = false;
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
this.store = new AuthStore(options);
|
|
70
|
-
this.ownsStore = true;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* 关闭底层连接。
|
|
75
|
-
*/
|
|
76
|
-
close(): void {
|
|
77
|
-
if (this.ownsStore) this.store.close();
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* 判断当前是否已经存在可用的本机 CLI access token。
|
|
82
|
-
*/
|
|
83
|
-
hasLocalCliAccess(): boolean {
|
|
84
|
-
const user = this.store.findUserByUsername(LOCAL_CLI_USERNAME);
|
|
85
|
-
if (!user) return false;
|
|
86
|
-
return this.store
|
|
87
|
-
.listTokensByUserId(user.id)
|
|
88
|
-
.some((item) => this.isTokenActive(item));
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* 确保存在本机 CLI 主体,并为其签发新的 access token。
|
|
93
|
-
*/
|
|
94
|
-
ensureLocalCliAccess(input: {
|
|
95
|
-
tokenName: string;
|
|
96
|
-
expiresAt?: string;
|
|
97
|
-
}): { user: AuthCurrentUserPayload; token: AuthIssuedToken } {
|
|
98
|
-
const token = this.createLocalCliToken({
|
|
99
|
-
name: input.tokenName,
|
|
100
|
-
expiresAt: input.expiresAt,
|
|
101
|
-
});
|
|
102
|
-
const user = this.requireLocalCliUser();
|
|
103
|
-
return {
|
|
104
|
-
user: this.toUserPayload(user),
|
|
105
|
-
token,
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* 读取本机 CLI 主体的 token 列表。
|
|
111
|
-
*/
|
|
112
|
-
listLocalCliTokens(): AuthTokenSummary[] {
|
|
113
|
-
const user = this.store.findUserByUsername(LOCAL_CLI_USERNAME);
|
|
114
|
-
if (!user) return [];
|
|
115
|
-
return this.store
|
|
116
|
-
.listTokensByUserId(user.id)
|
|
117
|
-
.filter((item) => !item.revokedAt)
|
|
118
|
-
.map((item) => this.store.toTokenSummary(item));
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* 为本机 CLI 主体签发新的 access token。
|
|
123
|
-
*/
|
|
124
|
-
createLocalCliToken(input: {
|
|
125
|
-
name: string;
|
|
126
|
-
expiresAt?: string;
|
|
127
|
-
}): AuthIssuedToken {
|
|
128
|
-
const user = this.ensureLocalCliUser();
|
|
129
|
-
const issued = this.issueTokenForUser({
|
|
130
|
-
user,
|
|
131
|
-
tokenName: input.name,
|
|
132
|
-
expiresAt: input.expiresAt,
|
|
133
|
-
});
|
|
134
|
-
this.store.insertAuditLog({
|
|
135
|
-
actorUserId: user.id,
|
|
136
|
-
resourceType: "auth_token",
|
|
137
|
-
resourceId: issued.record.id,
|
|
138
|
-
action: "token_create",
|
|
139
|
-
result: "success",
|
|
140
|
-
metaJson: JSON.stringify({
|
|
141
|
-
name: issued.record.name,
|
|
142
|
-
source: "local-cli",
|
|
143
|
-
}),
|
|
144
|
-
});
|
|
145
|
-
return issued.token;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* 删除本机 CLI 主体下的 token。
|
|
150
|
-
*/
|
|
151
|
-
deleteLocalCliToken(tokenIdInput: string): void {
|
|
152
|
-
const user = this.requireLocalCliUser();
|
|
153
|
-
const record = this.requireLocalCliTokenRecord(tokenIdInput, user.id);
|
|
154
|
-
const deleted = this.store.deleteToken(record.id);
|
|
155
|
-
if (!deleted) throw new AuthError("Token not found", 404);
|
|
156
|
-
this.store.insertAuditLog({
|
|
157
|
-
actorUserId: user.id,
|
|
158
|
-
resourceType: "auth_token",
|
|
159
|
-
resourceId: record.id,
|
|
160
|
-
action: "token_delete",
|
|
161
|
-
result: "success",
|
|
162
|
-
metaJson: JSON.stringify({
|
|
163
|
-
name: record.name,
|
|
164
|
-
source: "local-cli",
|
|
165
|
-
}),
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* 解析 Authorization 头并返回 principal。
|
|
171
|
-
*/
|
|
172
|
-
authenticateBearerHeader(headerValue: string | undefined): AuthPrincipal {
|
|
173
|
-
const plainToken = extractBearerToken(headerValue);
|
|
174
|
-
if (!plainToken) throw new AuthError("Missing bearer token", 401);
|
|
175
|
-
const record = this.store.findTokenByHash(hashAccessToken(plainToken));
|
|
176
|
-
if (!record) throw new AuthError("Invalid bearer token", 401);
|
|
177
|
-
if (record.revokedAt) throw new AuthError("Token is revoked", 401);
|
|
178
|
-
if (record.expiresAt && new Date(record.expiresAt).getTime() <= Date.now()) {
|
|
179
|
-
throw new AuthError("Token is expired", 401);
|
|
180
|
-
}
|
|
181
|
-
const user = this.store.getUserById(record.userId);
|
|
182
|
-
if (!user) throw new AuthError("User not found for token", 401);
|
|
183
|
-
this.ensureUserActive(user);
|
|
184
|
-
this.store.touchToken(record.id);
|
|
185
|
-
return {
|
|
186
|
-
userId: user.id,
|
|
187
|
-
username: user.username,
|
|
188
|
-
displayName: user.displayName,
|
|
189
|
-
status: user.status,
|
|
190
|
-
tokenId: record.id,
|
|
191
|
-
tokenName: record.name,
|
|
192
|
-
roles: this.store.listRoleNamesByUserId(user.id),
|
|
193
|
-
permissions: this.store.listPermissionKeysByUserId(user.id),
|
|
194
|
-
};
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* 返回当前用户信息。
|
|
199
|
-
*/
|
|
200
|
-
getCurrentUser(principal: AuthPrincipal): AuthCurrentUserPayload {
|
|
201
|
-
return {
|
|
202
|
-
id: principal.userId,
|
|
203
|
-
username: principal.username,
|
|
204
|
-
displayName: principal.displayName,
|
|
205
|
-
roles: [...principal.roles],
|
|
206
|
-
permissions: [...principal.permissions],
|
|
207
|
-
};
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
/**
|
|
211
|
-
* 为当前 Bearer 调用主体创建新的 token。
|
|
212
|
-
*/
|
|
213
|
-
createToken(principal: AuthPrincipal, input: {
|
|
214
|
-
name: string;
|
|
215
|
-
expiresAt?: string;
|
|
216
|
-
}): AuthIssuedToken {
|
|
217
|
-
const user = this.store.getUserById(principal.userId);
|
|
218
|
-
if (!user) throw new AuthError("User not found", 404);
|
|
219
|
-
const issued = this.issueTokenForUser({
|
|
220
|
-
user,
|
|
221
|
-
tokenName: input.name,
|
|
222
|
-
expiresAt: input.expiresAt,
|
|
223
|
-
});
|
|
224
|
-
this.store.insertAuditLog({
|
|
225
|
-
actorUserId: principal.userId,
|
|
226
|
-
actorTokenId: principal.tokenId,
|
|
227
|
-
resourceType: "auth_token",
|
|
228
|
-
resourceId: issued.record.id,
|
|
229
|
-
action: "token_create",
|
|
230
|
-
result: "success",
|
|
231
|
-
metaJson: JSON.stringify({ name: issued.record.name }),
|
|
232
|
-
});
|
|
233
|
-
return issued.token;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* 读取当前用户 token 列表。
|
|
238
|
-
*/
|
|
239
|
-
listTokens(principal: AuthPrincipal): AuthTokenSummary[] {
|
|
240
|
-
return this.store
|
|
241
|
-
.listTokensByUserId(principal.userId)
|
|
242
|
-
.filter((item) => !item.revokedAt)
|
|
243
|
-
.map((item) => this.store.toTokenSummary(item));
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
/**
|
|
247
|
-
* 删除当前用户的 token。
|
|
248
|
-
*/
|
|
249
|
-
deleteToken(principal: AuthPrincipal, tokenIdInput: string): void {
|
|
250
|
-
const tokenId = String(tokenIdInput || "").trim();
|
|
251
|
-
if (!tokenId) throw new AuthError("tokenId is required", 400);
|
|
252
|
-
const record = this.store.getTokenById(tokenId);
|
|
253
|
-
if (!record || record.userId !== principal.userId) {
|
|
254
|
-
throw new AuthError("Token not found", 404);
|
|
255
|
-
}
|
|
256
|
-
const deleted = this.store.deleteToken(record.id);
|
|
257
|
-
if (!deleted) throw new AuthError("Token not found", 404);
|
|
258
|
-
this.store.insertAuditLog({
|
|
259
|
-
actorUserId: principal.userId,
|
|
260
|
-
actorTokenId: principal.tokenId,
|
|
261
|
-
resourceType: "auth_token",
|
|
262
|
-
resourceId: tokenId,
|
|
263
|
-
action: "token_delete",
|
|
264
|
-
result: "success",
|
|
265
|
-
metaJson: JSON.stringify({ name: record.name }),
|
|
266
|
-
});
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
private issueTokenForUser(params: {
|
|
270
|
-
user: AuthUser;
|
|
271
|
-
tokenName: string;
|
|
272
|
-
expiresAt?: string;
|
|
273
|
-
}): { record: ReturnType<AuthStore["createToken"]>; token: AuthIssuedToken } {
|
|
274
|
-
const plainToken = generateAccessToken();
|
|
275
|
-
const record = this.store.createToken({
|
|
276
|
-
userId: params.user.id,
|
|
277
|
-
name: this.requireTokenName(params.tokenName),
|
|
278
|
-
tokenHash: hashAccessToken(plainToken),
|
|
279
|
-
expiresAt: optionalTrimmedText(params.expiresAt),
|
|
280
|
-
});
|
|
281
|
-
return {
|
|
282
|
-
record,
|
|
283
|
-
token: this.store.toIssuedToken(record, plainToken),
|
|
284
|
-
};
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
private ensureUserActive(user: AuthUser): void {
|
|
288
|
-
if (user.status !== "active") {
|
|
289
|
-
throw new AuthError("User is disabled", 403);
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
private isTokenActive(record: Pick<AuthTokenRecord, "revokedAt" | "expiresAt">): boolean {
|
|
294
|
-
if (record.revokedAt) return false;
|
|
295
|
-
if (!record.expiresAt) return true;
|
|
296
|
-
return new Date(record.expiresAt).getTime() > Date.now();
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
private ensureLocalCliUser(): AuthUser {
|
|
300
|
-
this.store.ensureDefaultCatalog();
|
|
301
|
-
const existing = this.store.findUserByUsername(LOCAL_CLI_USERNAME);
|
|
302
|
-
if (existing) {
|
|
303
|
-
this.ensureUserActive(existing);
|
|
304
|
-
return existing;
|
|
305
|
-
}
|
|
306
|
-
const user = this.store.createUser({
|
|
307
|
-
username: LOCAL_CLI_USERNAME,
|
|
308
|
-
passwordHash: LOCAL_CLI_PASSWORD_HASH,
|
|
309
|
-
displayName: LOCAL_CLI_DISPLAY_NAME,
|
|
310
|
-
status: "active",
|
|
311
|
-
});
|
|
312
|
-
this.store.assignRoleToUser({
|
|
313
|
-
userId: user.id,
|
|
314
|
-
roleName: "admin",
|
|
315
|
-
});
|
|
316
|
-
return user;
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
private requireTokenName(value: string): string {
|
|
320
|
-
const tokenName = String(value || "").trim();
|
|
321
|
-
if (!tokenName) throw new AuthError("token name is required", 400);
|
|
322
|
-
return tokenName;
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
private requireUserId(value: string): string {
|
|
326
|
-
const userId = String(value || "").trim();
|
|
327
|
-
if (!userId) throw new AuthError("userId is required", 400);
|
|
328
|
-
return userId;
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
private requireUser(userIdInput: string): AuthUser {
|
|
332
|
-
const userId = this.requireUserId(userIdInput);
|
|
333
|
-
const user = this.store.getUserById(userId);
|
|
334
|
-
if (!user) throw new AuthError("User not found", 404);
|
|
335
|
-
return user;
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
private requireLocalCliUser(): AuthUser {
|
|
339
|
-
const user = this.store.findUserByUsername(LOCAL_CLI_USERNAME);
|
|
340
|
-
if (!user) throw new AuthError("Local CLI access is not initialized", 404);
|
|
341
|
-
this.ensureUserActive(user);
|
|
342
|
-
return user;
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
private requireLocalCliTokenRecord(
|
|
346
|
-
tokenIdInput: string,
|
|
347
|
-
expectedUserId: string,
|
|
348
|
-
): AuthTokenRecord {
|
|
349
|
-
const tokenId = String(tokenIdInput || "").trim();
|
|
350
|
-
if (!tokenId) throw new AuthError("tokenId is required", 400);
|
|
351
|
-
const record = this.store.getTokenById(tokenId);
|
|
352
|
-
if (!record || record.userId !== expectedUserId) {
|
|
353
|
-
throw new AuthError("Token not found", 404);
|
|
354
|
-
}
|
|
355
|
-
return record;
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
private toUserPayload(user: AuthUser): AuthCurrentUserPayload {
|
|
359
|
-
return {
|
|
360
|
-
id: user.id,
|
|
361
|
-
username: user.username,
|
|
362
|
-
displayName: user.displayName,
|
|
363
|
-
roles: this.store.listRoleNamesByUserId(user.id),
|
|
364
|
-
permissions: this.store.listPermissionKeysByUserId(user.id),
|
|
365
|
-
};
|
|
366
|
-
}
|
|
367
|
-
}
|