Repository navigation
feat(platform): per-user API gateway over the OmniRoute API plane #111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
98f04e3
b8e1d79
5bc66cb
acbd5e4
2c8edd1
dd4bb6a
ec3e0f4
f345602
13854a2
c0a9726
c4bd6ec
796805c
601ff95
ecff00b
28a9c0d
708dfd4
460436c
d92611e
a8e7997
f89245a
fe20839
bae26ec
dfc810e
fc01622
e34c205
7533bb9
7333147
c8e8943
c140c75
34b86a9
62333b8
bf0d3f6
af0c267
bf7f46a
ca5c8f7
40fde6b
44e8524
d4cef92
d087014
24750fe
aae5cf0
5a39a18
0899555
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ import { GitHubProjectImportError, importPublicGitHubProject, listGitHubProjects | |
| import { resolveGitHubRepositoryReference } from './src/server/github-repository.mjs'; | ||
| import { resolveAuthorizedGitHubRepository } from './src/server/github-repository-metadata.mjs'; | ||
| import { createGitHubInstallationAuthorizationProvider } from './src/server/github-installation-authorization.mjs'; | ||
| import { bearerToken, createApiKeyStore, proxyToApiPlane } from './src/server/api-gateway.mjs'; | ||
|
|
||
| const dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
| const root = path.resolve(dirname, 'dist'); | ||
|
|
@@ -21,6 +22,9 @@ const githubCliBootstrapFile = process.env.BAPX_GITHUB_CLI_BOOTSTRAP_FILE || pat | |
| const platformStore = createPlatformStore({ workspaceRoot }); | ||
| const adminAuthorization = parseAdminGithubUserIds(process.env.BAPX_ADMIN_GITHUB_USER_IDS); | ||
| const agentsRuntimeOrigin = new URL(process.env.AGENTS_RUNTIME_ORIGIN || 'http://127.0.0.1:3003'); | ||
| const apiKeyStore = createApiKeyStore({ workspaceRoot }); | ||
| const apiPlaneOrigin = process.env.BAPX_API_PLANE_ORIGIN || 'http://127.0.0.1:20130'; | ||
| const apiPlaneToken = process.env.BAPX_API_PLANE_TOKEN || ''; | ||
| let githubInstallationTokenProvider; | ||
|
|
||
| const HOST_PREFIX = { | ||
|
|
@@ -582,10 +586,65 @@ async function handleAdminAPI(req, res, urlPath) { | |
| return false; | ||
| } | ||
|
|
||
| function readRawBody(req) { | ||
| return new Promise((resolve, reject) => { | ||
| const chunks = []; | ||
| req.on('data', (chunk) => chunks.push(chunk)); | ||
| req.on('end', () => resolve(Buffer.concat(chunks))); | ||
| req.on('error', reject); | ||
|
sourcery-ai[bot] marked this conversation as resolved.
Resolved
|
||
| }); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| // api.bapx.in — the customer-facing gateway. | ||
| // | ||
| // Only /v1/* is exposed. The plane's dashboard, admin and auth surfaces are | ||
| // single-tenant and must never be reachable from here. | ||
| async function handleApiGateway(req, res, urlPath) { | ||
| if (!urlPath.startsWith('/v1/')) { | ||
| jsonResponse(res, 404, { error: { message: 'Unknown endpoint', type: 'not_found' } }); | ||
| return; | ||
| } | ||
| const identity = apiKeyStore.verify(bearerToken(req)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift Key verification rewrites the whole key file on every request.
Fix the root cause in 🤖 Prompt for AI Agents
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed, not fixed yet. verify() does rewrite the whole key file on every authenticated request to stamp lastUsedAt, which is both a synchronous hot-path write and a lost-update race against concurrent issue/revoke. Tracking rather than patching here because the right fix is to separate usage stamping from verification (append-only or batched), which is a storage change rather than a one-line edit — and it sits alongside the wider point that platform-store writes whole JSON collections per mutation and will not survive real customer counts. 🤖 Addressed by Claude Code There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The finding remains valid. The current PR does not fix it. A follow-up should separate API-key verification from usage persistence and define a durable storage strategy for API keys and platform-store collections. Would you like me to create and assign a GitHub follow-up issue to you? ✏️ Learnings added
You are interacting with an AI system. |
||
| if (!identity) { | ||
| res.writeHead(401, { 'Content-Type': 'application/json', 'Cache-Control': 'no-store', 'WWW-Authenticate': 'Bearer' }); | ||
| res.end(JSON.stringify({ error: { message: 'Invalid bapX API key', type: 'invalid_request_error' } })); | ||
| return; | ||
| } | ||
| const body = req.method === 'GET' || req.method === 'HEAD' ? undefined : await readRawBody(req); | ||
| await proxyToApiPlane(req, res, { origin: apiPlaneOrigin, planeToken: apiPlaneToken, urlPath, body }); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When multiple businesses use issued keys, AGENTS.md reference: AGENTS.md:L8-L12 Useful? React with 👍 / 👎.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed and tracked, not fixed here — this is the architectural limit, not an oversight. The plane has no user dimension at all: That cannot be resolved in the gateway; it needs one plane instance per business. Filed as #113 with the evidence. The current gateway is correct for the shared BYOK API key path and is deliberately restricted to 🤖 Addressed by Claude Code |
||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| // Session-authenticated key management for the Platform UI. | ||
| async function handleApiKeyAdmin(req, res, urlPath) { | ||
| const account = getSessionAccount(req); | ||
| if (!account) { jsonResponse(res, 401, { error: 'authentication_required' }); return true; } | ||
| if (req.method === 'GET' && urlPath === '/api/platform/api-keys') { | ||
| jsonResponse(res, 200, { keys: apiKeyStore.list(account.id) }); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If Useful? React with 👍 / 👎.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in a8e7997. Both Platform route groups now sit inside a rejection boundary returning 503. Verified with both collections corrupted on disk: api-keys returns api_key_storage_unavailable and connections returns connector_storage_unavailable, both 503, and the process stays up with no unhandled rejections. 🤖 Addressed by Claude Code |
||
| return true; | ||
| } | ||
| if (req.method === 'POST' && urlPath === '/api/platform/api-keys') { | ||
| const payload = await parseBody(req).catch(() => ({})); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Unlike the gateway, password, and connector handlers, the session-authenticated API-key creation route still calls the unbounded Useful? React with 👍 / 👎.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in a8e7997. Key creation now reads through the bounded helper at 64 KB instead of the unbounded parseBody. Verified 413 payload_too_large on a ~90 KB body, process unaffected. 🤖 Addressed by Claude Code |
||
| const { secret, key } = apiKeyStore.issue(account.id, payload.name); | ||
| jsonResponse(res, 201, { key, secret }); | ||
| return true; | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
| if (req.method === 'DELETE' && urlPath.startsWith('/api/platform/api-keys/')) { | ||
| const id = decodeURIComponent(urlPath.slice('/api/platform/api-keys/'.length)); | ||
| jsonResponse(res, apiKeyStore.revoke(account.id, id) ? 200 : 404, { revoked: id }); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| http.createServer(async (req, res) => { | ||
| const host = req.headers.host?.toLowerCase().replace(/:\d+$/, '') ?? 'bapx.in'; | ||
| const prefix = HOST_PREFIX[host] ?? ''; | ||
| const urlPath = req.url?.split('?')[0] ?? ''; | ||
| if (host === 'api.bapx.in') { await handleApiGateway(req, res, urlPath); return; } | ||
| if (urlPath.startsWith('/api/platform/api-keys')) { | ||
| const handled = await handleApiKeyAdmin(req, res, urlPath); | ||
| if (handled) return; | ||
| } | ||
| if (host === 'docs.bapx.in' && urlPath === '/') { res.writeHead(302, { Location: 'https://docs.bapx.in/getting-started/quickstart/' }); res.end(); return; } | ||
| if (urlPath.startsWith('/api/auth/')) { | ||
| const handled = await handleAuthAPI(req, res, urlPath, host); | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,89 @@ | ||||||||||||||
| --- | ||||||||||||||
| title: Platform API | ||||||||||||||
| description: The bapX API plane — one OpenAI-compatible endpoint for every model your business connects, using your own provider credentials. | ||||||||||||||
| --- | ||||||||||||||
|
|
||||||||||||||
| The bapX API plane gives a business one OpenAI-compatible endpoint across every AI provider it has connected. You bring your own provider credentials; bapX routes, meters, and audits the calls. | ||||||||||||||
|
|
||||||||||||||
| ## Status | ||||||||||||||
|
|
||||||||||||||
| The customer-facing gateway is **not open yet**. This page documents the contract it will expose so integrations can be written against a stable shape, and so the boundary between what is implemented and what is planned stays explicit. | ||||||||||||||
|
|
||||||||||||||
| What is decided and will not change: | ||||||||||||||
|
|
||||||||||||||
| - The endpoint is **OpenAI-compatible**. Existing OpenAI SDK code works by changing the base URL and key. | ||||||||||||||
| - Access is **per business**, scoped by an issued bapX API key. | ||||||||||||||
| - Provider credentials are **yours** — see [Bring your own credentials](#bring-your-own-credentials) below, which is a product boundary, not a limitation we intend to remove. | ||||||||||||||
|
Comment on lines
+14
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Narrow the OpenAI SDK compatibility claim. The supported-operations table lists only chat completions and model listing. The statement that existing OpenAI SDK code works by changing the base URL and key is broader than that surface. Limit the claim to supported operations or document every supported endpoint. Proposed wording-Existing OpenAI SDK code works by changing the base URL and key.
+OpenAI SDK calls for the supported chat completions and model-listing operations work by changing the base URL and key.📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||
|
|
||||||||||||||
| Key issuance, quotas, and usage reporting go live with the gateway. | ||||||||||||||
|
|
||||||||||||||
| ## Base URL | ||||||||||||||
|
|
||||||||||||||
| ```text | ||||||||||||||
| https://api.bapx.in/v1 | ||||||||||||||
| ``` | ||||||||||||||
|
|
||||||||||||||
| Requests authenticate with a bapX-issued key in the standard `Authorization` header: | ||||||||||||||
|
|
||||||||||||||
| ```bash | ||||||||||||||
| curl https://api.bapx.in/v1/chat/completions \ | ||||||||||||||
| -H "Authorization: Bearer $BAPX_API_KEY" \ | ||||||||||||||
| -H "Content-Type: application/json" \ | ||||||||||||||
| -d '{ | ||||||||||||||
| "model": "openai/gpt-4o-mini", | ||||||||||||||
| "messages": [{"role": "user", "content": "Hello"}] | ||||||||||||||
| }' | ||||||||||||||
| ``` | ||||||||||||||
|
|
||||||||||||||
| Because the surface is OpenAI-compatible, the official SDKs work directly: | ||||||||||||||
|
|
||||||||||||||
| ```ts | ||||||||||||||
| import OpenAI from 'openai'; | ||||||||||||||
|
|
||||||||||||||
| const client = new OpenAI({ | ||||||||||||||
| apiKey: process.env.BAPX_API_KEY, | ||||||||||||||
| baseURL: 'https://api.bapx.in/v1', | ||||||||||||||
| }); | ||||||||||||||
| ``` | ||||||||||||||
|
|
||||||||||||||
| ## Supported operations | ||||||||||||||
|
|
||||||||||||||
| | Operation | Path | | ||||||||||||||
| | --- | --- | | ||||||||||||||
| | Chat completions | `POST /v1/chat/completions` | | ||||||||||||||
| | List models | `GET /v1/models` | | ||||||||||||||
|
|
||||||||||||||
| `GET /v1/models` returns only the models reachable through the providers your business has actually connected. A model absent from that list is not callable — connect its provider first. | ||||||||||||||
|
|
||||||||||||||
| ## Bring your own credentials | ||||||||||||||
|
|
||||||||||||||
| bapX does not resell model capacity. Every call is billed by your provider, to your account, under your own agreement with that provider. | ||||||||||||||
|
|
||||||||||||||
| This matters for more than pricing. Several providers' terms permit a self-hosted single-user proxy but **prohibit reselling API access or operating a multi-tenant proxy on pooled credentials**. Routing your own credentials keeps your usage inside the agreement you already accepted, rather than inside one bapX made on your behalf. | ||||||||||||||
|
|
||||||||||||||
| Practically: | ||||||||||||||
|
|
||||||||||||||
| - Add provider credentials under **Connectors** on [platform.bapx.in](https://platform.bapx.in/). | ||||||||||||||
| - Credentials are stored per business and are never shared across businesses. | ||||||||||||||
| - Agents receive only the connections authorized for their business workspace. | ||||||||||||||
| - Revoking a provider connection immediately removes those models from `/v1/models`. | ||||||||||||||
|
|
||||||||||||||
| ## Model naming | ||||||||||||||
|
|
||||||||||||||
| Models are addressed as `<provider>/<model>`, for example: | ||||||||||||||
|
|
||||||||||||||
| ```text | ||||||||||||||
| openai/gpt-4o-mini | ||||||||||||||
| anthropic/claude-sonnet-4-5 | ||||||||||||||
| google/gemini-2.5-flash | ||||||||||||||
| ``` | ||||||||||||||
|
|
||||||||||||||
| The provider prefix is what routes the call, so it is required even when a model name is globally unique. | ||||||||||||||
|
|
||||||||||||||
| ## Relationship to MCP | ||||||||||||||
|
|
||||||||||||||
| The API plane is for **model calls**. The [MCP gateway](/docs/mcp/overview/) is for **tools, resources, and workspace context**. They are separate surfaces with separate authorization, and an API key does not grant MCP access. | ||||||||||||||
|
|
||||||||||||||
| ## Attribution | ||||||||||||||
|
|
||||||||||||||
| The bapX API plane is built on [OmniRoute](https://github.com/diegosouzapw/OmniRoute), an MIT-licensed AI gateway (© 2026 diegosouzapw). bapX operates it as the routing layer beneath the gateway described here; the customer-facing contract on this page is bapX's own and is the supported surface. | ||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Every authenticated non-GET request is accumulated into memory with no byte limit before it is forwarded. A client holding even one bapX API key can therefore send a very large or never-ending chunked body and consume the
apps-wwwprocess's memory or keep a request open indefinitely, taking down the gateway for other customers; enforce a supported maximum and abort oversized or timed-out uploads while reading.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in f345602. Same cap as above, enforced during read so a chunked upload with no declared length is also stopped. First attempt destroyed the request socket on overflow, which killed the connection before the 413 could be written — corrected to pause, respond, then destroy.
🤖 Addressed by Claude Code