Skip to content

Commit c670d78

Browse files
wahaha1223ZengWen-DT
authored andcommitted
fix(msteams): ignore blank certificate settings
1 parent 2f00a41 commit c670d78

2 files changed

Lines changed: 66 additions & 7 deletions

File tree

extensions/msteams/src/token.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,42 @@ describe("token – federated credentials (certificate)", () => {
143143
expect(hasConfiguredMSTeamsCredentials(cfg)).toBe(false);
144144
});
145145

146+
it("ignores blank certificate settings", () => {
147+
process.env.MSTEAMS_CERTIFICATE_PATH = " ";
148+
const cfg = {
149+
appId: "app-id",
150+
tenantId: "tenant-id",
151+
authType: "federated",
152+
certificatePath: " ",
153+
} satisfies MSTeamsConfig;
154+
155+
expect(hasConfiguredMSTeamsCredentials(cfg)).toBe(false);
156+
expect(resolveMSTeamsCredentials(cfg)).toBeUndefined();
157+
});
158+
159+
it("falls back to env certificate settings without changing path bytes", () => {
160+
process.env.MSTEAMS_CERTIFICATE_PATH = " /env/cert.pem ";
161+
process.env.MSTEAMS_CERTIFICATE_THUMBPRINT = " EEFF0011 ";
162+
const cfg = {
163+
appId: "app-id",
164+
tenantId: "tenant-id",
165+
authType: "federated",
166+
certificatePath: " ",
167+
certificateThumbprint: " ",
168+
} satisfies MSTeamsConfig;
169+
170+
expect(hasConfiguredMSTeamsCredentials(cfg)).toBe(true);
171+
expect(resolveMSTeamsCredentials(cfg)).toEqual({
172+
type: "federated",
173+
appId: "app-id",
174+
tenantId: "tenant-id",
175+
certificatePath: " /env/cert.pem ",
176+
certificateThumbprint: "EEFF0011",
177+
useManagedIdentity: undefined,
178+
managedIdentityClientId: undefined,
179+
});
180+
});
181+
146182
it("resolves federated credentials with certificate from config", () => {
147183
const cfg = {
148184
appId: "app-id",

extensions/msteams/src/token.ts

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Msteams plugin module implements token behavior.
22
import { isFutureDateTimestampMs } from "openclaw/plugin-sdk/number-runtime";
3+
import { normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runtime";
34
import type { MSTeamsConfig } from "../runtime-api.js";
45
import { loadMSTeamsDelegatedTokens, saveMSTeamsDelegatedTokens } from "./delegated-state.js";
56
import type { MSTeamsDelegatedTokens } from "./oauth.shared.js";
@@ -47,6 +48,20 @@ function resolveAuthType(cfg?: MSTeamsConfig): "secret" | "federated" {
4748
return "secret";
4849
}
4950

51+
function resolveFederatedString(configValue: unknown, envValue: unknown): string | undefined {
52+
return normalizeOptionalString(configValue) ?? normalizeOptionalString(envValue);
53+
}
54+
55+
function resolveFederatedPath(configValue?: string, envValue?: string): string | undefined {
56+
if (normalizeOptionalString(configValue)) {
57+
return configValue;
58+
}
59+
if (normalizeOptionalString(envValue)) {
60+
return envValue;
61+
}
62+
return undefined;
63+
}
64+
5065
// ── hasConfiguredMSTeamsCredentials ────────────────────────────────────────
5166

5267
export function hasConfiguredMSTeamsCredentials(cfg?: MSTeamsConfig): boolean {
@@ -62,7 +77,9 @@ export function hasConfiguredMSTeamsCredentials(cfg?: MSTeamsConfig): boolean {
6277
);
6378

6479
if (authType === "federated") {
65-
const hasCert = Boolean(cfg?.certificatePath || process.env.MSTEAMS_CERTIFICATE_PATH);
80+
const hasCert = Boolean(
81+
resolveFederatedPath(cfg?.certificatePath, process.env.MSTEAMS_CERTIFICATE_PATH),
82+
);
6683
const hasManagedIdentity =
6784
cfg?.useManagedIdentity ?? process.env.MSTEAMS_USE_MANAGED_IDENTITY === "true";
6885

@@ -95,17 +112,23 @@ export function resolveMSTeamsCredentials(cfg?: MSTeamsConfig): MSTeamsCredentia
95112
}
96113

97114
if (authType === "federated") {
98-
const certificatePath =
99-
cfg?.certificatePath || process.env.MSTEAMS_CERTIFICATE_PATH || undefined;
115+
const certificatePath = resolveFederatedPath(
116+
cfg?.certificatePath,
117+
process.env.MSTEAMS_CERTIFICATE_PATH,
118+
);
100119

101-
const certificateThumbprint =
102-
cfg?.certificateThumbprint || process.env.MSTEAMS_CERTIFICATE_THUMBPRINT || undefined;
120+
const certificateThumbprint = resolveFederatedString(
121+
cfg?.certificateThumbprint,
122+
process.env.MSTEAMS_CERTIFICATE_THUMBPRINT,
123+
);
103124

104125
const useManagedIdentity =
105126
cfg?.useManagedIdentity ?? process.env.MSTEAMS_USE_MANAGED_IDENTITY === "true";
106127

107-
const managedIdentityClientId =
108-
cfg?.managedIdentityClientId || process.env.MSTEAMS_MANAGED_IDENTITY_CLIENT_ID || undefined;
128+
const managedIdentityClientId = resolveFederatedString(
129+
cfg?.managedIdentityClientId,
130+
process.env.MSTEAMS_MANAGED_IDENTITY_CLIENT_ID,
131+
);
109132

110133
// At least one federated mechanism must be configured.
111134
if (!certificatePath && !useManagedIdentity) {

0 commit comments

Comments
 (0)