Back to Engineering
Security December 5, 2025 · 10 min read

The Unified Authentication Layer

Authentication is the unglamorous foundation of API integration. Every provider has their own way of doing it—OAuth 2.0, API keys, JWT tokens, basic auth, custom headers. Some require refresh tokens. Some have short-lived access tokens. Some need scopes specified upfront; others determine permissions dynamically.

For developers building AI agents that need to access multiple services, managing all of this is a significant engineering burden. That's why we built a unified authentication layer that handles the complexity behind a single, consistent interface.

The Abstraction

From the developer's perspective, authentication with Aphelion is simple: connect an account, and we handle the rest. Behind that simplicity is a system that speaks every authentication dialect fluently.

We model authentication as a state machine. Each provider connection moves through states: unconnected, pending authorization, active, needs refresh, expired, revoked. Transitions happen automatically based on token lifetimes, API responses, and user actions.

The agent never sees these states directly. It just calls tools, and the authentication layer ensures valid credentials are attached to every request.

OAuth Done Right

OAuth is by far the most common authentication method for modern APIs. It's also notoriously tricky to implement correctly. Subtle bugs in OAuth implementations are a common source of security vulnerabilities.

We implement the full OAuth 2.0 specification, including PKCE (Proof Key for Code Exchange) for public clients. When a user connects a new service, we handle the entire flow: generating the authorization URL, receiving the callback, exchanging the code for tokens, and securely storing the result.

Token refresh is automatic and proactive. We don't wait for a token to expire and have a request fail. We track expiration times and refresh tokens before they expire, during low-traffic periods. This means agents never see authentication errors due to expired tokens.

Credential Storage

Access tokens and refresh tokens are extremely sensitive. A leaked token can give attackers full access to user accounts. We treat credential storage as a critical security surface.

All credentials are encrypted at rest using AES-256-GCM. Encryption keys are stored in a separate system with its own access controls. We use envelope encryption—each credential has a unique data encryption key, which is itself encrypted by a master key. This limits blast radius if any single key is compromised.

Credentials are only decrypted in memory, immediately before making an API call. They're never logged, never written to disk unencrypted, never passed to systems that don't need them.

Scope Management

OAuth scopes control what actions a token can perform. Request too few scopes, and your agent can't do its job. Request too many, and users rightfully get suspicious about why you need all that access.

We take a principle-of-least-privilege approach. When a developer registers an agent, they specify which tools it needs. We automatically calculate the minimum required scopes across all connected providers. If an agent only needs to read from Google Calendar, we don't request write access.

If an agent's requirements change and it needs additional scopes, we handle incremental authorization. The user sees a request for just the new permissions, not a full re-authorization.

API Key Management

Not everything uses OAuth. Many developer-focused APIs use simple API keys. These are actually harder to manage well because there's no standard format, no standard way to rotate them, and no automatic expiration.

We provide a unified interface for API key storage that handles the diversity. Some keys go in headers, some in query parameters, some in request bodies. Some APIs want the key by itself; others want it base64-encoded or combined with a secret. We normalize all of this.

For providers that support key rotation, we automate the process. Old keys are revoked after a grace period. New keys are distributed to all running agents. The rotation happens with zero downtime.

Multi-tenant Isolation

When multiple users connect the same service through an agent, their credentials must be strictly isolated. User A's Slack token should never be used for User B's requests, even if both use the same agent.

We maintain separate credential stores per user, per agent, per provider. The credential lookup path includes all three dimensions. Even a bug in our code can't accidentally cross these boundaries—the data simply isn't co-located.

We also log every credential access with full context: which user, which agent, which tool, what time, from what IP. This audit trail is immutable and retained for compliance purposes.

Handling Revocation

Users can revoke access at any time, either through the provider's settings or through Aphelion. When revocation happens, we need to handle it gracefully.

Some providers notify us immediately via webhooks. Others don't tell us at all—we only find out when a request fails. We detect revocation signals in API responses and update our state accordingly.

When credentials are revoked, we notify the agent developer and, optionally, prompt the user to reconnect. We never silently fail or let an agent keep trying with dead credentials.

The Developer Experience

All of this complexity is invisible to most developers. They see a list of connected accounts in the console. They call tools in their agent code. Authentication just works.

For developers who need more control, we expose lower-level APIs: check credential status, force a token refresh, programmatically trigger reconnection flows. But these are optional—the defaults handle almost every case.

Building a robust authentication layer is one of those invisible infrastructure investments that only pays off when something goes wrong. The best authentication system is one you never think about. That's what we're building.

Build with Aphelion

Connect to 50+ providers with a single authentication flow.