Building a Token-to-OAuth Bridge for Gemini & Self-Hosted Tools
Most modern AI assistants (like Google Gemini) support the Model Context Protocol (MCP), allowing them to pull live data from external tools. However, integrating cloud-hosted LLM panels with self-hosted ecosystem tools like ezBookkeeping presents a strict authentication mismatch:
Client ID, Client Secret, and explicit token exchange paths.Authorization: Bearer <TOKEN>) generated directly within its user settings panel. It does not natively run a complex OpenID Connect (OIDC) identity provider.Because Gemini cannot accept a raw static token in its console, trying to connect them directly results in a protocol rejection error.
Instead of deploying a heavy identity provider suite (like Keycloak or Authentik) just to authenticate one service, we built a serverless Token-to-OAuth Proxy Bridge using Cloudflare Workers.
The proxy acts as an adapter, pretending to be a complete OAuth 2.1 Identity Server to satisfy Gemini’s security pipeline, while secretly appending the static ledger token to outgoing server requests.
[ Google Gemini Cloud ]
│
▼ (OAuth 2.1 Handshake / Strict Verification)
[ Cloudflare Worker Bridge ]
│
▼ (Inject Master Bearer Token Header)
[ Target ezBookkeeping Server ]
We chose Cloudflare Workers in Service Worker format because:
The script we implemented intercepts inbound web traffic and handles it based on the URL path requested:
/.well-known/openid-configuration):
Gemini probes this path automatically to discover how the server handles authentication. The Worker intercepts this and returns a mock JSON schema announcing that it supports standard authorization code exchanges./oauth/authorize):
When Gemini initiates a connection request, the Worker instantly loops the request back to Gemini with a mock authorization code, bypassing interactive user login screens entirely./oauth/token):
Gemini POSTs its configured Client ID and Client Secret to this route. The Worker parses this payload (handling both HTTP body forms and standard HTTP Basic Auth headers). It strictly matches them against our hidden variables. If verified, it returns a temporary session token signature (BRIDGE_AUTHORIZED_SESSION).BRIDGE_AUTHORIZED_SESSION string, swaps out the header, and passes a clean request containing the real ezBookkeeping Bearer Token downstream to the target endpoint.Hardcoding API tokens inside raw code repos creates a security hazard. To ensure production-grade isolation, we:
EZBOOKKEEPING_TOKEN and GEMINI_SECRET) entirely out of the code block and into Cloudflare’s Encrypted Environment Variables database.typeof VARIABLE !== 'undefined') to extract configuration keys safely during execution context handshakes.cf: { tlsVerify: false }) to ensure Cloudflare edge nodes don't drop target connections due to common certificate path mismatches found on self-hosted environments.