UNPKG

@okta/okta-auth-js

Version:
79 lines (75 loc) 3.69 kB
/*! * Copyright (c) 2015-present, Okta, Inc. and/or its affiliates. All rights reserved. * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.") * * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * * See the License for the specific language governing permissions and limitations under the License. */ import { base64UrlToBuffer, bufferToBase64Url } from '../crypto/base64.js'; const getEnrolledCredentials = (authenticatorEnrollments = []) => { const credentials = []; authenticatorEnrollments.forEach((enrollement) => { var _a, _b; if (enrollement.key === 'webauthn') { const credential = { type: 'public-key', id: base64UrlToBuffer(enrollement.credentialId), }; const transports = (_a = enrollement.transports) !== null && _a !== void 0 ? _a : (_b = enrollement.profile) === null || _b === void 0 ? void 0 : _b.transports; if (Array.isArray(transports)) { credential.transports = transports; } credentials.push(credential); } }); return credentials; }; const buildCredentialCreationOptions = (activationData, authenticatorEnrollments) => { return { publicKey: Object.assign({ rp: activationData.rp, user: { id: base64UrlToBuffer(activationData.user.id), name: activationData.user.name, displayName: activationData.user.displayName }, challenge: base64UrlToBuffer(activationData.challenge), pubKeyCredParams: activationData.pubKeyCredParams, attestation: activationData.attestation, authenticatorSelection: activationData.authenticatorSelection, excludeCredentials: getEnrolledCredentials(authenticatorEnrollments) }, (activationData.hints && { hints: activationData.hints })) }; }; const buildCredentialRequestOptions = (challengeData, authenticatorEnrollments) => { return { publicKey: Object.assign(Object.assign({ challenge: base64UrlToBuffer(challengeData.challenge), userVerification: challengeData.userVerification, allowCredentials: getEnrolledCredentials(authenticatorEnrollments) }, (challengeData.rpId && { rpId: challengeData.rpId })), (challengeData.hints && { hints: challengeData.hints })) }; }; const getAttestation = (credential) => { const response = credential.response; const id = credential.id; const clientData = bufferToBase64Url(response.clientDataJSON); const attestation = bufferToBase64Url(response.attestationObject); const getTransportsFn = response.getTransports; const result = { id, clientData, attestation, }; if (typeof getTransportsFn === 'function') { result.transports = JSON.stringify(getTransportsFn.call(response)); } return result; }; const getAssertion = (credential) => { const response = credential.response; const id = credential.id; const clientData = bufferToBase64Url(response.clientDataJSON); const authenticatorData = bufferToBase64Url(response.authenticatorData); const signatureData = bufferToBase64Url(response.signature); return { id, clientData, authenticatorData, signatureData }; }; export { buildCredentialCreationOptions, buildCredentialRequestOptions, getAssertion, getAttestation }; //# sourceMappingURL=webauthn.js.map