actor type on user, lookup table

This commit is contained in:
Moon Man 2024-01-01 07:52:40 -05:00
parent 5423bf31b7
commit 4ffa9626f3
3 changed files with 77 additions and 4 deletions

View File

@ -0,0 +1,72 @@
import { generateKeyPair } from "node:crypto";
const SITE_USER_ID = 1_000_000_000;
/**
* Add a lookup table.
* Add an actor type to "users" table.
*/
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
export const up = async (knex) => {
return knex.schema
.createTable("lookup", (table) => {
table.string("key").primary();
table.string("value");
})
.then(async () => {
await knex.schema.table("users", (table) => {
table.string("actor_type");
});
const { pub, priv } = await (new Promise((resolve, reject) => {
generateKeyPair("rsa", {
modulusLength: 2048,
publicKeyEncoding: {
type: "spki",
format: "pem"
},
privateKeyEncoding: {
type: "pkcs8",
format: "pem"
}
}, (err, pub, priv) => {
if (err) reject(err);
else resolve({ pub, priv });
});
}));
await knex("users").insert({
id: SITE_USER_ID,
actor_type: "Application",
name: "site",
nickname: process.env.blog_host,
bio: "The site itself.",
public_key: pub,
private_key: priv,
deleted: false
});
});
;
};
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
export const down = async (knex) => {
await knex("users")
.delete()
.where("id", SITE_USER_ID)
;
return knex.schema
.dropTableIfExists("lookup")
.table("users", (table) => {
table.dropColumn("actor_type");
});
;
};

View File

@ -5,7 +5,7 @@ import { User, getByActor } from "./user.js";
import { fillRoute, reverseRoute } from "./router.js"; import { fillRoute, reverseRoute } from "./router.js";
import { streamToString, hashDigest } from "./util.js"; import { streamToString, hashDigest } from "./util.js";
import { signedFetch, SignedInit, getActor } from "./net.js"; import { signedFetch, SignedInit, getActor } from "./net.js";
import { getById as getUserById, getKeyId } from "./user.js"; import { getById as getUserById, keyIdFromNickname } from "./user.js";
import parser from "activitypub-http-signatures"; import parser from "activitypub-http-signatures";
import type { Request, Response } from "express"; import type { Request, Response } from "express";
import { addFollower } from "./follower.js"; import { addFollower } from "./follower.js";
@ -60,7 +60,7 @@ export const handleInboxPost = async (req: Request, res: Response) => {
console.log("found article author"); console.log("found article author");
const init: SignedInit = { const init: SignedInit = {
keyId: getKeyId(user.nickname), keyId: keyIdFromNickname(user.nickname),
privateKey: user.private_key privateKey: user.private_key
}; };
@ -98,7 +98,7 @@ export const handleInboxPost = async (req: Request, res: Response) => {
} }
const init: SignedInit = { const init: SignedInit = {
keyId: getKeyId(signer.nickname), keyId: keyIdFromNickname(signer.nickname),
privateKey: signer.private_key privateKey: signer.private_key
}; };
@ -271,7 +271,7 @@ export const sendAccept = async (user: User, followId: string, follower: string,
console.log("Accept payload:", payload); console.log("Accept payload:", payload);
const init: RequestInit = { method: "POST", body: payload }; const init: RequestInit = { method: "POST", body: payload };
const init2 = { privateKey: user.private_key, keyId: getKeyId(user.nickname) }; const init2 = { privateKey: user.private_key, keyId: keyIdFromNickname(user.nickname) };
const result = await signedFetch(inbox, init, init2); const result = await signedFetch(inbox, init, init2);
console.log("response status:", result.status); console.log("response status:", result.status);

View File

@ -8,6 +8,7 @@ export const nicknameRegex = /^[a-zA-Z0-9_]+$/;
export const zUser = z.object({ export const zUser = z.object({
id: z.number().min(0), id: z.number().min(0),
actor_type: z.string().min(1),
name: z.string().min(1), name: z.string().min(1),
nickname: z.string().regex(nicknameRegex), nickname: z.string().regex(nicknameRegex),
bio: z.string(), bio: z.string(),