完善微博半自动凭证与本地开发环境

This commit is contained in:
yml
2026-11-13 22:52:51 +08:00
parent 90173e3bd7
commit 654c087c1b
32 changed files with 1701 additions and 223 deletions
@@ -31,6 +31,15 @@ function createFetchMock(
}
function defaultHandler(url: string): Response {
if (url.includes('/sso/signin')) {
return new Response('<html></html>', {
status: 200,
headers: {
'content-type': 'text/html',
'set-cookie': 'X-CSRF-TOKEN=CK-demo; Path=/sso',
},
})
}
if (url.includes('/sso/v2/web/config')) {
return jsonResponse({
retcode: 20000000,
@@ -62,15 +71,26 @@ function defaultHandler(url: string): Response {
return jsonResponse({ retcode: 20000000 })
}
if (url.includes('/sso/v2/login')) {
return jsonResponse({
retcode: 20000000,
data: {
data: {
act: 'goto',
location: 'https://passport.weibo.com/sso/v2/goto?ticket=demo',
},
},
const headers = new Headers({
'content-type': 'application/json',
})
headers.append('set-cookie', 'SUB=demo-sub; Path=/; Domain=.weibo.com')
headers.append('set-cookie', 'PASSPORT_ONLY=demo-passport; Path=/sso')
return new Response(
JSON.stringify({
retcode: 20000000,
data: {
data: {
act: 'goto',
location: 'https://passport.weibo.com/sso/v2/goto?ticket=demo',
},
},
}),
{
status: 200,
headers,
},
)
}
if (url.includes('/sso/v2/goto')) {
return new Response(null, {
@@ -122,7 +142,7 @@ test('微博二维码未扫码时保持等待扫码状态', async () => {
test('微博发送短信验证码使用大陆 11 位手机号', async () => {
const { fetchImpl, calls } = createFetchMock(defaultHandler)
const connector = new WeiboNativeConnector({ fetchImpl })
await connector.sendSmsCode('18900762182')
await connector.sendSmsCode({ taskId: 1, mobile: '18900762182' })
const call = calls.find((item) => item.url.includes('/sso/v2/sms/send'))
assert.ok(call)
@@ -136,7 +156,11 @@ test('微博发送短信验证码使用大陆 11 位手机号', async () => {
test('微博短信发送支持其他国家码并按 00 前缀编码', async () => {
const { fetchImpl, calls } = createFetchMock(defaultHandler)
const connector = new WeiboNativeConnector({ fetchImpl })
await connector.sendSmsCode('61234567', '852')
await connector.sendSmsCode({
taskId: 1,
mobile: '61234567',
countryCode: '852',
})
const call = calls.find((item) => item.url.includes('/sso/v2/sms/send'))
assert.ok(call)
@@ -173,7 +197,7 @@ test('微博短信登录会带上发码响应下发的 Cookie', async () => {
return defaultHandler(url)
})
const connector = new WeiboNativeConnector({ fetchImpl })
await connector.sendSmsCode('13800138000')
await connector.sendSmsCode({ taskId: 1, mobile: '13800138000' })
const { session } = await connector.loginWithSms({
taskId: 1,
orderNo: 'MOCKWB1',
@@ -191,6 +215,7 @@ test('微博短信登录会带上发码响应下发的 Cookie', async () => {
test('微博短信登录建立会话并可生成一次性注入票据', async () => {
const { fetchImpl, calls } = createFetchMock(defaultHandler)
const connector = new WeiboNativeConnector({ fetchImpl })
await connector.sendSmsCode({ taskId: 1, mobile: '138 0013 8000' })
const { sessionId, session } = await connector.loginWithSms({
taskId: 1,
orderNo: 'MOCKWB1',
@@ -222,3 +247,83 @@ test('微博短信登录建立会话并可生成一次性注入票据', async ()
await assert.rejects(() => connector.consumeBrowserTicket(ticket.ticket), /票据不存在或已过期/)
})
test('微博短信发送遇到 mfa_1 时返回易盾挑战,完成挑战后可用 mfa_id 重发', async () => {
let sendCount = 0
const { fetchImpl, calls } = createFetchMock((url, init) => {
if (url.includes('/sso/v2/sms/send')) {
sendCount += 1
const form = new URLSearchParams(String(init.body))
if (!form.get('mfa_id')) {
return jsonResponse({
retcode: 0,
msg: 'need mfa',
data: { act: 'mfa_1', mfa_id: 'mfa-demo' },
})
}
return jsonResponse({ retcode: 20000000, msg: '', data: [] })
}
return defaultHandler(url)
})
const connector = new WeiboNativeConnector({ fetchImpl })
const first = await connector.sendSmsCode({ taskId: 1, mobile: '13800138000' })
assert.equal(first.mfaRequired, true)
assert.equal(first.mfaId, 'mfa-demo')
assert.equal(first.captchaId, '7cda0ba2785647ada4d6e946ddb2d465')
const second = await connector.sendSmsCode({
taskId: 1,
mobile: '13800138000',
mfaId: 'mfa-demo',
})
assert.equal(second.mfaRequired, false)
assert.equal(sendCount, 2)
const secondForm = new URLSearchParams(
String(calls.filter((call) => call.url.includes('/sso/v2/sms/send'))[1]?.init.body),
)
assert.equal(secondForm.get('mfa_id'), 'mfa-demo')
})
test('微博 CK 可按账号级凭证导出并从序列化 Cookie 恢复会话', async () => {
const { fetchImpl } = createFetchMock(defaultHandler)
const connector = new WeiboNativeConnector({ fetchImpl })
await connector.sendSmsCode({ taskId: 1, mobile: '13800138000' })
const { sessionId } = await connector.loginWithSms({
taskId: 1,
orderNo: 'MOCKWB1',
mobile: '13800138000',
code: '123456',
})
const credential = connector.exportSessionCredential(sessionId)
assert.equal(credential.platformUid, '123456')
assert.ok(credential.cookies.some((cookie) => cookie.name === 'SUB'))
assert.ok(
credential.cookies.some(
(cookie) =>
cookie.name === 'PASSPORT_ONLY' &&
cookie.domain === 'passport.weibo.com' &&
cookie.path === '/sso',
),
)
const ticket = await connector.createBrowserTicket({
sessionId,
targetUrl: 'https://weibo.com/',
})
const injection = await connector.consumeBrowserTicket(ticket.ticket)
assert.equal(
injection.cookies.some((cookie) => cookie.name === 'PASSPORT_ONLY'),
false,
)
const restored = await connector.restoreSessionFromCredential({
taskId: 2,
orderNo: 'MOCKWB2',
platformUid: credential.platformUid,
nickname: credential.nickname,
cookies: credential.cookies,
})
assert.equal(restored.status, 'authenticated')
assert.equal(restored.hasCredential, true)
})