diff --git a/src/components/ImportFeature.tsx b/src/components/ImportFeature.tsx index d4b97cc..3f9f2da 100644 --- a/src/components/ImportFeature.tsx +++ b/src/components/ImportFeature.tsx @@ -17,7 +17,7 @@ import { NativeSelect, } from "@mui/material"; import { DataProvider, useTranslate } from "ra-core"; -import { generateRandomUser } from "./users"; +import { generateRandomMxId, generateRandomPassword } from "../synapse/synapse"; const LOGGING = true; @@ -270,38 +270,16 @@ const FilePicker = () => { try { setProgress({ done: entriesDone, limit: entriesCount }); for (const entry of data) { - let userRecord = {}; - let overwriteData = {}; + const userRecord = { ...entry }; // No need to do a bunch of cryptographic random number getting if // we are using neither a generated password nor a generated user id. - if ( - useridMode === "ignore" || - entry.id === undefined || - entry.password === undefined || - passwordMode === false - ) { - overwriteData = generateRandomUser(); - // Ignoring IDs or the entry lacking an ID means we keep the - // ID field in the overwrite data. - if (!(useridMode === "ignore" || entry.id === undefined)) { - delete overwriteData.id; - } - - // Not using passwords from the csv or this entry lacking a password - // means we keep the password field in the overwrite data. - if ( - !( - passwordMode === false || - entry.password === undefined || - entry.password === "" - ) - ) { - delete overwriteData.password; - } + if (useridMode === "ignore" || userRecord.id === undefined) { + userRecord.id = generateRandomMxId(); + } + if (passwordMode === false || entry.password === undefined) { + userRecord.password = generateRandomPassword(); } /* TODO record update stats (especially admin no -> yes, deactivated x -> !x, ... */ - Object.assign(userRecord, entry); - Object.assign(userRecord, overwriteData); /* For these modes we will consider the ID that's in the record. * If the mode is "stop", we will not continue adding more records, and @@ -346,9 +324,8 @@ const FilePicker = () => { }) ); } else { - const overwriteData = generateRandomUser(); const newRecordData = Object.assign({}, recordData, { - id: overwriteData.id, + id: generateRandomMxId(), }); retries++; if (retries > 512) { diff --git a/src/components/users.tsx b/src/components/users.tsx index 8fb7a9a..97b9cf1 100644 --- a/src/components/users.tsx +++ b/src/components/users.tsx @@ -154,47 +154,6 @@ const validateUser = [ const validateAddress = [required(), maxLength(255)]; -export function generateRandomUser() { - const homeserver = localStorage.getItem("home_server"); - const user_id = - "@" + - Array(8) - .fill("0123456789abcdefghijklmnopqrstuvwxyz") - .map( - x => - x[ - Math.floor( - (crypto.getRandomValues(new Uint32Array(1))[0] / - (0xffffffff + 1)) * - x.length - ) - ] - ) - .join("") + - ":" + - homeserver; - - const password = Array(20) - .fill( - "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz~!@-#$" - ) - .map( - x => - x[ - Math.floor( - (crypto.getRandomValues(new Uint32Array(1))[0] / (0xffffffff + 1)) * - x.length - ) - ] - ) - .join(""); - - return { - id: user_id, - password: password, - }; -} - const UserEditActions = () => { const record = useRecordContext(); const translate = useTranslate(); diff --git a/src/synapse/synapse.ts b/src/synapse/synapse.ts index 915144c..20ff132 100644 --- a/src/synapse/synapse.ts +++ b/src/synapse/synapse.ts @@ -58,3 +58,28 @@ export const getMediaUrl = media_id => { const baseUrl = localStorage.getItem("base_url"); return `${baseUrl}/_matrix/media/v1/download/${media_id}?allow_redirect=true`; }; + +/** + * Generate a random MXID for current homeserver + * @returns full MXID as string + */ +export function generateRandomMxId(): string { + const homeserver = localStorage.getItem("home_server"); + const characters = "0123456789abcdefghijklmnopqrstuvwxyz"; + const localpart = Array.from(crypto.getRandomValues(new Uint32Array(8))) + .map(x => characters[x % characters.length]) + .join(""); + return `@${localpart}:${homeserver}`; +} + +/** + * Generate a random user password + * @returns a new random password as string + */ +export function generateRandomPassword(length = 20): string { + const characters = + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz~!@-#$"; + return Array.from(crypto.getRandomValues(new Uint32Array(length))) + .map(x => characters[x % characters.length]) + .join(""); +}