# How the SAM In-App Chat Durable Object Is Authorized and Why the OAuth Wrapper Is Bypassed for `/agents/*`

> Discover how the SAM in-app chat Durable Object authorizes users with signed cookies and bypasses OAuth for /agents/* routes, ensuring low latency and consistent sessions.

- Repository: [Every App/open-seo](https://github.com/every-app/open-seo)
- Tags: internals
- Published: 2026-09-12

---

**The SAM in-app chat Durable Object in every-app/open-seo validates user sessions internally using signed cookies and the `SamSessionRepository`, deliberately bypassing the standard OAuth middleware for `/agents/*` routes to minimize latency and maintain session consistency.**

The every-app/open-seo repository implements real-time AI chat through a Cloudflare Durable Object called SAM (Self-Assistant Model). Unlike standard API endpoints that rely on external OAuth validation, the chat system handles **authorization inside the Durable Object itself** to support low-latency WebSocket and HTTP interactions.

## Routing Logic That Skips the OAuth Wrapper

In [`src/server.ts`](https://github.com/every-app/open-seo/blob/main/src/server.ts), the request router explicitly checks for the `/agents/` path prefix and forwards traffic directly to the SAM Durable Object binding. This bypasses the `ensureUser` middleware that normally performs full OAuth exchanges for other routes.

```typescript
// src/server.ts
if (pathname.startsWith("/agents/")) {
  // Directly forward the request; auth is handled by the DO itself.
  // The OAuth wrapper is not applied here.
  return routeAgentRequest(request, env.samDO);
}

```

By routing through `routeAgentRequest` immediately, the server avoids the external redirect flow that would add round-trip latency to chat interactions.

## Internal Authorization Inside the Durable Object

### Session Extraction and Validation

The Durable Object extracts the user's session information from the request cookie—the same signed session cookie used by the rest of the application. It validates this against the internal `SamSessionRepository` defined in [`src/serverFunctions/sam.ts`](https://github.com/every-app/open-seo/blob/main/src/serverFunctions/sam.ts).

```typescript
// src/serverFunctions/sam.ts
import { SamSessionRepository } from "@/server/features/sam/SamSessionRepository";

export async function getSamUser(request: Request) {
  // The SAM DO uses the same session repository as the rest of the app.
  // It reads the signed session cookie and looks up the user.
  const session = await SamSessionRepository.fromRequest(request);
  if (!session?.userId) {
    throw new Error("Unauthenticated");
  }
  return session.userId;
}

```

Because the chat Durable Object runs in the same Cloudflare Workers environment, it can safely read and verify the signed session without requiring an external OAuth round-trip.

### The Durable Object Implementation

The `SamChatAgent` class receives the request after the cookie-based validation and proceeds with the verified user context.

```typescript
// src/server/features/sam/SamChatAgent.ts
export class SamChatAgent extends DurableObject {
  async fetch(request: Request) {
    // No additional OAuth flow is required because the DO already trusts
    // the session token that was validated in the router layer.
    const userId = await getSamUser(request);
    
    // Proceed with chat logic, confident the user is authenticated
    return handleChatInteraction(request, userId);
  }
}

```

## Why the OAuth Wrapper Is Bypassed for `/agents/*`

Three architectural requirements drive the decision to skip the generic OAuth middleware for chat routes:

- **Performance**: Chat interactions are latency-sensitive. Avoiding a full OAuth redirect saves critical round-trip time that would degrade the real-time user experience.

- **Consistency**: The SAM chat requires the same session context as the web UI, including shared memory and project context. Using the existing session cookie guarantees the chat sees identical user state without duplicating authentication logic.

- **Security**: The Durable Object executes in the same trusted Workers environment where the session cookie's integrity is already guaranteed by Cloudflare's cryptographic signing. Adding another OAuth layer would not increase security but would introduce unnecessary complexity.

## Summary

- **[`src/server.ts`](https://github.com/every-app/open-seo/blob/main/src/server.ts)** routes `/agents/*` requests directly to the SAM Durable Object binding (`samDO`) via `routeAgentRequest`, skipping the OAuth middleware.
- **Authorization occurs inside the Durable Object**, which validates the signed session cookie against `SamSessionRepository` in [`src/serverFunctions/sam.ts`](https://github.com/every-app/open-seo/blob/main/src/serverFunctions/sam.ts).
- The **SAM chat trusts the existing session cookie** validated at the edge, eliminating the need for redundant OAuth exchanges.
- This bypass improves **performance**, maintains **session consistency** with the web UI, and preserves **security** through Cloudflare's signed cookie infrastructure.

## Frequently Asked Questions

### Why doesn't the SAM chat use the standard OAuth middleware?

The standard OAuth middleware performs external redirects and token exchanges that add unacceptable latency to real-time chat interactions. According to the every-app/open-seo source code, the chat routes require immediate access to the Durable Object to maintain WebSocket and HTTP responsiveness, so they rely on pre-validated session cookies instead.

### How does the Durable Object validate user sessions?

The `SamChatAgent` Durable Object calls `getSamUser()` from [`src/serverFunctions/sam.ts`](https://github.com/every-app/open-seo/blob/main/src/serverFunctions/sam.ts), which uses `SamSessionRepository.fromRequest()` to parse and verify the signed session cookie included in the request. This matches the same validation logic used by the web interface but executes within the Durable Object's isolated state.

### Is bypassing OAuth less secure for chat endpoints?

No. The bypass does not reduce security because the Durable Object runs in the same trusted Cloudflare Workers environment that cryptographically signs the session cookies. The OAuth wrapper primarily manages external identity provider flows, which are unnecessary when the session is already verified through Cloudflare's internal cookie validation.

### What happens if the session cookie is missing or invalid?

If `SamSessionRepository.fromRequest()` cannot find a valid user ID in the request cookies, `getSamUser()` throws an "Unauthenticated" error immediately. The Durable Object then returns an appropriate HTTP error response before executing any chat logic, ensuring unauthorized requests cannot access the conversation state.