Skip to main content
The Palyra authentication system is centered around Auth Profiles, which provide a unified registry for managing credentials (API keys and OAuth tokens) across multiple model providers. This subsystem, implemented primarily in palyra-auth and managed by the palyra-daemon via the openai_surface and openai_auth modules, ensures that sensitive credentials are never leaked in configuration files, instead persisting them in the secure Vault while referencing them via unique identifiers.

Auth Profile Registry

The AuthProfileRegistry is the central management entity for credentials. It supports two primary types of credentials: static API keys and managed OAuth tokens. Profiles are scoped to either a global level or bound to a specific agent.

Key Data Structures

Code Entity Mapping: Registry to Storage

The following diagram illustrates how the AuthProfileRegistry interacts with the Vault to protect credentials. Registry and Vault Interaction Sources: crates/palyra-auth/src/registry.rs#21, crates/palyra-auth/src/models.rs#10-16, crates/palyra-daemon/src/openai_surface.rs#42-48

OAuth Flows and Token Management

Palyra implements robust OAuth 2.0 flows, specifically optimized for OpenAI’s model provider surface. This includes PKCE (Proof Key for Code Exchange) for secure browser-based authorization and background token refreshing.

OAuth Lifecycle

  1. Bootstrap: The user initiates a flow in the Web Console. The daemon generates a PKCE verifier and challenge crates/palyra-daemon/src/openai_auth.rs#99-107.
  2. Exchange: Upon callback, the daemon exchanges the authorization code for an access token and a refresh token crates/palyra-daemon/src/openai_auth.rs#132-140.
  3. Persistence: Tokens are stored in the Vault. The AuthProfileRecord stores the access_token_vault_ref and refresh_token_vault_ref crates/palyra-cli/src/commands/auth.rs#161-166.
  4. Refresh: The OAuthRefreshAdapter handles background refreshes when tokens expire, using a provider-specific backoff policy crates/palyra-auth/src/refresh.rs#17-20.

Implementation Details

Sources: crates/palyra-daemon/src/openai_auth.rs#11-26, crates/palyra-daemon/src/openai_surface.rs#144-188

Session Management and Handoff

The palyrad daemon manages administrative sessions for the Web Console using a combination of secure cookies and CSRF tokens.

Console Sessions

When a user logs into the console, the daemon issues a ConsoleSession which includes a session_token (stored in a SET-COOKIE header) and a csrf_token (returned in the JSON body) crates/palyra-daemon/src/transport/http/handlers/console/auth.rs#101-110.

Browser Handoff

For flows that require a local browser (like OAuth callbacks), Palyra uses a “handoff” mechanism:
  1. A short-lived ConsoleBrowserHandoff token is minted crates/palyra-daemon/src/transport/http/handlers/console/auth.rs#140-146.
  2. The browser is redirected to a loopback URL (typically 127.0.0.1) to consume the token and establish a session in the local environment crates/palyra-daemon/src/transport/http/handlers/console/auth.rs#154-157.
Authentication Sequence Sources: crates/palyra-daemon/src/transport/http/handlers/console/auth.rs#19-111, crates/palyra-daemon/src/openai_surface.rs#42-61

Provider Validation and Health

Before saving a profile, Palyra performs active validation to ensure the credentials are functional.

Auth Surface Testing

The system is verified via integration tests that mock provider responses. For example, openai_auth_surface.rs validates that API keys are correctly persisted as Vault references and that the default profile selection is reflected in the daemon’s configuration crates/palyra-daemon/tests/openai_auth_surface.rs#29-57. Sources: crates/palyra-daemon/src/openai_surface.rs#34-40, crates/palyra-daemon/src/openai_surface.rs#96-102, apps/web/src/console/hooks/useAuthDomain.ts#96-116

CLI and Web UI Integration

CLI Commands

The palyra CLI provides a full suite of commands for managing profiles:

Web Console (AuthSection)

The React-based AuthSection provides a user-friendly interface for the useAuthDomain hook. It allows users to: Sources: crates/palyra-cli/src/commands/auth.rs#4-24, apps/web/src/console/hooks/useAuthDomain.ts#48-68, apps/web/src/console/sections/AuthSection.tsx#62-111