完善虎牙短信登录与图形安全验证
This commit is contained in:
@@ -6,7 +6,11 @@ import {
|
||||
devMockConnectorBaseUrl,
|
||||
} from '../dev-mock/connector-endpoint.js'
|
||||
import { isDevMockEnabled } from '../dev-mock/dev-mock-enabled.js'
|
||||
import { exportMockSessionCredential } from '../dev-mock/mock-semi-auto-connector.js'
|
||||
import {
|
||||
exportMockSessionCredential,
|
||||
loginMockHuyaSms,
|
||||
sendMockHuyaSmsCode,
|
||||
} from '../dev-mock/mock-semi-auto-connector.js'
|
||||
import {
|
||||
HUYA_NATIVE_TICKET_PREFIX,
|
||||
huyaNativeConnector,
|
||||
@@ -15,7 +19,14 @@ import {
|
||||
|
||||
export type ConnectorLoginSession = {
|
||||
sessionId: string
|
||||
status: 'waiting_scan' | 'scanned' | 'authenticated' | 'expired' | 'failed'
|
||||
status:
|
||||
| 'verification_pending'
|
||||
| 'waiting_code'
|
||||
| 'waiting_scan'
|
||||
| 'scanned'
|
||||
| 'authenticated'
|
||||
| 'expired'
|
||||
| 'failed'
|
||||
qrContent: string
|
||||
qrImage?: string
|
||||
expiresAt: string
|
||||
@@ -25,6 +36,53 @@ export type ConnectorLoginSession = {
|
||||
message: string
|
||||
}
|
||||
|
||||
type HuyaSmsChallengeBase = {
|
||||
image: string
|
||||
width: number
|
||||
height: number
|
||||
}
|
||||
|
||||
export type HuyaSmsChallenge =
|
||||
| (HuyaSmsChallengeBase & {
|
||||
type: 'slider'
|
||||
puzzleImage: string
|
||||
puzzleWidth: number
|
||||
puzzleY: number
|
||||
})
|
||||
| (HuyaSmsChallengeBase & {
|
||||
type: 'rotate'
|
||||
puzzleImage: string
|
||||
puzzleWidth: number
|
||||
puzzleHeight: number
|
||||
puzzleX: number
|
||||
puzzleY: number
|
||||
})
|
||||
| (HuyaSmsChallengeBase & {
|
||||
type: 'click'
|
||||
tipImage: string
|
||||
question: string
|
||||
wordSize: number
|
||||
})
|
||||
|
||||
export type HuyaChallengeSolution = {
|
||||
distance?: number
|
||||
angle?: number
|
||||
points?: Array<{ x: number; y: number }>
|
||||
}
|
||||
|
||||
export type HuyaSmsChallengeInput = HuyaChallengeSolution & {
|
||||
sessionId: string
|
||||
}
|
||||
|
||||
export type HuyaSmsSendResult = {
|
||||
session: ConnectorLoginSession
|
||||
challenge: HuyaSmsChallenge | null
|
||||
}
|
||||
|
||||
/*
|
||||
* Connector 的登录会话由运行期实现返回,具体挑战结构见上方联合类型。
|
||||
*/
|
||||
|
||||
export type ConnectorBindingSession = {
|
||||
sessionId: string
|
||||
status:
|
||||
@@ -220,6 +278,108 @@ export async function getHuyaLoginSession(
|
||||
)
|
||||
}
|
||||
|
||||
export async function sendHuyaSmsCode(
|
||||
input: {
|
||||
taskId: number
|
||||
orderNo: string
|
||||
mobile: string
|
||||
},
|
||||
mode: SemiAutoConnectorMode = defaultSemiAutoConnectorMode(),
|
||||
): Promise<HuyaSmsSendResult> {
|
||||
if (mode === 'embedded') return huyaNativeConnector.sendSmsCode(input)
|
||||
if (mode === 'mock') {
|
||||
return {
|
||||
session: sendMockHuyaSmsCode({
|
||||
taskId: input.taskId,
|
||||
orderNo: input.orderNo,
|
||||
mobile: input.mobile,
|
||||
}),
|
||||
challenge: null,
|
||||
}
|
||||
}
|
||||
const payload = await callConnector<HuyaSmsSendResult | ConnectorLoginSession>(
|
||||
'/api/internal/huya-semi-auto/sms/send',
|
||||
{
|
||||
method: 'POST',
|
||||
body: {
|
||||
task_id: input.taskId,
|
||||
order_no: input.orderNo,
|
||||
mobile: input.mobile,
|
||||
},
|
||||
},
|
||||
mode,
|
||||
)
|
||||
return 'session' in payload ? payload : { session: payload, challenge: null }
|
||||
}
|
||||
|
||||
export async function verifyHuyaSmsChallenge(
|
||||
input: HuyaSmsChallengeInput,
|
||||
mode: SemiAutoConnectorMode = defaultSemiAutoConnectorMode(),
|
||||
): Promise<HuyaSmsSendResult> {
|
||||
if (mode === 'embedded') {
|
||||
return huyaNativeConnector.verifySmsChallenge(input.sessionId, input)
|
||||
}
|
||||
if (mode === 'mock') {
|
||||
throw createHttpError('Mock Connector 不使用虎牙安全验证', {
|
||||
statusCode: 409,
|
||||
errorCode: 'mock_huya_sms_challenge_unsupported',
|
||||
})
|
||||
}
|
||||
return callConnector<HuyaSmsSendResult>(
|
||||
'/api/internal/huya-semi-auto/sms/verify',
|
||||
{
|
||||
method: 'POST',
|
||||
body: {
|
||||
session_id: input.sessionId,
|
||||
distance: input.distance,
|
||||
angle: input.angle,
|
||||
points: input.points,
|
||||
},
|
||||
},
|
||||
mode,
|
||||
)
|
||||
}
|
||||
|
||||
export async function loginHuyaSms(
|
||||
input: {
|
||||
sessionId: string
|
||||
taskId: number
|
||||
orderNo: string
|
||||
mobile: string
|
||||
code: string
|
||||
},
|
||||
mode: SemiAutoConnectorMode = defaultSemiAutoConnectorMode(),
|
||||
): Promise<ConnectorLoginSession> {
|
||||
if (mode === 'embedded') {
|
||||
return huyaNativeConnector.loginWithSms({
|
||||
sessionId: input.sessionId,
|
||||
mobile: input.mobile,
|
||||
code: input.code,
|
||||
})
|
||||
}
|
||||
if (mode === 'mock') {
|
||||
return loginMockHuyaSms({
|
||||
sessionId: input.sessionId,
|
||||
mobile: input.mobile,
|
||||
code: input.code,
|
||||
})
|
||||
}
|
||||
return callConnector<ConnectorLoginSession>(
|
||||
'/api/internal/huya-semi-auto/sms/login',
|
||||
{
|
||||
method: 'POST',
|
||||
body: {
|
||||
session_id: input.sessionId,
|
||||
task_id: input.taskId,
|
||||
order_no: input.orderNo,
|
||||
mobile: input.mobile,
|
||||
code: input.code,
|
||||
},
|
||||
},
|
||||
mode,
|
||||
)
|
||||
}
|
||||
|
||||
export async function startHuyaBinding(
|
||||
sessionId: string,
|
||||
mode: SemiAutoConnectorMode = defaultSemiAutoConnectorMode(),
|
||||
|
||||
Reference in New Issue
Block a user