refactor: share allowlist wildcard matching

This commit is contained in:
Peter Steinberger 2026-03-14 01:20:59 +00:00
parent f4094ab19e
commit c0831927b0
4 changed files with 19 additions and 17 deletions

View File

@ -1,7 +1,7 @@
import {
compileAllowlist,
normalizeStringEntries,
resolveAllowlistCandidates,
resolveCompiledAllowlistMatch,
type AllowlistMatch,
} from "openclaw/plugin-sdk/matrix";
@ -77,19 +77,13 @@ export function resolveMatrixAllowListMatch(params: {
userId?: string;
}): MatrixAllowListMatch {
const compiledAllowList = compileAllowlist(params.allowList);
if (compiledAllowList.set.size === 0) {
return { allowed: false };
}
if (compiledAllowList.wildcard) {
return { allowed: true, matchKey: "*", matchSource: "wildcard" };
}
const userId = normalizeMatrixUser(params.userId);
const candidates: Array<{ value?: string; source: MatrixAllowListSource }> = [
{ value: userId, source: "id" },
{ value: userId ? `matrix:${userId}` : "", source: "prefixed-id" },
{ value: userId ? `user:${userId}` : "", source: "prefixed-user" },
];
return resolveAllowlistCandidates({
return resolveCompiledAllowlistMatch({
compiledAllowlist: compiledAllowList,
candidates,
});

View File

@ -60,11 +60,24 @@ export function resolveAllowlistCandidates<TSource extends string>(params: {
return { allowed: false };
}
export function resolveCompiledAllowlistMatch<TSource extends string>(params: {
compiledAllowlist: CompiledAllowlist;
candidates: Array<{ value?: string; source: TSource }>;
}): AllowlistMatch<TSource> {
if (params.compiledAllowlist.set.size === 0) {
return { allowed: false };
}
if (params.compiledAllowlist.wildcard) {
return { allowed: true, matchKey: "*", matchSource: "wildcard" as TSource };
}
return resolveAllowlistCandidates(params);
}
export function resolveAllowlistMatchByCandidates<TSource extends string>(params: {
allowList: ReadonlyArray<string>;
candidates: Array<{ value?: string; source: TSource }>;
}): AllowlistMatch<TSource> {
return resolveAllowlistCandidates({
return resolveCompiledAllowlistMatch({
compiledAllowlist: compileAllowlist(params.allowList),
candidates: params.candidates,
});

View File

@ -11,6 +11,7 @@ export {
export type { ReplyPayload } from "../auto-reply/types.js";
export {
compileAllowlist,
resolveCompiledAllowlistMatch,
resolveAllowlistCandidates,
resolveAllowlistMatchByCandidates,
} from "../channels/allowlist-match.js";

View File

@ -1,6 +1,6 @@
import {
compileAllowlist,
resolveAllowlistCandidates,
resolveCompiledAllowlistMatch,
type AllowlistMatch,
} from "../../channels/allowlist-match.js";
import {
@ -58,12 +58,6 @@ export function resolveSlackAllowListMatch(params: {
allowNameMatching?: boolean;
}): SlackAllowListMatch {
const compiledAllowList = compileAllowlist(params.allowList);
if (compiledAllowList.set.size === 0) {
return { allowed: false };
}
if (compiledAllowList.wildcard) {
return { allowed: true, matchKey: "*", matchSource: "wildcard" };
}
const id = params.id?.toLowerCase();
const name = params.name?.toLowerCase();
const slug = normalizeSlackSlug(name);
@ -79,7 +73,7 @@ export function resolveSlackAllowListMatch(params: {
] satisfies Array<{ value?: string; source: SlackAllowListSource }>)
: []),
];
return resolveAllowlistCandidates({
return resolveCompiledAllowlistMatch({
compiledAllowlist: compiledAllowList,
candidates,
});