brainz-social-old-3/scripts/create_user.ts

159 lines
No EOL
7.9 KiB
TypeScript

import { pubPrivKeys, user } from '@/schema';
import db from '../db/drizzle';
import { start as promptStart, get as promptGet } from 'prompt';
import * as bcrypt from 'bcrypt';
import crypto from 'crypto';
const main = async () => {
// const rl = readline.createInterface({input: process.stdin, output: process.stdout});
// const user = await rl.question("Username: ");
promptStart();
promptGet({
properties: {
Username: {
required: true
},
Domain: {
required: true
},
Password: {
hidden: true
}
}
}, (err, result) => {
if (!err) {
bcrypt.hash(result.Password.toString(), 10, (err, hashedValue) => {
// @ts-ignore
crypto.generateKeyPair('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem'
}
}, (err: Error | null, publicKey: string, privateKey: string) => {
db.insert(user).values({
name: result.Username.toString(),
pwHash: hashedValue,
// TODO: Reconsider the default @context
activity: JSON.stringify({
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",
{
"manuallyApprovesFollowers": "as:manuallyApprovesFollowers",
"toot": "http://joinmastodon.org/ns#",
"featured": {
"@id": "toot:featured",
"@type": "@id"
},
"featuredTags": {
"@id": "toot:featuredTags",
"@type": "@id"
},
"alsoKnownAs": {
"@id": "as:alsoKnownAs",
"@type": "@id"
},
"movedTo": {
"@id": "as:movedTo",
"@type": "@id"
},
"schema": "http://schema.org#",
"PropertyValue": "schema:PropertyValue",
"value": "schema:value",
"discoverable": "toot:discoverable",
"Device": "toot:Device",
"Ed25519Signature": "toot:Ed25519Signature",
"Ed25519Key": "toot:Ed25519Key",
"Curve25519Key": "toot:Curve25519Key",
"EncryptedMessage": "toot:EncryptedMessage",
"publicKeyBase64": "toot:publicKeyBase64",
"deviceId": "toot:deviceId",
"claim": {
"@type": "@id",
"@id": "toot:claim"
},
"fingerprintKey": {
"@type": "@id",
"@id": "toot:fingerprintKey"
},
"identityKey": {
"@type": "@id",
"@id": "toot:identityKey"
},
"devices": {
"@type": "@id",
"@id": "toot:devices"
},
"messageFranking": "toot:messageFranking",
"messageType": "toot:messageType",
"cipherText": "toot:cipherText",
"suspended": "toot:suspended",
"memorial": "toot:memorial",
"indexable": "toot:indexable",
"focalPoint": {
"@container": "@list",
"@id": "toot:focalPoint"
}
}
],
"id": `https://${result.Domain}/users/${result.Username}`,
"type": "Person",
"following": `https://${result.Domain}/users/${result.Username}/following`,
"followers": `https://${result.Domain}/users/${result.Username}/followers`,
"inbox": `https://${result.Domain}/users/${result.Username}/inbox`,
"outbox": `https://${result.Domain}/users/${result.Username}/outbox`,
"featured": `https://${result.Domain}/users/${result.Username}/collections/featured`,
"featuredTags": `https://${result.Domain}/users/${result.Username}/collections/tags`,
"preferredUsername": `${result.Username}`,
"name": `${result.Username}`,
"summary": "",
"url": `https://${result.Domain}/@${result.Username}`,
"manuallyApprovesFollowers": false,
"discoverable": true,
"indexable": true,
"published": new Date(Date.now()).toISOString(),
"memorial": false,
"devices": `https://${result.Domain}/users/${result.Username}/collections/devices`,
"alsoKnownAs": [],
"publicKey": {
"id": `https://${result.Domain}/users/${result.Username}#main-key`,
"owner": `https://${result.Domain}/users/${result.Username}`,
"publicKeyPem": publicKey.toString()
},
"tag": [],
"attachment": [],
"endpoints": {
"sharedInbox": `https://${result.Domain}/inbox`
},
"icon": {
"type": "Image",
"mediaType": "image/png",
"url": `https://${result.Domain}/avatar.png`
}
}, null, 4)
}).then(
() => {
db.select({ id: user.id }).from(user).then(
(result) => {
db.insert(pubPrivKeys).values({
user: result[0].id,
publicKey: publicKey,
privateKey: privateKey
}).then();
}
)
}
)
})
});
}
})
}
main();