Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

3.3 KB
Raw Blame
import type { LocalFile } from './local-file'
import { AsyncEntry } from '@napi-rs/keyring'
import { z } from 'zod'
import { BaseError } from '@/errors/base'
import { ErrorCode } from '@/errors/codes'
import { BINARY } from '@/version/info'

/**
 * Credential store keyed by an opaque (host, email) pair.
 */
export type TokenStore = {
  read: (host: string, email: string) => Promise<string>
  write: (host: string, email: string, bearer: string) => Promise<void>
  remove: (host: string, email: string) => Promise<void>
}

const KEYCHAIN_ENTRY_PREFIX = 'tokens'

export const TOKENS_SCHEMA = z.object({
  tokens: z.record(z.string(), z.record(z.string(), z.string())).default({}),
})

type TokensDoc = z.infer<typeof TOKENS_SCHEMA>

export type TokensFile = LocalFile<TokensDoc>

export class FileTokenStore implements TokenStore {
  private readonly file: TokensFile

  constructor(file: TokensFile) {
    this.file = file
  }

  async read(host: string, email: string): Promise<string> {
    return (await this.file.read())?.tokens[host]?.[email] ?? ''
  }

  async write(host: string, email: string, bearer: string): Promise<void> {
    await this.file.update((doc) => {
      const tokens = doc?.tokens ?? {}
      return {
        tokens: {
          ...tokens,
          [host]: { ...tokens[host], [email]: bearer },
        },
      }
    })
  }

  async remove(host: string, email: string): Promise<void> {
    await this.file.update((doc) => {
      if (doc === null) return undefined
      const hostMap = doc.tokens[host]
      if (hostMap === undefined || !(email in hostMap)) return undefined
      delete hostMap[email]
      if (Object.keys(hostMap).length === 0) delete doc.tokens[host]
      return doc
    })
  }
}

/**
 * One OS-keyring entry per (host, email).
 */
export class KeychainTokenStore implements TokenStore {
  private readonly service: string

  constructor(service: string) {
    this.service = service
  }

  async read(host: string, email: string): Promise<string> {
    let raw: string | null | undefined
    try {
      raw = await new AsyncEntry(this.service, this.entry(host, email)).getPassword()
    } catch (err) {
      throw keyringUnavailableError(err)
    }
    if (raw === null || raw === undefined || raw === '') return ''
    try {
      const parsed: unknown = JSON.parse(raw)
      return typeof parsed === 'string' ? parsed : ''
    } catch {
      return ''
    }
  }

  async write(host: string, email: string, bearer: string): Promise<void> {
    try {
      await new AsyncEntry(this.service, this.entry(host, email)).setPassword(
        JSON.stringify(bearer),
      )
    } catch (err) {
      throw keyringUnavailableError(err)
    }
  }

  async remove(host: string, email: string): Promise<void> {
    try {
      await new AsyncEntry(this.service, this.entry(host, email)).deletePassword()
    } catch {
      /* missing entry is fine */
    }
  }

  private entry(host: string, email: string): string {
    return `${KEYCHAIN_ENTRY_PREFIX}.${host}.${email}`
  }
}

function keyringUnavailableError(cause: unknown): BaseError {
  return new BaseError({
    code: ErrorCode.KeyringUnavailable,
    message: 'OS keychain is unreachable',
    hint: `unlock the keychain (or the login session) and retry, or run ${BINARY} login --server <url> --no-keyring to keep the token in a file`,
    cause,
  })
}