Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,19 @@ FRED_API_KEY=
NEWSAPI_KEY=
METACULUS_API_KEY=

# --- Connect: OAuth sign-in (OPTIONAL) ---
# Lets a block that reads your own Google data (Calendar, Drive, YouTube)
# sign in with read-only access. Register an OAuth client of type "Web
# application" in Google Cloud and add the redirect URI
# <your origin>/api/oauth/google/callback
# (http://127.0.0.1:3000/api/oauth/google/callback for `npm run dev`).
Comment thread
sykosyber marked this conversation as resolved.
Resolved
# Server-side only; the secret never reaches the browser. The access token
# goes to the page that asked, for that session. Off in the public preview.
# OMNI_OAUTH_ORIGIN pins the redirect origin when a proxy hides it.
OMNI_GOOGLE_CLIENT_ID=
OMNI_GOOGLE_CLIENT_SECRET=
OMNI_OAUTH_ORIGIN=

# --- Inference ledger (OPTIONAL) ---
# Durable server-side record of LLM executions. Postgres owns ONLY this;
# canvas state stays in IndexedDB. Leave DATABASE_URL blank and the ledger
Expand Down
4 changes: 4 additions & 0 deletions APEX_PLAN.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,7 @@ this order:
8. **Connect.** OAuth sign-in for providers that require it, Google first
(504 directory APIs are blocked only by OAuth). Needs an OAuth client the
owner registers.
Status: implemented and tested with stubs, for Google, read operations
with read-only scopes only. No live sign-in has been run (no client is
registered). The API index still counts Google APIs as unusable until it
is rebuilt (`npm run catalog:build`).
20 changes: 20 additions & 0 deletions DEPLOYMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,26 @@ and the 5-minute shared cache are per process, and a caller is the address
`clientKey()` sees. The public preview runs no broker, so it uses no house key;
turning one on there is an owner decision under this checklist.

### Connect (OAuth sign-in)

`OMNI_GOOGLE_CLIENT_ID` and `OMNI_GOOGLE_CLIENT_SECRET` turn on "Sign in with
Google" for blocks that read the person's own Google data
(`src/core/services/server/oauthConnect.ts`). To set it up:

1. In Google Cloud, enable each API you want (Calendar, Drive, YouTube Data).
2. Configure the OAuth consent screen. While it is in Testing, only the test
users you list can sign in, and Google shows them an unverified-app notice.
3. Create an OAuth client of type "Web application" with the authorized
redirect URI `<origin>/api/oauth/google/callback`. Set `OMNI_OAUTH_ORIGIN`
when the server cannot see its public origin.

Flyspace only asks for scopes ending in `.readonly`, checks that the person
granted them, requests no refresh token, and stores nothing on the server. The
access token goes to the page that started the sign-in and lasts that session
(about an hour); the device vault never keeps it. The public preview has no
sign-in. Anyone who can reach a deployment with a client configured can sign
in to their own Google account through it, so keep it on a private network.

Hosted inference also requires `DATABASE_URL` and migration 003. Each paid
request carries `Idempotency-Key`; the ledger stores owner, key, and digest
with the `running` attempt before provider dispatch. Reusing a key with the
Expand Down
2 changes: 2 additions & 0 deletions src/app/CitadelApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { useMindShellSync, useShellNavigation } from '@/core/hooks';
import { VoiceControl } from '@/components/voice/VoiceControl';
import { startKeyVault } from '@/core/capabilities/keyVault';
import { loadHouseKeys } from '@/core/capabilities/houseKeys';
import { loadConnectProviders } from '@/core/capabilities/connect';

export default function CitadelApp() {
// The removed API Dashboard stored client-side keys under this key; drop any that remain.
Expand All @@ -34,6 +35,7 @@ export default function CitadelApp() {
// Which curated APIs this server answers on its own key, if the owner set any.
useEffect(() => {
void loadHouseKeys();
void loadConnectProviders();
}, []);

const { activeTool, selection, captureSelection, clearSelection } = useToolStore();
Expand Down
35 changes: 35 additions & 0 deletions src/app/api/oauth/[provider]/callback/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// GET /api/oauth/:provider/callback
// Where the provider returns the browser (oauthConnect.ts). Exchanges the
// code with the owner's client secret, checks the granted scopes, and hands
// the token to the page that started the sign-in. Nothing is stored or logged.

import { NextRequest, NextResponse } from 'next/server';
import { callbackPage, CONNECT_COOKIE, CONNECT_COOKIE_PATH, finishConnect } from '@/core/services/server/oauthConnect';
import { redirectUriFor } from '../redirectUri';

export const runtime = 'nodejs';
export const dynamic = 'force-dynamic';

export async function GET(request: NextRequest, context: { params: Promise<{ provider: string }> }) {
const { provider } = await context.params;
const result = await finishConnect({
providerId: provider,
query: new URL(request.url).searchParams,
cookie: request.cookies.get(CONNECT_COOKIE)?.value,
redirectUri: redirectUriFor(request, provider)
});
const page = callbackPage(result);
const response = new NextResponse(page.html, {
status: result.ok ? 200 : 400,
headers: {
'content-type': 'text/html; charset=utf-8',
'content-security-policy': page.csp,
'cache-control': 'no-store',
'referrer-policy': 'no-referrer',
'x-content-type-options': 'nosniff'
}
});
// One flow per cookie: spent whether it worked or not.
response.cookies.set(CONNECT_COOKIE, '', { httpOnly: true, sameSite: 'lax', path: CONNECT_COOKIE_PATH, maxAge: 0 });
return response;
}
23 changes: 23 additions & 0 deletions src/app/api/oauth/[provider]/redirectUri.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { afterEach, describe, expect, it } from 'vitest';
import { NextRequest } from 'next/server';
import { redirectUriFor } from './redirectUri';

const saved = process.env.OMNI_OAUTH_ORIGIN;
afterEach(() => {
if (saved === undefined) delete process.env.OMNI_OAUTH_ORIGIN;
else process.env.OMNI_OAUTH_ORIGIN = saved;
});

describe('the OAuth redirect URI', () => {
it('stays on the host the browser used, where the flow cookie lives', () => {
delete process.env.OMNI_OAUTH_ORIGIN;
const request = new NextRequest('http://localhost:3000/api/oauth/google/start', { headers: { host: '127.0.0.1:3000' } });
expect(redirectUriFor(request, 'google')).toBe('http://127.0.0.1:3000/api/oauth/google/callback');
});

it('uses the pinned origin when one is set', () => {
process.env.OMNI_OAUTH_ORIGIN = 'https://flyspace.internal.example/';
const request = new NextRequest('http://10.0.0.5:3000/api/oauth/google/start', { headers: { host: '10.0.0.5:3000' } });
expect(redirectUriFor(request, 'google')).toBe('https://flyspace.internal.example/api/oauth/google/callback');
});
});
18 changes: 18 additions & 0 deletions src/app/api/oauth/[provider]/redirectUri.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { NextRequest } from 'next/server';

/**
* The callback URL registered with the provider. It must be on the origin the
* browser started from, or the flow cookie set at start is not sent back.
* OMNI_OAUTH_ORIGIN pins it (behind a proxy that rewrites Host); otherwise it
* is the Host the browser sent, which can differ from the server's own idea
* of its URL (`next start` reports localhost for 127.0.0.1). The provider
* refuses any URL the owner did not register, so a forged Host header cannot
* send a code elsewhere.
*/
export function redirectUriFor(request: NextRequest, provider: string): string {
const pinned = process.env.OMNI_OAUTH_ORIGIN?.trim();
const url = new URL(request.url);
const host = request.headers.get('host');
const origin = pinned || (host ? `${url.protocol}//${host}` : url.origin);
return `${origin.replace(/\/+$/, '')}/api/oauth/${encodeURIComponent(provider)}/callback`;
}
39 changes: 39 additions & 0 deletions src/app/api/oauth/[provider]/start/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// GET /api/oauth/:provider/start?scope=...&scope=...&nonce=...
// Starts a read-only sign-in (oauthConnect.ts): a redirect to the provider,
// and an httpOnly cookie that binds the flow to this browser.

import { NextRequest, NextResponse } from 'next/server';
import { CONNECT_COOKIE, CONNECT_COOKIE_PATH, startConnect } from '@/core/services/server/oauthConnect';
import { redirectUriFor } from '../redirectUri';

export const runtime = 'nodejs';
export const dynamic = 'force-dynamic';

export async function GET(request: NextRequest, context: { params: Promise<{ provider: string }> }) {
// A top-level navigation from this app is same-origin; another site has no reason to start one.
if (request.headers.get('sec-fetch-site') === 'cross-site') {
return NextResponse.json({ error: 'Sign-in must start from this site.' }, { status: 403 });
}
const { provider } = await context.params;
const url = new URL(request.url);
const started = startConnect({
providerId: provider,
scopes: url.searchParams.getAll('scope'),
nonce: url.searchParams.get('nonce') ?? '',
redirectUri: redirectUriFor(request, provider)
});
if (!started.ok) {
return new NextResponse(started.error, { status: started.status, headers: { 'content-type': 'text/plain; charset=utf-8', 'cache-control': 'no-store' } });
}
const response = NextResponse.redirect(started.redirect, 302);
response.headers.set('cache-control', 'no-store');
response.headers.set('referrer-policy', 'no-referrer');
response.cookies.set(CONNECT_COOKIE, started.cookie, {
httpOnly: true,
sameSite: 'lax',
secure: url.protocol === 'https:',
path: CONNECT_COOKIE_PATH,
maxAge: 600
});
return response;
}
13 changes: 13 additions & 0 deletions src/app/api/oauth/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// GET /api/oauth
// Which sign-in providers this server has an OAuth client for (connectProviders.ts).
// Names only. Empty in the public preview.

import { NextResponse } from 'next/server';
import { configuredProviders } from '@/core/services/server/oauthConnect';

export const runtime = 'nodejs';
export const dynamic = 'force-dynamic';

export async function GET() {
return NextResponse.json({ providers: configuredProviders() }, { headers: { 'cache-control': 'no-store' } });
}
22 changes: 19 additions & 3 deletions src/blocks/capability/CapabilityBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type { OmniItem } from '@/core/gateway';
import { EffectPill } from '@/components/apis/ApiReview';
import { KeyField, keySlotOf, useHasKey, useHouseKeyCovers } from '@/components/apis/KeyField';
import { useRememberingKeys } from '@/components/apis/RememberKeys';
import { SignInPrompt, useSignIn } from '@/components/apis/SignIn';
import { argumentsFrom, CapabilityInputs, constantOf, inputSummaryOf, missingInputs } from './CapabilityInputs';
import { CapabilityResult } from './CapabilityResult';

Expand Down Expand Up @@ -92,6 +93,10 @@ export function CapabilityBlockView({ instanceId }: { instanceId: string }) {
const onHouseKey = houseKey && keySlot !== null && !hasKey;
// Keys live for the session, so after a reload a keyed block asks again.
const needsKey = keySlot !== null && !hasKey && !houseKey;
// An oauth API needs a sign-in instead (connect.ts), for this session.
const signIn = useSignIn(manifest);
const needsSignIn = signIn !== null && !signIn.signedIn;
const needsCredential = needsKey || needsSignIn;

const sideEffect = manifest?.effect === 'write' || manifest?.effect === 'destructive';
const params = isPlainParams(block?.params) ? block.params : {};
Expand All @@ -101,7 +106,7 @@ export function CapabilityBlockView({ instanceId }: { instanceId: string }) {
const trigger = manifest?.trigger ?? { kind: 'manual' as const };

const run = (idempotencyKey?: string, confirmedRun?: string) => {
if (!manifest || running || needsKey) return;
if (!manifest || running || needsCredential) return;
if (sideEffect && (manifest.approval !== 'approved' || !confirmedRun)) return;
const key = idempotencyKey ?? attemptKey.current ?? `click_${newId()}`;
attemptKey.current = key;
Expand Down Expand Up @@ -198,10 +203,16 @@ export function CapabilityBlockView({ instanceId }: { instanceId: string }) {
) : null}

{/* What you look at after Run comes first: the error or the result. */}
{block?.error && !(needsKey && isUnboundKeyError(block.error)) ? (
{needsSignIn && manifest && signIn ? <SignInPrompt manifest={manifest} signIn={signIn} /> : null}

{block?.error && !(needsCredential && isUnboundKeyError(block.error)) ? (
<p role="alert" className="rounded-md border border-[var(--truth-red)]/40 px-2 py-1 text-xs text-[var(--truth-red)] [overflow-wrap:anywhere]">
{block.error}
{errorHint(block.error) ? <span className="block text-[var(--text-muted)]">{errorHint(block.error)}</span> : null}
{signIn?.signedIn && isRefusal(block.error) && !changingKey ? (
<button type="button" onClick={() => setChangingKey(true)}
className="mt-1 block text-[11px] text-[var(--text-secondary)] underline">Sign in again</button>
) : null}
{keySlot && hasKey && isRefusal(block.error) && !changingKey ? (
<button type="button" onClick={() => setChangingKey(true)}
className="mt-1 block text-[11px] text-[var(--text-secondary)] underline">Change key</button>
Expand All @@ -214,6 +225,9 @@ export function CapabilityBlockView({ instanceId }: { instanceId: string }) {
<button type="button" onClick={() => setChangingKey(true)} className="underline">Use my own key</button>
</p>
) : null}
{changingKey && manifest && signIn?.signedIn ? (
<SignInPrompt manifest={manifest} signIn={signIn} again onDone={() => setChangingKey(false)} />
) : null}
{changingKey && manifest && (hasKey || onHouseKey) ? (
<KeyField manifest={manifest} compact autoFocus onSaved={() => setChangingKey(false)} onCancel={() => setChangingKey(false)} />
) : null}
Expand Down Expand Up @@ -294,7 +308,7 @@ export function CapabilityBlockView({ instanceId }: { instanceId: string }) {
<button
type="button"
onClick={requestRun}
disabled={!manifest || running || blocked || needsKey || missing.length > 0}
disabled={!manifest || running || blocked || needsCredential || missing.length > 0}
className="shrink-0 rounded-md bg-[var(--citadel-primary)] px-2 py-1.5 text-xs font-medium text-white hover:opacity-90 disabled:cursor-not-allowed disabled:bg-[var(--citadel-elevated)] disabled:text-[var(--text-muted)]"
>
{running
Expand All @@ -303,6 +317,8 @@ export function CapabilityBlockView({ instanceId }: { instanceId: string }) {
? 'Needs approval'
: needsKey
? 'Enter the key'
: needsSignIn
? 'Sign in first'
: missing.length > 0
? `Enter ${missing.map(input => input.name).join(', ')}`
: 'Run'}
Expand Down
73 changes: 73 additions & 0 deletions src/blocks/capability/SignIn.component.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// @vitest-environment happy-dom
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { act, fireEvent, render, screen } from '@testing-library/react';
import { CapabilityBlockView } from './CapabilityBlock';
import { setConnectProvidersForTests } from '@/core/capabilities/connect';
import type { CapabilityManifest } from '@/core/capabilities/manifest';
import { compileOpenApi } from '@/core/capabilities/openapi';
import { clearCapabilities, installProposal } from '@/core/capabilities/registry';
import { capabilitySecrets } from '@/core/capabilities/secrets';
import { blockRegistry } from '@/core/registry/BlockRegistry';
import { useBlockStore, useWireStore } from '@/core/stores';

const READ = 'https://www.googleapis.com/auth/calendar.readonly';

function placeColors(): { manifest: CapabilityManifest; instanceId: string } {
const [manifest] = compileOpenApi({
openapi: '3.0.0',
info: { title: 'Calendar', version: 'v3' },
servers: [{ url: 'https://www.googleapis.com/calendar/v3' }],
components: { securitySchemes: { Oauth2c: { type: 'oauth2', flows: { authorizationCode: { authorizationUrl: 'https://accounts.google.com/o/oauth2/auth', tokenUrl: 'https://oauth2.googleapis.com/token', scopes: { [READ]: 'read' } } } } } },
paths: { '/colors': { get: { operationId: 'calendar.colors.get', summary: 'Calendar colors', security: [{ Oauth2c: [READ] }], responses: { '200': { description: 'ok', content: { 'application/json': { schema: { type: 'object', properties: { kind: { type: 'string' } } } } } } } } } }
}).manifests;
expect(installProposal(manifest).ok).toBe(true);
const instanceId = useBlockStore.getState().addBlock(blockRegistry.get(manifest.id)!, { x: 0, y: 0 });
return { manifest, instanceId };
}

beforeEach(() => {
clearCapabilities();
capabilitySecrets.clear();
setConnectProvidersForTests([]);
useBlockStore.setState({ blocks: [], activeShellId: 'root' });
useWireStore.setState({ wires: [] });
});

afterEach(() => {
vi.unstubAllGlobals();
setConnectProvidersForTests([]);
clearCapabilities();
capabilitySecrets.clear();
useBlockStore.setState({ blocks: [] });
});

describe('a block that needs a sign-in', () => {
it('says the server has no sign-in set up, and does not run', () => {
const { instanceId } = placeColors();
render(<CapabilityBlockView instanceId={instanceId} />);
expect(screen.getByText('This API needs you to sign in to Google.')).toBeTruthy();
expect(screen.getByText(/Google sign-in is not set up on this server/)).toBeTruthy();
expect(screen.getByText(/Read-only access: calendar\.readonly/)).toBeTruthy();
expect((screen.getByRole('button', { name: 'Sign in first' }) as HTMLButtonElement).disabled).toBe(true);
});

it('opens the sign-in window from the click when the server has a Google client', () => {
const { instanceId } = placeColors();
setConnectProvidersForTests(['google']);
const open = vi.fn(() => ({}));
vi.stubGlobal('open', open);
render(<CapabilityBlockView instanceId={instanceId} />);
fireEvent.click(screen.getByRole('button', { name: 'Sign in with Google' }));
expect(open).toHaveBeenCalledTimes(1);
expect(String((open.mock.calls[0] as unknown[])[0])).toMatch(/^\/api\/oauth\/google\/start\?nonce=[0-9a-f]{48}&scope=/);
expect(screen.getByText('Waiting for the sign-in window…')).toBeTruthy();
});

it('runs once the session holds a token for its slot', () => {
const { manifest, instanceId } = placeColors();
render(<CapabilityBlockView instanceId={instanceId} />);
act(() => capabilitySecrets.set(manifest.auth.secretRef!, 'ya29.token'));
expect(screen.queryByText('This API needs you to sign in to Google.')).toBeNull();
expect((screen.getByRole('button', { name: 'Run' }) as HTMLButtonElement).disabled).toBe(false);
});
});
Loading
Loading