-
Notifications
You must be signed in to change notification settings - Fork 47
feat(javascript-sdk): allow for json response outcome for webauthn #537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@forgerock/javascript-sdk': minor | ||
| --- | ||
|
|
||
| Add feature to provide JSON outcome response to callback if requested |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -104,20 +104,28 @@ abstract class FRWebAuthn { | |
| const { hiddenCallback, metadataCallback, textOutputCallback } = this.getCallbacks(step); | ||
| if (hiddenCallback && (metadataCallback || textOutputCallback)) { | ||
| let outcome: ReturnType<typeof this.getAuthenticationOutcome>; | ||
| let credential: PublicKeyCredential | null = null; | ||
|
|
||
| try { | ||
| let publicKey: PublicKeyCredentialRequestOptions; | ||
| if (metadataCallback) { | ||
| const meta = metadataCallback.getOutputValue('data') as WebAuthnAuthenticationMetadata; | ||
| publicKey = this.createAuthenticationPublicKey(meta); | ||
|
|
||
| credential = await this.getAuthenticationCredential( | ||
| publicKey as PublicKeyCredentialRequestOptions, | ||
| ); | ||
| outcome = this.getAuthenticationOutcome(credential); | ||
| } else if (textOutputCallback) { | ||
| publicKey = parseWebAuthnAuthenticateText(textOutputCallback.getMessage()); | ||
|
|
||
| credential = await this.getAuthenticationCredential( | ||
| publicKey as PublicKeyCredentialRequestOptions, | ||
| ); | ||
| outcome = this.getAuthenticationOutcome(credential); | ||
| } else { | ||
| throw new Error('No Credential found from Public Key'); | ||
| } | ||
| // TypeScript doesn't like `publicKey` being assigned in conditionals above | ||
| // eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
| // @ts-ignore | ||
| const credential = await this.getAuthenticationCredential(publicKey); | ||
| outcome = this.getAuthenticationOutcome(credential); | ||
| } catch (error) { | ||
| if (!(error instanceof Error)) throw error; | ||
| // NotSupportedError is a special case | ||
|
|
@@ -129,6 +137,18 @@ abstract class FRWebAuthn { | |
| throw error; | ||
| } | ||
|
|
||
| if (metadataCallback) { | ||
| const meta = metadataCallback.getOutputValue('data') as WebAuthnAuthenticationMetadata; | ||
| if (meta?.supportsJsonResponse && credential && 'authenticatorAttachment' in credential) { | ||
| hiddenCallback.setInputValue( | ||
| JSON.stringify({ | ||
| authenticatorAttachment: credential.authenticatorAttachment, | ||
| legacyData: outcome, | ||
| }), | ||
| ); | ||
| return step; | ||
| } | ||
| } | ||
| hiddenCallback.setInputValue(outcome); | ||
| return step; | ||
| } else { | ||
|
|
@@ -154,19 +174,20 @@ abstract class FRWebAuthn { | |
| const { hiddenCallback, metadataCallback, textOutputCallback } = this.getCallbacks(step); | ||
| if (hiddenCallback && (metadataCallback || textOutputCallback)) { | ||
| let outcome: OutcomeWithName<string, AttestationType, PublicKeyCredential>; | ||
| let credential: PublicKeyCredential | null = null; | ||
|
|
||
| try { | ||
| let publicKey: PublicKeyCredentialRequestOptions; | ||
| if (metadataCallback) { | ||
| const meta = metadataCallback.getOutputValue('data') as WebAuthnRegistrationMetadata; | ||
| publicKey = this.createRegistrationPublicKey(meta); | ||
| const credential = await this.getRegistrationCredential( | ||
| credential = await this.getRegistrationCredential( | ||
| publicKey as PublicKeyCredentialCreationOptions, | ||
| ); | ||
| outcome = this.getRegistrationOutcome(credential); | ||
| } else if (textOutputCallback) { | ||
| publicKey = parseWebAuthnRegisterText(textOutputCallback.getMessage()); | ||
| const credential = await this.getRegistrationCredential( | ||
| credential = await this.getRegistrationCredential( | ||
| publicKey as PublicKeyCredentialCreationOptions, | ||
| ); | ||
| outcome = this.getRegistrationOutcome(credential); | ||
|
|
@@ -183,6 +204,21 @@ abstract class FRWebAuthn { | |
| hiddenCallback.setInputValue(`${WebAuthnOutcome.Error}::${error.name}:${error.message}`); | ||
| throw error; | ||
| } | ||
|
|
||
| if (metadataCallback) { | ||
| const meta = metadataCallback.getOutputValue('data') as WebAuthnAuthenticationMetadata; | ||
| if (meta?.supportsJsonResponse && credential && 'authenticatorAttachment' in credential) { | ||
| hiddenCallback.setInputValue( | ||
| JSON.stringify({ | ||
| authenticatorAttachment: credential.authenticatorAttachment, | ||
| legacyData: | ||
| deviceName && deviceName.length > 0 ? `${outcome}::${deviceName}` : outcome, | ||
| }), | ||
| ); | ||
| return step; | ||
| } | ||
| } | ||
|
|
||
| hiddenCallback.setInputValue( | ||
| deviceName && deviceName.length > 0 ? `${outcome}::${deviceName}` : outcome, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that you will be missing the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, good catch. Will update now. |
||
| ); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One minor I would suggest keys like
authenticatorAttachmentanddatato be used from Constants files as they are re-used across files.