Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 20 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readFileSync } from 'fs'
import { parse } from 'yaml'
import { readFileSync, writeFileSync } from 'fs'
import { parse, stringify } from 'yaml'
import { resolve } from 'path'

export type TokenEntry = {
Expand Down Expand Up @@ -65,3 +65,21 @@ export function loadConfig(configPath?: string): Config {

return config
}

let configFilePath: string | undefined

export function setConfigPath(path: string) {
configFilePath = path
}

/**
* Persist a rotated refresh token back to config.yaml so it survives restarts.
*/
export function persistRefreshToken(newToken: string) {
const filePath = configFilePath || resolve(process.cwd(), 'config.yaml')
const raw = readFileSync(filePath, 'utf-8')
const config = parse(raw) as Config
if (config.oauth.refresh_token === newToken) return // no change
config.oauth.refresh_token = newToken
writeFileSync(filePath, stringify(config), 'utf-8')
}
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { loadConfig } from './config.js'
import { loadConfig, setConfigPath } from './config.js'
import { setLogLevel, log } from './logger.js'
import { initOAuth } from './oauth.js'
import { startProxy } from './proxy.js'
Expand All @@ -7,6 +7,7 @@ const configPath = process.argv[2]

try {
const config = loadConfig(configPath)
if (configPath) setConfigPath(configPath)
setLogLevel(config.logging.level)

log('info', 'CC Gateway starting...')
Expand Down
12 changes: 11 additions & 1 deletion src/oauth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { request as httpsRequest } from 'https'
import { log } from './logger.js'
import { persistRefreshToken } from './config.js'

const TOKEN_URL = 'https://platform.claude.com/v1/oauth/token'
const CLIENT_ID = '9d1c250a-e61b-44d9-88ed-5944d1962f5e'
Expand Down Expand Up @@ -97,9 +98,18 @@ function refreshOAuthToken(refreshToken: string): Promise<OAuthTokens> {
reject(new Error(`OAuth refresh failed (${res.statusCode}): ${JSON.stringify(data)}`))
return
}
const newRefreshToken = data.refresh_token || refreshToken
if (data.refresh_token && data.refresh_token !== refreshToken) {
log('info', 'Refresh token rotated, persisting to config...')
try {
persistRefreshToken(data.refresh_token)
} catch (err) {
log('error', `Failed to persist rotated refresh token: ${err}`)
}
}
resolve({
accessToken: data.access_token,
refreshToken: data.refresh_token || refreshToken,
refreshToken: newRefreshToken,
expiresAt: Date.now() + (data.expires_in || 3600) * 1000,
})
})
Expand Down