import path from "path"import { Global } from "../global"import { JsonStore } from "../util/jsonstore"import z from "zod" export const OAUTH_DUMMY_KEY = "synsc-oauth-dummy-key" export namespace Auth { export const Oauth = z .object({ type: z.literal("oauth"), refresh: z.string(), access: z.string(), expires: z.number(), accountId: z.string().optional(), enterpriseUrl: z.string().optional(), }) .meta({ ref: "OAuth" }) export const Api = z .object({ type: z.literal("api"), key: z.string(), }) .meta({ ref: "ApiAuth" }) export const WellKnown = z .object({ type: z.literal("wellknown"), key: z.string(), token: z.string(), }) .meta({ ref: "WellKnownAuth" }) export const Info = z.discriminatedUnion("type", [Oauth, Api, WellKnown]).meta({ ref: "Auth" }) export type Info = z.infer<typeof Info> const filepath = path.join(Global.Path.data, "auth.json") export async function get(providerID: string) { const auth = await all() return auth[providerID] } export async function all(): Promise<Record<string, Info>> { const data = await JsonStore.read(filepath) return Object.entries(data).reduce( (acc, [key, value]) => { const parsed = Info.safeParse(value) if (!parsed.success) return acc acc[key] = parsed.data return acc }, {} as Record<string, Info>, ) } export async function set(key: string, info: Info) { await JsonStore.update(filepath, (data) => ({ ...data, [key]: info })) } export async function remove(key: string) { await JsonStore.update(filepath, (data) => { delete data[key] }) }}
import path from "path"
import { Global } from "../global"
import { JsonStore } from "../util/jsonstore"
import z from "zod"
export const OAUTH_DUMMY_KEY = "synsc-oauth-dummy-key"
export namespace Auth {
export const Oauth = z
.object({
type: z.literal("oauth"),
refresh: z.string(),
access: z.string(),
expires: z.number(),
accountId: z.string().optional(),
enterpriseUrl: z.string().optional(),
})
.meta({ ref: "OAuth" })
export const Api = z
.object({
type: z.literal("api"),
key: z.string(),
})
.meta({ ref: "ApiAuth" })
export const WellKnown = z
.object({
type: z.literal("wellknown"),
key: z.string(),
token: z.string(),
})
.meta({ ref: "WellKnownAuth" })
export const Info = z.discriminatedUnion("type", [Oauth, Api, WellKnown]).meta({ ref: "Auth" })
export type Info = z.infer<typeof Info>
const filepath = path.join(Global.Path.data, "auth.json")
export async function get(providerID: string) {
const auth = await all()
return auth[providerID]
}
export async function all(): Promise<Record<string, Info>> {
const data = await JsonStore.read(filepath)
return Object.entries(data).reduce(
(acc, [key, value]) => {
const parsed = Info.safeParse(value)
if (!parsed.success) return acc
acc[key] = parsed.data
return acc
},
{} as Record<string, Info>,
)
}
export async function set(key: string, info: Info) {
await JsonStore.update(filepath, (data) => ({ ...data, [key]: info }))
}
export async function remove(key: string) {
await JsonStore.update(filepath, (data) => {
delete data[key]
})
}
}