prepare to switch

This commit is contained in:
May 2024-05-24 18:49:54 +00:00 committed by GitHub
parent e93e56e8cd
commit c85de0e86a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 17 additions and 10 deletions

View file

@ -161,6 +161,16 @@ export namespace files {
}
}
export type AccountResolvable = Account | string | `@${string}`
export function resolve(obj: AccountResolvable) {
return typeof obj == "object"
? obj
: obj.startsWith("@")
? getFromUsername(obj.slice(1))
: getFromId(obj)
}
Db.read()
.then(() => {
if (!Db.data.find(e => e.admin)) {

View file

@ -6,6 +6,7 @@ import { z } from "zod"
import { AuthSchemas } from "./schemas/index.js"
import DbFile from "./dbfile.js"
import * as jose from "jose"
import { AccountResolvable } from "./accounts.js"
export let AuthTokenTO: { [key: string]: NodeJS.Timeout } = {}
export type TokenPermission = z.infer<typeof AuthSchemas.Scope>
@ -15,17 +16,15 @@ export type AuthToken = z.infer<typeof AuthSchemas.AuthToken>
export const Db = new DbFile<AuthToken[]>("tokens", [])
export function create(
id: string,
account: AccountResolvable,
expire: number | null = 24 * 60 * 60 * 1000,
type: TokenType = "User",
tokenPermissions?: TokenPermission[],
issuer?: string
tokenPermissions?: TokenPermission[]
) {
let token = AuthSchemas.AuthToken.parse({
account: id,
token: crypto.randomBytes(36).toString("hex"),
account,
id: crypto.randomUUID(),
expire: typeof expire == "number" ? Date.now() + expire : null,
issuer,
type,
tokenPermissions:
type != "User" ? tokenPermissions || ["user"] : undefined,

View file

@ -35,7 +35,6 @@ export const AuthToken = z.discriminatedUnion("type",[
}),
BaseAuthToken.extend({
type: z.literal("App"),
tokenPermissions: z.array(Scope).default(["user"]),
issuer: z.string()
tokenPermissions: z.array(Scope).default(["user"])
})
])

View file

@ -114,8 +114,7 @@ export default function (files: Files) {
"ApiKey",
params.scopes == "all"
? AuthSchemas.Scope.options
: Array.from(new Set(params.scopes)),
"monofile"
: Array.from(new Set(params.scopes))
)
return ctx.text(token)
}