Admin Authentication & Principal Binding
The daemon enforces administrative security through a combination of static tokens and principal binding. This is the primary gate for all REST and gRPC operations that modify the system state.Admin Token Auth
When administrative authentication is enabled viaPALYRA_ADMIN_REQUIRE_AUTH, the daemon requires a Bearer token in the Authorization header crates/palyra-daemon/src/transport/http/handlers/console/auth.rs#41-45. This token is validated against the configured admin_token.
Principal Binding
The system uses “Principals” to identify the actor performing an operation. For the web console, the principal must follow theadmin:* pattern (e.g., admin:web-console) crates/palyra-daemon/src/transport/http/handlers/console/auth.rs#31-35.
- Bound Principals: Administrators can bind specific tokens to specific principals using the
PALYRA_ADMIN_BOUND_PRINCIPALenvironment variable crates/palyra-daemon/tests/openai_auth_surface.rs#36-36. - Context Extraction: The
request_context_from_headersfunction extracts the principal and device ID from incoming HTTP headers to populate theRequestContextcrates/palyra-daemon/src/transport/http/handlers/console/auth.rs#89-89.
Authentication Data Flow
The following diagram illustrates the transition from a raw HTTP request to an authorized application context. Admin Auth Sequence Sources: crates/palyra-daemon/src/transport/http/handlers/console/auth.rs#19-111, crates/palyra-daemon/src/app/state.rs#1-50Console Session & CSRF Model
The Web Console uses a stateful session model backed by secure cookies and CSRF (Cross-Site Request Forgery) protection.Session Lifecycle
- Login: Upon successful admin token validation, the daemon calls
issue_console_sessioncrates/palyra-daemon/src/transport/http/handlers/console/auth.rs#101-101. - Session Store: Sessions are stored in an in-memory map within
AppStateasConsoleSessionobjects crates/palyra-daemon/src/app/runtime.rs#70-70. - Token Issuance: A random
session_tokenis minted and returned to the client via aSet-Cookieheader crates/palyra-daemon/src/transport/http/handlers/console/auth.rs#104-106.
CSRF Protection
EveryConsoleSession includes a unique csrf_token crates/palyra-control-plane/src/models.rs#13-13.
- The client must include this token in the
X-CSRF-Tokenheader for all mutating (POST/PUT/DELETE) requests. - The daemon validates this in
authorize_console_sessioncrates/palyra-daemon/src/transport/http/handlers/console/auth.rs#132-132.
Browser Handoff
To support transitions from the CLI to the Web Console (e.g.,palyra console), the daemon implements a handoff mechanism. It generates a short-lived ConsoleBrowserHandoff token crates/palyra-daemon/src/transport/http/handlers/console/auth.rs#141-147 which is consumed by the browser to establish a session without re-entering the admin token.
Sources: crates/palyra-daemon/src/transport/http/handlers/console/auth.rs#127-160, crates/palyra-control-plane/src/models.rs#8-16
OpenAI-Compatible Auth Surface
Palyra provides a specialized auth surface for model providers (OpenAI and Anthropic), allowing users to manage API keys and OAuth profiles through the web console or CLI.Auth Profiles
Thepalyra-auth crate defines the core registry for these credentials.
- AuthProfileRecord: Stores the profile identity, provider kind, and scope crates/palyra-auth/src/lib.rs#10-16.
- Credential Storage: API keys are never stored in the configuration file. Instead, they are stored in the
Vaultand referenced via avault_refcrates/palyra-daemon/src/openai_surface.rs#40-46.
Validation Flow
When a user adds a new API key (e.g., viaconnect_openai_api_key), the daemon performs real-time validation by calling the provider’s /v1/models endpoint crates/palyra-daemon/src/openai_auth.rs#189-195. This ensures the key is valid before persisting it crates/palyra-daemon/src/openai_surface.rs#32-38.
OAuth Bootstrap
For OpenAI, Palyra supports a PKCE-based OAuth flow:- Bootstrap:
start_openai_oauth_attemptgenerates a PKCE verifier and challenge crates/palyra-daemon/src/openai_auth.rs#99-107. - Callback: The daemon hosts a callback handler at
console/v1/auth/providers/openai/callbackto capture the authorization code crates/palyra-daemon/src/openai_surface.rs#13-13. - Exchange: The daemon exchanges the code for tokens using
exchange_authorization_codecrates/palyra-daemon/src/openai_auth.rs#132-140.
The palyra-auth Crate
The palyra-auth crate is the library responsible for managing the lifecycle of authentication profiles and handling OAuth token refreshes.
| Component | Role | File Reference |
|---|---|---|
AuthProfileRegistry | Manages the persistence and lookup of AuthProfileRecords. | crates/palyra-auth/src/registry.rs#21-21 |
OAuthRefreshAdapter | Trait for performing provider-specific token refreshes. | crates/palyra-auth/src/refresh.rs#18-18 |
AuthHealthReport | Aggregates the status of all configured profiles (valid, expiring, or failed). | crates/palyra-auth/src/models.rs#11-11 |
compute_backoff_ms | Implements exponential backoff for failed credential validations. | crates/palyra-auth/src/refresh.rs#17-17 |
Profile Scoping
Profiles can be scoped to eitherGlobal (available to all agents) or Agent (restricted to a specific agent_id) crates/palyra-auth/src/models.rs#13-13. This is enforced during the model provider selection process.
Sources: crates/palyra-auth/src/lib.rs#1-33
Access Control Layer
The Access Control layer manages theAccessRegistry, which tracks permissions and identity mappings within the daemon.
Implementation Mapping
The following diagram bridges the logical access control concepts to the specific code entities implementing them. Code Entity Mapping: Access Control Sources: crates/palyra-daemon/src/access_control.rs#1-20, crates/palyra-daemon/src/app/runtime.rs#11-16, palyra-identity: IdentityManagerRate Limiting
The daemon implements tiered rate limiting to prevent abuse:- Admin Rate Limits: Tracked per IP address in
admin_rate_limitcrates/palyra-daemon/src/app/runtime.rs#62-62. - OpenAI-Compatible API Limits: Tracked per API key in
compat_api_rate_limitcrates/palyra-daemon/src/app/runtime.rs#64-66.
CLI Access Management
The CLI provides commands to manage these settings directly viapalyra auth access crates/palyra-cli/src/commands/auth.rs#16-19, which communicates with the AuthService gRPC endpoint crates/palyra-cli/src/commands/auth.rs#31-33.
Sources: crates/palyra-daemon/src/app/runtime.rs#27-83, crates/palyra-cli/src/commands/auth.rs#1-139