mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
Member Settings gets Passkeys: add a passkey on this device under a name, see the account's passkeys and remove one. The sign-in page gets "Sign in with a passkey": the browser offers this WeKan's passkeys, no user name needed. Verification is @simplewebauthn/server 14.0.3 (MIT, pinned) against ROOT_URL's host and origin, so a passkey cannot sign in to another site or through another address; each sign-in signs a fresh challenge that is used once and expires in five minutes, and the signature counter must move forward, so a copied passkey is refused. A disabled account cannot sign in with one (the existing validateLoginAttempt hooks apply). Passkeys are a top-level user field, never published and not writable from the client; up to 20 per account. Not offered on Sandstorm. Member Settings also gets Log out everywhere: after a confirmation every login token of the account goes, so Meteor ends every session - other browsers, phones, the desktop app - and this one. Nothing else changes. models/lib/passkeys.js holds the relying party, base64url and the browser conversions; client/lib/passkeys.js uses navigator.credentials directly. tests/passkeys.test.cjs signs assertions with a software ES256 authenticator: a real signature verifies, and another key, another origin, another challenge or a counter that went back are refused; it also checks single-use challenges, the unpublished field and the pin. server/lib/tests/passkeys.tests.js runs in Meteor (2 passing): the module bundles, login options name no account, and registration needs a user. Documented in docs/Features/Login/Passkeys.md. The new strings are English placeholders until the next translation pass. Thanks to xet7 ! Fixes #5847.
94 lines
3.9 KiB
JavaScript
94 lines
3.9 KiB
JavaScript
'use strict';
|
|
|
|
// #5847: passkey (WebAuthn) login. Shared by the server (server/passkeys.js),
|
|
// the browser (client/lib/passkeys.js) and tests/passkeys.test.cjs - pure, no
|
|
// Meteor, and the same in Node and the browser.
|
|
//
|
|
// The relying party is WeKan's own address: its id is ROOT_URL's host name and
|
|
// the only origin a response may come from is ROOT_URL's origin, so a passkey
|
|
// made for one WeKan cannot sign in to another, nor through any other address.
|
|
|
|
function relyingParty(rootUrl) {
|
|
let u;
|
|
try { u = new URL(String(rootUrl || '')); } catch (e) { return null; }
|
|
if (u.protocol !== 'https:' && !(u.protocol === 'http:' && ['localhost', '127.0.0.1'].includes(u.hostname))) return null;
|
|
return { rpID: u.hostname, origin: u.origin };
|
|
}
|
|
|
|
// base64url <-> bytes, without Buffer, so the browser can use it too.
|
|
const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
|
|
function bytesToBase64url(input) {
|
|
const bytes = input instanceof Uint8Array ? input : new Uint8Array(input);
|
|
let out = '';
|
|
for (let i = 0; i < bytes.length; i += 3) {
|
|
const n = (bytes[i] << 16) | ((bytes[i + 1] || 0) << 8) | (bytes[i + 2] || 0);
|
|
out += ALPHABET[(n >> 18) & 63] + ALPHABET[(n >> 12) & 63];
|
|
if (i + 1 < bytes.length) out += ALPHABET[(n >> 6) & 63];
|
|
if (i + 2 < bytes.length) out += ALPHABET[n & 63];
|
|
}
|
|
return out;
|
|
}
|
|
function base64urlToBytes(text) {
|
|
const s = String(text || '');
|
|
if (!/^[A-Za-z0-9_-]*$/.test(s) || s.length % 4 === 1) throw new Error('invalid base64url');
|
|
const out = new Uint8Array(Math.floor((s.length * 3) / 4));
|
|
let o = 0;
|
|
for (let i = 0; i < s.length; i += 4) {
|
|
const n = (ALPHABET.indexOf(s[i]) << 18) | (ALPHABET.indexOf(s[i + 1]) << 12)
|
|
| ((i + 2 < s.length ? ALPHABET.indexOf(s[i + 2]) : 0) << 6) | (i + 3 < s.length ? ALPHABET.indexOf(s[i + 3]) : 0);
|
|
out[o++] = (n >> 16) & 255;
|
|
if (i + 2 < s.length) out[o++] = (n >> 8) & 255;
|
|
if (i + 3 < s.length) out[o++] = n & 255;
|
|
}
|
|
return out.slice(0, o);
|
|
}
|
|
|
|
// The server's JSON options as the browser's navigator.credentials wants them.
|
|
function creationOptionsForBrowser(json) {
|
|
return {
|
|
...json,
|
|
challenge: base64urlToBytes(json.challenge),
|
|
user: { ...json.user, id: base64urlToBytes(json.user.id) },
|
|
excludeCredentials: (json.excludeCredentials || []).map(c => ({ ...c, id: base64urlToBytes(c.id) })),
|
|
};
|
|
}
|
|
function requestOptionsForBrowser(json) {
|
|
return {
|
|
...json,
|
|
challenge: base64urlToBytes(json.challenge),
|
|
allowCredentials: (json.allowCredentials || []).map(c => ({ ...c, id: base64urlToBytes(c.id) })),
|
|
};
|
|
}
|
|
|
|
// A PublicKeyCredential as the JSON the server verifies.
|
|
function credentialToJSON(credential) {
|
|
const r = credential.response;
|
|
const response = { clientDataJSON: bytesToBase64url(r.clientDataJSON) };
|
|
if (r.attestationObject) {
|
|
response.attestationObject = bytesToBase64url(r.attestationObject);
|
|
if (typeof r.getTransports === 'function') response.transports = r.getTransports();
|
|
}
|
|
if (r.authenticatorData) response.authenticatorData = bytesToBase64url(r.authenticatorData);
|
|
if (r.signature) response.signature = bytesToBase64url(r.signature);
|
|
if (r.userHandle) response.userHandle = bytesToBase64url(r.userHandle);
|
|
return {
|
|
id: credential.id,
|
|
rawId: bytesToBase64url(credential.rawId),
|
|
type: credential.type,
|
|
response,
|
|
clientExtensionResults: typeof credential.getClientExtensionResults === 'function' ? credential.getClientExtensionResults() : {},
|
|
authenticatorAttachment: credential.authenticatorAttachment || undefined,
|
|
};
|
|
}
|
|
|
|
// A passkey's name as the member gave it, or a default.
|
|
function passkeyName(name) {
|
|
const n = typeof name === 'string' ? name.replace(/[\u0000-\u001f\u007f]/g, '').trim().slice(0, 60) : '';
|
|
return n || 'Passkey';
|
|
}
|
|
|
|
const MAX_PASSKEYS = 20;
|
|
|
|
module.exports = { relyingParty, bytesToBase64url, base64urlToBytes, creationOptionsForBrowser,
|
|
requestOptionsForBrowser, credentialToJSON, passkeyName, MAX_PASSKEYS };
|