Jobley / website / test / authenticator.mjs
| 1 | // A software passkey for the tests: an ES256 key that answers WebAuthn registration and |
| 2 | // authentication options the way Apple's platform authenticator does (origin https://<rpID>, |
| 3 | // "none" attestation, user present and verified, counter 0). |
| 4 | import { createHash, generateKeyPairSync, randomBytes, sign } from 'node:crypto'; |
| 5 | import { isoBase64URL, isoCBOR } from '@simplewebauthn/server/helpers'; |
| 6 | |
| 7 | const b64 = (bytes) => isoBase64URL.fromBuffer(new Uint8Array(bytes)); |
| 8 | const sha = (bytes) => createHash('sha256').update(bytes).digest(); |
| 9 | const UP = 0x01, UV = 0x04, BE = 0x08, BS = 0x10, AT = 0x40; |
| 10 | |
| 11 | export class Authenticator { |
| 12 | constructor({ rpID = 'jobley.ai', origin = `https://${rpID}`, verify = true } = {}) { |
| 13 | this.rpID = rpID; this.origin = origin; this.verify = verify; |
| 14 | this.credentialId = randomBytes(16); |
| 15 | const { privateKey, publicKey } = generateKeyPairSync('ec', { namedCurve: 'P-256' }); |
| 16 | this.privateKey = privateKey; |
| 17 | const jwk = publicKey.export({ format: 'jwk' }); |
| 18 | this.cose = isoCBOR.encode(new Map([[1, 2], [3, -7], [-1, 1], [-2, isoBase64URL.toBuffer(jwk.x)], [-3, isoBase64URL.toBuffer(jwk.y)]])); |
| 19 | } |
| 20 | |
| 21 | get id() { return b64(this.credentialId); } |
| 22 | |
| 23 | flags(extra = 0) { return UP | (this.verify ? UV : 0) | BE | BS | extra; } |
| 24 | |
| 25 | clientData(type, challenge) { return Buffer.from(JSON.stringify({ type, challenge, origin: this.origin, crossOrigin: false })); } |
| 26 | |
| 27 | register(options) { |
| 28 | this.userHandle = options.user.id; |
| 29 | const clientDataJSON = this.clientData('webauthn.create', options.challenge); |
| 30 | const idLength = Buffer.alloc(2); idLength.writeUInt16BE(this.credentialId.length); |
| 31 | const authData = Buffer.concat([sha(this.rpID), Buffer.from([this.flags(AT)]), Buffer.alloc(4), Buffer.alloc(16), idLength, this.credentialId, Buffer.from(this.cose)]); |
| 32 | const attestationObject = isoCBOR.encode(new Map([['fmt', 'none'], ['attStmt', new Map()], ['authData', new Uint8Array(authData)]])); |
| 33 | return { |
| 34 | id: this.id, rawId: this.id, type: 'public-key', clientExtensionResults: {}, authenticatorAttachment: 'platform', |
| 35 | response: { clientDataJSON: b64(clientDataJSON), attestationObject: b64(attestationObject), transports: ['internal', 'hybrid'] }, |
| 36 | }; |
| 37 | } |
| 38 | |
| 39 | login(options) { |
| 40 | const clientDataJSON = this.clientData('webauthn.get', options.challenge); |
| 41 | const authenticatorData = Buffer.concat([sha(this.rpID), Buffer.from([this.flags()]), Buffer.alloc(4)]); |
| 42 | const signature = sign('sha256', Buffer.concat([authenticatorData, sha(clientDataJSON)]), this.privateKey); |
| 43 | return { |
| 44 | id: this.id, rawId: this.id, type: 'public-key', clientExtensionResults: {}, authenticatorAttachment: 'platform', |
| 45 | response: { clientDataJSON: b64(clientDataJSON), authenticatorData: b64(authenticatorData), signature: b64(signature), userHandle: this.userHandle }, |
| 46 | }; |
| 47 | } |
| 48 | } |