Refactor random MXID and password generator

Change-Id: Ifd8afde0a294adba2d28ca4f620e298aac9a1fa6
This commit is contained in:
Manuel Stahl 2024-04-26 09:03:52 +02:00
parent 39dd6617de
commit 72f5ab937e
3 changed files with 33 additions and 72 deletions

View file

@ -17,7 +17,7 @@ import {
NativeSelect, NativeSelect,
} from "@mui/material"; } from "@mui/material";
import { DataProvider, useTranslate } from "ra-core"; import { DataProvider, useTranslate } from "ra-core";
import { generateRandomUser } from "./users"; import { generateRandomMxId, generateRandomPassword } from "../synapse/synapse";
const LOGGING = true; const LOGGING = true;
@ -270,38 +270,16 @@ const FilePicker = () => {
try { try {
setProgress({ done: entriesDone, limit: entriesCount }); setProgress({ done: entriesDone, limit: entriesCount });
for (const entry of data) { for (const entry of data) {
let userRecord = {}; const userRecord = { ...entry };
let overwriteData = {};
// No need to do a bunch of cryptographic random number getting if // No need to do a bunch of cryptographic random number getting if
// we are using neither a generated password nor a generated user id. // we are using neither a generated password nor a generated user id.
if ( if (useridMode === "ignore" || userRecord.id === undefined) {
useridMode === "ignore" || userRecord.id = generateRandomMxId();
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 (passwordMode === false || entry.password === undefined) {
userRecord.password = generateRandomPassword();
} }
/* TODO record update stats (especially admin no -> yes, deactivated x -> !x, ... */ /* 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. /* 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 * If the mode is "stop", we will not continue adding more records, and
@ -346,9 +324,8 @@ const FilePicker = () => {
}) })
); );
} else { } else {
const overwriteData = generateRandomUser();
const newRecordData = Object.assign({}, recordData, { const newRecordData = Object.assign({}, recordData, {
id: overwriteData.id, id: generateRandomMxId(),
}); });
retries++; retries++;
if (retries > 512) { if (retries > 512) {

View file

@ -154,47 +154,6 @@ const validateUser = [
const validateAddress = [required(), maxLength(255)]; 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 UserEditActions = () => {
const record = useRecordContext(); const record = useRecordContext();
const translate = useTranslate(); const translate = useTranslate();

View file

@ -58,3 +58,28 @@ export const getMediaUrl = media_id => {
const baseUrl = localStorage.getItem("base_url"); const baseUrl = localStorage.getItem("base_url");
return `${baseUrl}/_matrix/media/v1/download/${media_id}?allow_redirect=true`; 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("");
}