Skip to content

Personal AI, On Personal Devices

Python

fix: generic connector setup UI + non-Google OAuth credential registration (#820)

havolline1a235df6c0f83
diff --git a/frontend/src/lib/connectors-api.auth.test.ts b/frontend/src/lib/connectors-api.auth.test.ts
index b02961f8..42bfefe7 100644
--- a/frontend/src/lib/connectors-api.auth.test.ts
+++ b/frontend/src/lib/connectors-api.auth.test.ts
@@ -95,6 +95,51 @@ describe('connectors-api sends the Bearer auth header', () => {
expect(authHeaderSent()).toBe('Bearer sk-local-123');
});
+ it('disconnectSource surfaces a 409 with its actionable backend detail', async () => {
+ fetchMock.mockResolvedValue(
+ new Response(
+ JSON.stringify({
+ detail: "Sync for 'gmail' is still stopping; indexed content was not purged",
+ }),
+ {
+ status: 409,
+ headers: { 'Content-Type': 'application/json' },
+ },
+ ),
+ );
+ const { ConnectorApiError, disconnectSource } = await freshConnectorsApi();
+
+ await expect(disconnectSource('gmail')).rejects.toEqual(
+ expect.objectContaining({
+ name: ConnectorApiError.name,
+ status: 409,
+ message: expect.stringContaining('still stopping'),
+ }),
+ );
+ });
+
+ it('keeps a pending disconnect active and retries until cleanup succeeds', async () => {
+ fetchMock
+ .mockResolvedValueOnce(
+ new Response(JSON.stringify({ detail: 'Sync is still stopping' }), {
+ status: 409,
+ headers: { 'Content-Type': 'application/json' },
+ }),
+ )
+ .mockResolvedValueOnce(okJson({ status: 'disconnected' }));
+ const onPending = vi.fn();
+ const { disconnectSourceUntilComplete } = await freshConnectorsApi();
+
+ await disconnectSourceUntilComplete('gmail', {
+ retryDelayMs: 0,
+ onPending,
+ });
+
+ expect(onPending).toHaveBeenCalledOnce();
+ expect(onPending).toHaveBeenCalledWith('Sync is still stopping');
+ expect(fetchMock).toHaveBeenCalledTimes(2);
+ });
+
it('getSyncStatus', async () => {
fetchMock.mockResolvedValue(okJson({}));
const { getSyncStatus } = await freshConnectorsApi();
@@ -108,4 +153,32 @@ describe('connectors-api sends the Bearer auth header', () => {
await triggerSync('gmail');
expect(authHeaderSent()).toBe('Bearer sk-local-123');
});
+
+ it('OAuth polling waits for a real connected state before resolving', async () => {
+ vi.useFakeTimers();
+ const open = vi.fn();
+ vi.stubGlobal('window', { open });
+ try {
+ fetchMock
+ .mockResolvedValueOnce(okJson({ connected: false }))
+ .mockResolvedValueOnce(okJson({ connected: true }));
+ const { startServerOAuth } = await freshConnectorsApi();
+
+ let resolved = false;
+ const flow = startServerOAuth('spotify').then(() => {
+ resolved = true;
+ });
+ await vi.advanceTimersByTimeAsync(2000);
+ expect(resolved).toBe(false);
+ await vi.advanceTimersByTimeAsync(2000);
+ await flow;
+
+ expect(resolved).toBe(true);
+ expect(fetchMock).toHaveBeenCalledTimes(2);
+ expect(open).toHaveBeenCalledOnce();
+ } finally {
+ vi.useRealTimers();
+ vi.unstubAllGlobals();
+ }
+ });
});
diff --git a/frontend/src/lib/connectors-api.ts b/frontend/src/lib/connectors-api.ts
index 63eceb22..049d231d 100644
--- a/frontend/src/lib/connectors-api.ts
+++ b/frontend/src/lib/connectors-api.ts
@@ -65,11 +65,83 @@ export function startServerOAuth(id: string, oauthStartPath?: string): Promise<v
});
}
-export async function disconnectSource(id: string): Promise<void> {
+export class ConnectorApiError extends Error {
+ constructor(
+ message: string,
+ readonly status: number,
+ ) {
+ super(message);
+ this.name = 'ConnectorApiError';
+ }
+}
+
+export async function disconnectSource(
+ id: string,
+ signal?: AbortSignal,
+): Promise<void> {
const res = await apiFetch(`/v1/connectors/${encodeURIComponent(id)}/disconnect`, {
method: 'POST',
+ signal,
+ });
+ if (!res.ok) {
+ const err = await res.json().catch(() => ({ detail: res.statusText }));
+ throw new ConnectorApiError(
+ err.detail || `Failed to disconnect ${id}: ${res.status}`,
+ res.status,
+ );
+ }
+}
+
+export interface DisconnectUntilCompleteOptions {
+ signal?: AbortSignal;
+ retryDelayMs?: number;
+ onPending?: (message: string) => void;
+}
+
+function waitForDisconnectRetry(delayMs: number, signal?: AbortSignal): Promise<void> {
+ return new Promise((resolve, reject) => {
+ if (signal?.aborted) {
+ reject(new DOMException('Disconnect cancelled', 'AbortError'));
+ return;
+ }
+
+ const timer = setTimeout(() => {
+ signal?.removeEventListener('abort', handleAbort);
+ resolve();
+ }, delayMs);
+ const handleAbort = () => {
+ clearTimeout(timer);
+ reject(new DOMException('Disconnect cancelled', 'AbortError'));
+ };
+ signal?.addEventListener('abort', handleAbort, { once: true });
});
- if (!res.ok) throw new Error(`Failed to disconnect ${id}: ${res.status}`);
+}
+
+/**
+ * Finish a disconnect even when the backend first returns 409 while an active
+ * sync is stopping. The server deliberately keeps credentials and indexed
+ * data intact until that worker exits; retrying is what completes cleanup.
+ */
+export async function disconnectSourceUntilComplete(
+ id: string,
+ {
+ signal,
+ retryDelayMs = 1500,
+ onPending,
+ }: DisconnectUntilCompleteOptions = {},
+): Promise<void> {
+ while (true) {
+ try {
+ await disconnectSource(id, signal);
+ return;
+ } catch (err) {
+ if (!(err instanceof ConnectorApiError) || err.status !== 409) {
+ throw err;
+ }
+ onPending?.(err.message);
+ await waitForDisconnectRetry(retryDelayMs, signal);
+ }
+ }
}
export async function getSyncStatus(id: string): Promise<SyncStatus> {
diff --git a/frontend/src/lib/store.ts b/frontend/src/lib/store.ts
index f2c91f79..4f45b042 100644
--- a/frontend/src/lib/store.ts
+++ b/frontend/src/lib/store.ts
@@ -23,6 +23,7 @@ export interface CachedConnector {
display_name: string;
connected: boolean;
chunks: number;
+ auth_type: string;
}
export interface AgentEvent {
diff --git a/frontend/src/pages/DataSourcesPage.lifecycle.test.tsx b/frontend/src/pages/DataSourcesPage.lifecycle.test.tsx
new file mode 100644
index 00000000..84e50d55
--- /dev/null
+++ b/frontend/src/pages/DataSourcesPage.lifecycle.test.tsx
@@ -0,0 +1,45 @@
+import { renderToStaticMarkup } from 'react-dom/server';
+import { describe, expect, it, vi } from 'vitest';
+
+vi.mock('../lib/store', () => ({ useAppStore: vi.fn() }));
+
+import { SyncStatusDisplay } from './DataSourcesPage';
+import type { SyncStatus } from '../types/connectors';
+
+const baseStatus: SyncStatus = {
+ state: 'idle',
+ items_synced: 0,
+ items_total: 0,
+ last_sync: null,
+ error: null,
+};
+
+function renderStatus(sync: SyncStatus, disabled = false): string {
+ return renderToStaticMarkup(
+ <SyncStatusDisplay
+ chunks={0}
+ sync={sync}
+ unitLabel="items"
+ connectorId="obsidian"
+ disabled={disabled}
+ onSyncTriggered={vi.fn()}
+ />,
+ );
+}
+
+describe('connector lifecycle status', () => {
+ it('renders pending cleanup without a competing sync action', () => {
+ const html = renderStatus({ ...baseStatus, state: 'stopping' });
+
+ expect(html).toContain('Disconnect pending');
+ expect(html).toContain('cleaned up before this source disconnects');
+ expect(html).not.toContain('<button');
+ });
+
+ it('disables sync while another connector lifecycle action is active', () => {
+ const html = renderStatus(baseStatus, true);
+
+ expect(html).toContain('Sync Now');
+ expect(html).toMatch(/<button[^>]*disabled/);
+ });
+});
diff --git a/frontend/src/pages/DataSourcesPage.tsx b/frontend/src/pages/DataSourcesPage.tsx
index 0ed27349..147ee00a 100644
--- a/frontend/src/pages/DataSourcesPage.tsx
+++ b/frontend/src/pages/DataSourcesPage.tsx
@@ -23,9 +23,8 @@ import {
} from 'lucide-react';
import type { LucideIcon } from 'lucide-react';
import { SOURCE_CATALOG } from '../types/connectors';
-import type { ConnectRequest, ConnectorMeta } from '../types/connectors';
-import { listConnectors, connectSource, disconnectSource, getSyncStatus, triggerSync, startServerOAuth } from '../lib/connectors-api';
-import type { SyncStatus } from '../types/connectors';
+import type { ConnectRequest, ConnectorMeta, SyncStatus, OAuthSetupInfo } from '../types/connectors';
+import { listConnectors, connectSource, disconnectSourceUntilComplete, getConnector, getSyncStatus, triggerSync, startServerOAuth } from '../lib/connectors-api';
// ---------------------------------------------------------------------------
// Inline connect form (reused from AgentsPage pattern)
@@ -34,10 +33,12 @@ import type { SyncStatus } from '../types/connectors';
function InlineConnectForm({
fields,
loading,
+ disabled = false,
onSubmit,
}: {
fields: NonNullable<ConnectorMeta['inputFields']>;
loading: boolean;
+ disabled?: boolean;
onSubmit: (req: ConnectRequest) => void;
}) {
const [inputs, setInputs] = useState<Record<string, string>>(() =>
@@ -50,6 +51,7 @@ function InlineConnectForm({
const allFilled = fields
.filter((field) => field.required !== false)
.every((field) => inputs[field.name]?.trim());
+ const submitDisabled = loading || disabled || !allFilled;
const submit = () => {
const req: ConnectRequest = {};
@@ -63,6 +65,7 @@ function InlineConnectForm({
else if (f.name === 'security' && (inputs.security === 'tls' || inputs.security === 'starttls')) {
req.security = inputs.security;
}
+ else req.config = { ...(req.config || {}), [f.name]: inputs[f.name] };
}
if (req.email && req.password) {
req.token = `${req.email}:${req.password}`;
@@ -115,12 +118,13 @@ function InlineConnectForm({
))}
<button
onClick={submit}
- disabled={loading || !allFilled}
+ disabled={submitDisabled}
style={{
width: '100%', padding: 8,
- background: loading || !allFilled ? 'var(--color-disabled-bg)' : 'var(--color-accent-purple)',
+ background: submitDisabled ? 'var(--color-disabled-bg)' : 'var(--color-accent-purple)',
color: 'var(--color-on-accent)', border: 'none',
- borderRadius: 6, fontSize: 12, cursor: 'pointer',
+ borderRadius: 6, fontSize: 12,
+ cursor: submitDisabled ? 'default' : 'pointer',
}}
>
Connect
@@ -129,6 +133,185 @@ function InlineConnectForm({
);
}
+// ---------------------------------------------------------------------------
+// Generic fallback connect panel — for any backend connector that has no
+// hardcoded SOURCE_CATALOG entry (no `steps`). Without this, expanding such
+// a connector's "+ Add" card rendered nothing at all: no button, no input,
+// no explanation, just an empty box (e.g. Spotify, Strava, Oura, GitHub
+// Notifications, Google Tasks, Weather, and the Apple/News local sources).
+// Branches on auth_type using the same primitives the hardcoded catalog
+// entries already use (InlineConnectForm, oauth start+poll).
+// ---------------------------------------------------------------------------
+
+function GenericConnectPanel({
+ connectorId,
+ displayName,
+ authType,
+ loading,
+ disabled = false,
+ onConnect,
+ onOAuthStart,
+}: {
+ connectorId: string;
+ displayName: string;
+ authType: string;
+ loading: boolean;
+ disabled?: boolean;
+ onConnect: (req: ConnectRequest) => void;
+ onOAuthStart: () => void;
+}) {
+ const [oauthSetup, setOauthSetup] = useState<OAuthSetupInfo | null>(null);
+ const [feedUrls, setFeedUrls] = useState('');
+
+ useEffect(() => {
+ if (authType !== 'oauth') return;
+ let cancelled = false;
+ getConnector(connectorId)
+ .then((info) => {
+ if (!cancelled) setOauthSetup(info.oauth_setup ?? null);
+ })
+ .catch(() => {});
+ return () => {
+ cancelled = true;
+ };
+ }, [connectorId, authType]);
+
+ if (authType === 'local') {
+ if (connectorId === 'news_rss') {
+ const feeds = feedUrls
+ .split('\n')
+ .map((url) => url.trim())
+ .filter(Boolean)
+ .map((url) => ({ url }));
+ return (
+ <div>
+ <div style={{ fontSize: 12, color: 'var(--color-text-secondary)', marginBottom: 10 }}>
+ Add one RSS or Atom feed URL per line.
+ </div>
+ <textarea
+ value={feedUrls}
+ onChange={(event) => setFeedUrls(event.target.value)}
+ placeholder={'https://example.com/feed.xml\nhttps://example.org/rss'}
+ rows={4}
+ style={{ width: '100%', padding: '7px 10px', background: 'var(--color-bg)', border: '1px solid var(--color-border)', borderRadius: 4, color: 'var(--color-text)', fontSize: 12, marginBottom: 6, boxSizing: 'border-box' }}
+ />
+ <button
+ onClick={() => onConnect({ config: { feeds } })}
+ disabled={loading || disabled || feeds.length === 0}
+ style={{ width: '100%', padding: 8, background: loading || disabled || feeds.length === 0 ? 'var(--color-disabled-bg)' : 'var(--color-accent-purple)', color: 'var(--color-on-accent)', border: 'none', borderRadius: 6, fontSize: 12, cursor: loading || disabled || feeds.length === 0 ? 'default' : 'pointer' }}
+ >
+ {loading ? 'Connecting...' : 'Save feeds'}
+ </button>
+ </div>
+ );
+ }
+ return (
+ <div>
+ <div style={{ fontSize: 12, color: 'var(--color-text-secondary)', marginBottom: 10 }}>
+ {displayName} reads data directly from this device. Grant access if
+ prompted, then connect.
+ </div>
+ <button
+ onClick={() => onConnect({})}
+ disabled={loading || disabled}
+ style={{
+ width: '100%', padding: 8,
+ background: loading || disabled ? 'var(--color-disabled-bg)' : 'var(--color-accent-purple)',
+ color: 'var(--color-on-accent)', border: 'none',
+ borderRadius: 6, fontSize: 12,
+ cursor: loading || disabled ? 'default' : 'pointer',
+ }}
+ >
+ {loading ? 'Connecting...' : `Connect ${displayName}`}
+ </button>
+ </div>
+ );
+ }
+
+ if (authType === 'token') {
+ return (
+ <div>
+ <div style={{ fontSize: 12, color: 'var(--color-text-secondary)', marginBottom: 10 }}>
+ Enter your {displayName} API token.
+ </div>
+ <InlineConnectForm
+ fields={connectorId === 'weather'
+ ? [
+ { name: 'token', placeholder: 'OpenWeather API key', type: 'password' },
+ { name: 'location', placeholder: 'City, country (for example: Boston,US)', type: 'text' },
+ ]
+ : [{ name: 'token', placeholder: `${displayName} API token`, type: 'password' }]}
+ loading={loading}
+ disabled={disabled}
+ onSubmit={onConnect}
+ />
+ </div>
+ );
+ }
+
+ // auth_type === 'oauth' (default for anything else without a catalog entry)
+ if (oauthSetup?.has_credentials) {
+ return (
+ <div>
+ <div style={{ fontSize: 12, color: 'var(--color-text-secondary)', marginBottom: 10 }}>
+ OAuth app credentials are already configured. Continue directly to
+ {oauthSetup.provider ? ` ${oauthSetup.provider}` : ' the provider'} sign-in.
+ </div>
+ <button
+ onClick={onOAuthStart}
+ disabled={loading || disabled}
+ style={{
+ width: '100%', padding: 8,
+ background: loading || disabled ? 'var(--color-disabled-bg)' : 'var(--color-accent-purple)',
+ color: 'var(--color-on-accent)', border: 'none',
+ borderRadius: 6, fontSize: 12,
+ cursor: loading || disabled ? 'default' : 'pointer',
+ }}
+ >
+ {loading ? 'Connecting...' : `Continue with ${oauthSetup.provider || displayName}`}
+ </button>
+ </div>
+ );
+ }
+
+ return (
+ <div>
+ {oauthSetup && !oauthSetup.has_credentials && (
+ <div style={{
+ background: 'var(--color-bg)',
+ border: '1px solid var(--color-border)',
+ borderRadius: 6, padding: 10, marginBottom: 10,
+ }}>
+ <div style={{ color: 'var(--color-accent-purple)', fontSize: 10, fontWeight: 600, marginBottom: 3 }}>
+ SETUP REQUIRED
+ </div>
+ <div style={{ fontSize: 12, marginBottom: 6 }}>{oauthSetup.setup_hint}</div>
+ <a
+ href={oauthSetup.setup_url}
+ target="_blank"
+ rel="noopener noreferrer"
+ style={{ color: 'var(--color-accent)', fontSize: 11, textDecoration: 'underline' }}
+ >
+ Open developer dashboard &rarr;
+ </a>
+ </div>
+ )}
+ <div style={{ fontSize: 12, color: 'var(--color-text-secondary)', marginBottom: 10 }}>
+ Paste the Client ID and Client Secret from the app you created above.
+ </div>
+ <InlineConnectForm
+ fields={[
+ { name: 'email', placeholder: 'Client ID', type: 'text' },
+ { name: 'password', placeholder: 'Client Secret', type: 'password' },
+ ]}
+ loading={loading}
+ disabled={disabled}
+ onSubmit={onConnect}
+ />
+ </div>
+ );
+}
+
// ---------------------------------------------------------------------------
// Upload / Paste form
// ---------------------------------------------------------------------------
@@ -358,9 +541,11 @@ function metaFor(connectorId: string) {
// because the Gmail card is the only one with a dual-flow shape.
function GmailOAuthAdvanced({
loading,
+ disabled = false,
onConnect,
}: {
loading: boolean;
+ disabled?: boolean;
onConnect: (req: ConnectRequest) => void;
}) {
const [open, setOpen] = useState(false);
@@ -369,13 +554,15 @@ function GmailOAuthAdvanced({
<button
type="button"
onClick={() => setOpen((o) => !o)}
+ disabled={disabled}
style={{
background: 'transparent',
border: 'none',
padding: 0,
fontSize: 11,
color: 'var(--color-text-tertiary)',
- cursor: 'pointer',
+ cursor: disabled ? 'default' : 'pointer',
+ opacity: disabled ? 0.5 : 1,
textDecoration: 'underline',
}}
>
@@ -410,6 +597,7 @@ function GmailOAuthAdvanced({
{ name: 'password', placeholder: 'Client Secret', type: 'password' },
]}
loading={loading}
+ disabled={disabled}
onSubmit={onConnect}
/>
</div>
@@ -457,23 +645,26 @@ function formatBacklogRange(iso: string | null | undefined): string | null {
return `past ${years} year${years === 1 ? '' : 's'}`;
}
-function SyncStatusDisplay({
+export function SyncStatusDisplay({
chunks,
sync,
unitLabel,
connectorId,
+ disabled = false,
onSyncTriggered,
}: {
chunks: number;
sync: SyncStatus | undefined;
unitLabel: string;
connectorId: string;
+ disabled?: boolean;
onSyncTriggered: () => void;
}) {
const [syncing, setSyncing] = useState(false);
const [syncError, setSyncError] = useState('');
const handleSync = async () => {
+ if (disabled || sync?.state === 'stopping') return;
setSyncing(true);
setSyncError('');
try {
@@ -486,6 +677,22 @@ function SyncStatusDisplay({
}
};
+ // Disconnect cleanup only begins after the active sync worker exits. Keep
+ // every competing lifecycle action unavailable while the backend reports
+ // this transitional state; the disconnect handler retries automatically.
+ if (sync?.state === 'stopping') {
+ return (
+ <div>
+ <div style={{ fontSize: 12, color: 'var(--color-warning)', marginBottom: 4 }}>
+ Disconnect pending — waiting for the active sync to stop.
+ </div>
+ <div style={{ fontSize: 10.5, color: 'var(--color-text-tertiary)' }}>
+ Indexed data will be cleaned up before this source disconnects.
+ </div>
+ </div>
+ );
+ }
+
// Error state
if (sync?.error) {
return (
@@ -495,13 +702,13 @@ function SyncStatusDisplay({
</div>
<button
onClick={handleSync}
- disabled={syncing}
+ disabled={syncing || disabled}
style={{
fontSize: 10, padding: '2px 10px',
background: 'var(--color-accent-purple)', color: 'var(--color-on-accent)',
border: 'none', borderRadius: 3,
- cursor: 'pointer', fontWeight: 600,
- opacity: syncing ? 0.5 : 1,
+ cursor: syncing || disabled ? 'default' : 'pointer', fontWeight: 600,
+ opacity: syncing || disabled ? 0.5 : 1,
}}
>{syncing ? 'Retrying...' : 'Retry Sync'}</button>
</div>
@@ -570,13 +777,15 @@ function SyncStatusDisplay({
</span>
<button
onClick={handleSync}
- disabled={syncing}
+ disabled={syncing || disabled}
style={{
fontSize: 9, padding: '1px 6px',
background: 'transparent',
color: 'var(--color-text-tertiary)',
border: '1px solid var(--color-border)',
- borderRadius: 3, cursor: 'pointer',
+ borderRadius: 3,
+ cursor: syncing || disabled ? 'default' : 'pointer',
+ opacity: syncing || disabled ? 0.5 : 1,
}}
>{syncing ? '...' : 'Re-sync'}</button>
</div>
@@ -601,13 +810,13 @@ function SyncStatusDisplay({
</span>
<button
onClick={handleSync}
- disabled={syncing}
+ disabled={syncing || disabled}
style={{
fontSize: 10, padding: '2px 10px',
background: 'var(--color-accent-purple)', color: 'var(--color-on-accent)',
border: 'none', borderRadius: 3,
- cursor: 'pointer', fontWeight: 600,
- opacity: syncing ? 0.5 : 1,
+ cursor: syncing || disabled ? 'default' : 'pointer', fontWeight: 600,
+ opacity: syncing || disabled ? 0.5 : 1,
}}
>{syncing ? 'Syncing...' : hasSynced ? 'Re-sync' : 'Sync Now'}</button>
</div>
@@ -633,20 +842,25 @@ function DataSourcesSection() {
const [syncStatuses, setSyncStatuses] = useState<Record<string, SyncStatus>>({});
const [expandedId, setExpandedId] = useState<string | null>(null);
const [loading, setLoading] = useState(false);
+ const [disconnectingId, setDisconnectingId] = useState<string | null>(null);
+ const [disconnectError, setDisconnectError] = useState<{ id: string; message: string } | null>(null);
+ const disconnectAbortRef = useRef<AbortController | null>(null);
- const loadConnectors = useCallback(() => {
- listConnectors()
- .then((list) =>
- setCachedConnectors(
- list.map((c) => ({
- connector_id: c.connector_id,
- display_name: c.display_name,
- connected: c.connected,
- chunks: (c as any).chunks || 0,
- })),
- ),
- )
- .catch(() => {});
+ const loadConnectors = useCallback(async () => {
+ try {
+ const list = await listConnectors();
+ setCachedConnectors(
+ list.map((c) => ({
+ connector_id: c.connector_id,
+ display_name: c.display_name,
+ connected: c.connected,
+ chunks: (c as any).chunks || 0,
+ auth_type: c.auth_type,
+ })),
+ );
+ } catch {
+ // The periodic refresh will try again.
+ }
}, [setCachedConnectors]);
const setConnectors = setCachedConnectors;
@@ -666,8 +880,8 @@ function DataSourcesSection() {
}, [connectors]);
useEffect(() => {
- loadConnectors();
- const interval = setInterval(loadConnectors, 10000);
+ void loadConnectors();
+ const interval = setInterval(() => void loadConnectors(), 10000);
return () => clearInterval(interval);
}, [loadConnectors]);
@@ -682,29 +896,65 @@ function DataSourcesSection() {
const [connectingId, setConnectingId] = useState<string | null>(null);
const [connectStage, setConnectStage] = useState<string>('');
const [connectError, setConnectError] = useState<string>('');
- const [disconnectingId, setDisconnectingId] = useState<string | null>(null);
+
+ useEffect(() => () => disconnectAbortRef.current?.abort(), []);
const handleDisconnect = async (id: string) => {
- if (disconnectingId) return;
+ if (disconnectAbortRef.current || loading) return;
+ const controller = new AbortController();
+ disconnectAbortRef.current = controller;
setDisconnectingId(id);
+ setDisconnectError(null);
try {
- await disconnectSource(id);
- loadConnectors();
- } catch {
- // Surface failures silently — the connector list will refresh on the
- // next poll and reflect the true state regardless.
+ await disconnectSourceUntilComplete(id, {
+ signal: controller.signal,
+ onPending: () => {
+ setSyncStatuses((prev) => {
+ const current = prev[id];
+ return {
+ ...prev,
+ [id]: {
+ state: 'stopping',
+ items_synced: current?.items_synced ?? 0,
+ items_total: current?.items_total ?? 0,
+ new_items_synced: current?.new_items_synced ?? null,
+ oldest_item_date: current?.oldest_item_date ?? null,
+ last_sync: current?.last_sync ?? null,
+ error: null,
+ },
+ };
+ });
+ },
+ });
+ setSyncStatuses((prev) => {
+ const next = { ...prev };
+ delete next[id];
+ return next;
+ });
+ await loadConnectors();
+ } catch (err) {
+ if (!(err instanceof DOMException && err.name === 'AbortError')) {
+ setDisconnectError({
+ id,
+ message: err instanceof Error ? err.message : 'Disconnect failed',
+ });
+ }
} finally {
- setDisconnectingId(null);
+ if (disconnectAbortRef.current === controller) {
+ disconnectAbortRef.current = null;
+ setDisconnectingId(null);
+ }
}
};
- const handleConnect = async (id: string, req: ConnectRequest) => {
+ const handleConnect = async (id: string, req: ConnectRequest | null) => {
+ if (loading || disconnectAbortRef.current) return;
setLoading(true);
setConnectingId(id);
setConnectStage('Connecting...');
setConnectError('');
try {
- const resp = await connectSource(id, req);
+ const resp = req === null ? null : await connectSource(id, req);
// OAuth connectors (Google Drive/Calendar/Contacts/Gmail/Tasks): pasting
// a Client ID / Secret only registers the app credentials. The backend
@@ -712,9 +962,9 @@ function DataSourcesSection() {
// which is the only path that actually mints an access token. Open it now
// and wait for the callback to flip the connector to connected. Without
// this the connector would stay "pending" forever — the exact #512 bug.
- if (resp.status === 'oauth_required') {
- setConnectStage('Opening Google sign-in...');
- await startServerOAuth(id, resp.oauth_start);
+ if (req === null || resp?.status === 'oauth_required') {
+ setConnectStage('Opening provider sign-in...');
+ await startServerOAuth(id, resp?.oauth_start);
}
setConnectStage('Connected! Starting sync...');
@@ -730,6 +980,7 @@ function DataSourcesSection() {
display_name: c.display_name,
connected: c.connected,
chunks: (c as any).chunks || 0,
+ auth_type: c.auth_type,
})));
break;
}
@@ -788,10 +1039,11 @@ function DataSourcesSection() {
const connected = unifiedConnectors.filter((c) => c.connected);
const notConnectedBase = unifiedConnectors.filter((c) => !c.connected);
// Always show the upload card in the not-connected list (it has no backend connector)
- const uploadEntry = { connector_id: 'upload', display_name: 'Upload / Paste', connected: false, chunks: 0 };
+ const uploadEntry = { connector_id: 'upload', display_name: 'Upload / Paste', connected: false, chunks: 0, auth_type: 'local' };
const notConnected = notConnectedBase.some((c) => c.connector_id === 'upload')
? notConnectedBase
: [...notConnectedBase, uploadEntry];
+ const connectorActionsBusy = loading || disconnectingId !== null;
if (isFirstLoad) {
return (
@@ -856,12 +1108,18 @@ function DataSourcesSection() {
sync={sync}
unitLabel={unit}
connectorId={c.connector_id}
+ disabled={connectorActionsBusy}
onSyncTriggered={loadConnectors}
/>
+ {disconnectError?.id === c.connector_id && (
+ <div style={{ fontSize: 11, color: 'var(--color-error)', marginTop: 4 }}>
+ Disconnect failed: {disconnectError.message}
+ </div>
+ )}
</div>
<button
onClick={() => handleDisconnect(c.connector_id)}
- disabled={disconnectingId === c.connector_id}
+ disabled={connectorActionsBusy}
className="hud-label"
style={{
padding: '6px 12px',
@@ -869,12 +1127,14 @@ function DataSourcesSection() {
color: 'var(--color-text-secondary)',
border: '1px solid var(--color-border)',
borderRadius: 4,
- cursor: disconnectingId === c.connector_id ? 'default' : 'pointer',
+ cursor: connectorActionsBusy ? 'default' : 'pointer',
letterSpacing: '0.15em',
- opacity: disconnectingId === c.connector_id ? 0.5 : 1,
+ opacity: connectorActionsBusy ? 0.5 : 1,
}}
>
- {disconnectingId === c.connector_id ? 'Disconnecting…' : 'Disconnect'}
+ {disconnectingId === c.connector_id
+ ? sync?.state === 'stopping' ? 'Cleaning up…' : 'Disconnecting…'
+ : sync?.state === 'stopping' ? 'Finish disconnect' : 'Disconnect'}
</button>
</div>
</div>
@@ -968,12 +1228,14 @@ function DataSourcesSection() {
<InlineConnectForm
fields={meta.inputFields}
loading={loading && connectingId === c.connector_id}
+ disabled={connectorActionsBusy}
onSubmit={(req) => handleConnect(c.connector_id, req)}
/>
)}
{c.connector_id === 'gmail_imap' && (
<GmailOAuthAdvanced
loading={loading && connectingId === 'gmail'}
+ disabled={connectorActionsBusy}
onConnect={(req) => handleConnect('gmail', req)}
/>
)}
@@ -1026,6 +1288,30 @@ function DataSourcesSection() {
)}
</div>
)}
+
+ {isExpanded && c.connector_id !== 'upload' && !meta?.steps && (
+ <div style={{ borderTop: '1px solid var(--color-border)', padding: 12 }}>
+ <GenericConnectPanel
+ connectorId={c.connector_id}
+ displayName={meta?.display_name ?? c.display_name}
+ authType={c.auth_type || 'oauth'}
+ loading={loading && connectingId === c.connector_id}
+ disabled={connectorActionsBusy}
+ onConnect={(req) => handleConnect(c.connector_id, req)}
+ onOAuthStart={() => handleConnect(c.connector_id, null)}
+ />
+ {connectingId === c.connector_id && connectStage && (
+ <div style={{ marginTop: 8, fontSize: 12, color: 'var(--color-warning)' }}>
+ {connectStage}
+ </div>
+ )}
+ {connectError && connectingId === null && expandedId === c.connector_id && (
+ <div style={{ fontSize: 11, color: 'var(--color-error)', marginTop: 6 }}>
+ {connectError}
+ </div>
+ )}
+ </div>
+ )}
</div>
);
})}
diff --git a/frontend/src/types/connectors.ts b/frontend/src/types/connectors.ts
index 146f173f..0a411739 100644
--- a/frontend/src/types/connectors.ts
+++ b/frontend/src/types/connectors.ts
@@ -7,7 +7,7 @@ export interface SetupStep {
export interface ConnectorMeta {
connector_id: string;
display_name: string;
- auth_type: 'oauth' | 'local' | 'bridge' | 'filesystem';
+ auth_type: 'oauth' | 'local' | 'bridge' | 'filesystem' | 'token';
category: 'communication' | 'documents' | 'pim' | 'other';
icon: string;
color: string;
@@ -25,18 +25,26 @@ export interface ConnectorMeta {
}>;
}
+export interface OAuthSetupInfo {
+ provider: string;
+ setup_url: string;
+ setup_hint: string;
+ has_credentials: boolean;
+}
+
export interface ConnectorInfo {
connector_id: string;
display_name: string;
- auth_type: "oauth" | "local" | "bridge" | "filesystem";
+ auth_type: "oauth" | "local" | "bridge" | "filesystem" | "token";
connected: boolean;
auth_url?: string;
mcp_tools?: string[];
chunks?: number;
+ oauth_setup?: OAuthSetupInfo | null;
}
export interface SyncStatus {
- state: "idle" | "syncing" | "paused" | "error";
+ state: "idle" | "syncing" | "stopping" | "paused" | "error";
items_synced: number;
items_total: number;
/** Items processed in the current (or most recent) run only. `null`
@@ -56,6 +64,8 @@ export interface ConnectRequest {
code?: string;
email?: string;
password?: string;
+ /** Non-secret connector configuration such as a weather location or RSS feeds. */
+ config?: Record<string, unknown>;
host?: string;
port?: number;
security?: 'tls' | 'starttls';
diff --git a/src/openjarvis/connectors/github_notifications.py b/src/openjarvis/connectors/github_notifications.py
index 9bd96a8e..8583eb5b 100644
--- a/src/openjarvis/connectors/github_notifications.py
+++ b/src/openjarvis/connectors/github_notifications.py
@@ -54,8 +54,23 @@ class GitHubNotificationsConnector(BaseConnector):
data = json.loads(self._token_path.read_text(encoding="utf-8"))
return data["token"]
+ def set_token(self, token: str) -> None:
+ """Validate and persist a PAT supplied through Data Sources."""
+ token = token.strip()
+ if not token:
+ raise ValueError("A GitHub personal access token is required")
+ # Do not create/overwrite a credential file until GitHub accepts the
+ # token for the exact API this connector consumes.
+ _github_api_get(token, params={"per_page": "1"})
+ from openjarvis.security.file_utils import secure_write_json
+
+ secure_write_json(self._token_path, {"token": token})
+
def is_connected(self) -> bool:
- return self._token_path.exists()
+ try:
+ return bool(self._load_token())
+ except (OSError, KeyError, json.JSONDecodeError):
+ return False
def disconnect(self) -> None:
if self._token_path.exists():
diff --git a/src/openjarvis/connectors/news_rss.py b/src/openjarvis/connectors/news_rss.py
index 7809e05e..724dd53b 100644
--- a/src/openjarvis/connectors/news_rss.py
+++ b/src/openjarvis/connectors/news_rss.py
@@ -7,12 +7,17 @@ functions for easy mocking in tests.
from __future__ import annotations
+import http.client
+import ipaddress
import json
+import socket
import xml.etree.ElementTree as ET
+from dataclasses import dataclass
from datetime import datetime
from email.utils import parsedate_to_datetime
from pathlib import Path
-from typing import Dict, Iterator, List, Optional
+from typing import Any, Dict, Iterator, List, Optional
+from urllib.parse import urljoin, urlparse
import httpx
@@ -21,13 +26,208 @@ from openjarvis.core.config import DEFAULT_CONFIG_DIR
from openjarvis.core.registry import ConnectorRegistry
_DEFAULT_CONFIG_PATH = str(DEFAULT_CONFIG_DIR / "connectors" / "news_rss.json")
+_MAX_REDIRECTS = 5
+_MAX_FEED_BYTES = 10 * 1024 * 1024
+
+
+@dataclass(frozen=True)
+class _FeedTarget:
+ """A validated URL plus the exact public addresses it may connect to."""
+
+ scheme: str
+ hostname: str
+ port: int
+ request_target: str
+ host_header: str
+ addresses: tuple[str, ...]
+
+
+def _resolve_host_addresses(hostname: str, port: int) -> tuple[str, ...]:
+ """Resolve once, reject every non-global answer, and return pinned IPs."""
+ from openjarvis.security.ssrf import is_private_ip
+
+ try:
+ results = socket.getaddrinfo(
+ hostname,
+ port,
+ socket.AF_UNSPEC,
+ socket.SOCK_STREAM,
+ )
+ except socket.gaierror as exc:
+ raise ValueError(
+ f"RSS feed hostname could not be resolved: {hostname}"
+ ) from exc
+
+ addresses: list[str] = []
+ for _family, _type, _proto, _canonname, sockaddr in results:
+ raw_ip = sockaddr[0].split("%", 1)[0]
+ try:
+ address = ipaddress.ip_address(raw_ip)
+ except ValueError as exc:
+ raise ValueError(f"RSS feed resolved to an invalid IP: {raw_ip}") from exc
+ # ``is_global`` closes gaps outside the project's original RFC1918
+ # list (CGNAT, benchmarking, documentation, and other special ranges).
+ if not address.is_global or is_private_ip(str(address)):
+ raise ValueError(f"RSS feed resolved to a non-public IP: {address}")
+ normalized = str(address)
+ if normalized not in addresses:
+ addresses.append(normalized)
+
+ if not addresses:
+ raise ValueError(f"RSS feed hostname returned no addresses: {hostname}")
+ return tuple(addresses)
+
+
+def _validate_feed_url(url: str) -> _FeedTarget:
+ """Reject malformed/private targets and pin the addresses just checked."""
+ parsed = urlparse(url)
+ if parsed.scheme not in {"http", "https"} or not parsed.netloc:
+ raise ValueError("Each RSS feed must be an http(s) URL")
+ if parsed.username is not None or parsed.password is not None:
+ raise ValueError("RSS feed URLs must not contain user credentials")
+ hostname = parsed.hostname
+ if not hostname:
+ raise ValueError("Each RSS feed must include a hostname")
+ try:
+ port = parsed.port or (443 if parsed.scheme == "https" else 80)
+ except ValueError as exc:
+ raise ValueError("RSS feed URL contains an invalid port") from exc
+
+ from openjarvis.security.ssrf import check_ssrf
+
+ error = check_ssrf(url)
+ if error:
+ raise ValueError(f"RSS feed URL is not allowed: {error}")
+
+ # IDNA is used consistently for DNS, HTTP Host, TLS SNI, and certificate
+ # verification. IP literals are already ASCII and remain unchanged.
+ try:
+ ipaddress.ip_address(hostname)
+ connection_hostname = hostname
+ except ValueError:
+ try:
+ connection_hostname = hostname.encode("idna").decode("ascii")
+ except UnicodeError as exc:
+ raise ValueError("RSS feed hostname is not valid IDNA") from exc
+
+ addresses = _resolve_host_addresses(connection_hostname, port)
+ default_port = 443 if parsed.scheme == "https" else 80
+ displayed_host = (
+ f"[{connection_hostname}]"
+ if ":" in connection_hostname
+ else connection_hostname
+ )
+ host_header = displayed_host if port == default_port else f"{displayed_host}:{port}"
+ request_target = parsed.path or "/"
+ if parsed.query:
+ request_target = f"{request_target}?{parsed.query}"
+
+ return _FeedTarget(
+ scheme=parsed.scheme,
+ hostname=connection_hostname,
+ port=port,
+ request_target=request_target,
+ host_header=host_header,
+ addresses=addresses,
+ )
+
+
+class _PinnedConnectionMixin:
+ """Connect to a verified IP while retaining the URL host for HTTP/TLS."""
+
+ def __init__(self, host: str, port: int, *, pinned_ip: str, **kwargs: Any) -> None:
+ self._pinned_ip = pinned_ip
+ super().__init__(host, port, **kwargs)
+ # HTTPConnection.__init__ deliberately installs socket.create_connection
+ # as an instance attribute, so replace that hook after initialization.
+ self._create_connection = self._create_pinned_connection
+
+ def _create_pinned_connection(self, address, timeout, source_address):
+ # Ignore ``address[0]`` so http.client never performs a second DNS
+ # lookup. HTTPSConnection still uses ``self.host`` as TLS SNI and for
+ # certificate hostname verification.
+ return socket.create_connection(
+ (self._pinned_ip, address[1]),
+ timeout,
+ source_address,
+ )
+
+
+class _PinnedHTTPConnection(_PinnedConnectionMixin, http.client.HTTPConnection):
+ pass
+
+
+class _PinnedHTTPSConnection(_PinnedConnectionMixin, http.client.HTTPSConnection):
+ pass
+
+
+def _request_feed(url: str, target: _FeedTarget) -> httpx.Response:
+ """GET *url* using only the already-validated addresses in *target*."""
+ request = httpx.Request("GET", url)
+ last_error: Exception | None = None
+
+ for address in target.addresses:
+ connection_cls = (
+ _PinnedHTTPSConnection
+ if target.scheme == "https"
+ else _PinnedHTTPConnection
+ )
+ connection = connection_cls(
+ target.hostname,
+ target.port,
+ pinned_ip=address,
+ timeout=30.0,
+ )
+ try:
+ connection.request(
+ "GET",
+ target.request_target,
+ headers={
+ "Host": target.host_header,
+ "Accept": (
+ "application/rss+xml, application/atom+xml, "
+ "application/xml, text/xml;q=0.9, */*;q=0.1"
+ ),
+ "User-Agent": "OpenJarvis-RSS/1.0",
+ "Connection": "close",
+ },
+ )
+ raw_response = connection.getresponse()
+ body = raw_response.read(_MAX_FEED_BYTES + 1)
+ if len(body) > _MAX_FEED_BYTES:
+ raise ValueError(f"RSS feed exceeded {_MAX_FEED_BYTES} response bytes")
+ return httpx.Response(
+ raw_response.status,
+ headers=raw_response.getheaders(),
+ content=body,
+ request=request,
+ )
+ except (OSError, http.client.HTTPException) as exc:
+ last_error = exc
+ finally:
+ connection.close()
+
+ raise httpx.RequestError(
+ f"Unable to fetch RSS feed from its verified addresses: {last_error}",
+ request=request,
+ ) from last_error
def _fetch_feed(url: str) -> str:
- """Download raw XML from a feed URL."""
- resp = httpx.get(url, timeout=30.0, follow_redirects=True)
- resp.raise_for_status()
- return resp.text
+ """Download feed XML while validating every redirect target."""
+ current_url = url
+ for _ in range(_MAX_REDIRECTS + 1):
+ target = _validate_feed_url(current_url)
+ resp = _request_feed(current_url, target)
+ if resp.status_code not in {301, 302, 303, 307, 308}:
+ resp.raise_for_status()
+ return resp.text
+ location = resp.headers.get("location", "")
+ if not location:
+ resp.raise_for_status()
+ return resp.text
+ current_url = urljoin(current_url, location)
+ raise ValueError(f"RSS feed exceeded {_MAX_REDIRECTS} redirects")
def _parse_rss_items(xml_text: str, max_items: int = 5) -> List[Dict[str, str]]:
@@ -103,6 +303,23 @@ class NewsRSSConnector(BaseConnector):
data = json.loads(self._config_path.read_text(encoding="utf-8"))
return data.get("feeds", [])
+ def configure(self, feeds: List[Dict[str, Any]]) -> None:
+ """Validate and persist RSS feed configuration from the connect UI."""
+ normalized: List[Dict[str, str]] = []
+ for feed in feeds:
+ if not isinstance(feed, dict):
+ continue
+ url = str(feed.get("url", "")).strip()
+ _validate_feed_url(url)
+ parsed = urlparse(url)
+ name = str(feed.get("name", "")).strip() or parsed.netloc
+ normalized.append({"name": name, "url": url})
+ if not normalized:
+ raise ValueError("At least one RSS feed URL is required")
+ from openjarvis.security.file_utils import secure_write_json
+
+ secure_write_json(self._config_path, {"feeds": normalized})
+
def is_connected(self) -> bool:
if not self._config_path.exists():
return False
@@ -130,7 +347,7 @@ class NewsRSSConnector(BaseConnector):
try:
xml_text = _fetch_feed(feed_url)
- except httpx.HTTPError:
+ except (httpx.HTTPError, ValueError):
continue
items = _parse_rss_items(xml_text)
diff --git a/src/openjarvis/connectors/oauth.py b/src/openjarvis/connectors/oauth.py
index e7a042d7..d0fc010d 100644
--- a/src/openjarvis/connectors/oauth.py
+++ b/src/openjarvis/connectors/oauth.py
@@ -262,10 +262,24 @@ def save_tokens(path: str, tokens: Dict[str, Any]) -> None:
Creates parent directories as needed.
"""
- p = Path(path)
- p.parent.mkdir(parents=True, exist_ok=True)
- p.write_text(json.dumps(tokens, indent=2), encoding="utf-8")
- os.chmod(path, 0o600)
+ from openjarvis.security.file_utils import secure_write_json
+
+ secure_write_json(Path(path), tokens)
+
+
+def require_access_token(tokens: Any) -> str:
+ """Return a non-empty OAuth access token or reject the response.
+
+ Client registration credentials and error-shaped token responses are not
+ authenticated sessions. Centralizing this check keeps every OAuth entry
+ point from persisting a false-success credential file.
+ """
+ if not isinstance(tokens, dict):
+ raise RuntimeError("OAuth token response was not a JSON object")
+ access_token = tokens.get("access_token")
+ if not isinstance(access_token, str) or not access_token.strip():
+ raise RuntimeError("OAuth token response did not include an access_token")
+ return access_token.strip()
def delete_tokens(path: str) -> None:
@@ -375,7 +389,9 @@ def exchange_google_token(
timeout=30.0,
)
resp.raise_for_status()
- return resp.json()
+ tokens = resp.json()
+ require_access_token(tokens)
+ return tokens
def run_oauth_flow(
@@ -642,7 +658,9 @@ def _exchange_token(
resp = httpx.post(provider.token_endpoint, data=data, headers=headers, timeout=30.0)
resp.raise_for_status()
- return resp.json()
+ tokens = resp.json()
+ require_access_token(tokens)
+ return tokens
def run_connector_oauth(
@@ -702,10 +720,11 @@ def run_connector_oauth(
# Exchange code for tokens
tokens = _exchange_token(provider, code, client_id, client_secret, redirect_uri)
+ access_token = require_access_token(tokens)
# Build payload with client credentials included (needed for refresh)
payload = {
- "access_token": tokens.get("access_token", ""),
+ "access_token": access_token,
"refresh_token": tokens.get("refresh_token", ""),
"token_type": tokens.get("token_type", "Bearer"),
"expires_in": tokens.get("expires_in", 3600),
diff --git a/src/openjarvis/connectors/oura.py b/src/openjarvis/connectors/oura.py
index a5969448..1ab179f0 100644
--- a/src/openjarvis/connectors/oura.py
+++ b/src/openjarvis/connectors/oura.py
@@ -52,8 +52,21 @@ class OuraConnector(BaseConnector):
data = json.loads(self._token_path.read_text(encoding="utf-8"))
return data["token"]
+ def set_token(self, token: str) -> None:
+ """Validate and persist an Oura personal access token."""
+ token = token.strip()
+ if not token:
+ raise ValueError("An Oura personal access token is required")
+ _oura_api_get(token, "personal_info")
+ from openjarvis.security.file_utils import secure_write_json
+
+ secure_write_json(self._token_path, {"token": token})
+
def is_connected(self) -> bool:
- return self._token_path.exists()
+ try:
+ return bool(self._load_token())
+ except (OSError, KeyError, json.JSONDecodeError):
+ return False
def disconnect(self) -> None:
if self._token_path.exists():
diff --git a/src/openjarvis/connectors/spotify.py b/src/openjarvis/connectors/spotify.py
index b11655e0..f5e7f349 100644
--- a/src/openjarvis/connectors/spotify.py
+++ b/src/openjarvis/connectors/spotify.py
@@ -54,7 +54,11 @@ class SpotifyConnector(BaseConnector):
return self._load_tokens()["access_token"]
def is_connected(self) -> bool:
- return self._token_path.exists()
+ try:
+ access_token = self._load_tokens().get("access_token")
+ except (OSError, json.JSONDecodeError):
+ return False
+ return isinstance(access_token, str) and bool(access_token.strip())
def disconnect(self) -> None:
if self._token_path.exists():
@@ -92,6 +96,7 @@ class SpotifyConnector(BaseConnector):
_exchange_token,
get_client_credentials,
get_provider_for_connector,
+ require_access_token,
save_tokens,
)
@@ -102,8 +107,9 @@ class SpotifyConnector(BaseConnector):
client_id, client_secret = creds
redirect_uri = f"http://{provider.callback_host}:{provider.callback_port}{provider.callback_path}"
tokens = _exchange_token(provider, code, client_id, client_secret, redirect_uri)
+ access_token = require_access_token(tokens)
payload = {
- "access_token": tokens.get("access_token", ""),
+ "access_token": access_token,
"refresh_token": tokens.get("refresh_token", ""),
"client_id": client_id,
"client_secret": client_secret,
diff --git a/src/openjarvis/connectors/strava.py b/src/openjarvis/connectors/strava.py
index c641ab9b..87e763be 100644
--- a/src/openjarvis/connectors/strava.py
+++ b/src/openjarvis/connectors/strava.py
@@ -69,14 +69,20 @@ class StravaConnector(BaseConnector):
return json.loads(self._token_path.read_text(encoding="utf-8"))
def _save_tokens(self, tokens: Dict[str, str]) -> None:
- self._token_path.write_text(json.dumps(tokens), encoding="utf-8")
+ from openjarvis.security.file_utils import secure_write_json
+
+ secure_write_json(self._token_path, tokens)
def _get_access_token(self) -> str:
tokens = self._load_tokens()
return tokens["access_token"]
def is_connected(self) -> bool:
- return self._token_path.exists()
+ try:
+ access_token = self._load_tokens().get("access_token")
+ except (OSError, json.JSONDecodeError):
+ return False
+ return isinstance(access_token, str) and bool(access_token.strip())
def disconnect(self) -> None:
if self._token_path.exists():
@@ -114,6 +120,7 @@ class StravaConnector(BaseConnector):
_exchange_token,
get_client_credentials,
get_provider_for_connector,
+ require_access_token,
save_tokens,
)
@@ -124,8 +131,9 @@ class StravaConnector(BaseConnector):
client_id, client_secret = creds
redirect_uri = f"http://{provider.callback_host}:{provider.callback_port}{provider.callback_path}"
tokens = _exchange_token(provider, code, client_id, client_secret, redirect_uri)
+ access_token = require_access_token(tokens)
payload = {
- "access_token": tokens.get("access_token", ""),
+ "access_token": access_token,
"refresh_token": tokens.get("refresh_token", ""),
"client_id": client_id,
"client_secret": client_secret,
diff --git a/src/openjarvis/connectors/weather.py b/src/openjarvis/connectors/weather.py
index 90559742..db36c998 100644
--- a/src/openjarvis/connectors/weather.py
+++ b/src/openjarvis/connectors/weather.py
@@ -44,6 +44,25 @@ class WeatherConnector(BaseConnector):
data = json.loads(self._token_path.read_text(encoding="utf-8"))
return data
+ def configure(self, *, api_key: str, location: str) -> None:
+ """Validate and persist the API key and required location."""
+ api_key = api_key.strip()
+ location = location.strip()
+ if not api_key:
+ raise ValueError("An OpenWeather API key is required")
+ if not location:
+ raise ValueError("A weather location is required")
+ _weather_api_get(
+ "https://api.openweathermap.org/data/2.5/weather",
+ params={"q": location, "appid": api_key, "units": "imperial"},
+ )
+ from openjarvis.security.file_utils import secure_write_json
+
+ secure_write_json(
+ self._token_path,
+ {"api_key": api_key, "location": location},
+ )
+
def is_connected(self) -> bool:
if not self._token_path.exists():
return False
diff --git a/src/openjarvis/security/file_utils.py b/src/openjarvis/security/file_utils.py
index eb960fdf..7d7b65a7 100644
--- a/src/openjarvis/security/file_utils.py
+++ b/src/openjarvis/security/file_utils.py
@@ -6,8 +6,11 @@ through these helpers to ensure consistent, restrictive permissions.
from __future__ import annotations
+import json
import os
+import tempfile
from pathlib import Path
+from typing import Any
def secure_mkdir(path: Path, mode: int = 0o700) -> Path:
@@ -32,3 +35,53 @@ def secure_create(path: Path, mode: int = 0o600) -> Path:
path.touch()
os.chmod(path, mode)
return path
+
+
+def secure_write_text(
+ path: Path,
+ content: str,
+ *,
+ mode: int = 0o600,
+ encoding: str = "utf-8",
+) -> Path:
+ """Atomically replace *path* with owner-only text content.
+
+ The temporary file is created in the destination directory so
+ :func:`os.replace` remains atomic. Permissions are restricted before
+ secret bytes are written, then re-applied after replacement for platforms
+ whose replace semantics preserve the destination's prior mode.
+ """
+ secure_mkdir(path.parent, mode=0o700)
+ fd, temporary_name = tempfile.mkstemp(
+ prefix=f".{path.name}.",
+ suffix=".tmp",
+ dir=str(path.parent),
+ )
+ temporary_path = Path(temporary_name)
+ try:
+ if hasattr(os, "fchmod"):
+ os.fchmod(fd, mode)
+ with os.fdopen(fd, "w", encoding=encoding) as handle:
+ handle.write(content)
+ handle.flush()
+ os.fsync(handle.fileno())
+ os.replace(temporary_path, path)
+ os.chmod(path, mode)
+ except Exception:
+ try:
+ os.close(fd)
+ except OSError:
+ pass
+ temporary_path.unlink(missing_ok=True)
+ raise
+ return path
+
+
+def secure_write_json(path: Path, payload: Any, *, mode: int = 0o600) -> Path:
+ """Atomically persist JSON with restrictive permissions."""
+ return secure_write_text(
+ path,
+ json.dumps(payload, indent=2),
+ mode=mode,
+ encoding="utf-8",
+ )
diff --git a/src/openjarvis/server/connectors_router.py b/src/openjarvis/server/connectors_router.py
index 18e39b7b..0418d751 100644
--- a/src/openjarvis/server/connectors_router.py
+++ b/src/openjarvis/server/connectors_router.py
@@ -94,6 +94,10 @@ try:
code: Optional[str] = None
email: Optional[str] = None
password: Optional[str] = None
+ # Connector-specific, non-secret setup values. Keep credentials in
+ # the dedicated fields above so callers do not accidentally log them
+ # together with ordinary configuration.
+ config: Optional[Dict[str, Any]] = None
host: Optional[str] = None
port: Optional[int] = None
security: Optional[str] = None
@@ -183,13 +187,23 @@ def create_connectors_router():
)
raw = (req.code or req.token or "").strip()
- # Only the client-registration pair routes through the server flow.
- # A raw access token (no ".apps.googleusercontent.com") is handled by
- # the connector's handle_callback unchanged.
- if ".apps.googleusercontent.com" not in raw or ":" not in raw:
+ provider = get_provider_for_connector(connector_id)
+
+ # Only the client-registration pair routes through the server flow;
+ # a raw access token is handled by the connector's handle_callback
+ # unchanged. Google's client IDs have a distinctive suffix, so a
+ # colon-containing string is only treated as a Google credential
+ # pair when it matches that format -- otherwise some other raw
+ # token that happens to contain a colon could be misidentified.
+ # Non-Google providers (Spotify, Strava, ...) have no such
+ # ambiguity: every non-Google OAuth connector only ever expects a
+ # client_id:client_secret pair through this path, so any
+ # colon-containing string is enough to route here.
+ is_google_pair = ".apps.googleusercontent.com" in raw
+ is_non_google_pair = provider is not None and provider.name != "google"
+ if ":" not in raw or not (is_google_pair or is_non_google_pair):
return None
- provider = get_provider_for_connector(connector_id)
if provider is None:
raise HTTPException(
status_code=400,
@@ -566,6 +580,34 @@ def create_connectors_router():
else:
instance.handle_callback(req.token)
+ elif connector_id in {"github_notifications", "oura"}:
+ if not req.token:
+ raise HTTPException(status_code=400, detail="A token is required")
+ # These token connectors persist their credentials themselves;
+ # they intentionally do not expose the OAuth-only ``_token``
+ # attribute or ``handle_callback`` hook.
+ instance.set_token(req.token)
+
+ elif connector_id == "weather":
+ if not req.token:
+ raise HTTPException(
+ status_code=400, detail="An OpenWeather API key is required"
+ )
+ location = (req.config or {}).get("location")
+ if not isinstance(location, str) or not location.strip():
+ raise HTTPException(
+ status_code=400, detail="A weather location is required"
+ )
+ instance.configure(api_key=req.token, location=location)
+
+ elif connector_id == "news_rss":
+ feeds = (req.config or {}).get("feeds")
+ if not isinstance(feeds, list):
+ raise HTTPException(
+ status_code=400, detail="At least one RSS feed URL is required"
+ )
+ instance.configure(feeds)
+
else:
# Generic: try to store token or credentials if the instance
# exposes the relevant attributes.
@@ -755,6 +797,7 @@ def create_connectors_router():
_exchange_token,
get_client_credentials,
get_provider_for_connector,
+ require_access_token,
save_tokens,
)
@@ -794,6 +837,7 @@ def create_connectors_router():
tokens = _exchange_token(
provider, code, client_id, client_secret, redirect_uri
)
+ access_token = require_access_token(tokens)
except Exception as exc:
_style = "font-family:system-ui;text-align:center;padding:60px"
return HTMLResponse(
@@ -807,7 +851,7 @@ def create_connectors_router():
)
payload = {
- "access_token": tokens.get("access_token", ""),
+ "access_token": access_token,
"refresh_token": tokens.get("refresh_token", ""),
"token_type": tokens.get("token_type", "Bearer"),
"expires_in": tokens.get("expires_in", 3600),
diff --git a/tests/connectors/test_connectors_router_oauth.py b/tests/connectors/test_connectors_router_oauth.py
index ef3e8c5e..d8f16b28 100644
--- a/tests/connectors/test_connectors_router_oauth.py
+++ b/tests/connectors/test_connectors_router_oauth.py
@@ -95,20 +95,31 @@ def hermetic_connectors(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path
"openjarvis.connectors.gcontacts",
"openjarvis.connectors.gmail",
"openjarvis.connectors.google_tasks",
+ # Non-Google OAuth providers (Spotify, Strava) derive their default
+ # credentials path from DEFAULT_CONFIG_DIR the same way -- reload
+ # them too so the client-pair-detection tests below are hermetic.
+ "openjarvis.connectors.spotify",
+ "openjarvis.connectors.strava",
]
for name in google_mods:
- if name in sys.modules:
- importlib.reload(sys.modules[name])
+ module = importlib.import_module(name)
+ # Always execute each decorator under the patched config root. Merely
+ # reloading modules that happen to be cached made this fixture depend
+ # on collection order and could leave the registry empty.
+ importlib.reload(module)
router_mod._instances.clear()
yield conn_dir
router_mod._instances.clear()
ConnectorRegistry.clear()
- # Restore the connector modules to their real (unpatched) default paths so
- # subsequent tests in the same process see ~/.openjarvis again.
+ # Restore the config root before reloading. Fixture finalizers otherwise
+ # run in dependency order (this teardown before monkeypatch's), which used
+ # to bake the temporary directory back into module-level defaults.
+ monkeypatch.undo()
for name in google_mods:
- if name in sys.modules:
- importlib.reload(sys.modules[name])
+ module = sys.modules.get(name)
+ if module is not None:
+ importlib.reload(module)
@pytest.fixture()
@@ -182,6 +193,68 @@ def test_connect_raw_token_still_handled(
assert saved.get("token") == "ya29.raw-access-token"
+# ---------------------------------------------------------------------------
+# Non-Google OAuth providers (Spotify, Strava) — the client-pair detection
+# only recognized Google's client ID format (`.apps.googleusercontent.com`),
+# so pasting a Spotify or Strava client_id:client_secret pair silently fell
+# through to the raw-token path instead of registering credentials and
+# returning oauth_required. The UI's "connect this account" flow for any
+# non-Google OAuth connector was unreachable as a result.
+# ---------------------------------------------------------------------------
+
+
+ "connector_id,credentials_file",
+ [("spotify", "spotify.json"), ("strava", "strava.json")],
+)
+def test_connect_client_pair_returns_oauth_required_for_non_google_providers(
+ client: TestClient,
+ hermetic_connectors: Path,
+ connector_id: str,
+ credentials_file: str,
+) -> None:
+ pair = f"{connector_id}-client-id:{connector_id}-client-secret"
+
+ with patch("openjarvis.core.open_browser") as mock_browser:
+ resp = client.post(
+ f"/v1/connectors/{connector_id}/connect", json={"code": pair}
+ )
+
+ assert resp.status_code == 200, resp.text
+ body = resp.json()
+ assert body["status"] == "oauth_required"
+ assert body["oauth_start"] == f"/v1/connectors/{connector_id}/oauth/start"
+ assert body["connected"] is False
+ mock_browser.assert_not_called()
+
+ saved = json.loads((hermetic_connectors / credentials_file).read_text())
+ assert saved["client_id"] == f"{connector_id}-client-id"
+ assert saved["client_secret"] == f"{connector_id}-client-secret"
+
+
+def test_connect_malformed_client_pair_raises_400_for_spotify(
+ client: TestClient,
+) -> None:
+ """A blank secret surfaces an actionable 400 for non-Google providers too."""
+ resp = client.post(
+ "/v1/connectors/spotify/connect", json={"code": "some-client-id:"}
+ )
+ assert resp.status_code == 400
+ assert "CLIENT_ID:CLIENT_SECRET" in resp.json()["detail"]
+
+
+def test_google_client_pair_detection_unaffected_by_generalization(
+ client: TestClient, hermetic_connectors: Path
+) -> None:
+ """Guard against regressing defect A/B: Google's pair must still only be
+ recognized via its distinctive client ID suffix, not just any colon."""
+ resp = client.post("/v1/connectors/gdrive/connect", json={"code": _CLIENT_PAIR})
+ assert resp.status_code == 200, resp.text
+ assert resp.json()["status"] == "oauth_required"
+ saved = json.loads((hermetic_connectors / "gdrive.json").read_text())
+ assert saved["client_id"] == _CLIENT_ID
+
+
# ---------------------------------------------------------------------------
# Defect C-1 — GET /oauth/start must redirect (was HTTP 422)
# ---------------------------------------------------------------------------
@@ -271,3 +344,29 @@ def test_oauth_callback_exchange_failure_renders_error(
assert resp.status_code == 500
assert "Token Exchange Failed" in resp.text
+
+
+def test_oauth_callback_rejects_missing_access_token_without_false_success(
+ client: TestClient,
+ hermetic_connectors: Path,
+) -> None:
+ """Client credentials alone must not complete the consent flow."""
+ import openjarvis.connectors.oauth as oauth_mod
+
+ client.post("/v1/connectors/spotify/connect", json={"code": "id:secret"})
+ with patch.object(
+ oauth_mod,
+ "_exchange_token",
+ return_value={"refresh_token": "refresh-only"},
+ ):
+ resp = client.get("/v1/connectors/spotify/oauth/callback?code=bad-payload")
+
+ assert resp.status_code == 500
+ assert "Token Exchange Failed" in resp.text
+ assert "Connected!" not in resp.text
+ saved = json.loads((hermetic_connectors / "spotify.json").read_text())
+ assert "access_token" not in saved
+
+ from openjarvis.connectors.spotify import SpotifyConnector
+
+ assert SpotifyConnector().is_connected() is False
diff --git a/tests/connectors/test_news_rss.py b/tests/connectors/test_news_rss.py
index dadcf3ef..58cf5ccf 100644
--- a/tests/connectors/test_news_rss.py
+++ b/tests/connectors/test_news_rss.py
@@ -2,8 +2,11 @@
from __future__ import annotations
+import os
+import socket
+import stat
from datetime import datetime
-from unittest.mock import patch
+from unittest.mock import MagicMock, patch
import pytest
@@ -171,3 +174,185 @@ def test_sync_filters_by_since(connector):
def test_disconnect(connector):
connector.disconnect()
assert connector.is_connected() is False
+
+
+def test_configure_rejects_private_network_feed(tmp_path):
+ from openjarvis.connectors.news_rss import NewsRSSConnector
+
+ connector = NewsRSSConnector(config_path=str(tmp_path / "feeds.json"))
+ with pytest.raises(ValueError, match="not allowed"):
+ connector.configure([{"url": "http://127.0.0.1:8080/private.xml"}])
+ assert not (tmp_path / "feeds.json").exists()
+
+
+def test_fetch_feed_rechecks_redirect_target():
+ from openjarvis.connectors.news_rss import _fetch_feed
+
+ redirect = MagicMock()
+ redirect.status_code = 302
+ redirect.headers = {"location": "http://169.254.169.254/latest/meta-data"}
+
+ with (
+ patch(
+ "openjarvis.connectors.news_rss._validate_feed_url",
+ side_effect=[MagicMock(), ValueError("RSS feed URL is not allowed")],
+ ),
+ patch(
+ "openjarvis.connectors.news_rss._request_feed",
+ return_value=redirect,
+ ) as request_feed,
+ pytest.raises(ValueError, match="not allowed"),
+ ):
+ _fetch_feed("https://example.com/feed.xml")
+
+ request_feed.assert_called_once()
+
+
+def test_fetch_feed_pins_the_verified_address_against_dns_rebinding():
+ from openjarvis.connectors.news_rss import _fetch_feed
+
+ public_answer = [
+ (
+ socket.AF_INET,
+ socket.SOCK_STREAM,
+ 6,
+ "",
+ ("93.184.216.34", 80),
+ )
+ ]
+ rebound_private_answer = [
+ (
+ socket.AF_INET,
+ socket.SOCK_STREAM,
+ 6,
+ "",
+ ("127.0.0.1", 80),
+ )
+ ]
+ calls: list[dict[str, object]] = []
+
+ class _Response:
+ status = 200
+
+ def read(self, _limit):
+ return _SAMPLE_RSS.encode()
+
+ def getheaders(self):
+ return [("Content-Type", "application/rss+xml; charset=utf-8")]
+
+ class _Connection:
+ def __init__(self, host, port, *, pinned_ip, timeout):
+ calls.append(
+ {
+ "host": host,
+ "port": port,
+ "pinned_ip": pinned_ip,
+ "timeout": timeout,
+ }
+ )
+
+ def request(self, method, target, *, headers):
+ calls[-1].update(method=method, target=target, headers=headers)
+
+ def getresponse(self):
+ return _Response()
+
+ def close(self):
+ pass
+
+ with (
+ patch("openjarvis.security.ssrf.check_ssrf", return_value=None),
+ patch(
+ "openjarvis.connectors.news_rss.socket.getaddrinfo",
+ side_effect=[public_answer, rebound_private_answer],
+ ) as resolve,
+ patch(
+ "openjarvis.connectors.news_rss._PinnedHTTPConnection",
+ _Connection,
+ ),
+ ):
+ xml = _fetch_feed("http://feeds.example/rss.xml")
+
+ assert "Test Feed" in xml
+ assert resolve.call_count == 1
+ assert calls[0]["host"] == "feeds.example"
+ assert calls[0]["pinned_ip"] == "93.184.216.34"
+ assert calls[0]["headers"]["Host"] == "feeds.example"
+
+
+def test_fetch_feed_rejects_private_answer_after_initial_ssrf_check():
+ from openjarvis.connectors.news_rss import _fetch_feed
+
+ private_answer = [
+ (
+ socket.AF_INET,
+ socket.SOCK_STREAM,
+ 6,
+ "",
+ ("127.0.0.1", 80),
+ )
+ ]
+ with (
+ patch("openjarvis.security.ssrf.check_ssrf", return_value=None),
+ patch(
+ "openjarvis.connectors.news_rss.socket.getaddrinfo",
+ return_value=private_answer,
+ ),
+ patch("openjarvis.connectors.news_rss._PinnedHTTPConnection") as connection,
+ pytest.raises(ValueError, match="non-public IP"),
+ ):
+ _fetch_feed("http://feeds.example/rss.xml")
+
+ connection.assert_not_called()
+
+
+def test_https_connection_uses_url_hostname_for_tls_sni():
+ from openjarvis.connectors.news_rss import _PinnedHTTPSConnection
+
+ raw_socket = MagicMock()
+ tls_socket = MagicMock()
+ ssl_context = MagicMock()
+ ssl_context.wrap_socket.return_value = tls_socket
+
+ with patch(
+ "openjarvis.connectors.news_rss.socket.create_connection",
+ return_value=raw_socket,
+ ) as create_connection:
+ connection = _PinnedHTTPSConnection(
+ "feeds.example",
+ 443,
+ pinned_ip="93.184.216.34",
+ timeout=30.0,
+ context=ssl_context,
+ )
+ connection.connect()
+
+ create_connection.assert_called_once_with(
+ ("93.184.216.34", 443),
+ 30.0,
+ None,
+ )
+ ssl_context.wrap_socket.assert_called_once_with(
+ raw_socket,
+ server_hostname="feeds.example",
+ )
+ assert connection.sock is tls_socket
+
+
+def test_configure_writes_private_atomic_config(tmp_path):
+ from openjarvis.connectors.news_rss import NewsRSSConnector
+
+ path = tmp_path / "feeds.json"
+ connector = NewsRSSConnector(config_path=str(path))
+ with (
+ patch("openjarvis.security.ssrf.check_ssrf", return_value=None),
+ patch(
+ "openjarvis.connectors.news_rss._resolve_host_addresses",
+ return_value=("93.184.216.34",),
+ ),
+ ):
+ connector.configure([{"url": "https://example.com/feed.xml"}])
+
+ assert connector.is_connected()
+ if os.name != "nt":
+ assert stat.S_IMODE(path.stat().st_mode) == 0o600
diff --git a/tests/connectors/test_oauth_flow.py b/tests/connectors/test_oauth_flow.py
index 0402b0d1..47fde03b 100644
--- a/tests/connectors/test_oauth_flow.py
+++ b/tests/connectors/test_oauth_flow.py
@@ -5,6 +5,8 @@ from __future__ import annotations
from pathlib import Path
from unittest.mock import MagicMock, patch
+import pytest
+
def test_exchange_google_token_calls_endpoint() -> None:
from openjarvis.connectors.oauth import exchange_google_token
@@ -30,6 +32,27 @@ def test_exchange_google_token_calls_endpoint() -> None:
mock_post.assert_called_once()
+def test_generic_exchange_rejects_response_without_access_token() -> None:
+ from openjarvis.connectors.oauth import OAUTH_PROVIDERS, _exchange_token
+
+ mock_resp = MagicMock()
+ mock_resp.json.return_value = {"refresh_token": "refresh-only"}
+
+ with (
+ patch("httpx.post", return_value=mock_resp),
+ pytest.raises(RuntimeError, match="access_token"),
+ ):
+ _exchange_token(
+ OAUTH_PROVIDERS["spotify"],
+ "authorization-code",
+ "client-id",
+ "client-secret",
+ "http://127.0.0.1:8888/callback",
+ )
+
+ mock_resp.raise_for_status.assert_called_once()
+
+
def test_gdrive_handle_callback_persists_creds_no_background_flow(
tmp_path: Path,
) -> None:
diff --git a/tests/connectors/test_spotify.py b/tests/connectors/test_spotify.py
index 37f5dde2..ddf20eae 100644
--- a/tests/connectors/test_spotify.py
+++ b/tests/connectors/test_spotify.py
@@ -3,6 +3,7 @@
from __future__ import annotations
from datetime import datetime
+from pathlib import Path
from unittest.mock import patch
import pytest
@@ -70,3 +71,23 @@ def test_sync_yields_tracks(connector):
assert docs[0].doc_type == "recently_played"
assert "Queen" in docs[0].title
assert docs[0].metadata["track_name"] == "Bohemian Rhapsody"
+
+
+ "contents",
+ [
+ '{"client_id": "id", "client_secret": "secret"}',
+ '{"access_token": ""}',
+ "not-json",
+ ],
+)
+def test_client_registration_file_is_not_connected(
+ tmp_path: Path,
+ contents: str,
+) -> None:
+ from openjarvis.connectors.spotify import SpotifyConnector
+
+ token_path = tmp_path / "spotify.json"
+ token_path.write_text(contents, encoding="utf-8")
+
+ assert SpotifyConnector(token_path=str(token_path)).is_connected() is False
diff --git a/tests/connectors/test_strava.py b/tests/connectors/test_strava.py
index 7932862b..e458cf80 100644
--- a/tests/connectors/test_strava.py
+++ b/tests/connectors/test_strava.py
@@ -3,6 +3,7 @@
from __future__ import annotations
from datetime import datetime
+from pathlib import Path
from unittest.mock import patch
import pytest
@@ -73,3 +74,23 @@ def test_sync_yields_activities(connector):
assert docs[0].title == "Morning Run"
assert docs[1].doc_type == "ride"
assert docs[0].metadata["distance_m"] == 5200.0
+
+
+ "contents",
+ [
+ '{"client_id": "id", "client_secret": "secret"}',
+ '{"access_token": " "}',
+ "not-json",
+ ],
+)
+def test_client_registration_file_is_not_connected(
+ tmp_path: Path,
+ contents: str,
+) -> None:
+ from openjarvis.connectors.strava import StravaConnector
+
+ token_path = tmp_path / "strava.json"
+ token_path.write_text(contents, encoding="utf-8")
+
+ assert StravaConnector(token_path=str(token_path)).is_connected() is False
diff --git a/tests/security/test_file_utils.py b/tests/security/test_file_utils.py
new file mode 100644
index 00000000..a2b2ba4f
--- /dev/null
+++ b/tests/security/test_file_utils.py
@@ -0,0 +1,42 @@
+"""Security invariants for atomic private-file persistence."""
+
+from __future__ import annotations
+
+import json
+import os
+import stat
+from pathlib import Path
+from unittest.mock import patch
+
+import pytest
+
+from openjarvis.security.file_utils import secure_write_json
+
+
+def test_secure_write_json_is_owner_only(tmp_path: Path) -> None:
+ path = tmp_path / "credentials.json"
+
+ secure_write_json(path, {"access_token": "secret"})
+
+ assert json.loads(path.read_text(encoding="utf-8")) == {"access_token": "secret"}
+ if os.name != "nt":
+ assert stat.S_IMODE(path.stat().st_mode) == 0o600
+
+
+def test_secure_write_json_preserves_old_file_when_replace_fails(
+ tmp_path: Path,
+) -> None:
+ path = tmp_path / "credentials.json"
+ secure_write_json(path, {"access_token": "old"})
+
+ with (
+ patch(
+ "openjarvis.security.file_utils.os.replace",
+ side_effect=OSError("simulated replace failure"),
+ ),
+ pytest.raises(OSError, match="simulated replace failure"),
+ ):
+ secure_write_json(path, {"access_token": "new"})
+
+ assert json.loads(path.read_text(encoding="utf-8")) == {"access_token": "old"}
+ assert list(tmp_path.glob(".credentials.json.*.tmp")) == []
diff --git a/tests/server/test_connectors_router.py b/tests/server/test_connectors_router.py
index b5526086..a17a0d13 100644
--- a/tests/server/test_connectors_router.py
+++ b/tests/server/test_connectors_router.py
@@ -2,6 +2,9 @@
from __future__ import annotations
+import json
+import os
+import stat
import threading
import time
from pathlib import Path
@@ -556,3 +559,151 @@ def test_connect_granola_invalid_key_returns_400_keeps_existing(
assert json.loads(creds.read_text())["token"] == "grl_real_existing_key"
finally:
_instances.pop("granola", None)
+
+
+ ("connector_id", "filename", "payload", "expected"),
+ [
+ (
+ "github_notifications",
+ "github.json",
+ {"token": "ghp_test"},
+ {"token": "ghp_test"},
+ ),
+ ("oura", "oura.json", {"token": "oura_test"}, {"token": "oura_test"}),
+ (
+ "weather",
+ "weather.json",
+ {"token": "weather_key", "config": {"location": "Boston,US"}},
+ {"api_key": "weather_key", "location": "Boston,US"},
+ ),
+ ],
+)
+def test_connect_persists_generic_token_connector_credentials(
+ app,
+ tmp_path: Path,
+ monkeypatch,
+ connector_id: str,
+ filename: str,
+ payload: dict,
+ expected: dict,
+) -> None:
+ """The generic token panel must actually configure each token connector."""
+
+ from openjarvis.connectors.github_notifications import GitHubNotificationsConnector
+ from openjarvis.connectors.oura import OuraConnector
+ from openjarvis.connectors.weather import WeatherConnector
+ from openjarvis.core.registry import ConnectorRegistry
+ from openjarvis.server.connectors_router import _instances
+
+ path = tmp_path / filename
+ constructors = {
+ "github_notifications": lambda: GitHubNotificationsConnector(
+ token_path=str(path)
+ ),
+ "oura": lambda: OuraConnector(token_path=str(path)),
+ "weather": lambda: WeatherConnector(token_path=str(path)),
+ }
+ instance = constructors[connector_id]()
+ ConnectorRegistry.register_value(connector_id, type(instance))
+ validators = {
+ "github_notifications": (
+ "openjarvis.connectors.github_notifications._github_api_get",
+ [],
+ ),
+ "oura": ("openjarvis.connectors.oura._oura_api_get", {}),
+ "weather": ("openjarvis.connectors.weather._weather_api_get", {}),
+ }
+ target, result = validators[connector_id]
+ monkeypatch.setattr(target, lambda *args, **kwargs: result)
+ # The endpoint deliberately starts an asynchronous initial sync. Replace
+ # it with an empty iterator so this endpoint test never reaches a real API.
+ instance.sync = lambda **_kwargs: iter(())
+ _instances[connector_id] = instance
+ try:
+ resp = app.post(f"/v1/connectors/{connector_id}/connect", json=payload)
+ assert resp.status_code == 200, resp.text
+ assert resp.json()["connected"] is True
+ assert json.loads(path.read_text()) == expected
+ if os.name != "nt":
+ assert stat.S_IMODE(path.stat().st_mode) == 0o600
+ finally:
+ _instances.pop(connector_id, None)
+
+
+def test_invalid_token_is_not_persisted_or_synced(app, tmp_path, monkeypatch) -> None:
+ """Validation failure preserves an existing credential byte-for-byte."""
+ from openjarvis.connectors.github_notifications import (
+ GitHubNotificationsConnector,
+ )
+ from openjarvis.core.registry import ConnectorRegistry
+ from openjarvis.server.connectors_router import _instances
+
+ path = tmp_path / "github.json"
+ original = '{"token":"known-good"}'
+ path.write_text(original, encoding="utf-8")
+ instance = GitHubNotificationsConnector(token_path=str(path))
+ ConnectorRegistry.register_value("github_notifications", type(instance))
+ sync_called = False
+
+ def sync(**kwargs):
+ nonlocal sync_called
+ sync_called = True
+ return iter(())
+
+ instance.sync = sync
+ monkeypatch.setattr(
+ "openjarvis.connectors.github_notifications._github_api_get",
+ lambda *args, **kwargs: (_ for _ in ()).throw(RuntimeError("401 Unauthorized")),
+ )
+ _instances["github_notifications"] = instance
+ try:
+ response = app.post(
+ "/v1/connectors/github_notifications/connect",
+ json={"token": "bad-token"},
+ )
+ assert response.status_code == 400
+ assert path.read_text(encoding="utf-8") == original
+ assert sync_called is False
+ finally:
+ _instances.pop("github_notifications", None)
+
+
+def test_connect_weather_requires_location(app, tmp_path: Path) -> None:
+ """Weather must not report connected with an API key it cannot use."""
+ from openjarvis.connectors.weather import WeatherConnector
+ from openjarvis.server.connectors_router import _instances
+
+ path = tmp_path / "weather.json"
+ _instances["weather"] = WeatherConnector(token_path=str(path))
+ try:
+ resp = app.post("/v1/connectors/weather/connect", json={"token": "weather_key"})
+ assert resp.status_code == 400
+ assert "location" in resp.json()["detail"].lower()
+ assert not path.exists()
+ finally:
+ _instances.pop("weather", None)
+
+
+def test_connect_news_rss_requires_and_persists_feeds(app, tmp_path: Path) -> None:
+ """A local connector with required setup cannot claim success for ``{}``."""
+ from openjarvis.connectors.news_rss import NewsRSSConnector
+ from openjarvis.server.connectors_router import _instances
+
+ path = tmp_path / "news_rss.json"
+ instance = NewsRSSConnector(config_path=str(path))
+ instance.sync = lambda **_kwargs: iter(())
+ _instances["news_rss"] = instance
+ try:
+ assert app.post("/v1/connectors/news_rss/connect", json={}).status_code == 400
+ resp = app.post(
+ "/v1/connectors/news_rss/connect",
+ json={"config": {"feeds": [{"url": "https://example.com/feed.xml"}]}},
+ )
+ assert resp.status_code == 200, resp.text
+ assert resp.json()["connected"] is True
+ assert json.loads(path.read_text())["feeds"] == [
+ {"name": "example.com", "url": "https://example.com/feed.xml"}
+ ]
+ finally:
+ _instances.pop("news_rss", None)