chore: passkey perf

This commit is contained in:
xiaojunnuo
2026-03-17 19:16:11 +08:00
parent 6d43623f45
commit 0ddcb9c00a
2 changed files with 29 additions and 19 deletions
@@ -278,20 +278,22 @@ async function doRegisterPasskey(deviceName: string) {
// type: "public-key", // type: "public-key",
// })); // }));
const credential = await (navigator.credentials as any).create({ const publicKey = {
publicKey: { challenge: Uint8Array.from(atob(options.challenge.replace(/-/g, "+").replace(/_/g, "/")), c => c.charCodeAt(0)),
challenge: Uint8Array.from(atob(options.challenge.replace(/-/g, "+").replace(/_/g, "/")), c => c.charCodeAt(0)), rp: options.rp,
rp: options.rp, pubKeyCredParams: options.pubKeyCredParams,
pubKeyCredParams: options.pubKeyCredParams, timeout: options.timeout || 60000,
timeout: options.timeout || 60000, attestation: options.attestation,
attestation: options.attestation, // excludeCredentials: excludeCredentials,
// excludeCredentials: excludeCredentials, user: {
user: { id: new TextEncoder().encode(options.userId + ""),
id: new TextEncoder().encode(options.userId + ""), name: userInfo.value.username,
name: userInfo.value.username, displayName: deviceName,
displayName: deviceName,
},
}, },
};
console.log("passkey register publicKey:", publicKey);
const credential = await (navigator.credentials as any).create({
publicKey,
}); });
if (!credential) { if (!credential) {
@@ -1,4 +1,4 @@
import { cache } from "@certd/basic"; import { cache, logger } from "@certd/basic";
import { AuthException, BaseService, SysInstallInfo, SysSettingsService, SysSiteInfo } from "@certd/lib-server"; import { AuthException, BaseService, SysInstallInfo, SysSettingsService, SysSiteInfo } from "@certd/lib-server";
import { isComm } from "@certd/plus-core"; import { isComm } from "@certd/plus-core";
import { Inject, Provide, Scope, ScopeEnum } from "@midwayjs/core"; import { Inject, Provide, Scope, ScopeEnum } from "@midwayjs/core";
@@ -54,14 +54,14 @@ export class PasskeyService extends BaseService<PasskeyEntity> {
const options = await generateRegistrationOptions({ const options = await generateRegistrationOptions({
rpName: rpName, rpName: rpName,
rpID: rpId, rpID: rpId,
userID: new Uint8Array([userId]), userID: new TextEncoder().encode(userId + ""),
userName: username, userName: username,
userDisplayName: user.nickName || username, userDisplayName: user.nickName || username,
timeout: 60000, timeout: 60000,
attestationType: "none", attestationType: "none",
excludeCredentials: [], excludeCredentials: [],
}); });
logger.info('[passkey] 注册选项:', JSON.stringify(options));
cache.set(`passkey:registration:${options.challenge}`, userId, { cache.set(`passkey:registration:${options.challenge}`, userId, {
ttl: 5 * 60 * 1000, ttl: 5 * 60 * 1000,
}); });
@@ -86,16 +86,24 @@ export class PasskeyService extends BaseService<PasskeyEntity> {
const { rpId, origin } = await this.getRpInfo(); const { rpId, origin } = await this.getRpInfo();
const verification = await verifyRegistrationResponse({ let verification: any = null;
const verifyReq = {
response, response,
expectedChallenge: challenge, expectedChallenge: challenge,
expectedOrigin: origin, expectedOrigin: origin,
expectedRPID: rpId, expectedRPID: rpId,
}); };
try {
verification = await verifyRegistrationResponse(verifyReq);
} catch (error) {
// 后端验证时
logger.error('[passkey] 注册验证失败:', JSON.stringify(verifyReq));
throw new AuthException(`注册验证失败:${error.message || error}`);
}
if (!verification.verified) { if (!verification.verified) {
throw new AuthException("注册验证失败"); throw new AuthException("注册验证失败");
} }
cache.delete(`passkey:registration:${challenge}`); cache.delete(`passkey:registration:${challenge}`);