refactor: share channel config security scaffolding

This commit is contained in:
Peter Steinberger
2026-03-10 20:31:14 +00:00
parent 725958c66f
commit 4a8e039a5f
6 changed files with 112 additions and 116 deletions

View File

@@ -2,6 +2,7 @@ import {
deleteAccountFromConfigSection,
setAccountEnabledInConfigSection,
} from "../channels/plugins/config-helpers.js";
import { buildAccountScopedDmSecurityPolicy } from "../channels/plugins/helpers.js";
import { normalizeWhatsAppAllowFromEntries } from "../channels/plugins/normalize/whatsapp.js";
import type { ChannelConfigAdapter } from "../channels/plugins/types.adapters.js";
import type { OpenClawConfig } from "../config/config.js";
@@ -104,6 +105,45 @@ export function createScopedChannelConfigBase<
};
}
export function createScopedDmSecurityResolver<
ResolvedAccount extends { accountId?: string | null },
>(params: {
channelKey: string;
resolvePolicy: (account: ResolvedAccount) => string | null | undefined;
resolveAllowFrom: (account: ResolvedAccount) => Array<string | number> | null | undefined;
resolveFallbackAccountId?: (account: ResolvedAccount) => string | null | undefined;
defaultPolicy?: string;
allowFromPathSuffix?: string;
policyPathSuffix?: string;
approveChannelId?: string;
approveHint?: string;
normalizeEntry?: (raw: string) => string;
}) {
return ({
cfg,
accountId,
account,
}: {
cfg: OpenClawConfig;
accountId?: string | null;
account: ResolvedAccount;
}) =>
buildAccountScopedDmSecurityPolicy({
cfg,
channelKey: params.channelKey,
accountId,
fallbackAccountId: params.resolveFallbackAccountId?.(account) ?? account.accountId,
policy: params.resolvePolicy(account),
allowFrom: params.resolveAllowFrom(account) ?? [],
defaultPolicy: params.defaultPolicy,
allowFromPathSuffix: params.allowFromPathSuffix,
policyPathSuffix: params.policyPathSuffix,
approveChannelId: params.approveChannelId,
approveHint: params.approveHint,
normalizeEntry: params.normalizeEntry,
});
}
export function resolveWhatsAppConfigAllowFrom(params: {
cfg: OpenClawConfig;
accountId?: string | null;

View File

@@ -390,6 +390,7 @@ export {
formatTrimmedAllowFromEntries,
mapAllowFromEntries,
resolveOptionalConfigString,
createScopedDmSecurityResolver,
formatWhatsAppConfigAllowFromEntries,
resolveIMessageConfigAllowFrom,
resolveIMessageConfigDefaultTo,