mirror of
https://github.com/anomalyco/opencode.git
synced 2026-07-23 02:36:57 -04:00
53 lines
1.4 KiB
TypeScript
53 lines
1.4 KiB
TypeScript
export * as ServerAuth from "./auth"
|
|
|
|
import { Context, Layer, Option, Redacted } from "effect"
|
|
|
|
export type Credentials = {
|
|
password?: string
|
|
}
|
|
|
|
export type DecodedCredentials = {
|
|
readonly username: string
|
|
readonly password: Redacted.Redacted
|
|
}
|
|
|
|
export type Info = {
|
|
readonly password: Option.Option<string>
|
|
readonly username: string
|
|
}
|
|
|
|
export class Config extends Context.Service<Config, Info>()("@opencode/ServerAuthConfig") {
|
|
static configLayer(input: Pick<Info, "password">) {
|
|
return Layer.succeed(this, this.of({ ...input, username: "opencode" }))
|
|
}
|
|
|
|
static get layer() {
|
|
return this.configLayer({ password: Option.none() })
|
|
}
|
|
}
|
|
|
|
export function required(config: Info) {
|
|
return Option.isSome(config.password) && config.password.value !== ""
|
|
}
|
|
|
|
export function authorized(credentials: DecodedCredentials, config: Info) {
|
|
return (
|
|
Option.isSome(config.password) &&
|
|
credentials.username === config.username &&
|
|
Redacted.value(credentials.password) === config.password.value
|
|
)
|
|
}
|
|
|
|
export function header(credentials?: Credentials) {
|
|
const password = credentials?.password
|
|
if (!password) return undefined
|
|
|
|
return `Basic ${Buffer.from(`opencode:${password}`).toString("base64")}`
|
|
}
|
|
|
|
export function headers(credentials?: Credentials) {
|
|
const authorization = header(credentials)
|
|
if (!authorization) return undefined
|
|
return { Authorization: authorization }
|
|
}
|