This commit is contained in:
Dax Raad
2025-06-10 13:30:08 -04:00
parent 96b5a079ff
commit ef7f1f0761
10 changed files with 193 additions and 31 deletions

View File

@@ -2,11 +2,12 @@ import { generatePKCE } from "@openauthjs/openauth/pkce"
import { Global } from "../global"
import path from "path"
import fs from "fs/promises"
import type { BunFile } from "bun"
export namespace AuthAnthropic {
const CLIENT_ID = "9d1c250a-e61b-44d9-88ed-5944d1962f5e"
const file = Bun.file(path.join(Global.Path.data, "auth", "anthropic.json"))
export async function authorize() {
const pkce = await generatePKCE()
const url = new URL("https://claude.ai/oauth/authorize", import.meta.url)
@@ -47,13 +48,11 @@ export namespace AuthAnthropic {
}),
})
if (!result.ok) throw new ExchangeFailed()
const file = Bun.file(path.join(Global.Path.data, "anthropic.json"))
await Bun.write(file, result)
await fs.chmod(file.name!, 0o600)
}
export async function access() {
const file = Bun.file(path.join(Global.Path.data, "anthropic.json"))
if (!(await file.exists())) return
const result = await file.json()
const refresh = result.refresh_token

View File

@@ -0,0 +1,20 @@
import path from "path"
import { Global } from "../global"
import fs from "fs/promises"
export namespace AuthKeys {
const file = Bun.file(path.join(Global.Path.data, "auth", "keys.json"))
export async function get() {
return file
.json()
.catch(() => ({}))
.then((x) => x as Record<string, string>)
}
export async function set(key: string, value: string) {
const env = await get()
await Bun.write(file, JSON.stringify({ ...env, [key]: value }))
await fs.chmod(file.name!, 0o600)
}
}