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() Db.read()
.then(() => { .then(() => {
if (!Db.data.find(e => e.admin)) { if (!Db.data.find(e => e.admin)) {

View file

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

View file

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

View file

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