Skip to content

Commit 4a2d511

Browse files
authored
fix: request llm_gateway:read scope during signup provisioning (#435)
1 parent b9a1888 commit 4a2d511

4 files changed

Lines changed: 50 additions & 10 deletions

File tree

src/lib/constants.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,44 @@ export const POSTHOG_DEV_CLIENT_ID = 'DC5uRLVbGI02YQ82grxgnK6Qn12SXWpCqdPb60oZ';
9191
export const POSTHOG_PROXY_CLIENT_ID = POSTHOG_US_CLIENT_ID;
9292
export const DUMMY_PROJECT_API_KEY = '_YOUR_POSTHOG_PROJECT_TOKEN_';
9393

94+
/**
95+
* Scopes the wizard requests during the agentic provisioning signup flow.
96+
*
97+
* Each entry is justified by what the wizard's agent step does after signup:
98+
* - user:read identify the user for analytics + agent context
99+
* - project:read look up the freshly-provisioned project
100+
* - llm_gateway:read authenticate to gateway.{us,eu}.posthog.com/wizard
101+
* (the agent's LLM calls — without this scope, every
102+
* agent message returns 401)
103+
* - query:read run HogQL queries when the agent needs data
104+
* - dashboard:write create the onboarding dashboard during setup
105+
* - insight:write create the onboarding insights during setup
106+
*
107+
* Must be a subset of `ALLOWED_PROVISIONING_SCOPES` in
108+
* `ee/api/agentic_provisioning/views.py` on the backend.
109+
*/
110+
export const WIZARD_PROVISIONING_SCOPES = [
111+
'user:read',
112+
'project:read',
113+
'llm_gateway:read',
114+
'dashboard:write',
115+
'insight:write',
116+
'query:read',
117+
] as const;
118+
119+
/**
120+
* Scopes the wizard requests during the OAuth login flow. Superset of
121+
* `WIZARD_PROVISIONING_SCOPES` with two scopes that only apply to the login
122+
* path and are not in the provisioning allowlist:
123+
* - introspection lets the wizard introspect its own token
124+
* - health_issue:read used by `wizard doctor`
125+
*/
126+
export const WIZARD_OAUTH_SCOPES = [
127+
...WIZARD_PROVISIONING_SCOPES,
128+
'introspection',
129+
'health_issue:read',
130+
] as const;
131+
94132
// ── Wizard run / variants ───────────────────────────────────────────
95133

96134
export const WIZARD_INTERACTION_EVENT_NAME = 'wizard interaction';

src/utils/__tests__/provisioning.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ describe('provisionNewAccount', () => {
8989
(accountCall[1] as Record<string, unknown>).code_challenge,
9090
).toBeTruthy();
9191
expect((accountCall[1] as Record<string, unknown>).client_id).toBeTruthy();
92+
expect((accountCall[1] as Record<string, unknown>).scopes).toEqual([
93+
'user:read',
94+
'project:read',
95+
'llm_gateway:read',
96+
'dashboard:write',
97+
'insight:write',
98+
'query:read',
99+
]);
92100

93101
// Verify token exchange includes code_verifier
94102
const tokenCall = mockedAxios.post.mock.calls[1];

src/utils/provisioning.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
IS_DEV,
1515
POSTHOG_DEV_CLIENT_ID,
1616
POSTHOG_US_CLIENT_ID,
17+
WIZARD_PROVISIONING_SCOPES,
1718
WIZARD_USER_AGENT,
1819
} from '../lib/constants';
1920
import { logToFile } from './debug';
@@ -117,6 +118,7 @@ export async function provisionNewAccount(
117118
client_id: WIZARD_CLIENT_ID,
118119
code_challenge: codeChallenge,
119120
code_challenge_method: 'S256',
121+
scopes: WIZARD_PROVISIONING_SCOPES,
120122
configuration: {
121123
region,
122124
...(opts?.orgName ? { organization_name: opts.orgName } : {}),

src/utils/setup-utils.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
DEFAULT_HOST_URL,
1818
DUMMY_PROJECT_API_KEY,
1919
ISSUES_URL,
20+
WIZARD_OAUTH_SCOPES,
2021
} from '../lib/constants';
2122
import { analytics } from './analytics';
2223
import { getUI } from '../ui';
@@ -489,16 +490,7 @@ async function askForWizardLogin(options: {
489490
}
490491

491492
const tokenResponse = await performOAuthFlow({
492-
scopes: [
493-
'user:read',
494-
'project:read',
495-
'introspection',
496-
'llm_gateway:read',
497-
'dashboard:write',
498-
'insight:write',
499-
'query:read',
500-
'health_issue:read',
501-
],
493+
scopes: [...WIZARD_OAUTH_SCOPES],
502494
signup: false,
503495
});
504496

0 commit comments

Comments
 (0)