mirror of
https://github.com/UA-Fediland/synapse-admin.git
synced 2024-11-21 22:11:27 +00:00
Refactor random MXID and password generator
Change-Id: Ifd8afde0a294adba2d28ca4f620e298aac9a1fa6
This commit is contained in:
parent
39dd6617de
commit
72f5ab937e
3 changed files with 33 additions and 72 deletions
|
@ -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) {
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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("");
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue