Skip to content

Commit

Permalink
Add cancel feature to web authenticator (#876)
Browse files Browse the repository at this point in the history
  • Loading branch information
musidlo authored Nov 20, 2024
1 parent 7c039b9 commit 396a3ae
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
25 changes: 25 additions & 0 deletions packages/webauthn-authenticator/src/web-authenticator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@ function generateAssertionObject(

export class Authenticator {
public readonly credentials: Record<string, WebauthnCredential> = {}
private cancelNext = false

private throwNotAllowedError() {
throw {
name: 'NotAllowedError',
message: 'The operation either timed out or was not allowed.',
constructor: { name: 'DOMException' },
}
}

/**
* Creates a new public key credential to be used for WebAuthn attestations and assertions.
Expand Down Expand Up @@ -180,6 +189,12 @@ export class Authenticator {
credentialOptions: CredentialCreationOptionsSerialized
): Promise<PublicKeyCredentialAttestationSerialized> {
log('createPublicKeyCredential', credentialOptions)

if (this.cancelNext) {
this.cancelNext = false
this.throwNotAllowedError()
}

const credOptsPubKey = credentialOptions.publicKey
if (
!credOptsPubKey ||
Expand Down Expand Up @@ -240,6 +255,12 @@ export class Authenticator {
*/
async getPublicKeyCredential(credentialRequestOptions: CredentialRequestOptionsSerialized) {
log('getPublicKeyCredential', credentialRequestOptions)

if (this.cancelNext) {
this.cancelNext = false
this.throwNotAllowedError()
}

const credReqOptsPubKey = credentialRequestOptions.publicKey
const clientDataJSON = {
type: 'webauthn.get',
Expand Down Expand Up @@ -286,4 +307,8 @@ export class Authenticator {

return credentialResponse
}

cancelNextOperation() {
this.cancelNext = true
}
}
34 changes: 34 additions & 0 deletions packages/webauthn-authenticator/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,23 @@ M2r/eobZPWzLAuuKhc4rKm6jQJtExXSvmg==

verifyAssertion({ cred: cred2, testBytes, keyPair })
})

it('should throw exception after calling cancelNextOperation', async () => {
const { Authenticator } = await import('../src')

const authenticator = new Authenticator()
authenticator.cancelNextOperation()

await expect(
authenticator.createPublicKeyCredential({} as CredentialCreationOptionsSerialized)
).rejects.toThrowError('The operation either timed out or was not allowed.')

authenticator.cancelNextOperation()

await expect(
authenticator.getPublicKeyCredential({} as CredentialRequestOptionsSerialized)
).rejects.toThrowError('The operation either timed out or was not allowed.')
})
})

describe('without mock', () => {
Expand Down Expand Up @@ -196,6 +213,23 @@ M2r/eobZPWzLAuuKhc4rKm6jQJtExXSvmg==

verifyAssertion({ cred: cred2, testBytes, keyPair })
})

it('should throw exception after calling cancelNextOperation', async () => {
const { Authenticator } = await import('../src')

const authenticator = new Authenticator()
authenticator.cancelNextOperation()

await expect(
authenticator.createPublicKeyCredential({} as CredentialCreationOptionsSerialized)
).rejects.toThrowError('The operation either timed out or was not allowed.')

authenticator.cancelNextOperation()

await expect(
authenticator.getPublicKeyCredential({} as CredentialRequestOptionsSerialized)
).rejects.toThrowError('The operation either timed out or was not allowed.')
})
})
})
})
Expand Down

0 comments on commit 396a3ae

Please sign in to comment.