Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

feat(backend): connector gateway apps live in MongoDB (connector_apps), kept as is

bubu-dudu3396ae8904ee
diff --git a/backend/README.md b/backend/README.md
index 2ab52ef0d..546db530a 100644
--- a/backend/README.md
+++ b/backend/README.md
@@ -107,22 +107,24 @@ registered with them are used, with the same requests and answers as the Grid co
To turn it on:
-1. **The apps' config**, never in git: the Grid control plane's `config-connector-auth.json` shape
- (`{"connectors": {"github": {"auth_type": "app", "client_id", "client_secret", "auth_url",
- "token_url", "scopes", …}}}`), as a file (`CONNECTOR_AUTH_FILE`) or inline (`CONNECTOR_AUTH_JSON`,
- for a k8s secret). Only `auth_type: app` entries are used. Production's are the `app` entries of
- Grid prod's `/var/lib/grid-apis/config-connector-auth.json`; staging's, Grid dev's.
-2. **`CONNECTOR_ENCRYPTION_KEY`**: 32 random bytes as base64 (`openssl rand -base64 32`), its own key.
- It seals the tokens kept per account (`ConnectorCredential`). Losing it means everyone connects again.
-3. **`CONNECTOR_REDIRECT_URI`**: the page every one of those apps allows. Production keeps the default,
+1. **`CONNECTOR_REDIRECT_URI`**: the page every one of those apps allows. Production keeps the default,
`https://www.autonomous.ai/connector/callback`; staging,
`https://staging.autonomousdev.xyz/connector/callback`.
-4. **The index** for `ConnectorCredential` (`npx prisma db push` creates `connector_credentials` and its
- unique `userId + connector` index).
-5. **The Autonomous web callback page** forwards a state that starts `harness_` to
- `POST /api/connectors/callback`, as it forwards `grid_` to Grid (see the PR that added this gateway).
-
-Unset, the gateway lists no service and starts no sign-in; nothing else changes.
+2. **The collections**: `prisma db push` (run at startup) creates `connector_apps` and
+ `connector_credentials` with their unique indexes. Both are kept as they are, not encrypted: the
+ database is private.
+3. **The apps**, one row per service in `connector_apps` (`code`, `clientId`, `clientSecret`, `authUrl`,
+ `tokenUrl`, `scopes`, `extra: {mcp_url, token_field, mcp_auth_header}`, `enabled`…): edited in
+ Compass, or written from a file with `npm run connectors:import <config-connector-auth.json>` (the
+ backend's `DATABASE_URL`). The file is the Grid control plane's shape; only `auth_type: app` entries
+ are written. Production's are the `app` entries of Grid prod's
+ `/var/lib/grid-apis/config-connector-auth.json`; staging's, Grid dev's. The file is never in git.
+ Running it again replaces those services; the gateway sees any change within a minute.
+ `enabled: false` switches a service off.
+4. **The Autonomous web callback page** forwards a state that starts `harness_` to
+ `POST /api/connectors/callback`, as it forwards `grid_` to Grid (ecm-website v1.28.18).
+
+With no row in `connector_apps`, the gateway lists no service and starts no sign-in; nothing else changes.
## Persistence
diff --git a/backend/package.json b/backend/package.json
index 56891dc13..ee56f7d8e 100644
--- a/backend/package.json
+++ b/backend/package.json
@@ -10,6 +10,7 @@
"build": "node build.mjs",
"build:tsc": "tsc",
"typecheck": "tsc --noEmit",
+ "connectors:import": "tsx src/connectorAppsImport.ts",
"start": "pm2 start ecosystem.config.cjs",
"stop": "pm2 stop ecosystem.config.cjs",
"start:runtime": "pm2-runtime start ecosystem.config.cjs",
diff --git a/backend/prisma/schema.prisma b/backend/prisma/schema.prisma
index ade695387..7186b8bcf 100644
--- a/backend/prisma/schema.prisma
+++ b/backend/prisma/schema.prisma
@@ -907,15 +907,17 @@ model HarnessLink {
}
/// One account's sign-in to a service through the connector gateway (lib/connectorGateway.ts), as the
-/// Grid control plane keeps it: the provider's tokens sealed with CONNECTOR_ENCRYPTION_KEY (AES-256-GCM),
-/// so the gateway can renew them for any of the account's computers. Disconnecting deletes the row.
+/// Grid control plane keeps it: the provider's tokens, so the gateway can renew them for any of the
+/// account's computers. Kept as they came (this database is private). Disconnecting deletes the row.
model ConnectorCredential {
- id String @id @default(uuid()) @map("_id")
- userId String
- connector String
- /// The token response, sealed: access and refresh token, type, scope, expiry.
- sealed String
- accountName String @default("")
+ id String @id @default(uuid()) @map("_id")
+ userId String
+ connector String
+ accessToken String
+ refreshToken String @default("")
+ tokenType String @default("Bearer")
+ scope String @default("")
+ accountName String @default("")
expiresAt Int @default(0)
connectedAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@ -923,3 +925,34 @@ model ConnectorCredential {
@@unique([userId, connector])
@@map("connector_credentials")
}
+
+/// A service the connector gateway signs in to (lib/connectorGateway.ts): the OAuth app Autonomous
+/// registered with it, in the Grid control plane's config-connector-auth.json fields, kept as they are
+/// (this database is private). Edited directly (Compass) or written by `npm run connectors:import`;
+/// read by the gateway, a minute at a time. `enabled: false` switches a service off.
+model ConnectorApp {
+ id String @id @default(uuid()) @map("_id")
+ code String @unique
+ enabled Boolean @default(true)
+ label String @default("")
+ description String @default("")
+ imageUrl String @default("")
+ clientId String
+ /// Empty for a public client using PKCE.
+ clientSecret String @default("")
+ authUrl String
+ tokenUrl String
+ refreshUrl String @default("")
+ userinfoUrl String @default("")
+ scopes String[]
+ authStyle String @default("")
+ pkce Boolean @default(false)
+ refresh Boolean @default(false)
+ /// Extra consent-URL parameters (Google's access_type=offline).
+ authParams Json?
+ /// Grid's `extra`: mcp_url, mcp_auth_header, token_field.
+ extra Json?
+ updatedAt DateTime @updatedAt
+
+ @@map("connector_apps")
+}
diff --git a/backend/src/config/env.ts b/backend/src/config/env.ts
index 57c0ad2f5..b68bce844 100644
--- a/backend/src/config/env.ts
+++ b/backend/src/config/env.ts
@@ -89,15 +89,8 @@ const envSchema = z.object({
// the backend authenticates the manager's dial-in socket (/api/manager-ws) against that apiKey and
// picks a manager on create by capacity (machines.managerId → managers). See lib/managers.ts.
- // The connector gateway (lib/connectorGateway.ts): the OAuth apps Autonomous registered with GitHub,
- // Slack, Google and the others that let no computer register its own, in the Grid control plane's
- // config-connector-auth.json shape. A file path, or the JSON inline (a k8s secret). Neither → the
- // gateway answers that no service signs in through it. Never in git.
- CONNECTOR_AUTH_FILE: z.string().optional(),
- CONNECTOR_AUTH_JSON: z.string().optional(),
- // Seals each account's tokens kept for those services (ConnectorCredential): 32 random bytes as
- // base64 (`openssl rand -base64 32`), its own key. Unset → the gateway starts no sign-in.
- CONNECTOR_ENCRYPTION_KEY: z.string().optional(),
+ // The connector gateway (lib/connectorGateway.ts): its OAuth apps (GitHub, Slack, Google…) live in
+ // MongoDB (`connector_apps`), edited directly or written by `npm run connectors:import <file>`.
// The redirect every one of those apps allows: the Autonomous web page, which forwards a `harness_`
// state here (POST /api/connectors/callback). Staging: https://staging.autonomousdev.xyz/connector/callback.
CONNECTOR_REDIRECT_URI: z.string().url().default('https://www.autonomous.ai/connector/callback'),
diff --git a/backend/src/connectorAppsImport.ts b/backend/src/connectorAppsImport.ts
new file mode 100644
index 000000000..7c3cf1d10
--- /dev/null
+++ b/backend/src/connectorAppsImport.ts
@@ -0,0 +1,24 @@
+/**
+ * `npm run connectors:import <config-connector-auth.json>`: write the connector gateway's OAuth apps
+ * into MongoDB (`connector_apps`), with the backend's DATABASE_URL. Only the `auth_type: app` entries are
+ * written; others sign in from the computer. A convenience: the rows can as well be edited in Compass.
+ * Prints the codes written, never a secret.
+ */
+import { readFileSync } from 'fs'
+import { importApps } from './lib/connectorGateway.js'
+import { prisma } from './lib/prisma.js'
+
+const file = process.argv[2]
+if (!file) {
+ console.error('Usage: npm run connectors:import <config-connector-auth.json>')
+ process.exit(2)
+}
+try {
+ const codes = await importApps(readFileSync(file, 'utf8'))
+ console.log(`connector apps written: ${codes.join(', ') || 'none (no auth_type: app entry)'}`)
+} catch (error) {
+ console.error(`connector apps not written: ${(error as Error).message}`)
+ process.exitCode = 1
+} finally {
+ await prisma.$disconnect()
+}
diff --git a/backend/src/lib/connectorGateway.ts b/backend/src/lib/connectorGateway.ts
index 4e7714afb..7d3aef448 100644
--- a/backend/src/lib/connectorGateway.ts
+++ b/backend/src/lib/connectorGateway.ts
@@ -11,16 +11,16 @@
* computer → POST /api/connectors/refresh (signed in) {connector} → a new token
*
* As the Grid control plane does it (autonomous-grid-be grid_networks/connectors.py), with the same
- * requests and answers: the account's tokens are kept here (ConnectorCredential, sealed with
- * CONNECTOR_ENCRYPTION_KEY), so any of its computers renews them by naming the service, and
- * disconnecting forgets them here. A sign-in in progress (its state, PKCE verifier and, once exchanged,
- * its one-time result for the computer) lives in Redis for ten minutes, under hashed keys.
+ * requests and answers: the account's tokens are kept here (ConnectorCredential), so any of its
+ * computers renews them by naming the service, and disconnecting forgets them here. A sign-in in
+ * progress (its state, PKCE verifier and, once exchanged, its one-time result for the computer) lives in
+ * Redis for ten minutes, under hashed keys.
*
- * The apps' config is the Grid control plane's `config-connector-auth.json` shape, given as a file
- * (CONNECTOR_AUTH_FILE) or inline (CONNECTOR_AUTH_JSON): never in git.
+ * The apps' config lives in MongoDB (`connector_apps`), edited directly or written from a file in the
+ * Grid control plane's `config-connector-auth.json` shape by `npm run connectors:import <file>`. Like
+ * the tokens, it is kept as it is: this database is private.
*/
-import { createCipheriv, createDecipheriv, createHash, randomBytes, timingSafeEqual } from 'crypto'
-import { readFileSync } from 'fs'
+import { createHash, randomBytes, timingSafeEqual } from 'crypto'
import { pub } from './bus.js'
import { prisma } from './prisma.js'
import { env } from '../config/env.js'
@@ -92,60 +92,80 @@ export function parseConfig(raw: string): Record<string, ConnectorApp> {
return apps
}
-let cached: { apps: Record<string, ConnectorApp> } | null = null
+const CONFIG_CACHE_MS = 60_000
+let cached: { at: number, apps: Record<string, ConnectorApp> } | null = null
-/** The configured apps; none (the gateway off) when no config is given. Read once per process. */
-export function apps(): Record<string, ConnectorApp> {
- if (cached) return cached.apps
- let raw = env.CONNECTOR_AUTH_JSON
- if (!raw && env.CONNECTOR_AUTH_FILE) {
- try { raw = readFileSync(env.CONNECTOR_AUTH_FILE, 'utf8') } catch (error) {
- logger.error('connector gateway: cannot read CONNECTOR_AUTH_FILE', { error: String(error) })
- }
- }
- try { cached = { apps: raw ? parseConfig(raw) : {} } } catch (error) {
- logger.error('connector gateway: invalid connector config', { error: String(error) })
- cached = { apps: {} }
- }
- return cached.apps
+type AppRow = {
+ code: string, label: string, description: string, imageUrl: string, clientId: string, clientSecret: string,
+ authUrl: string, tokenUrl: string, refreshUrl: string, userinfoUrl: string, scopes: string[], authStyle: string,
+ pkce: boolean, refresh: boolean, authParams: unknown, extra: unknown,
}
-/** For tests: forget the parsed config. */
-export function resetConfig(): void { cached = null }
+const record = (value: unknown): Record<string, unknown> => (value && typeof value === 'object' && !Array.isArray(value) ? value as Record<string, unknown> : {})
-function app(code: unknown): ConnectorApp {
- const found = typeof code === 'string' ? apps()[code] : undefined
- if (!found) throw new GatewayError('This service does not sign in through Harness.', 'UNKNOWN_CONNECTOR', 404)
- return found
+function fromRow(row: AppRow): ConnectorApp {
+ const extra = record(row.extra)
+ return {
+ code: row.code, label: row.label || row.code, description: row.description, imageUrl: row.imageUrl,
+ clientId: row.clientId, clientSecret: row.clientSecret,
+ authUrl: row.authUrl, tokenUrl: row.tokenUrl, refreshUrl: row.refreshUrl, userinfoUrl: row.userinfoUrl,
+ scopes: row.scopes, authStyle: row.authStyle, pkce: row.pkce, refresh: row.refresh,
+ authParams: Object.fromEntries(Object.entries(record(row.authParams)).map(([k, v]) => [k, str(v)])),
+ tokenField: str(extra.token_field), mcpUrl: str(extra.mcp_url), mcpAuthHeader: str(extra.mcp_auth_header),
+ }
}
-/** AES-256-GCM, version.iv.tag.ciphertext, as lib/machineCredential.ts seals, under its own key. */
-function sealKey(): Buffer {
- const value = Buffer.from(env.CONNECTOR_ENCRYPTION_KEY ?? '', 'base64')
- if (value.length !== 32) throw new GatewayError('Connections through Harness are not set up on this server.', 'GATEWAY_NOT_CONFIGURED', 503)
- return value
+/**
+ * The services this gateway signs in to: the enabled rows of `connector_apps`, read a minute at a time
+ * (a row edited in Compass or written by `npm run connectors:import` is live within a minute, no deploy).
+ */
+export async function apps(): Promise<Record<string, ConnectorApp>> {
+ if (cached && Date.now() - cached.at < CONFIG_CACHE_MS) return cached.apps
+ const rows = await prisma.connectorApp.findMany({ where: { enabled: true } }) as AppRow[]
+ const loaded: Record<string, ConnectorApp> = {}
+ for (const row of rows) loaded[row.code] = fromRow(row)
+ cached = { at: Date.now(), apps: loaded }
+ return loaded
}
-export function seal(value: unknown): string {
- const iv = randomBytes(12)
- const cipher = createCipheriv('aes-256-gcm', sealKey(), iv)
- const body = Buffer.concat([cipher.update(JSON.stringify(value), 'utf8'), cipher.final()])
- return ['v1', iv.toString('base64url'), cipher.getAuthTag().toString('base64url'), body.toString('base64url')].join('.')
+/** Forget the cached apps: after an import, and in tests. */
+export function resetConfig(): void { cached = null }
+
+/**
+ * Write a config-connector-auth.json document's `app` entries into `connector_apps`. Replaces a service
+ * already there; leaves the others. Returns the codes written.
+ */
+export async function importApps(raw: string): Promise<string[]> {
+ const parsed = parseConfig(raw)
+ const document = record(JSON.parse(raw))
+ for (const [code, config] of Object.entries(parsed)) {
+ const entry = record(record(document.connectors)[code])
+ const fields = {
+ enabled: true, label: config.label, description: config.description, imageUrl: config.imageUrl,
+ clientId: config.clientId, clientSecret: config.clientSecret,
+ authUrl: config.authUrl, tokenUrl: config.tokenUrl, refreshUrl: config.refreshUrl, userinfoUrl: config.userinfoUrl,
+ scopes: config.scopes, authStyle: config.authStyle, pkce: config.pkce, refresh: config.refresh,
+ authParams: config.authParams,
+ extra: { ...record(entry.extra), ...(config.tokenField ? { token_field: config.tokenField } : {}),
+ ...(config.mcpUrl ? { mcp_url: config.mcpUrl } : {}), ...(config.mcpAuthHeader ? { mcp_auth_header: config.mcpAuthHeader } : {}) },
+ }
+ await prisma.connectorApp.upsert({ where: { code }, create: { code, ...fields }, update: fields })
+ }
+ resetConfig()
+ return Object.keys(parsed).sort()
}
-export function unseal(envelope: string): Record<string, unknown> {
- const [version, iv, tag, body, extra] = envelope.split('.')
- if (version !== 'v1' || !iv || !tag || !body || extra) throw new Error('Invalid connector credential')
- const decipher = createDecipheriv('aes-256-gcm', sealKey(), Buffer.from(iv, 'base64url'))
- decipher.setAuthTag(Buffer.from(tag, 'base64url'))
- return JSON.parse(Buffer.concat([decipher.update(Buffer.from(body, 'base64url')), decipher.final()]).toString('utf8'))
+async function app(code: unknown): Promise<ConnectorApp> {
+ const found = typeof code === 'string' ? (await apps())[code] : undefined
+ if (!found) throw new GatewayError('This service does not sign in through Harness.', 'UNKNOWN_CONNECTOR', 404)
+ return found
}
/** What the account's Connectors page lists for these services, with its own state. Never a secret. */
export async function list(userId: string): Promise<Record<string, unknown>[]> {
const rows = await prisma.connectorCredential.findMany({ where: { userId }, select: { connector: true, accountName: true, expiresAt: true, connectedAt: true } })
const mine = new Map(rows.map(row => [row.connector, row]))
- return Object.values(apps()).map(a => {
+ return Object.values(await apps()).map(a => {
const row = mine.get(a.code)
return {
code: a.code, label: a.label, description: a.description, image_url: a.imageUrl,
@@ -157,8 +177,10 @@ export async function list(userId: string): Promise<Record<string, unknown>[]> {
}
async function keep(userId: string, connector: string, token: Record<string, unknown>): Promise<void> {
- const sealed = seal({ access_token: token.access_token, refresh_token: token.refresh_token, token_type: token.token_type, scope: token.scope, expires_at: token.expires_at })
- const fields = { sealed, accountName: str(token.account_name), expiresAt: Number(token.expires_at) || 0 }
+ const fields = {
+ accessToken: str(token.access_token), refreshToken: str(token.refresh_token), tokenType: str(token.token_type) || 'Bearer',
+ scope: str(token.scope), accountName: str(token.account_name), expiresAt: Number(token.expires_at) || 0,
+ }
await prisma.connectorCredential.upsert({ where: { userId_connector: { userId, connector } }, create: { userId, connector, ...fields }, update: fields })
}
@@ -170,8 +192,7 @@ interface FlowState { userId: string, connector: string, pickup: string, verifie
interface PickupRecord { userId: string, connector: string, status: 'pending' | 'ready' | 'failed', error?: string, token?: Record<string, unknown> }
export async function start(userId: string, connector: unknown): Promise<Record<string, unknown>> {
- const config = app(connector)
- sealKey() // A sign-in this server could not keep is not started.
+ const config = await app(connector)
const state = STATE_PREFIX + randomBytes(24).toString('base64url')
const pickup = randomBytes(24).toString('base64url')
const verifier = config.pkce ? randomBytes(48).toString('base64url') : ''
@@ -270,7 +291,7 @@ export async function callback(body: { code?: unknown, state?: unknown, error?:
const raw = taken?.[0]?.[1] as string | null | undefined
if (!raw) throw new GatewayError('This sign-in has ended. Start it again from Harness.', 'STATE_EXPIRED', 410)
const flow = JSON.parse(raw) as FlowState
- const config = app(flow.connector)
+ const config = await app(flow.connector)
const save = (record: PickupRecord) => pub.set(pickupKey(flow.pickup), JSON.stringify(record), 'EX', TTL_SEC)
if (body.error || !str(body.code)) {
await save({ userId: flow.userId, connector: config.code, status: 'failed', error: str(body.error) === 'access_denied' ? 'The sign-in was cancelled.' : 'The service did not complete the sign-in.' })
@@ -313,13 +334,13 @@ export async function poll(userId: string, pickupCode: unknown): Promise<Record<
* expires; a revoked one asks for connecting again and leaves the row for the next sign-in to replace.
*/
export async function refresh(userId: string, connector: unknown): Promise<Record<string, unknown>> {
- const config = app(connector)
+ const config = await app(connector)
const row = await prisma.connectorCredential.findUnique({ where: { userId_connector: { userId, connector: config.code } } })
if (!row) throw new GatewayError('This service is not connected. Connect it again.', 'NOT_CONNECTED', 404)
- const held = unseal(row.sealed)
- const refreshToken = str(held.refresh_token)
+ const refreshToken = row.refreshToken
if (!refreshToken || !config.refresh) {
- return tokenFor(config, { ...held, expires_in: undefined }, refreshToken, row.accountName, Number(held.expires_at) || 0)
+ const held = { access_token: row.accessToken, token_type: row.tokenType, scope: row.scope }
+ return tokenFor(config, held, refreshToken, row.accountName, row.expiresAt)
}
const payload = await tokenRequest(config, { grant_type: 'refresh_token', refresh_token: refreshToken }, true)
const token = tokenFor(config, payload, refreshToken, row.accountName)
@@ -329,7 +350,7 @@ export async function refresh(userId: string, connector: unknown): Promise<Recor
/** Forget the account's sign-in here. Revoking access at the service is the service's own setting. */
export async function disconnect(userId: string, connector: unknown): Promise<Record<string, unknown>> {
- const config = app(connector)
+ const config = await app(connector)
const { count } = await prisma.connectorCredential.deleteMany({ where: { userId, connector: config.code } })
return { connector: config.code, disconnected: count > 0 }
}
diff --git a/backend/src/routes/connectors.test.ts b/backend/src/routes/connectors.test.ts
index 454e4952a..9475b1300 100644
--- a/backend/src/routes/connectors.test.ts
+++ b/backend/src/routes/connectors.test.ts
@@ -26,7 +26,7 @@ const mocks = vi.hoisted(() => {
}),
}
// The account's kept sign-ins (ConnectorCredential), by userId + connector.
- type Row = { userId: string, connector: string, sealed: string, accountName: string, expiresAt: number, connectedAt: Date }
+ type Row = { userId: string, connector: string, accessToken: string, refreshToken: string, tokenType: string, scope: string, accountName: string, expiresAt: number, connectedAt: Date }
const rows = new Map<string, Row>()
const id = (where: { userId_connector: { userId: string, connector: string } }) => `${where.userId_connector.userId}/${where.userId_connector.connector}`
const connectorCredential = {
@@ -40,15 +40,23 @@ const mocks = vi.hoisted(() => {
const had = rows.delete(`${where.userId}/${where.connector}`); return { count: had ? 1 : 0 }
}),
}
- return { store, pub, rows, prisma: { connectorCredential }, auth: vi.fn() }
+ // connector_apps, by code.
+ const appRows = new Map<string, Record<string, unknown>>()
+ const connectorApp = {
+ findMany: vi.fn(async ({ where }: { where: { enabled: boolean } }) => [...appRows.values()].filter(row => row.enabled === where.enabled)),
+ upsert: vi.fn(async ({ where, create, update }: { where: { code: string }, create: Record<string, unknown>, update: Record<string, unknown> }) => {
+ const row = appRows.has(where.code) ? { ...appRows.get(where.code)!, ...update } : create
+ appRows.set(where.code, row); return row
+ }),
+ }
+ return { store, pub, rows, appRows, prisma: { connectorCredential, connectorApp }, auth: vi.fn() }
})
vi.mock('../lib/bus.js', () => ({ pub: mocks.pub }))
vi.mock('../lib/prisma.js', () => ({ prisma: mocks.prisma }))
vi.mock('../lib/ssoAuth.js', async original => ({ ...await original<typeof import('../lib/ssoAuth.js')>(), authenticateAccessToken: mocks.auth }))
-vi.mock('../config/env.js', () => {
- return { env: { NODE_ENV: 'test', CONNECTOR_REDIRECT_URI: 'https://www.autonomous.ai/connector/callback', CONNECTOR_AUTH_FILE: undefined,
- CONNECTOR_ENCRYPTION_KEY: Buffer.alloc(32, 7).toString('base64'),
- CONNECTOR_AUTH_JSON: JSON.stringify({ connectors: {
+vi.mock('../config/env.js', () => ({ env: { NODE_ENV: 'test', CONNECTOR_REDIRECT_URI: 'https://www.autonomous.ai/connector/callback' } }))
+/** The apps, in the Grid config-connector-auth.json shape `npm run connectors:import` takes. */
+const CONFIG = JSON.stringify({ connectors: {
github: { auth_type: 'app', label: 'GitHub', client_id: 'gh-client', client_secret: 'gh-secret', auth_url: 'https://github.com/login/oauth/authorize',
token_url: 'https://github.com/login/oauth/access_token', scopes: ['repo', 'read:user'], userinfo_url: 'https://api.github.com/user', mcp_url: 'https://api.githubcopilot.com/mcp/' },
gmail: { auth_type: 'app', label: 'Gmail', client_id: 'g-client', client_secret: 'g-secret', auth_url: 'https://accounts.google.com/o/oauth2/v2/auth',
@@ -58,10 +66,9 @@ vi.mock('../config/env.js', () => {
slack: { auth_type: 'app', client_id: 's', client_secret: 's-secret', auth_url: 'https://slack.com/oauth/v2/authorize', token_url: 'https://slack.com/api/oauth.v2.access',
extra: { token_field: 'authed_user.access_token', mcp_url: 'https://mcp.slack.com/mcp' } },
linear: { auth_type: 'dcr', mcp_url: 'https://mcp.linear.app/mcp' },
- } }) } }
-})
+} })
import { connectorRoutes } from './connectors.js'
-import { resetConfig } from '../lib/connectorGateway.js'
+import { importApps, resetConfig } from '../lib/connectorGateway.js'
import { registerAuthMiddleware } from '../middlewares/authMiddleware.js'
import { errorHandler } from '../middlewares/errorHandler.js'
@@ -74,7 +81,8 @@ describe('the connector gateway', () => {
const owner = { authorization: 'Bearer owner' }
beforeEach(async () => {
- vi.clearAllMocks(); mocks.store.clear(); mocks.rows.clear(); resetConfig(); fetched = []
+ vi.clearAllMocks(); mocks.store.clear(); mocks.rows.clear(); mocks.appRows.clear(); resetConfig(); fetched = []
+ expect(await importApps(CONFIG)).toEqual(['github', 'gmail', 'notion', 'slack'])
mocks.auth.mockImplementation(async (token: string) => ({ sub: token === 'stranger' ? 'stranger' : 'owner', email: '[email protected]', role: 'user', autonomousEnv: 'prod' }))
answer = (url) => url.includes('/user')
? Response.json({ login: 'octo' })
@@ -118,12 +126,9 @@ describe('the connector gateway', () => {
mcp_entry: { url: 'https://api.githubcopilot.com/mcp/', headers: { Authorization: 'Bearer gho-1' } } })
expect(JSON.stringify(ready)).not.toContain('gh-secret')
expect((await post('/api/connectors/poll', { pickup_code: started.pickup_code })).json().data.status).toBe('expired')
- // Nothing of the sign-in is left in Redis; the account's sign-in is kept, sealed.
+ // Nothing of the sign-in is left in Redis; the account's sign-in is kept for renewing.
expect(mocks.store.size).toBe(0)
- const kept = mocks.rows.get('owner/github')!
- expect(kept.accountName).toBe('octo')
- expect(kept.sealed.startsWith('v1.')).toBe(true)
- expect(kept.sealed).not.toContain('gho-1')
+ expect(mocks.rows.get('owner/github')).toMatchObject({ accountName: 'octo', accessToken: 'gho-1', tokenType: 'bearer' })
const listed = (await app.inject({ method: 'GET', url: '/api/connectors', headers: owner })).json().data.connectors
expect(listed.find((row: { code: string }) => row.code === 'github')).toMatchObject({ status: 'connected', account_name: 'octo' })
const theirs = (await app.inject({ method: 'GET', url: '/api/connectors', headers: { authorization: 'Bearer stranger' } })).json().data.connectors
@@ -181,6 +186,15 @@ describe('the connector gateway', () => {
expect(ready).toMatchObject({ access_token: 'xoxp-user', mcp_entry: { url: 'https://mcp.slack.com/mcp', headers: { Authorization: 'Bearer xoxp-user' } } })
})
+ it('keeps the apps in connector_apps; a disabled one is not served', async () => {
+ const github = mocks.appRows.get('github')!
+ expect(github).toMatchObject({ clientId: 'gh-client', clientSecret: 'gh-secret', enabled: true })
+ expect(mocks.appRows.has('linear')).toBe(false)
+ expect(mocks.appRows.get('slack')!.extra).toEqual({ token_field: 'authed_user.access_token', mcp_url: 'https://mcp.slack.com/mcp' })
+ mocks.appRows.set('github', { ...github, enabled: false }); resetConfig()
+ expect((await post('/api/connectors/start', { connector: 'github' })).statusCode).toBe(404)
+ })
+
it('stores only hashes of the state and the pickup code', async () => {
const started = (await post('/api/connectors/start', { connector: 'github' })).json().data
const state = new URL(started.authorize_url).searchParams.get('state')!