108 lines
2.6 KiB
JavaScript
108 lines
2.6 KiB
JavaScript
import { createConnection } from 'mysql2/promise';
|
|
import tt from 'thirty-two';
|
|
import fs from 'fs/promises';
|
|
|
|
async function init() {
|
|
const dump = await createConnection({
|
|
host: 'localhost',
|
|
user: 'icynet',
|
|
database: 'icynet',
|
|
port: 13306,
|
|
password: 'icynet',
|
|
});
|
|
|
|
const userQuery = await dump.execute('SELECT * FROM users');
|
|
let users = userQuery[0];
|
|
|
|
const oauthQuery = await dump.execute('SELECT * FROM oauth2_client');
|
|
const clients = oauthQuery[0].map((entry) => ({
|
|
...entry,
|
|
client_id: entry.id,
|
|
client_secret: entry.secret,
|
|
secret: undefined,
|
|
icon: undefined,
|
|
activated: true,
|
|
verified: Boolean(entry.verified),
|
|
ownerId: entry.user_id,
|
|
user_id: undefined,
|
|
urls: [
|
|
entry.url
|
|
? {
|
|
url: entry.url,
|
|
type: 'website',
|
|
created_at: entry.created_at,
|
|
}
|
|
: null,
|
|
entry.redirect_url
|
|
? {
|
|
url: entry.redirect_url,
|
|
type: 'redirect_uri',
|
|
created_at: entry.created_at,
|
|
}
|
|
: null,
|
|
].filter((item) => item),
|
|
url: undefined,
|
|
redirect_url: undefined,
|
|
}));
|
|
|
|
const totpQuery = await dump.execute('SELECT * FROM totp_token');
|
|
const totp = totpQuery[0].map((item) => ({
|
|
token: tt.encode(item.token).toString(),
|
|
created_at: item.created_at,
|
|
type: 'totp',
|
|
userId: item.user_id,
|
|
}));
|
|
|
|
const uploads = users
|
|
.filter((user) => user.avatar_file)
|
|
.map((item) => ({
|
|
original_name: 'blob',
|
|
file: item.avatar_file,
|
|
mimetype: item.avatar_file.endsWith('.png') ? 'image/png' : 'image/jpeg',
|
|
created_at: item.created_at,
|
|
updated_at: item.updated_at,
|
|
uploaderId: item.id,
|
|
}));
|
|
|
|
const auditLogs = [
|
|
...users.map((entry) => ({
|
|
action: 'registration',
|
|
actorId: entry.id,
|
|
actor_ip: entry.ip_address,
|
|
created_at: entry.created_at,
|
|
})),
|
|
...totp.map((entry) => ({
|
|
action: 'totp_activate',
|
|
actorId: entry.userId,
|
|
created_at: entry.created_at,
|
|
})),
|
|
];
|
|
|
|
users = users.map((entry) => ({
|
|
...entry,
|
|
locked: undefined,
|
|
nw_privilege: undefined,
|
|
ip_address: undefined,
|
|
avatar_file: undefined,
|
|
password: !entry.password && entry.activated ? 'external' : entry.password,
|
|
activated: Boolean(entry.activated),
|
|
picture: uploads.find((item) => item.file === entry.avatar_file),
|
|
}));
|
|
|
|
await fs.writeFile(
|
|
'output.json',
|
|
JSON.stringify(
|
|
{
|
|
users,
|
|
clients,
|
|
totp,
|
|
auditLogs,
|
|
},
|
|
undefined,
|
|
2,
|
|
),
|
|
);
|
|
}
|
|
|
|
init().catch(console.error).then(process.exit);
|