Skip to content

Commit

Permalink
Use try/catch
Browse files Browse the repository at this point in the history
  • Loading branch information
varjolintu committed Aug 22, 2023
1 parent 72bed1f commit 6f3b1cb
Showing 1 changed file with 81 additions and 73 deletions.
154 changes: 81 additions & 73 deletions keepassxc-browser/content/passkeys-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,97 +38,105 @@ kpxcPasskeysUtils.sendPasskeysResponse = function(publicKey) {

// Create a new object with base64 strings for KeePassXC
kpxcPasskeysUtils.buildCredentialCreationOptions = function(pkOptions) {
checkErrors(pkOptions);
try {
checkErrors(pkOptions);

if (pkOptions.user.id && (pkOptions.user.id.length < 1 || pkOptions.user.id.length > 64)) {
throw new TypeError('user.id does not match the required length.');
}
if (pkOptions.user.id && (pkOptions.user.id.length < 1 || pkOptions.user.id.length > 64)) {
throw new TypeError('user.id does not match the required length.');
}

if (!pkOptions.rp.id) {
pkOptions.rp.id = window.location.host;
pkOptions.rp.name = window.location.host;
} else if (!window.location.host.endsWith(pkOptions.rp.id)) {
throw new DOMException('Site domain differs from RP ID', DOMException.SecurityError);
}
if (!pkOptions.rp.id) {
pkOptions.rp.id = window.location.host;
pkOptions.rp.name = window.location.host;
} else if (!window.location.host.endsWith(pkOptions.rp.id)) {
throw new DOMException('Site domain differs from RP ID', DOMException.SecurityError);
}

if (!pkOptions.pubKeyCredParams || pkOptions.pubKeyCredParams.length === 0) {
pkOptions.pubKeyCredParams.push({
'type': 'public-key',
'alg': -7
});
pkOptions.pubKeyCredParams.push({
'type': 'public-key',
'alg': -257
});
}
if (!pkOptions.pubKeyCredParams || pkOptions.pubKeyCredParams.length === 0) {
pkOptions.pubKeyCredParams.push({
'type': 'public-key',
'alg': -7
});
pkOptions.pubKeyCredParams.push({
'type': 'public-key',
'alg': -257
});
}

const publicKey = {};
publicKey.attestation = pkOptions.attestation;
publicKey.authenticatorSelection = pkOptions.authenticatorSelection;
publicKey.challenge = arrayBufferToBase64(pkOptions.challenge);
publicKey.extensions = pkOptions.extensions;
publicKey.pubKeyCredParams = pkOptions.pubKeyCredParams;
publicKey.rp = pkOptions.rp;
publicKey.timeout = pkOptions.timeout;

publicKey.excludeCredentials = [];
if (pkOptions.excludeCredentials && pkOptions.excludeCredentials.length > 0) {
for (const cred of pkOptions.excludeCredentials) {
const arr = {
id: arrayBufferToBase64(cred.id),
transports: cred.transports,
type: cred.type
};

publicKey.excludeCredentials.push(arr);
const publicKey = {};
publicKey.attestation = pkOptions.attestation;
publicKey.authenticatorSelection = pkOptions.authenticatorSelection;
publicKey.challenge = arrayBufferToBase64(pkOptions.challenge);
publicKey.extensions = pkOptions.extensions;
publicKey.pubKeyCredParams = pkOptions.pubKeyCredParams;
publicKey.rp = pkOptions.rp;
publicKey.timeout = pkOptions.timeout;

publicKey.excludeCredentials = [];
if (pkOptions.excludeCredentials && pkOptions.excludeCredentials.length > 0) {
for (const cred of pkOptions.excludeCredentials) {
const arr = {
id: arrayBufferToBase64(cred.id),
transports: cred.transports,
type: cred.type
};

publicKey.excludeCredentials.push(arr);
}
}
}

publicKey.user = {};
publicKey.user.displayName = pkOptions.user.displayName;
publicKey.user.id = arrayBufferToBase64(pkOptions.user.id);
publicKey.user.name = pkOptions.user.name;
publicKey.user = {};
publicKey.user.displayName = pkOptions.user.displayName;
publicKey.user.id = arrayBufferToBase64(pkOptions.user.id);
publicKey.user.name = pkOptions.user.name;

return publicKey;
return publicKey;
} catch (e) {
console.log(e);
}
};

// Create a new object with base64 strings for KeePassXC
kpxcPasskeysUtils.buildCredentialRequestOptions = function(pkOptions) {
checkErrors(pkOptions);
try {
checkErrors(pkOptions);

if (!pkOptions.rpId) {
pkOptions.rpId = window.location.host;
} else if (!window.location.host.endsWith(pkOptions.rpId)) {
throw new DOMException('Site domain differs from RP ID', DOMException.SecurityError);
}
if (!pkOptions.rpId) {
pkOptions.rpId = window.location.host;
} else if (!window.location.host.endsWith(pkOptions.rpId)) {
throw new DOMException('Site domain differs from RP ID', DOMException.SecurityError);
}

const publicKey = {};
publicKey.challenge = arrayBufferToBase64(pkOptions.challenge);
publicKey.rpId = pkOptions.rpId;
publicKey.timeout = pkOptions.timeout;
publicKey.userVerification = pkOptions.userVerification;

publicKey.allowCredentials = [];
if (pkOptions.allowCredentials && pkOptions.allowCredentials.length > 0) {
for (const cred of pkOptions.allowCredentials) {
const transports = [];
if (cred.transports) {
for (const tp of cred.transports) {
transports.push(tp);
const publicKey = {};
publicKey.challenge = arrayBufferToBase64(pkOptions.challenge);
publicKey.rpId = pkOptions.rpId;
publicKey.timeout = pkOptions.timeout;
publicKey.userVerification = pkOptions.userVerification;

publicKey.allowCredentials = [];
if (pkOptions.allowCredentials && pkOptions.allowCredentials.length > 0) {
for (const cred of pkOptions.allowCredentials) {
const transports = [];
if (cred.transports) {
for (const tp of cred.transports) {
transports.push(tp);
}
}
}

const arr = {
id: arrayBufferToBase64(cred.id),
transports: transports,
type: cred.type
};
const arr = {
id: arrayBufferToBase64(cred.id),
transports: transports,
type: cred.type
};

publicKey.allowCredentials.push(arr);
publicKey.allowCredentials.push(arr);
}
}
}

return publicKey;
return publicKey;
} catch (e) {
console.log(e);
}
};

// Parse register response back from base64 strings to ByteArrays
Expand Down

0 comments on commit 6f3b1cb

Please sign in to comment.