Co-authored-by: Brendan Allan <14191578+Brendonovich@users.noreply.github.com> Co-authored-by: Kit Langton <kit.langton@gmail.com> Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: Affan Ali <93028901+affanali2k3@users.noreply.github.com> Co-authored-by: affanali2k3 <affanalikhanxx@gmail.com> Co-authored-by: Frank <frank@anoma.ly> Co-authored-by: opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com> Co-authored-by: 𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴 <little-frank@opencord.local> Co-authored-by: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Co-authored-by: Jay V <air@live.ca> Co-authored-by: Dax Raad <d@ironbay.co> Co-authored-by: Aarav Sareen <96787824+arvsrn@users.noreply.github.com> Co-authored-by: OpeOginni <107570612+OpeOginni@users.noreply.github.com> Co-authored-by: Luke Parker <10430890+Hona@users.noreply.github.com> Co-authored-by: Ben Guthrie <benjee.012@gmail.com> Co-authored-by: Dax <mail@thdxr.com> Co-authored-by: Filip <34747899+neriousy@users.noreply.github.com> Co-authored-by: Max Anderson <max.a.anderson95@gmail.com> Co-authored-by: Brendan Allan <git@brendonovich.dev> Co-authored-by: Jack <jack@anoma.ly> Co-authored-by: Shoubhit Dash <shoubhit2005@gmail.com> Co-authored-by: Dustin Deus <deusdustin@gmail.com> Co-authored-by: starptech <starptech@starptechs-MBP.fritz.box> Co-authored-by: Aiden Cline <aidenpcline@gmail.com> Co-authored-by: usrnk1 <7547651+usrnk1@users.noreply.github.com> Co-authored-by: Jay <53023+jayair@users.noreply.github.com> Co-authored-by: runvip <164729189+runvip@users.noreply.github.com> Co-authored-by: opencode <opencode@sst.dev> Co-authored-by: Julian Coy <julian@ex-machina.co> Co-authored-by: Vladimir Glafirov <vglafirov@gitlab.com>
97 lines
3.4 KiB
TypeScript
97 lines
3.4 KiB
TypeScript
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
|
|
import path from "path"
|
|
import { Effect, Layer, Record, Result, Schema, Context } from "effect"
|
|
import { NonNegativeInt } from "@opencode-ai/core/schema"
|
|
import { Global } from "@opencode-ai/core/global"
|
|
import { FSUtil } from "@opencode-ai/core/fs-util"
|
|
|
|
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 AuthError({ message, cause })
|
|
|
|
export class Oauth extends Schema.Class<Oauth>("OAuth")({
|
|
type: Schema.Literal("oauth"),
|
|
refresh: Schema.String,
|
|
access: Schema.String,
|
|
expires: NonNegativeInt,
|
|
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,
|
|
metadata: Schema.optional(Schema.Record(Schema.String, Schema.String)),
|
|
}) {}
|
|
|
|
export class WellKnown extends Schema.Class<WellKnown>("WellKnownAuth")({
|
|
type: Schema.Literal("wellknown"),
|
|
key: Schema.String,
|
|
token: Schema.String,
|
|
}) {}
|
|
|
|
export const Info = Schema.Union([Oauth, Api, WellKnown]).annotate({ discriminator: "type", identifier: "Auth" })
|
|
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 Context.Service<Service, Interface>()("@opencode/Auth") {}
|
|
|
|
const layer = Layer.effect(
|
|
Service,
|
|
Effect.gen(function* () {
|
|
const fsys = yield* FSUtil.Service
|
|
const decode = Schema.decodeUnknownOption(Info)
|
|
|
|
const all = Effect.fn("Auth.all")(function* () {
|
|
if (process.env.OPENCODE_AUTH_CONTENT) {
|
|
try {
|
|
return JSON.parse(process.env.OPENCODE_AUTH_CONTENT)
|
|
} catch (err) {}
|
|
}
|
|
|
|
const data = (yield* fsys.readJson(file).pipe(Effect.orElseSucceed(() => ({})))) as Record<string, unknown>
|
|
return Record.filterMap(data, (value) => Result.fromOption(decode(value), () => undefined))
|
|
})
|
|
|
|
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* fsys
|
|
.writeJson(file, { ...data, [norm]: info }, 0o600)
|
|
.pipe(Effect.mapError(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* fsys.writeJson(file, data, 0o600).pipe(Effect.mapError(fail("Failed to write auth data")))
|
|
})
|
|
|
|
return Service.of({ get, all, set, remove })
|
|
}),
|
|
)
|
|
|
|
export const node = LayerNode.make({ service: Service, layer: layer, deps: [FSUtil.node] })
|
|
|
|
export * as Auth from "."
|