Skip to content
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

Fix bugs with WebAuthn preventing sign in and registration. #22651

Merged
merged 10 commits into from
Feb 1, 2023
11 changes: 0 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"swagger-ui-dist": "4.15.5",
"tippy.js": "6.3.7",
"tributejs": "5.1.3",
"uint8-to-base64": "0.2.0",
"vue": "3.2.45",
"vue-bar-graph": "2.0.0",
"vue-loader": "17.0.1",
Expand Down
52 changes: 35 additions & 17 deletions web_src/js/features/user-auth-webauthn.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import $ from 'jquery';
import {encode, decode} from 'uint8-to-base64';

const {appSubUrl, csrfToken} = window.config;

function encodeToBase64(toEncode /* Uint8Array */) {
const output = [];
for (let i = 0; i < toEncode.length; i++) {
output.push(String.fromCharCode(toEncode[i]));
}
return window.btoa(output.join(''));
}

function decodeFromBase64(toDecode) {
return Uint8Array.from(window.atob(toDecode), (c) => c.charCodeAt(0));
}

export function initUserAuthWebAuthn() {
if ($('.user.signin.webauthn-prompt').length === 0) {
return;
Expand All @@ -14,9 +25,9 @@ export function initUserAuthWebAuthn() {

$.getJSON(`${appSubUrl}/user/webauthn/assertion`, {})
.done((makeAssertionOptions) => {
makeAssertionOptions.publicKey.challenge = decode(makeAssertionOptions.publicKey.challenge);
makeAssertionOptions.publicKey.challenge = decodeURLEncodedBase64(makeAssertionOptions.publicKey.challenge);
for (let i = 0; i < makeAssertionOptions.publicKey.allowCredentials.length; i++) {
makeAssertionOptions.publicKey.allowCredentials[i].id = decode(makeAssertionOptions.publicKey.allowCredentials[i].id);
makeAssertionOptions.publicKey.allowCredentials[i].id = decodeURLEncodedBase64(makeAssertionOptions.publicKey.allowCredentials[i].id);
}
navigator.credentials.get({
publicKey: makeAssertionOptions.publicKey
Expand Down Expand Up @@ -56,14 +67,14 @@ function verifyAssertion(assertedCredential) {
type: 'POST',
data: JSON.stringify({
id: assertedCredential.id,
rawId: bufferEncode(rawId),
rawId: bufferURLEncodedBase64(rawId),
type: assertedCredential.type,
clientExtensionResults: assertedCredential.getClientExtensionResults(),
response: {
authenticatorData: bufferEncode(authData),
clientDataJSON: bufferEncode(clientDataJSON),
signature: bufferEncode(sig),
userHandle: bufferEncode(userHandle),
authenticatorData: bufferURLEncodedBase64(authData),
clientDataJSON: bufferURLEncodedBase64(clientDataJSON),
signature: bufferURLEncodedBase64(sig),
userHandle: bufferURLEncodedBase64(userHandle),
},
}),
contentType: 'application/json; charset=utf-8',
Expand All @@ -85,14 +96,21 @@ function verifyAssertion(assertedCredential) {
});
}

// Encode an ArrayBuffer into a base64 string.
function bufferEncode(value) {
return encode(value)
// Encode an ArrayBuffer into a URLEncoded base64 string.
function bufferURLEncodedBase64(value) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it could be encodeURLEncodedBase64 like decodeURLEncodedBase64

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return encodeToBase64(value)
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=/g, '');
}

// Dccode a URLEncoded base64 to an ArrayBuffer string.
function decodeURLEncodedBase64(value) {
return decodeFromBase64(value
.replace(/_/g, '/')
.replace(/-/g, '+'));
}

function webauthnRegistered(newCredential) {
const attestationObject = new Uint8Array(newCredential.response.attestationObject);
const clientDataJSON = new Uint8Array(newCredential.response.clientDataJSON);
Expand All @@ -104,11 +122,11 @@ function webauthnRegistered(newCredential) {
headers: {'X-Csrf-Token': csrfToken},
data: JSON.stringify({
id: newCredential.id,
rawId: bufferEncode(rawId),
rawId: bufferURLEncodedBase64(rawId),
type: newCredential.type,
response: {
attestationObject: bufferEncode(attestationObject),
clientDataJSON: bufferEncode(clientDataJSON),
attestationObject: bufferURLEncodedBase64(attestationObject),
clientDataJSON: bufferURLEncodedBase64(clientDataJSON),
},
}),
dataType: 'json',
Expand Down Expand Up @@ -184,11 +202,11 @@ function webAuthnRegisterRequest() {
}).done((makeCredentialOptions) => {
$('#nickname').closest('div.field').removeClass('error');

makeCredentialOptions.publicKey.challenge = decode(makeCredentialOptions.publicKey.challenge);
makeCredentialOptions.publicKey.user.id = decode(makeCredentialOptions.publicKey.user.id);
makeCredentialOptions.publicKey.challenge = decodeURLEncodedBase64(makeCredentialOptions.publicKey.challenge);
makeCredentialOptions.publicKey.user.id = decodeURLEncodedBase64(makeCredentialOptions.publicKey.user.id);
if (makeCredentialOptions.publicKey.excludeCredentials) {
for (let i = 0; i < makeCredentialOptions.publicKey.excludeCredentials.length; i++) {
makeCredentialOptions.publicKey.excludeCredentials[i].id = decode(makeCredentialOptions.publicKey.excludeCredentials[i].id);
makeCredentialOptions.publicKey.excludeCredentials[i].id = decodeURLEncodedBase64(makeCredentialOptions.publicKey.excludeCredentials[i].id);
}
}

Expand Down