openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
188 lines (187 loc) • 7.47 kB
JavaScript
import { a as normalizeLowercaseStringOrEmpty } from "./string-coerce-DW4mBlAt.js";
import { _ as uniqueStrings } from "./string-normalization-CRyoFBPt.js";
import { a as resolveAllowlistMatchSimple } from "./allowlist-match-Cg15MVcF.js";
import { a as parseAccessGroupAllowFromEntry } from "./allow-from-o-cfFFcK.js";
import "./media-runtime-CP3Buesq.js";
import "./agent-media-payload-CSLL6yrE.js";
import "./number-runtime-DBLVDypr.js";
import "./string-coerce-runtime-BtLK_KtO.js";
import "./core-DSBgt6Pg.js";
import "./allow-from-nMWw_kVf.js";
import { n as isDangerousNameMatchingEnabled } from "./dangerous-name-matching-Z6nhxFXz.js";
import "./reply-history-DDmYe8Zf.js";
import "./channel-outbound-CHb-uDxw.js";
import "./outbound-media-DbqHlRw6.js";
import "./dangerous-name-runtime-cJriWyuh.js";
import "./command-auth-native-IFmI8urX.js";
import "./channel-inbound-Bl4qL4ZS.js";
import "./channel-feedback-CVLJbdV5.js";
import { i as resolveStableChannelMessageIngress } from "./message-access-CeqV-XzC.js";
import "./channel-ingress-runtime-WZoA_t1N.js";
import "./channel-pairing-Cr6y6rdp.js";
import "./models-provider-runtime-DA6nGT_Z.js";
import "./webhook-ingress-Btd-EgwJ.js";
import "./webhook-targets-k9Oe4zzO.js";
//#region extensions/mattermost/src/mattermost/monitor-auth.ts
const mattermostIngressIdentity = {
key: "sender-id",
normalize: normalizeMattermostAllowEntry,
aliases: [{
key: "sender-name",
kind: "plugin:mattermost-user-name",
normalizeEntry: normalizeMattermostAllowEntry,
normalizeSubject: normalizeMattermostAllowEntry,
dangerous: true
}],
isWildcardEntry: (entry) => normalizeMattermostAllowEntry(entry) === "*",
resolveEntryId: ({ entryIndex, fieldKey }) => `mattermost-entry-${entryIndex + 1}:${fieldKey === "sender-name" ? "name" : "user"}`
};
function normalizeMattermostAllowEntry(entry) {
const trimmed = entry.trim();
if (!trimmed) return "";
if (trimmed === "*") return "*";
const accessGroupName = parseAccessGroupAllowFromEntry(trimmed);
if (accessGroupName) return `accessGroup:${accessGroupName}`;
const normalized = trimmed.replace(/^(mattermost|user):/i, "").replace(/^@/, "").trim();
return normalized ? normalizeLowercaseStringOrEmpty(normalized) : "";
}
function normalizeMattermostAllowList(entries) {
return uniqueStrings(entries.map((entry) => normalizeMattermostAllowEntry(String(entry))).filter(Boolean));
}
function formatMattermostDirectMessageDropLog(params) {
const reason = params.reasonCode ? ` reason=${params.reasonCode}` : "";
const hint = params.dmPolicy === "open" && params.reasonCode === "dm_policy_not_allowlisted" ? " hint=add-allowFrom-wildcard" : "";
return `mattermost: drop dm sender=${params.senderId} (dmPolicy=${params.dmPolicy}${reason}${hint})`;
}
function isMattermostSenderAllowed(params) {
const allowFrom = normalizeMattermostAllowList(params.allowFrom);
if (allowFrom.length === 0) return false;
return resolveAllowlistMatchSimple({
allowFrom,
senderId: normalizeMattermostAllowEntry(params.senderId),
senderName: params.senderName ? normalizeMattermostAllowEntry(params.senderName) : void 0,
allowNameMatching: params.allowNameMatching
}).allowed;
}
function mapMattermostChannelKind(channelType) {
const normalized = channelType?.trim().toUpperCase();
if (normalized === "D") return "direct";
if (normalized === "G" || normalized === "P") return "group";
return "channel";
}
async function resolveMattermostMonitorInboundAccess(params) {
const { account, cfg, senderId, senderName, channelId, kind, groupPolicy, storeAllowFrom, allowTextCommands, hasControlCommand } = params;
const dmPolicy = account.config.dmPolicy ?? "pairing";
const allowNameMatching = isDangerousNameMatchingEnabled(account.config);
const configAllowFrom = account.config.allowFrom ?? [];
const configGroupAllowFrom = account.config.groupAllowFrom ?? [];
const readStoreAllowFrom = params.readStoreAllowFrom ?? (storeAllowFrom != null ? async () => [...storeAllowFrom] : void 0);
return await resolveStableChannelMessageIngress({
channelId: "mattermost",
accountId: account.accountId,
identity: mattermostIngressIdentity,
cfg,
...readStoreAllowFrom ? { readStoreAllowFrom } : {},
useDefaultPairingStore: params.readStoreAllowFrom === void 0 && storeAllowFrom == null,
subject: {
stableId: senderId,
aliases: { "sender-name": senderName }
},
conversation: {
kind,
id: channelId
},
event: {
kind: params.eventKind ?? "message",
authMode: "inbound",
mayPair: params.mayPair ?? true
},
dmPolicy,
groupPolicy,
policy: {
groupAllowFromFallbackToAllowFrom: true,
mutableIdentifierMatching: allowNameMatching ? "enabled" : "disabled"
},
allowFrom: configAllowFrom,
groupAllowFrom: configGroupAllowFrom,
command: {
allowTextCommands,
hasControlCommand: allowTextCommands && hasControlCommand,
directGroupAllowFrom: kind === "direct" ? "effective" : "none"
}
});
}
function resolveMattermostCommandDenyReason(params) {
if (params.decision.decision === "allow") return null;
if (params.kind === "direct") {
if (params.decision.reasonCode === "dm_policy_disabled") return "dm-disabled";
if (params.dmPolicy === "pairing" && (params.decision.admission === "pairing-required" || params.decision.reasonCode === "dm_policy_pairing_required")) return "dm-pairing";
return "unauthorized";
}
if (params.decision.reasonCode === "group_policy_disabled") return "channels-disabled";
if (params.decision.reasonCode === "group_policy_empty_allowlist") return "channel-no-allowlist";
return "unauthorized";
}
async function authorizeMattermostCommandInvocation(params) {
const { account, cfg, senderId, senderName, channelId, channelInfo, storeAllowFrom, readStoreAllowFrom, allowTextCommands, hasControlCommand } = params;
if (!channelInfo?.type) return {
ok: false,
denyReason: "unknown-channel",
commandAuthorized: false,
channelInfo,
kind: "channel",
chatType: "channel",
channelName: "",
channelDisplay: "",
roomLabel: `#${channelId}`
};
const kind = mapMattermostChannelKind(channelInfo.type);
const chatType = kind;
const channelName = channelInfo.name ?? "";
const channelDisplay = channelInfo.display_name ?? channelName;
const roomLabel = channelName ? `#${channelName}` : channelDisplay || `#${channelId}`;
const defaultGroupPolicy = cfg.channels?.defaults?.groupPolicy;
const ingress = await resolveMattermostMonitorInboundAccess({
account,
cfg,
senderId,
senderName,
channelId,
kind,
groupPolicy: account.config.groupPolicy ?? defaultGroupPolicy ?? "allowlist",
storeAllowFrom,
readStoreAllowFrom,
allowTextCommands,
hasControlCommand,
eventKind: "native-command",
mayPair: true
});
const denyReason = resolveMattermostCommandDenyReason({
decision: ingress.ingress,
kind,
dmPolicy: account.config.dmPolicy ?? "pairing"
});
if (denyReason) return {
ok: false,
denyReason,
commandAuthorized: false,
channelInfo,
kind,
chatType,
channelName,
channelDisplay,
roomLabel
};
return {
ok: true,
commandAuthorized: ingress.commandAccess.authorized,
channelInfo,
kind,
chatType,
channelName,
channelDisplay,
roomLabel
};
}
//#endregion
export { normalizeMattermostAllowList as a, normalizeMattermostAllowEntry as i, formatMattermostDirectMessageDropLog as n, resolveMattermostMonitorInboundAccess as o, isMattermostSenderAllowed as r, authorizeMattermostCommandInvocation as t };