@humanlayer/agentlayer-provider-auth
v0.0.81
Published
Storage and normalization for provider credentials (OAuth tokens and API keys) used by AgentLayer's model providers. It defines a small `AuthStore` interface with an in-memory implementation for tests and a file-backed implementation that persists to `~/.
Readme
@humanlayer/agentlayer-provider-auth
Storage and normalization for provider credentials (OAuth tokens and API keys) used by AgentLayer's model providers. It defines a small AuthStore interface with an in-memory implementation for tests and a file-backed implementation that persists to ~/.local/share/agentlayer/auth.json (or $XDG_DATA_HOME), with automatic import from AgentSDK's and OpenCode's auth files so existing logins keep working.
Install
bun add @humanlayer/agentlayer-provider-authUsage
import { ensureFileAuthStore, requireAuth, type AuthInfo } from '@humanlayer/agentlayer-provider-auth'
// Creates the store, importing from the Agent SDK auth file on first use if
// the AgentLayer auth file doesn't exist yet.
const authStore = await ensureFileAuthStore()
await authStore.set('copilot', { kind: 'api', apiKey: 'sk-...' })
const auth = await authStore.get('codex') // AuthInfo | undefined
const apiAuth = await requireAuth(authStore, 'copilot', 'api') // throws if missing/wrong kindFor tests, use createMemoryAuthStore(initialAuth?) instead — same AuthStore interface, nothing touches disk.
Key exports
AuthStore— interface withget/set/delete/getAll, all async;get/getAllreturn deep clones of stored data, andsetstores a deep clone of its input, so callers can't mutate stored state.AuthInfo— discriminated union:OAuthAuthInfo(kind: 'oauth',accessToken, optionalrefreshToken/expiresAt/idToken/scope/tokenType/accountId/enterpriseUrl) orApiAuthInfo(kind: 'api',apiKey, optionalmetadata).createMemoryAuthStore(initialAuth?)— in-memoryAuthStore.createFileAuthStore(options?)— file-backedAuthStore.options.filePathdefaults togetDefaultAgentLayerAuthPath().ensureFileAuthStore(options?)— likecreateFileAuthStore, but first seeds the AgentLayer auth file from the Agent SDK auth file (getDefaultAgentSdkAuthPath()) if the AgentLayer file doesn't exist yet. Use this at startup.readAuth/writeAuth/removeAuth/readAllAuth— one-shot convenience wrappers aroundcreateFileAuthStore(options).get/set/delete/getAll.requireAuth(store, providerId, expectedKind?)— fetches auth or throws; withexpectedKindit also narrows the return type and throws on akindmismatch.normalizeProviderId(providerId)— trims trailing slashes and maps known aliases/subpaths (openai,openai.codex,codex.*→codex;github-copilot→copilot;github-copilot-enterprise→copilot-enterprise) to their canonical id; unrecognized ids pass through unchanged. Returnsstring(notCanonicalAuthProviderId). All store methods normalize the id internally.getDefaultAgentLayerAuthPath()/getDefaultAgentSdkAuthPath()/getDefaultOpenCodeAuthPath()— resolve default file locations, each overridable viaAGENTLAYER_AUTH_PATH,AGENT_SDK_AUTH_PATH,OPENCODE_AUTH_PATH.
Notes
- File stores are written with mode
0o600. - Pass
openCodeAuthFilePath(or setenableOpenCodeFallback: true) to haveget/getAllfall back to and import entries from an OpenCodeauth.json, translating itstype/access/refresh/expires/keyfields intoAuthInfo. Imported entries are written back to the AgentLayer file but never overwrite existing entries. - Consumers:
@humanlayer/agentlayer-provider-openai-codex,@humanlayer/agentlayer-provider-github-copilot, andagents/codelayerall read credentials through this package (seeagents/codelayer/src/providers.ts).
