@ory/argus 0.9.0 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -16,7 +16,7 @@ const session = await client.verifySession(sessionToken);
16
16
  const result = await client.checkPermission({
17
17
  namespace: "AgentTools",
18
18
  object: "Bash",
19
- relation: "invoke",
19
+ relation: "use",
20
20
  subjectId: `session:${sessionId}`,
21
21
  });
22
22
 
@@ -57,7 +57,7 @@ async function onBeforeTool(toolName: string, sessionId: string) {
57
57
  const result = await client.checkPermission({
58
58
  namespace: "AgentTools",
59
59
  object: toolName,
60
- relation: "invoke",
60
+ relation: "use",
61
61
  subjectId: `session:${sessionId}`,
62
62
  });
63
63
  if (result.allowed === false) {
@@ -84,7 +84,7 @@ The wrapped Ory client. One instance per harness session.
84
84
  | Sessions and tokens | `verifySession`, `introspectToken`, `classifyError` |
85
85
  | Permission checks | `checkPermission`, `batchCheckPermissions`, `checkMcpPermission` |
86
86
  | Principals (who is acting) | `setUserPrincipal`, `setAgentPrincipal` |
87
- | Delegation tuples | `createRelationship`, `deleteRelationship` |
87
+ | Delegation relations | `createRelationship`, `deleteRelationship` |
88
88
  | Tracing | `tracer` (see Tracer below) |
89
89
 
90
90
  ### Identity gates
@@ -32,6 +32,48 @@ If you find yourself writing `flow.ui.nodes.map(...)` to render auth UI,
32
32
  stop and switch to Ory Elements. Custom node rendering is a fallback,
33
33
  not a parallel option.
34
34
 
35
+ ## Correctness contract — satisfy these or the app will 404 / 500 / CSRF
36
+
37
+ Almost every "Ory doesn't work" report comes from one of four wiring
38
+ mistakes in the generated app — **not** from Ory itself and **not** from
39
+ the agent plugin governing this session. Treat each as a hard requirement,
40
+ and verify all four (Step 10) before telling the user the app is ready.
41
+
42
+ 1. **Ory must be first-party (same site as your app), or every flow fails
43
+ CSRF.** Ory's browser flows depend on a CSRF cookie. If the browser calls
44
+ Ory on a *different* site than your app, the browser drops that cookie and
45
+ you get `CSRF token mismatch`, 403s, or redirect loops. So the browser SDK
46
+ URL must be **your own origin** — never `https://<slug>.projects.oryapis.com`
47
+ directly:
48
+ - **Local dev against Ory Network** — run `ory tunnel` and point the SDK at
49
+ the tunnel (`http://localhost:4000`). See Step 9.
50
+ - **Local dev against the local stack** — the gateway is already on
51
+ `http://localhost:4000` (first-party to `localhost`). Use `http://localhost`,
52
+ not `127.0.0.1`.
53
+ - **Production** — serve Ory from your own domain via an Ory Network custom
54
+ domain (CNAME) or the `@ory/nextjs` proxy, and set the SDK URL to that.
55
+ 2. **The app's routes must match what Ory redirects to, or you get 404s.** Ory
56
+ redirects the browser to the self-service UI URLs configured on the project
57
+ (login, registration, recovery, verification, settings, **error**). If a page
58
+ lives at `/auth/login` but the project points at `/login`, the redirect 404s.
59
+ Pick one set of paths and make these four agree: the page files you create,
60
+ the `@ory/nextjs` UI-path config, the project's `selfservice.flows.*.ui_url`
61
+ (+ `error.ui_url`), and the middleware path lists. Always create an error
62
+ page — Ory redirects there on any flow error. See Step 8.
63
+ 3. **Server-side session/flow calls must forward the request cookies, or they
64
+ 500/401.** A `FrontendApi` built with `credentials: "include"` only sends
65
+ cookies in the **browser**. In Next.js server components, route handlers, and
66
+ middleware you must forward the incoming request's cookies — the `@ory/nextjs`
67
+ server helpers do this for you. Reusing the browser client on the server is
68
+ the single most common 500. See Step 7.
69
+ 4. **The SDK URL env var must be set at runtime, or the SDK 500s on
70
+ construction.** Validate `NEXT_PUBLIC_ORY_SDK_URL` (browser) / `ORY_SDK_URL`
71
+ (server) at startup and fail with a clear message instead of constructing the
72
+ client with `undefined`. See Step 4.
73
+
74
+ If a flow still fails after all four hold, isolate it with the **App bug vs.
75
+ plugin bug** triage in Step 10 before assuming a bug in Ory or in the plugin.
76
+
35
77
  ## Step 1: Check prerequisites
36
78
 
37
79
  Before installing the Ory CLI, decide where the auth backend will run:
@@ -107,29 +149,54 @@ React island or a small client bundle so you still get the Elements UI
107
149
  on the auth routes. Rendering flow nodes directly on the server is the
108
150
  fallback of last resort.
109
151
 
152
+ **Match the installed major versions.** `@ory/nextjs` and
153
+ `@ory/elements-react` change import names and component props across major
154
+ versions. After installing, run `npm ls @ory/nextjs @ory/elements-react`
155
+ and follow the docs and TypeScript types for *that* major — do not assume
156
+ symbol names from memory. A missing import or a prop type error at build
157
+ time is a version mismatch in the generated app, not a plugin bug.
158
+
110
159
  ## Step 4: Configure the Ory SDK
111
160
 
112
- Create a shared Ory client configuration. The SDK URL should come
113
- from an environment variable:
161
+ For **Next.js**, prefer wiring Ory through `@ory/nextjs` (its config proxies
162
+ Ory under your own origin and supplies cookie-forwarding server helpers — this
163
+ satisfies contract #1 and #3 for free). Build a hand-rolled `FrontendApi`
164
+ client only for **browser** code in a React SPA, or for explicit client-side
165
+ calls.
166
+
167
+ When you do build a client, read the URL from an env var and **validate it** —
168
+ constructing the SDK with an `undefined` `basePath` is a 500 waiting to happen
169
+ (contract #4):
114
170
 
115
171
  ```typescript
116
172
  import { Configuration, FrontendApi } from "@ory/client-fetch";
117
173
 
174
+ const sdkUrl = process.env.NEXT_PUBLIC_ORY_SDK_URL ?? process.env.ORY_SDK_URL;
175
+ if (!sdkUrl) {
176
+ throw new Error(
177
+ "Ory SDK URL is not set. Set NEXT_PUBLIC_ORY_SDK_URL (browser) " +
178
+ "or ORY_SDK_URL (server) — see Step 4.",
179
+ );
180
+ }
181
+
182
+ // Browser-only: `credentials: "include"` sends cookies from browser code.
183
+ // Do NOT reuse this client in server components / route handlers / middleware
184
+ // (contract #3) — use the @ory/nextjs server helpers there.
118
185
  const ory = new FrontendApi(
119
- new Configuration({
120
- basePath: process.env.NEXT_PUBLIC_ORY_SDK_URL || process.env.ORY_SDK_URL,
121
- credentials: "include",
122
- })
186
+ new Configuration({ basePath: sdkUrl, credentials: "include" }),
123
187
  );
124
188
 
125
189
  export default ory;
126
190
  ```
127
191
 
128
- Add to the project's `.env` or `.env.local`:
192
+ Add to the project's `.env` or `.env.local`. The **browser** value
193
+ (`NEXT_PUBLIC_ORY_SDK_URL`) must be your own origin (contract #1), so in local
194
+ development point it at the tunnel or the local gateway, not at `*.oryapis.com`:
129
195
 
130
196
  ```bash
131
- NEXT_PUBLIC_ORY_SDK_URL=https://<project-slug>.projects.oryapis.com
132
- # or
197
+ # Local dev (Ory Tunnel for Network, or the local stack gateway):
198
+ NEXT_PUBLIC_ORY_SDK_URL=http://localhost:4000
199
+ # Server-side only (safe to be the direct project URL):
133
200
  ORY_SDK_URL=https://<project-slug>.projects.oryapis.com
134
201
  ```
135
202
 
@@ -191,17 +258,27 @@ with the user that an Ory Elements island is not viable.
191
258
 
192
259
  ## Step 7: Add session middleware
193
260
 
194
- Protect authenticated routes by checking the session:
261
+ Protect authenticated routes by checking the session. **Where this code runs
262
+ matters** (contract #3):
195
263
 
196
- ```typescript
197
- const session = await ory.toSession();
198
- if (!session) {
199
- // Redirect to login
200
- }
201
- ```
264
+ - **Browser code** can use the `credentials: "include"` client from Step 4
265
+ directly the browser attaches the session cookie:
266
+
267
+ ```typescript
268
+ const session = await ory.toSession(); // browser only
269
+ if (!session) {
270
+ // Redirect to login
271
+ }
272
+ ```
273
+
274
+ - **Server code** (server components, route handlers, middleware) must forward
275
+ the incoming request's cookies. Do **not** reuse the browser client — it has
276
+ no cookies on the server and will throw (→ 500) or always return 401. Use the
277
+ `@ory/nextjs` server helpers, which read and forward the request cookies for
278
+ you.
202
279
 
203
280
  For Next.js, prefer the official `@ory/nextjs` middleware helper since
204
- it pairs with the Elements pages:
281
+ it pairs with the Elements pages and handles server-side cookie forwarding:
205
282
 
206
283
  ```typescript
207
284
  import { createOryMiddleware } from "@ory/nextjs/middleware";
@@ -216,37 +293,118 @@ export const config = {
216
293
  };
217
294
  ```
218
295
 
219
- ## Step 8: Configure allowed redirect URLs
220
-
221
- Update the Ory project to allow redirects back to your app:
222
-
223
- ```bash
224
- ory patch project <project-id> \
225
- --replace '/services/identity/config/selfservice/allowed_return_urls=["http://localhost:3000", "https://your-domain.com"]'
226
- ```
227
-
228
- ## Step 9: Set up the Ory Tunnel for local development
229
-
230
- For local development, use the Ory tunnel to proxy requests and handle cookies:
231
-
232
- ```bash
233
- ory tunnel http://localhost:3000 --project <project-slug>
234
- ```
235
-
236
- This runs a proxy on `http://localhost:4000` that handles cookie domains
237
- correctly for local development. Update your SDK URL to point to the
238
- tunnel during development.
239
-
240
- ## Step 10: Verify the setup
241
-
242
- 1. Start the development server
243
- 2. Navigate to the login page — confirm Ory Elements renders the form,
244
- including any social login buttons configured on the project
245
- 3. Create a test account via the registration page
246
- 4. Verify login works
247
- 5. Test account recovery flow
248
- 6. Test the verification flow
249
- 7. Check that protected routes redirect unauthenticated users
296
+ ## Step 8: Align the project's routes and return URLs with your app
297
+
298
+ This step prevents the 404s in contract #2. Ory redirects the browser to the
299
+ self-service UI URLs it has configured; those must point at pages your app
300
+ actually serves.
301
+
302
+ **Pick one route convention and use it everywhere.** This guide uses
303
+ `/auth/login`, `/auth/registration`, `/auth/recovery`, `/auth/verification`,
304
+ `/auth/settings`, and `/auth/error`. The page files, the middleware path lists,
305
+ and the project config below must all use the same paths.
306
+
307
+ 1. **Create an error page.** Ory redirects to the error UI on *any* flow error;
308
+ if it doesn't exist you get a 404 (or a blank page) instead of a readable
309
+ message. Create `app/auth/error/page.tsx` rendering the Elements error
310
+ component (`<Error>` / the flow-error view for your installed version).
311
+
312
+ 2. **Tell Ory where the pages are.**
313
+ - **Next.js with `@ory/nextjs`:** declare the UI paths in its config (the
314
+ UI-path overrides in `ory.config.ts`) so its helpers, middleware, and proxy
315
+ route to your pages. The SDK serves the UI under your own origin, so you
316
+ usually do not also edit the project's `ui_url`s.
317
+ - **React SPA / no `@ory/nextjs` proxy:** set the project's self-service UI
318
+ URLs to your app's routes:
319
+
320
+ ```bash
321
+ ory patch project <project-id> \
322
+ --replace '/services/identity/config/selfservice/flows/login/ui_url="https://your-domain.com/auth/login"' \
323
+ --replace '/services/identity/config/selfservice/flows/registration/ui_url="https://your-domain.com/auth/registration"' \
324
+ --replace '/services/identity/config/selfservice/flows/recovery/ui_url="https://your-domain.com/auth/recovery"' \
325
+ --replace '/services/identity/config/selfservice/flows/verification/ui_url="https://your-domain.com/auth/verification"' \
326
+ --replace '/services/identity/config/selfservice/flows/settings/ui_url="https://your-domain.com/auth/settings"' \
327
+ --replace '/services/identity/config/selfservice/flows/error/ui_url="https://your-domain.com/auth/error"'
328
+ ```
329
+
330
+ 3. **Allow redirects back to your app** (every origin you run on — dev and prod):
331
+
332
+ ```bash
333
+ ory patch project <project-id> \
334
+ --replace '/services/identity/config/selfservice/allowed_return_urls=["http://localhost:3000", "https://your-domain.com"]'
335
+ ```
336
+
337
+ ## Step 9: Serve Ory first-party (required — contract #1)
338
+
339
+ The browser must reach Ory on the same site as your app, or cookie-based flows
340
+ fail CSRF. How you achieve that depends on the environment:
341
+
342
+ - **Local dev against Ory Network — run the Ory Tunnel.** This is required, not
343
+ optional:
344
+
345
+ ```bash
346
+ ory tunnel http://localhost:3000 --project <project-slug>
347
+ ```
348
+
349
+ It runs a proxy on `http://localhost:4000` that serves Ory first-party to
350
+ `localhost`. Point `NEXT_PUBLIC_ORY_SDK_URL` at the tunnel
351
+ (`http://localhost:4000`) — **not** at `https://<slug>.projects.oryapis.com`.
352
+
353
+ - **Local dev against the local stack** — the gateway is already first-party on
354
+ `http://localhost:4000`; no tunnel needed (switch to {{REF_LOCAL_DEV}}).
355
+
356
+ - **Production** — serve Ory from your own domain via an Ory Network custom
357
+ domain (CNAME) or the `@ory/nextjs` proxy, and set the browser SDK URL to that
358
+ same-origin path.
359
+
360
+ If you skip this step, login will appear to work but `whoami`/`toSession` will
361
+ return 401 and form submits will fail with a CSRF error — the classic symptom of
362
+ a cross-site SDK URL.
363
+
364
+ ## Step 10: Verify the setup — and confirm it's correct, not just present
365
+
366
+ Do not declare success on "the pages render." Run these checks; each maps to a
367
+ contract item so a failure points straight at the cause.
368
+
369
+ 1. **SDK URL is first-party (contract #1).** Confirm the browser SDK URL is your
370
+ own origin (the tunnel/gateway in dev), not `*.oryapis.com`. In the browser
371
+ devtools Network tab, the flow requests should go to your origin and the
372
+ response should `Set-Cookie` a CSRF cookie.
373
+ 2. **The flow API itself works (isolates Ory from your app).** Against the URL
374
+ the browser uses:
375
+
376
+ ```bash
377
+ curl -i "$NEXT_PUBLIC_ORY_SDK_URL/self-service/login/browser"
378
+ ```
379
+
380
+ Expect `200` with a `Set-Cookie: csrf_token...` header. If this fails, the
381
+ problem is Ory config / the tunnel — not your app code.
382
+ 3. **Every redirect target exists (contract #2).** Visit `/auth/login`,
383
+ `/auth/registration`, `/auth/recovery`, `/auth/verification`,
384
+ `/auth/settings`, and `/auth/error` directly. Each must return `200`, not
385
+ `404`. A 404 here means a route/`ui_url` mismatch (Step 8).
386
+ 4. **End-to-end:** register a test account, log in, confirm a protected route
387
+ loads while signed in and redirects to login when signed out, then test
388
+ recovery and verification.
389
+
390
+ ### App bug vs. plugin bug
391
+
392
+ These are two different systems. Keep them straight so issues are filed in the
393
+ right place:
394
+
395
+ - **The generated app** (login pages, CSRF, sessions, the `/auth/*` routes) is
396
+ ordinary code running in the user's project. A `404`/`500`/CSRF on an `/auth/*`
397
+ route, or in the browser Network tab, is an **app/config** issue — work the
398
+ four contract items and the checks above.
399
+ - **The agent plugin** governs *this coding session* — it authenticates the
400
+ agent, checks Ory Permissions before each tool call, and writes trace spans. It
401
+ never serves your app's HTTP routes. Diagnose it with
402
+ `{{NPX}} status` and the debug log (`ORY_AGENT_DEBUG=true`), **not** by looking
403
+ at your app's auth pages.
404
+
405
+ Quick triage: if `curl` to the flow API (check 2) succeeds but the app page
406
+ fails, it's the app. If `curl` fails, it's Ory/tunnel config. Neither is the
407
+ plugin unless `{{NPX}} status` reports a problem.
250
408
 
251
409
  ## Customization
252
410
 
@@ -24,6 +24,27 @@ rendering belongs in the fallback section at the end of this skill.
24
24
  no per-app rewrite needed.
25
25
  - Works in Next.js (App Router and Pages Router) and any React SPA.
26
26
 
27
+ ## Avoid the 404 / 500 / CSRF traps
28
+
29
+ These pages only work if the surrounding wiring is correct — most "the login
30
+ page is broken" reports are wiring, not the pages or the plugin. If you have not
31
+ run {{REF_AUTH_SETUP}}, do that first; it establishes the four invariants these
32
+ pages depend on:
33
+
34
+ - **First-party SDK URL** — the browser must reach Ory on your own origin (the
35
+ Ory Tunnel or local gateway, `http://localhost:4000` in dev), never
36
+ `https://<slug>.projects.oryapis.com` directly, or every form submit fails
37
+ with a CSRF error.
38
+ - **Routes match the project config** — the `/auth/*` paths below must match the
39
+ project's self-service `ui_url`s and the middleware lists, and you must create
40
+ an `/auth/error` page, or Ory's redirects 404.
41
+ - **Server calls forward cookies** — server-side session/flow reads must forward
42
+ the request cookies (the `@ory/nextjs` server helpers do this); reusing the
43
+ browser client on the server 500s.
44
+
45
+ See the **Correctness contract** and the **App bug vs. plugin bug** triage in
46
+ {{REF_AUTH_SETUP}} for the full detail.
47
+
27
48
  ## Before you start
28
49
 
29
50
  Check the project setup:
@@ -270,18 +291,39 @@ Initialize with `createBrowserSettingsFlow` and render
270
291
  component handles password changes, profile traits, MFA enrollment,
271
292
  and connected social providers.
272
293
 
294
+ ## Build the error page (do not skip — missing it causes 404s)
295
+
296
+ Ory redirects the browser to the error UI on **any** flow error (expired flow,
297
+ validation failure, misconfiguration). If that route doesn't exist the user
298
+ hits a 404 or a blank page instead of a readable message, and the failure looks
299
+ like a plugin bug when it's just a missing page.
300
+
301
+ Create `app/auth/error/page.tsx` and render the Elements error view for your
302
+ installed version (`getFlowError` from `@ory/nextjs/app` + the Elements error
303
+ component). Make sure its path matches the project's `error.ui_url`
304
+ (see {{REF_AUTH_SETUP}}, Step 8). For a React SPA, fetch the error with
305
+ `getFlowError({ id })` from the `error` query param and display
306
+ `error.error.message`.
307
+
273
308
  ## Add session management
274
309
 
275
- Create a utility to check the current session:
310
+ Create a **browser** utility to check the current session. (For server
311
+ components, route handlers, and middleware, use the `@ory/nextjs` server
312
+ helpers instead — they forward the request cookies. Reusing this browser client
313
+ on the server has no cookies and will 500/401.)
276
314
 
277
315
  ```typescript
278
316
  import { FrontendApi, Configuration, Session } from "@ory/client-fetch";
279
317
 
318
+ const sdkUrl = process.env.NEXT_PUBLIC_ORY_SDK_URL;
319
+ if (!sdkUrl) {
320
+ throw new Error("NEXT_PUBLIC_ORY_SDK_URL is not set — see ory-auth-setup Step 4.");
321
+ }
322
+
323
+ // Browser-only client. `credentials: "include"` only sends cookies in the browser,
324
+ // and sdkUrl must be your own origin (the tunnel/gateway in dev), not *.oryapis.com.
280
325
  const ory = new FrontendApi(
281
- new Configuration({
282
- basePath: process.env.NEXT_PUBLIC_ORY_SDK_URL,
283
- credentials: "include",
284
- })
326
+ new Configuration({ basePath: sdkUrl, credentials: "include" }),
285
327
  );
286
328
 
287
329
  export async function getSession(): Promise<Session | null> {
@@ -345,14 +387,33 @@ falling back to custom node rendering.
345
387
 
346
388
  ## Test the flow
347
389
 
348
- 1. Start the dev server and Ory tunnel (if developing locally)
349
- 2. Visit `/auth/registration` to create an account — confirm Elements
350
- renders all configured methods (password, social, passkey, etc.)
351
- 3. Visit `/auth/login` to sign in
352
- 4. Verify session is established (check protected routes)
353
- 5. Test `/auth/recovery` with a registered email
354
- 6. Test `/auth/settings` for profile changes
355
- 7. Test logout
390
+ Don't stop at "the page renders." Run these in order each one isolates a
391
+ different failure class:
392
+
393
+ 1. Start the dev server **and the Ory tunnel** (local dev against Network), or
394
+ the local stack. Confirm the browser SDK URL is your own origin, not
395
+ `*.oryapis.com`.
396
+ 2. **Flow API reachable (isolates Ory from your app):**
397
+
398
+ ```bash
399
+ curl -i "$NEXT_PUBLIC_ORY_SDK_URL/self-service/login/browser"
400
+ ```
401
+
402
+ Expect `200` and a `Set-Cookie: csrf_token...` header. If this fails it's the
403
+ tunnel / Ory config, not your pages.
404
+ 3. **No 404s:** visit `/auth/login`, `/auth/registration`, `/auth/recovery`,
405
+ `/auth/verification`, `/auth/settings`, and `/auth/error` — each returns
406
+ `200`. A 404 means a route/`ui_url` mismatch.
407
+ 4. Register at `/auth/registration` — confirm Elements renders all configured
408
+ methods (password, social, passkey, etc.).
409
+ 5. Sign in at `/auth/login`; confirm a protected route loads, and redirects to
410
+ login when signed out.
411
+ 6. Test `/auth/recovery` with a registered email, `/auth/settings` for profile
412
+ changes, and logout.
413
+
414
+ If a submit fails with a CSRF error, the SDK URL is cross-site (trap #1). If a
415
+ redirect 404s, a route doesn't match the project config (trap #2). Neither is a
416
+ plugin bug — see the **App bug vs. plugin bug** triage in {{REF_AUTH_SETUP}}.
356
417
 
357
418
  ## Fallback: rendering UI nodes by hand
358
419
 
@@ -33,6 +33,15 @@ recommend switching to Ory Elements before adding more providers.
33
33
  recommend migrating to Ory Elements so social buttons render for
34
34
  free.
35
35
 
36
+ **Social login depends on the same wiring as the rest of Ory.** Before
37
+ debugging providers, confirm the base app is correct per the Correctness
38
+ contract in {{REF_AUTH_SETUP}}: a first-party SDK URL (the tunnel or local
39
+ gateway in dev, never `*.oryapis.com` in the browser), routes that match the
40
+ project config, and your app origin listed in `allowed_return_urls`. The
41
+ provider redirects the browser back *through Ory* to your app, so a missing
42
+ return URL or a cross-site SDK URL produces a post-login error or 404 that looks
43
+ like a provider bug but isn't.
44
+
36
45
  ## Step 1: Choose providers
37
46
 
38
47
  Ask the user which social login providers they want. Common options:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ory/argus",
3
- "version": "0.9.0",
3
+ "version": "0.9.1",
4
4
  "description": "Ory Argus: the core API for building authentication, authorization, and audit into AI agent harness plugins, extensions, and custom integrations",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://ory.com",