-
-
Couldn't load subscription status.
- Fork 202
toJSON implementation for createPublicKeyCredential #2613
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 |
|---|---|---|
|
|
@@ -26,6 +26,68 @@ | |
| return kpxcStringToArrayBuffer(window.atob(str?.replaceAll('-', '+').replaceAll('_', '/'))); | ||
| }; | ||
|
|
||
| const kpxcArrayBufferToBase64Url = function (buff) { | ||
| const str = new Uint8Array(buff).reduce((acc, x) => (acc += String.fromCharCode(x)), ""); | ||
| return window.btoa(str).replace(/\+/g, "-").replace(/\//g, "_").replace(/=*$/g, ""); | ||
| }; | ||
|
|
||
| const kpxcAuthenticatorAttestationResponseJson = function(response) { | ||
| const authData = response.getAuthenticatorData(); | ||
| const responsePk = response.getPublicKey(); | ||
|
|
||
| let publicKey; | ||
| if (responsePk === null) { | ||
| publicKey = null; | ||
| } else { | ||
| publicKey = kpxcArrayBufferToBase64Url(responsePk); | ||
| } | ||
|
|
||
| return { | ||
| clientDataJSON: kpxcArrayBufferToBase64Url(response.clientDataJSON), | ||
| authenticatorData: kpxcArrayBufferToBase64Url(authData), | ||
| transports: response.getTransports(), | ||
| publicKey, | ||
| publicKeyAlgorithm: response.getPublicKeyAlgorithm(), | ||
| attestationObject: kpxcArrayBufferToBase64Url(response.attestationObject), | ||
| }; | ||
| } | ||
|
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. Add missing |
||
|
|
||
| const kpxcAuthenticatorAssertionResponseJson = function(response) { | ||
| const userHandle = response.userHandle | ||
| ? kpxcArrayBufferToBase64Url(response.userHandle) | ||
| : undefined; | ||
| return { | ||
| clientDataJSON: kpxcArrayBufferToBase64Url(response.clientDataJSON), | ||
| authenticatorData: kpxcArrayBufferToBase64Url(response.authenticatorData), | ||
| signature: kpxcArrayBufferToBase64Url(response.signature), | ||
| userHandle, | ||
| }; | ||
| } | ||
|
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. Add missing |
||
|
|
||
| const kpxcPublicKeyCredentialJson = function (credential) { | ||
| const clientExtensionResults = credential.getClientExtensionResults(); | ||
| const type = credential.type; | ||
| const authenticatorAttachment = credential.authenticatorAttachment; | ||
| let response; | ||
|
|
||
| if (credential.response instanceof AuthenticatorAttestationResponse) { | ||
| response = kpxcAuthenticatorAttestationResponseJson(credential.response); | ||
| } | ||
|
|
||
| if (credential.response instanceof AuthenticatorAssertionResponse) { | ||
| response = kpxcAuthenticatorAssertionResponseJson(credential.response); | ||
| } | ||
|
|
||
| return { | ||
| id: kpxcArrayBufferToBase64Url(credential.rawId), | ||
| rawId: kpxcArrayBufferToBase64Url(credential.rawId), | ||
| response, | ||
| authenticatorAttachment, | ||
| clientExtensionResults, | ||
| type, | ||
| }; | ||
| } | ||
|
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. Add missing |
||
|
|
||
| // Wraps response to AuthenticatorAttestationResponse object | ||
| const createAttestationResponse = function(publicKey) { | ||
| const response = { | ||
|
|
@@ -63,7 +125,8 @@ | |
| response: authenticatorResponse, | ||
| type: publicKey.type, | ||
| clientExtensionResults: () => publicKey?.response?.clientExtensionResults || {}, | ||
| getClientExtensionResults: () => publicKey?.response?.clientExtensionResults || {} | ||
| getClientExtensionResults: () => publicKey?.response?.clientExtensionResults || {}, | ||
| toJSON: () => kpxcPublicKeyCredentialJson(publicKeyCredential) | ||
| }; | ||
|
|
||
| return Object.setPrototypeOf(publicKeyCredential, PublicKeyCredential.prototype); | ||
|
|
||
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.
Copy this function directly from
passkey-utils.js.