133 lines
4.0 KiB
JavaScript
133 lines
4.0 KiB
JavaScript
"use strict";
|
|
|
|
const assert = require("assert");
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
|
|
const toDataModuleUrl = (source) =>
|
|
`data:text/javascript;base64,${Buffer.from(source).toString("base64")}`;
|
|
|
|
const run = async () => {
|
|
const source = fs.readFileSync(
|
|
path.join(__dirname, "../utils/auth-verification.js"),
|
|
"utf8",
|
|
);
|
|
const {
|
|
AUTH_TAC_SCENE,
|
|
PASSWORD_TAC_BLOCKED_MESSAGE,
|
|
isAuthPhone,
|
|
assertSmsCode,
|
|
normalizeCaptchaRequirement,
|
|
createTacRenderContext,
|
|
normalizeTacSuccess,
|
|
} = await import(toDataModuleUrl(source));
|
|
|
|
assert.deepStrictEqual(
|
|
{ ...AUTH_TAC_SCENE },
|
|
{
|
|
SMS_LOGIN: "APP_SMS_LOGIN",
|
|
REGISTER: "APP_REGISTER",
|
|
FORGOT_PASSWORD: "APP_FORGOT_PASSWORD",
|
|
},
|
|
"认证 TAC 场景必须由受保护 OpenAPI 的唯一枚举拥有",
|
|
);
|
|
assert.match(PASSWORD_TAC_BLOCKED_MESSAGE, /服务端|安全验证|验证码登录/);
|
|
|
|
assert.strictEqual(isAuthPhone("13800138000"), true);
|
|
for (const invalid of ["", "12800138000", "1380013800", "138001380000", 13800138000, null]) {
|
|
assert.strictEqual(isAuthPhone(invalid), false, `非法认证手机号被放行:${invalid}`);
|
|
}
|
|
|
|
assert.strictEqual(assertSmsCode("1234"), "1234");
|
|
for (const invalid of ["", "123", "12345", "12a4", 1234, null]) {
|
|
assert.throws(() => assertSmsCode(invalid), /4 位短信验证码/);
|
|
}
|
|
|
|
const requirement = normalizeCaptchaRequirement(
|
|
{
|
|
required: true,
|
|
providerCode: "TIANAI",
|
|
captchaType: "SLIDER",
|
|
sceneCode: AUTH_TAC_SCENE.REGISTER,
|
|
ttlSeconds: 300,
|
|
},
|
|
AUTH_TAC_SCENE.REGISTER,
|
|
);
|
|
assert.deepStrictEqual(requirement, {
|
|
required: true,
|
|
providerCode: "TIANAI",
|
|
captchaType: "SLIDER",
|
|
sceneCode: AUTH_TAC_SCENE.REGISTER,
|
|
ttlSeconds: 300,
|
|
});
|
|
assert.throws(
|
|
() => normalizeCaptchaRequirement({ ...requirement, required: false }, AUTH_TAC_SCENE.REGISTER),
|
|
/未要求行为验证|票据/,
|
|
);
|
|
assert.throws(
|
|
() => normalizeCaptchaRequirement({ ...requirement, sceneCode: AUTH_TAC_SCENE.SMS_LOGIN }, AUTH_TAC_SCENE.REGISTER),
|
|
/场景/,
|
|
);
|
|
assert.throws(
|
|
() => normalizeCaptchaRequirement({ ...requirement, providerCode: "OTHER" }, AUTH_TAC_SCENE.REGISTER),
|
|
/TIANAI/,
|
|
);
|
|
assert.throws(
|
|
() => normalizeCaptchaRequirement({ ...requirement, captchaType: "math" }, AUTH_TAC_SCENE.REGISTER),
|
|
/验证码类型/,
|
|
);
|
|
|
|
const context = createTacRenderContext({
|
|
requestId: "register-1",
|
|
baseUrl: "https://backend-api.ddxcjp.cn/",
|
|
clientId: "client-1",
|
|
tenantId: "000000",
|
|
sceneCode: AUTH_TAC_SCENE.REGISTER,
|
|
subject: "13800138000",
|
|
requirement,
|
|
});
|
|
assert.deepStrictEqual(context, {
|
|
requestId: "register-1",
|
|
baseUrl: "https://backend-api.ddxcjp.cn",
|
|
challengeUrl: "https://backend-api.ddxcjp.cn/captcha/challenge",
|
|
verifyUrl: "https://backend-api.ddxcjp.cn/captcha/verify",
|
|
clientId: "client-1",
|
|
tenantId: "000000",
|
|
sceneCode: AUTH_TAC_SCENE.REGISTER,
|
|
subject: "13800138000",
|
|
providerCode: "TIANAI",
|
|
captchaType: "SLIDER",
|
|
});
|
|
assert.throws(
|
|
() => createTacRenderContext({ ...context, baseUrl: "http://backend-api.ddxcjp.cn", requirement }),
|
|
/HTTPS/,
|
|
);
|
|
assert.throws(
|
|
() => createTacRenderContext({ ...context, subject: "1380013800", requirement }),
|
|
/手机号/,
|
|
);
|
|
|
|
assert.deepStrictEqual(
|
|
normalizeTacSuccess(
|
|
{ requestId: "register-1", validToken: "ticket-1", expireSeconds: 300 },
|
|
"register-1",
|
|
),
|
|
{ requestId: "register-1", validToken: "ticket-1", expireSeconds: 300 },
|
|
);
|
|
assert.throws(
|
|
() => normalizeTacSuccess({ requestId: "stale", validToken: "ticket-1" }, "register-1"),
|
|
/已过期|不匹配/,
|
|
);
|
|
assert.throws(
|
|
() => normalizeTacSuccess({ requestId: "register-1", validToken: "" }, "register-1"),
|
|
/票据/,
|
|
);
|
|
|
|
process.stdout.write("AUTH-VERIFICATION-RUNTIME-SMOKE PASS\n");
|
|
};
|
|
|
|
run().catch((error) => {
|
|
process.stderr.write(`${error.stack || error.message}\n`);
|
|
process.exit(1);
|
|
});
|