Files
edgeever/apps/api/src/share-password.ts
T
tianma-if dcfd11468d feat(sharing): add optional password protection for public note shares
Allow owners to turn on an auto-generated access password when sharing a note. Visitors must unlock the share page before reading the note or its attachments; existing public links stay open until a password is enabled.
2026-09-17 14:44:43 +08:00

18 lines
830 B
TypeScript

export const SHARE_PASSWORD_ALPHABET = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789";
export const SHARE_PASSWORD_LENGTH = 8;
/** Unguessable enough for a second factor on an already-secret link, short enough to copy or read aloud. */
export const generateSharePassword = (): string => {
const result: string[] = [];
while (result.length < SHARE_PASSWORD_LENGTH) {
const bytes = crypto.getRandomValues(new Uint8Array(SHARE_PASSWORD_LENGTH * 2));
const maxUnbiased = Math.floor(256 / SHARE_PASSWORD_ALPHABET.length) * SHARE_PASSWORD_ALPHABET.length;
for (const byte of bytes) {
if (byte >= maxUnbiased) continue;
result.push(SHARE_PASSWORD_ALPHABET[byte % SHARE_PASSWORD_ALPHABET.length]!);
if (result.length === SHARE_PASSWORD_LENGTH) break;
}
}
return result.join("");
};