feat: 支持按品牌配置微信登录凭据
CI / Browser smoke (pull_request) Successful in 58s
CI / Static checks (pull_request) Successful in 1m32s
CI / API integration (pull_request) Successful in 1m27s
CI / Production build (pull_request) Successful in 2m7s

This commit is contained in:
que01 committed 2026-10-23 22:07:39 +08:00
1 parent e75c524343
commit d0a0eeea8c
23 files changed
+460 -62

No files matched your search

+79 -3
View File
@@ -11,6 +11,7 @@ import { getTemplatePackageManifest, getVisualTemplateManifest } from "@personal
import { createApp } from "../dist/main.js";
import { readIdentity, signIdentity } from "../dist/auth.js";
import { WechatAuthService } from "../dist/wechat-auth.js";
import { encryptMiniAppCredential } from "../dist/miniapp-credential-crypto.js";
import { AdminMiniAppReleaseService } from "../dist/modules/admin/admin-miniapp-release.service.js";
import { PostgresClientErrorThrottleStore } from "../dist/modules/admin/client-error-throttle.postgres.store.js";
import {
@@ -866,7 +867,7 @@ test("API restart recovers an expired persisted miniapp build attempt", async ()
}
});
test("miniapp release settings never expose or persist plaintext private keys", async () => {
test("miniapp release settings never expose or persist plaintext credentials", async () => {
const login = await request("/admin/session/login", {
method: "POST",
payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD },
@@ -897,6 +898,13 @@ test("miniapp release settings never expose or persist plaintext private keys",
payload: { appId: "wxtestsettings", apiBaseUrl: "http://wechat.example.com/api" },
});
assert.equal(rejected.statusCode, 400);
const invalidSecret = await request("/admin/miniapp-releases/settings", {
method: "PATCH",
headers,
payload: { appSecret: "invalid secret!" },
});
assert.equal(invalidSecret.statusCode, 400);
const appSecret = "0123456789abcdef0123456789abcdef";
const saved = await request("/admin/miniapp-releases/settings", {
method: "PATCH",
headers,
@@ -904,6 +912,7 @@ test("miniapp release settings never expose or persist plaintext private keys",
appId: "wxtestsettings",
apiBaseUrl: "https://wechat.example.com/api/",
privateKey,
appSecret,
},
});
assert.equal(saved.statusCode, 200);
@@ -912,17 +921,24 @@ test("miniapp release settings never expose or persist plaintext private keys",
assert.equal(body.apiBaseUrl, "https://wechat.example.com/api");
assert.equal(body.requestDomain, "wechat.example.com");
assert.equal(body.hasStoredCredentials, true);
assert.equal(body.hasStoredAppSecret, true);
assert.equal(body.loginConfigured, true);
assert.equal(body.privateKey, undefined);
assert.equal(body.credential?.algorithm, "aes-256-gcm");
assert.equal("appSecretCredential" in body, false);
assert.ok(body.requestDomain === null || typeof body.requestDomain === "string");
assert.ok(body.buildError === null || typeof body.buildError === "string");
if (typeof body.requestDomain === "string") assert.ok(!body.requestDomain.includes("localhost"));
const brand = await prisma.brandProfile.findUniqueOrThrow({ where: { id: brandId } });
const credential = brand.miniAppSettings?.credential;
const appSecretCredential = brand.miniAppSettings?.appSecretCredential;
assert.ok(credential);
assert.ok(appSecretCredential);
assert.equal(brand.miniAppSettings.apiBaseUrl, "https://wechat.example.com/api");
assert.equal(typeof credential.ciphertext, "string");
assert.equal(typeof appSecretCredential.ciphertext, "string");
assert.ok(!JSON.stringify(brand.miniAppSettings).includes(privateKey));
assert.ok(!JSON.stringify(brand.miniAppSettings).includes(appSecret));
const missing = await request("/admin/miniapp-releases/settings");
assert.equal(missing.statusCode, 401);
const forbidden = await request("/admin/miniapp-releases/settings", {
@@ -5597,13 +5613,30 @@ test("wechat login validates the request DTO before exchanging the code", async
assert.equal(response.statusCode, 400);
});
test("wechat login creates a customer session for the requested brand", async () => {
test("wechat login creates a customer session using the requested brand credentials", async () => {
const doorsBrand = await prisma.brandProfile.findUnique({ where: { slug: "linjing-doors" } });
assert.ok(doorsBrand);
const sessionOpenid = `doors-login-${Date.now()}`;
const appId = "wxintegrationdoors";
const appSecret = "0123456789abcdef0123456789abcdef";
const originalSettings = doorsBrand.miniAppSettings;
await prisma.brandProfile.update({
where: { id: doorsBrand.id },
data: {
miniAppSettings: {
...(originalSettings && typeof originalSettings === "object" ? originalSettings : {}),
appId,
appSecretCredential: encryptMiniAppCredential(appSecret),
},
},
});
const wechatAuth = app.get(WechatAuthService);
const originalExchangeCode = wechatAuth.exchangeCode;
wechatAuth.exchangeCode = async () => ({ openid: sessionOpenid });
wechatAuth.exchangeCode = async (code, credentials) => {
assert.equal(code, "test-code");
assert.deepEqual(credentials, { appId, appSecret });
return { openid: sessionOpenid };
};
try {
const response = await request("/customer/session/wechat?brandSlug=linjing-doors", {
method: "POST",
@@ -5621,6 +5654,49 @@ test("wechat login creates a customer session for the requested brand", async ()
} finally {
wechatAuth.exchangeCode = originalExchangeCode;
await prisma.customerUser.deleteMany({ where: { openid: sessionOpenid } });
await prisma.brandProfile.update({
where: { id: doorsBrand.id },
data: { miniAppSettings: originalSettings },
});
}
});
test("wechat login fails closed when a brand has no login AppSecret", async () => {
const brand = await prisma.brandProfile.findUnique({ where: { slug: "default-brand" } });
assert.ok(brand);
const originalSettings = brand.miniAppSettings;
const { appSecretCredential: _ignoredAppSecretCredential, ...settingsWithoutAppSecret } =
originalSettings && typeof originalSettings === "object" ? originalSettings : {};
await prisma.brandProfile.update({
where: { id: brand.id },
data: {
miniAppSettings: {
...settingsWithoutAppSecret,
appId: "wxmissingsessionsecret",
},
},
});
const wechatAuth = app.get(WechatAuthService);
const originalExchangeCode = wechatAuth.exchangeCode;
let exchanged = false;
wechatAuth.exchangeCode = async () => {
exchanged = true;
return { openid: `must-not-login-${Date.now()}` };
};
try {
const response = await request("/customer/session/wechat?brandSlug=default-brand", {
method: "POST",
payload: { code: "test-code" },
});
assert.equal(response.statusCode, 503);
assert.equal(JSON.parse(response.body).error.message, "该品牌尚未配置微信登录 AppSecret");
assert.equal(exchanged, false);
} finally {
wechatAuth.exchangeCode = originalExchangeCode;
await prisma.brandProfile.update({
where: { id: brand.id },
data: { miniAppSettings: originalSettings },
});
}
});