mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-26 04:15:48 -06:00
8bcbc3178b
* fix(msteams): ignore blank certificate settings * fix(msteams): preserve federated certificate path behavior --------- Co-authored-by: Peter Steinberger <steipete@gmail.com>
199 lines
6.3 KiB
TypeScript
199 lines
6.3 KiB
TypeScript
// Msteams plugin module implements token behavior.
|
|
import { isFutureDateTimestampMs } from "openclaw/plugin-sdk/number-runtime";
|
|
import { normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runtime";
|
|
import type { MSTeamsConfig } from "../runtime-api.js";
|
|
import { loadMSTeamsDelegatedTokens, saveMSTeamsDelegatedTokens } from "./delegated-state.js";
|
|
import type { MSTeamsDelegatedTokens } from "./oauth.shared.js";
|
|
import { refreshMSTeamsDelegatedTokens } from "./oauth.token.js";
|
|
import {
|
|
hasConfiguredSecretInput,
|
|
normalizeResolvedSecretInputString,
|
|
normalizeSecretInputString,
|
|
} from "./secret-input.js";
|
|
|
|
// ── Credential types ───────────────────────────────────────────────────────
|
|
|
|
type MSTeamsSecretCredentials = {
|
|
type: "secret";
|
|
appId: string;
|
|
appPassword: string;
|
|
tenantId: string;
|
|
};
|
|
|
|
export type MSTeamsFederatedCredentials = {
|
|
type: "federated";
|
|
appId: string;
|
|
tenantId: string;
|
|
certificatePath?: string;
|
|
certificateThumbprint?: string;
|
|
useManagedIdentity?: boolean;
|
|
managedIdentityClientId?: string;
|
|
};
|
|
|
|
export type MSTeamsCredentials = MSTeamsSecretCredentials | MSTeamsFederatedCredentials;
|
|
|
|
// ── Helpers ────────────────────────────────────────────────────────────────
|
|
|
|
function resolveAuthType(cfg?: MSTeamsConfig): "secret" | "federated" {
|
|
const fromCfg = cfg?.authType;
|
|
if (fromCfg === "secret" || fromCfg === "federated") {
|
|
return fromCfg;
|
|
}
|
|
|
|
const fromEnv = process.env.MSTEAMS_AUTH_TYPE;
|
|
if (fromEnv === "federated") {
|
|
return "federated";
|
|
}
|
|
|
|
return "secret";
|
|
}
|
|
|
|
function resolveFederatedPath(configValue?: string, envValue?: string): string | undefined {
|
|
// Reject blank settings without trimming a real path: surrounding whitespace
|
|
// can be part of the certificate filename on the filesystem.
|
|
if (normalizeOptionalString(configValue)) {
|
|
return configValue;
|
|
}
|
|
if (normalizeOptionalString(envValue)) {
|
|
return envValue;
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
// ── hasConfiguredMSTeamsCredentials ────────────────────────────────────────
|
|
|
|
export function hasConfiguredMSTeamsCredentials(cfg?: MSTeamsConfig): boolean {
|
|
const authType = resolveAuthType(cfg);
|
|
|
|
const hasAppId = Boolean(
|
|
normalizeSecretInputString(cfg?.appId) ||
|
|
normalizeSecretInputString(process.env.MSTEAMS_APP_ID),
|
|
);
|
|
const hasTenantId = Boolean(
|
|
normalizeSecretInputString(cfg?.tenantId) ||
|
|
normalizeSecretInputString(process.env.MSTEAMS_TENANT_ID),
|
|
);
|
|
|
|
if (authType === "federated") {
|
|
const hasCert = Boolean(
|
|
resolveFederatedPath(cfg?.certificatePath, process.env.MSTEAMS_CERTIFICATE_PATH),
|
|
);
|
|
const hasManagedIdentity =
|
|
cfg?.useManagedIdentity ?? process.env.MSTEAMS_USE_MANAGED_IDENTITY === "true";
|
|
|
|
return hasAppId && hasTenantId && (hasCert || hasManagedIdentity);
|
|
}
|
|
|
|
// "secret" (default) — original logic
|
|
return Boolean(
|
|
normalizeSecretInputString(cfg?.appId) &&
|
|
hasConfiguredSecretInput(cfg?.appPassword) &&
|
|
normalizeSecretInputString(cfg?.tenantId),
|
|
);
|
|
}
|
|
|
|
// ── resolveMSTeamsCredentials ─────────────────────────────────────────────
|
|
|
|
export function resolveMSTeamsCredentials(cfg?: MSTeamsConfig): MSTeamsCredentials | undefined {
|
|
const authType = resolveAuthType(cfg);
|
|
|
|
const appId =
|
|
normalizeSecretInputString(cfg?.appId) ||
|
|
normalizeSecretInputString(process.env.MSTEAMS_APP_ID);
|
|
|
|
const tenantId =
|
|
normalizeSecretInputString(cfg?.tenantId) ||
|
|
normalizeSecretInputString(process.env.MSTEAMS_TENANT_ID);
|
|
|
|
if (!appId || !tenantId) {
|
|
return undefined;
|
|
}
|
|
|
|
if (authType === "federated") {
|
|
const certificatePath = resolveFederatedPath(
|
|
cfg?.certificatePath,
|
|
process.env.MSTEAMS_CERTIFICATE_PATH,
|
|
);
|
|
|
|
const certificateThumbprint =
|
|
cfg?.certificateThumbprint || process.env.MSTEAMS_CERTIFICATE_THUMBPRINT || undefined;
|
|
|
|
const useManagedIdentity =
|
|
cfg?.useManagedIdentity ?? process.env.MSTEAMS_USE_MANAGED_IDENTITY === "true";
|
|
|
|
const managedIdentityClientId =
|
|
cfg?.managedIdentityClientId || process.env.MSTEAMS_MANAGED_IDENTITY_CLIENT_ID || undefined;
|
|
|
|
// At least one federated mechanism must be configured.
|
|
if (!certificatePath && !useManagedIdentity) {
|
|
return undefined;
|
|
}
|
|
|
|
return {
|
|
type: "federated",
|
|
appId,
|
|
tenantId,
|
|
certificatePath,
|
|
certificateThumbprint,
|
|
useManagedIdentity: useManagedIdentity || undefined,
|
|
managedIdentityClientId,
|
|
};
|
|
}
|
|
|
|
// "secret" (default) — original logic
|
|
const appPassword =
|
|
normalizeResolvedSecretInputString({
|
|
value: cfg?.appPassword,
|
|
path: "channels.msteams.appPassword",
|
|
}) || normalizeSecretInputString(process.env.MSTEAMS_APP_PASSWORD);
|
|
|
|
if (!appPassword) {
|
|
return undefined;
|
|
}
|
|
|
|
return { type: "secret", appId, appPassword, tenantId };
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Delegated token storage / resolution
|
|
// ---------------------------------------------------------------------------
|
|
|
|
export function loadDelegatedTokens(): MSTeamsDelegatedTokens | undefined {
|
|
return loadMSTeamsDelegatedTokens();
|
|
}
|
|
|
|
export function saveDelegatedTokens(tokens: MSTeamsDelegatedTokens): void {
|
|
saveMSTeamsDelegatedTokens(tokens);
|
|
}
|
|
|
|
export async function resolveDelegatedAccessToken(params: {
|
|
tenantId: string;
|
|
clientId: string;
|
|
clientSecret: string;
|
|
}): Promise<string | undefined> {
|
|
const tokens = loadDelegatedTokens();
|
|
if (!tokens) {
|
|
return undefined;
|
|
}
|
|
|
|
// Token still valid (5-min buffer already baked into expiresAt)
|
|
if (isFutureDateTimestampMs(tokens.expiresAt)) {
|
|
return tokens.accessToken;
|
|
}
|
|
|
|
// Attempt refresh
|
|
try {
|
|
const refreshed = await refreshMSTeamsDelegatedTokens({
|
|
tenantId: params.tenantId,
|
|
clientId: params.clientId,
|
|
clientSecret: params.clientSecret,
|
|
refreshToken: tokens.refreshToken,
|
|
scopes: tokens.scopes,
|
|
});
|
|
saveDelegatedTokens(refreshed);
|
|
return refreshed.accessToken;
|
|
} catch {
|
|
return undefined;
|
|
}
|
|
}
|