mirror of
https://github.com/anomalyco/opencode.git
synced 2026-05-03 03:06:44 +00:00
116 lines
3.9 KiB
TypeScript
116 lines
3.9 KiB
TypeScript
import path from "path"
|
|
import { Effect, Layer, Record, Result, Schema, ServiceMap } from "effect"
|
|
import { makeRunPromise } from "@/effect/run-service"
|
|
import { zod } from "@/util/effect-zod"
|
|
import { Global } from "../global"
|
|
import { Filesystem } from "../util/filesystem"
|
|
|
|
export const OAUTH_DUMMY_KEY = "opencode-oauth-dummy-key"
|
|
|
|
const file = path.join(Global.Path.data, "auth.json")
|
|
|
|
const fail = (message: string) => (cause: unknown) => new Auth.AuthError({ message, cause })
|
|
|
|
export namespace Auth {
|
|
export class Oauth extends Schema.Class<Oauth>("OAuth")({
|
|
type: Schema.Literal("oauth"),
|
|
refresh: Schema.String,
|
|
access: Schema.String,
|
|
expires: Schema.Number,
|
|
accountId: Schema.optional(Schema.String),
|
|
enterpriseUrl: Schema.optional(Schema.String),
|
|
}) {}
|
|
|
|
export class Api extends Schema.Class<Api>("ApiAuth")({
|
|
type: Schema.Literal("api"),
|
|
key: Schema.String,
|
|
}) {}
|
|
|
|
export class WellKnown extends Schema.Class<WellKnown>("WellKnownAuth")({
|
|
type: Schema.Literal("wellknown"),
|
|
key: Schema.String,
|
|
token: Schema.String,
|
|
}) {}
|
|
|
|
const _Info = Schema.Union([Oauth, Api, WellKnown]).annotate({ discriminator: "type", identifier: "Auth" })
|
|
export const Info = Object.assign(_Info, { zod: zod(_Info) })
|
|
export type Info = Schema.Schema.Type<typeof _Info>
|
|
|
|
export class AuthError extends Schema.TaggedErrorClass<AuthError>()("AuthError", {
|
|
message: Schema.String,
|
|
cause: Schema.optional(Schema.Defect),
|
|
}) {}
|
|
|
|
export interface Interface {
|
|
readonly get: (providerID: string) => Effect.Effect<Info | undefined, AuthError>
|
|
readonly all: () => Effect.Effect<Record<string, Info>, AuthError>
|
|
readonly set: (key: string, info: Info) => Effect.Effect<void, AuthError>
|
|
readonly remove: (key: string) => Effect.Effect<void, AuthError>
|
|
}
|
|
|
|
export class Service extends ServiceMap.Service<Service, Interface>()("@opencode/Auth") {}
|
|
|
|
export const layer = Layer.effect(
|
|
Service,
|
|
Effect.gen(function* () {
|
|
const decode = Schema.decodeUnknownOption(Info)
|
|
|
|
const all = Effect.fn("Auth.all")(() =>
|
|
Effect.tryPromise({
|
|
try: async () => {
|
|
const data = await Filesystem.readJson<Record<string, unknown>>(file).catch(() => ({}))
|
|
return Record.filterMap(data, (value) => Result.fromOption(decode(value), () => undefined))
|
|
},
|
|
catch: fail("Failed to read auth data"),
|
|
}),
|
|
)
|
|
|
|
const get = Effect.fn("Auth.get")(function* (providerID: string) {
|
|
return (yield* all())[providerID]
|
|
})
|
|
|
|
const set = Effect.fn("Auth.set")(function* (key: string, info: Info) {
|
|
const norm = key.replace(/\/+$/, "")
|
|
const data = yield* all()
|
|
if (norm !== key) delete data[key]
|
|
delete data[norm + "/"]
|
|
yield* Effect.tryPromise({
|
|
try: () => Filesystem.writeJson(file, { ...data, [norm]: info }, 0o600),
|
|
catch: fail("Failed to write auth data"),
|
|
})
|
|
})
|
|
|
|
const remove = Effect.fn("Auth.remove")(function* (key: string) {
|
|
const norm = key.replace(/\/+$/, "")
|
|
const data = yield* all()
|
|
delete data[key]
|
|
delete data[norm]
|
|
yield* Effect.tryPromise({
|
|
try: () => Filesystem.writeJson(file, data, 0o600),
|
|
catch: fail("Failed to write auth data"),
|
|
})
|
|
})
|
|
|
|
return Service.of({ get, all, set, remove })
|
|
}),
|
|
)
|
|
|
|
const runPromise = makeRunPromise(Service, layer)
|
|
|
|
export async function get(providerID: string) {
|
|
return runPromise((service) => service.get(providerID))
|
|
}
|
|
|
|
export async function all(): Promise<Record<string, Info>> {
|
|
return runPromise((service) => service.all())
|
|
}
|
|
|
|
export async function set(key: string, info: Info) {
|
|
return runPromise((service) => service.set(key, info))
|
|
}
|
|
|
|
export async function remove(key: string) {
|
|
return runPromise((service) => service.remove(key))
|
|
}
|
|
}
|