mirror of
https://github.com/RooVetGit/Roo-Code.git
synced 2026-10-08 03:07:53 +00:00
Co-authored-by: Roo Code <roomote@roocode.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Matt Rubens <mrubens@users.noreply.github.com> Co-authored-by: Daniel <57051444+daniel-lxs@users.noreply.github.com>
72 lines
1.7 KiB
TypeScript
72 lines
1.7 KiB
TypeScript
import fs from "fs/promises"
|
|
import path from "path"
|
|
|
|
import { getConfigDir } from "./index.js"
|
|
|
|
const CREDENTIALS_FILE = path.join(getConfigDir(), "cli-credentials.json")
|
|
|
|
export interface Credentials {
|
|
token: string
|
|
createdAt: string
|
|
userId?: string
|
|
orgId?: string
|
|
}
|
|
|
|
export async function saveToken(token: string, options?: { userId?: string; orgId?: string }): Promise<void> {
|
|
await fs.mkdir(getConfigDir(), { recursive: true })
|
|
|
|
const credentials: Credentials = {
|
|
token,
|
|
createdAt: new Date().toISOString(),
|
|
userId: options?.userId,
|
|
orgId: options?.orgId,
|
|
}
|
|
|
|
await fs.writeFile(CREDENTIALS_FILE, JSON.stringify(credentials, null, 2), {
|
|
mode: 0o600, // Read/write for owner only
|
|
})
|
|
}
|
|
|
|
export async function loadToken(): Promise<string | null> {
|
|
try {
|
|
const data = await fs.readFile(CREDENTIALS_FILE, "utf-8")
|
|
const credentials: Credentials = JSON.parse(data)
|
|
return credentials.token
|
|
} catch (error) {
|
|
if ((error as NodeJS.ErrnoException).code === "ENOENT") {
|
|
return null
|
|
}
|
|
throw error
|
|
}
|
|
}
|
|
|
|
export async function loadCredentials(): Promise<Credentials | null> {
|
|
try {
|
|
const data = await fs.readFile(CREDENTIALS_FILE, "utf-8")
|
|
return JSON.parse(data) as Credentials
|
|
} catch (error) {
|
|
if ((error as NodeJS.ErrnoException).code === "ENOENT") {
|
|
return null
|
|
}
|
|
throw error
|
|
}
|
|
}
|
|
|
|
export async function clearToken(): Promise<void> {
|
|
try {
|
|
await fs.unlink(CREDENTIALS_FILE)
|
|
} catch (error) {
|
|
if ((error as NodeJS.ErrnoException).code !== "ENOENT") {
|
|
throw error
|
|
}
|
|
}
|
|
}
|
|
|
|
export async function hasToken(): Promise<boolean> {
|
|
const token = await loadToken()
|
|
return token !== null
|
|
}
|
|
|
|
export function getCredentialsPath(): string {
|
|
return CREDENTIALS_FILE
|
|
}
|