Commit c8cd3f2a authored by Grant's avatar Grant
Browse files

add manual authsession admin endpoint

parent cee479da
Loading
Loading
Loading
Loading
+32 −0
Original line number Diff line number Diff line
import { Router } from "express";
import { prisma } from "./prisma.js";
import { AuthSession } from "../controllers/AuthSession.js";
import { APub } from "./apub/utils.js";

const app = Router();

@@ -15,6 +17,36 @@ app.use((req, res, next) => {
  next();
});

/**
 * Create AuthSession for specified handle and send verification code to user
 * The created session is returned as a response
 * Used for live testing DMs
 */
app.post("/create_auth_for", async (req, res) => {
  if (
    !req.query.handle ||
    typeof req.query.handle !== "string" ||
    req.query.handle.indexOf("@") <= 0
  ) {
    res.status(400).send("?handle is not valid (localpart@instance.domain)");
    return;
  }

  const [localpart, instance] = req.query.handle.split("@");

  const session = await AuthSession.create(`${localpart}@${instance}`);

  try {
    await APub.sendDM(session);
  } catch (e) {
    console.error("/create_auth_for failed for " + req.query.handle, e);
    await prisma.authSession.delete({ where: { id: session.id } });
    res.status(500).send("Failed to send DM: " + e);
  }

  res.status(201).json({ success: true, session });
});

app.get("/status", async (req, res) => {
  const completed = await prisma.fediverseUser.count({
    where: {