|
1 | | -import path from "path" |
2 | | -import { Effect, Layer, Record, Result, Schema, Context } from "effect" |
3 | | -import { zod } from "@/util/effect-zod" |
4 | | -import { Global } from "../global" |
5 | | -import { AppFileSystem } from "@opencode-ai/shared/filesystem" |
6 | | - |
7 | | -export const OAUTH_DUMMY_KEY = "opencode-oauth-dummy-key" |
8 | | - |
9 | | -const file = path.join(Global.Path.data, "auth.json") |
10 | | - |
11 | | -const fail = (message: string) => (cause: unknown) => new Auth.AuthError({ message, cause }) |
12 | | - |
13 | | -export namespace Auth { |
14 | | - export class Oauth extends Schema.Class<Oauth>("OAuth")({ |
15 | | - type: Schema.Literal("oauth"), |
16 | | - refresh: Schema.String, |
17 | | - access: Schema.String, |
18 | | - expires: Schema.Number, |
19 | | - accountId: Schema.optional(Schema.String), |
20 | | - enterpriseUrl: Schema.optional(Schema.String), |
21 | | - }) {} |
22 | | - |
23 | | - export class Api extends Schema.Class<Api>("ApiAuth")({ |
24 | | - type: Schema.Literal("api"), |
25 | | - key: Schema.String, |
26 | | - metadata: Schema.optional(Schema.Record(Schema.String, Schema.String)), |
27 | | - }) {} |
28 | | - |
29 | | - export class WellKnown extends Schema.Class<WellKnown>("WellKnownAuth")({ |
30 | | - type: Schema.Literal("wellknown"), |
31 | | - key: Schema.String, |
32 | | - token: Schema.String, |
33 | | - }) {} |
34 | | - |
35 | | - const _Info = Schema.Union([Oauth, Api, WellKnown]).annotate({ discriminator: "type", identifier: "Auth" }) |
36 | | - export const Info = Object.assign(_Info, { zod: zod(_Info) }) |
37 | | - export type Info = Schema.Schema.Type<typeof _Info> |
38 | | - |
39 | | - export class AuthError extends Schema.TaggedErrorClass<AuthError>()("AuthError", { |
40 | | - message: Schema.String, |
41 | | - cause: Schema.optional(Schema.Defect), |
42 | | - }) {} |
43 | | - |
44 | | - export interface Interface { |
45 | | - readonly get: (providerID: string) => Effect.Effect<Info | undefined, AuthError> |
46 | | - readonly all: () => Effect.Effect<Record<string, Info>, AuthError> |
47 | | - readonly set: (key: string, info: Info) => Effect.Effect<void, AuthError> |
48 | | - readonly remove: (key: string) => Effect.Effect<void, AuthError> |
49 | | - } |
50 | | - |
51 | | - export class Service extends Context.Service<Service, Interface>()("@opencode/Auth") {} |
52 | | - |
53 | | - export const layer = Layer.effect( |
54 | | - Service, |
55 | | - Effect.gen(function* () { |
56 | | - const fsys = yield* AppFileSystem.Service |
57 | | - const decode = Schema.decodeUnknownOption(Info) |
58 | | - |
59 | | - const all = Effect.fn("Auth.all")(function* () { |
60 | | - const data = (yield* fsys.readJson(file).pipe(Effect.orElseSucceed(() => ({})))) as Record<string, unknown> |
61 | | - return Record.filterMap(data, (value) => Result.fromOption(decode(value), () => undefined)) |
62 | | - }) |
63 | | - |
64 | | - const get = Effect.fn("Auth.get")(function* (providerID: string) { |
65 | | - return (yield* all())[providerID] |
66 | | - }) |
67 | | - |
68 | | - const set = Effect.fn("Auth.set")(function* (key: string, info: Info) { |
69 | | - const norm = key.replace(/\/+$/, "") |
70 | | - const data = yield* all() |
71 | | - if (norm !== key) delete data[key] |
72 | | - delete data[norm + "/"] |
73 | | - yield* fsys |
74 | | - .writeJson(file, { ...data, [norm]: info }, 0o600) |
75 | | - .pipe(Effect.mapError(fail("Failed to write auth data"))) |
76 | | - }) |
77 | | - |
78 | | - const remove = Effect.fn("Auth.remove")(function* (key: string) { |
79 | | - const norm = key.replace(/\/+$/, "") |
80 | | - const data = yield* all() |
81 | | - delete data[key] |
82 | | - delete data[norm] |
83 | | - yield* fsys.writeJson(file, data, 0o600).pipe(Effect.mapError(fail("Failed to write auth data"))) |
84 | | - }) |
85 | | - |
86 | | - return Service.of({ get, all, set, remove }) |
87 | | - }), |
88 | | - ) |
89 | | - |
90 | | - export const defaultLayer = layer.pipe(Layer.provide(AppFileSystem.defaultLayer)) |
91 | | -} |
| 1 | +export * as Auth from "./auth" |
| 2 | +export { OAUTH_DUMMY_KEY } from "./auth" |
0 commit comments