@@ -146,14 +146,20 @@ export class PasskeyService {
146146 throw new BadRequestException ( 'Verification failed' )
147147
148148 const {
149- credential : { id, publicKey }
149+ credential : { id : credentialID , publicKey : credentialPublicKey }
150150 } = verification . registrationInfo
151151
152+ const credentialIdBuf = this . toBuffer ( credentialID as any )
153+ const publicKeyBuf = this . toBuffer ( credentialPublicKey as any )
154+
155+ const credentialIdB64 = base64url . encode ( credentialIdBuf )
156+ const publicKeyB64 = base64url . encode ( publicKeyBuf )
157+
152158 await this . prismaService . passkey . create ( {
153159 data : {
154160 deviceName,
155- credentialId : id ,
156- publicKey : publicKey . toString ( ) ,
161+ credentialId : credentialIdB64 ,
162+ publicKey : publicKeyB64 ,
157163 userAgent,
158164 ip,
159165 lastUsedAt : new Date ( ) ,
@@ -244,7 +250,9 @@ export class PasskeyService {
244250 expectedRPID : this . WEBAUTHN_RP_ID ,
245251 credential : {
246252 id : passkey . credentialId ,
247- publicKey : base64url . toBuffer ( passkey . publicKey ) ,
253+ publicKey : this . toUint8Array (
254+ base64url . toBuffer ( passkey . publicKey )
255+ ) ,
248256 counter : passkey . counter ?? 0
249257 }
250258 } )
@@ -325,4 +333,20 @@ export class PasskeyService {
325333
326334 return recoveryCodes
327335 }
336+
337+ private toBuffer ( data : ArrayBuffer | Uint8Array | string ) : Buffer {
338+ if ( typeof data === 'string' ) return Buffer . from ( data )
339+
340+ if ( data instanceof ArrayBuffer )
341+ return Buffer . from ( new Uint8Array ( data ) )
342+ if ( data instanceof Uint8Array ) return Buffer . from ( data )
343+
344+ return Buffer . from ( data as any )
345+ }
346+
347+ private toUint8Array ( data : string | Buffer ) : Uint8Array < ArrayBuffer > {
348+ const buf = typeof data === 'string' ? base64url . toBuffer ( data ) : data
349+
350+ return new Uint8Array ( new ArrayBuffer ( buf . length ) ) . map ( ( _ , i ) => buf [ i ] )
351+ }
328352}
0 commit comments