mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 08:28:19 +01:00
51 lines
1.7 KiB
TypeScript
51 lines
1.7 KiB
TypeScript
import { Client, Token } from "@versia-server/kit/db";
|
|
import { randomUUIDv7 } from "bun";
|
|
import chalk from "chalk";
|
|
// @ts-expect-error - Root import is required or the Clec type definitions won't work
|
|
// biome-ignore lint/correctness/noUnusedImports: Root import is required or the Clec type definitions won't work
|
|
import { defineCommand, type Root } from "clerc";
|
|
import { randomString } from "@/math.ts";
|
|
import { retrieveUser } from "../utils.ts";
|
|
|
|
export const generateTokenCommand = defineCommand(
|
|
{
|
|
name: "user token",
|
|
description: "Generates a new access token for a user.",
|
|
parameters: ["<username>"],
|
|
},
|
|
async (context) => {
|
|
const { username } = context.parameters;
|
|
|
|
const user = await retrieveUser(username);
|
|
|
|
if (!user) {
|
|
throw new Error(`User ${chalk.gray(username)} not found.`);
|
|
}
|
|
|
|
const application = await Client.insert({
|
|
id:
|
|
user.id +
|
|
Buffer.from(
|
|
crypto.getRandomValues(new Uint8Array(32)),
|
|
).toString("base64"),
|
|
name: "Versia",
|
|
redirectUris: [],
|
|
scopes: ["openid", "profile", "email"],
|
|
secret: "",
|
|
});
|
|
|
|
const token = await Token.insert({
|
|
id: randomUUIDv7(),
|
|
accessToken: randomString(64, "base64url"),
|
|
scopes: ["read", "write", "follow"],
|
|
userId: user.id,
|
|
clientId: application.id,
|
|
});
|
|
|
|
console.info(
|
|
`Token generated for user ${chalk.gray(user.data.username)}.`,
|
|
);
|
|
console.info(`Access Token: ${chalk.blue(token.data.accessToken)}`);
|
|
},
|
|
);
|