@quatronic/sdk 0.6.0-test.9425afd → 0.6.0-test.ff9403f

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.
Files changed (46) hide show
  1. package/README.md +26 -0
  2. package/dist/cli/legacy/qdc-sourcemaps.js +237 -0
  3. package/dist/cli/legacy/qdc-test-report.js +200 -0
  4. package/dist/cli/skills-postinstall.js +603 -0
  5. package/dist/cli/skills.js +499 -0
  6. package/dist/cli/sourcemaps.js +9 -9
  7. package/dist/cli/test-report.js +2 -2
  8. package/dist/next/auth/index.js.map +1 -1
  9. package/dist/next/config/index.js +1 -1
  10. package/dist/next/config/index.js.map +1 -1
  11. package/migrations/no-raw-fetch/codemod.mjs +97 -0
  12. package/migrations/no-raw-fetch/instructions.md +68 -0
  13. package/migrations/no-raw-fetch/meta.json +6 -0
  14. package/package.json +17 -6
  15. package/postinstall.mjs +9 -0
  16. package/skills/.gitattributes +1 -0
  17. package/skills/catalog.json +24 -0
  18. package/skills/qore-add-api-route/SKILL.md +58 -0
  19. package/skills/qore-add-connection/SKILL.next.md +149 -0
  20. package/skills/qore-add-connection/SKILL.node.md +160 -0
  21. package/skills/qore-add-db-table/SKILL.md +88 -0
  22. package/skills/qore-add-db-table/references/cross-module-reference.md +74 -0
  23. package/skills/qore-add-feature/SKILL.md +77 -0
  24. package/skills/qore-add-integration/SKILL.next.md +217 -0
  25. package/skills/qore-add-integration/SKILL.node.md +216 -0
  26. package/skills/qore-add-page/SKILL.md +53 -0
  27. package/skills/qore-author-skill/SKILL.md +136 -0
  28. package/skills/qore-debug-issue/SKILL.md +48 -0
  29. package/skills/qore-enable-auth-method/SKILL.md +91 -0
  30. package/skills/qore-observability/SKILL.md +89 -0
  31. package/skills/qore-observability/references/errors.md +87 -0
  32. package/skills/qore-observability/references/logs.md +69 -0
  33. package/skills/qore-observability/references/tracing.md +58 -0
  34. package/skills/qore-recommended-tools/SKILL.md +58 -0
  35. package/skills/qore-secure-io/SKILL.md +174 -0
  36. package/skills/qore-ship-checklist/SKILL.md +80 -0
  37. package/skills/qore-translate-strings/SKILL.md +36 -0
  38. package/skills/qore-use-addons/SKILL.md +175 -0
  39. package/skills/qore-use-database/SKILL.md +222 -0
  40. package/skills/qore-use-design-system/SKILL.md +76 -0
  41. package/skills/qore-use-mail-domain/SKILL.next.md +67 -0
  42. package/skills/qore-use-mail-domain/SKILL.node.md +62 -0
  43. package/skills/qore-use-permissions/SKILL.md +116 -0
  44. package/skills/qore-verify-user/SKILL.md +56 -0
  45. package/dist/next/auth/migrations/0001_init.sql +0 -31
  46. package/dist/next/auth/migrations/0002_id_token.sql +0 -1
@@ -0,0 +1,116 @@
1
+ ---
2
+ name: qore-use-permissions
3
+ description: Use roles and permissions in this app — declare a permission, map it to a role, and gate server actions, API routes, pages and UI with it. Use when adding access control, adding or renaming a role, or building a feature only some users may use; also when the developer has no role locally, lands on Geen toegang / No access, or wants to test the app as a role (qore roles).
4
+ allowed-tools: Bash(pnpm:*), Bash(qore roles list:*), Bash(qore roles sync:*), Edit, Read
5
+ ---
6
+
7
+ # Use permissions & roles
8
+
9
+ This app authorizes by **permission**, not by raw role. Code declares the right
10
+ it needs (e.g. `app:rename`); a single map binds permissions to Keycloak roles.
11
+ That is the least-privilege principle: each piece of code asks for exactly the
12
+ right it needs, never "are you admin".
13
+
14
+ ## 1. The map — `src/lib/permissions.ts`
15
+
16
+ The only place roles and rights meet. Add a permission by listing it under each
17
+ role that should hold it:
18
+
19
+ ```ts
20
+ export const permissions = definePermissions({
21
+ admin: ["app:rename", "members:invite", "profile:edit"],
22
+ user: ["profile:edit"],
23
+ })
24
+ ```
25
+
26
+ The roles are the keys of this map. Unknown permissions are a compile error —
27
+ fail-closed.
28
+
29
+ ## 2. Gate by surface — use the right form
30
+
31
+ Import server guards from `@/lib/access`. Each surface has an idiomatic form:
32
+
33
+ **Server action** — gate in-body so the action keeps its own error shape:
34
+
35
+ ```ts
36
+ import { can } from "@/lib/access"
37
+ export async function updateThing(prev: State, fd: FormData): Promise<State> {
38
+ if (!(await can("app:rename"))) return { status: "error", message: "No permission" }
39
+ // ...mutate
40
+ }
41
+ ```
42
+
43
+ **API route handler** — wrap; returns 403 before the handler runs:
44
+
45
+ ```ts
46
+ import { withPermission } from "@/lib/access"
47
+ export const POST = withPermission("members:invite", async (req) => { /* ... */ })
48
+ ```
49
+
50
+ **Page / Server Component** — redirect, or load data conditionally:
51
+
52
+ ```ts
53
+ import { requirePermissionOrRedirect, can } from "@/lib/access"
54
+ await requirePermissionOrRedirect("app:rename") // gate the whole page
55
+ if (await can("members:invite")) { /* load extra data */ } // conditional
56
+ ```
57
+
58
+ **Client component (UX only)** — show/disable; never the sole gate:
59
+
60
+ ```tsx
61
+ "use client"
62
+ import { usePermissions, Can } from "@quatronic/sdk/auth"
63
+ import type { Permission } from "@/lib/permissions"
64
+ const { can } = usePermissions<Permission>()
65
+ // <Button isDisabled={!can("app:rename")}>Save</Button>
66
+ // or: <Can permission="members:invite"><InviteButton/></Can>
67
+ ```
68
+
69
+ > The client check is cosmetic. ALWAYS also gate the server action or route —
70
+ > a direct POST must be rejected server-side.
71
+
72
+ ## 3. A new role
73
+
74
+ A role is a new key in the map. The portal reads this file itself: staging and
75
+ production learn roles from the code that environment deploys (staging deploys
76
+ `main`, production the `production` branch), and `local` from the developer's
77
+ checkout through `qore roles`.
78
+
79
+ When you add a key, e.g. `inspector: ["report:read"]`:
80
+
81
+ 1. **Make local know it — run this yourself, in the same turn. Don't ask first.**
82
+ `qore roles sync` announces every role in `permissions.ts` for `local` and
83
+ changes nobody's roles. It needs write access on the module. If qore says
84
+ `sync` is an unknown command, the installed qore is too old: tell the
85
+ developer to run `npm i -g @quatronic/qore`. Any other failure: pass the
86
+ error on.
87
+ 2. **Ask the developer** whether they want the role on their own account on
88
+ local, and with which other roles. Run `qore roles list` first so the
89
+ question names what they hold now, e.g. "Je hebt nu `admin`. Wil je
90
+ `inspector` erbij (`admin inspector`), alleen `inspector` (om de app als
91
+ inspector te zien), of niets veranderen?"
92
+ 3. On yes, run `qore roles use <the roles they chose>`. It sets their direct
93
+ roles on local to exactly those — anything left out is dropped.
94
+ Only their direct roles change — roles they get through a group stay.
95
+ 4. Tell them to sign out and back in to the local app — the old session still
96
+ has the old roles.
97
+ 5. After merge and deploy the role exists on staging and production, but nobody
98
+ holds it there yet: it is granted in the portal's Access tab.
99
+
100
+ The same applies when the developer has no role on local, lands on
101
+ **Geen toegang** / **No access**, or wants to test as a role: run
102
+ `qore roles list`; if it shows roles that are new in the code, run
103
+ `qore roles sync` yourself first; then ask, then `qore roles use`.
104
+
105
+ Renaming a role is removing one and adding another; once code without the old
106
+ role deploys, the portal marks it deprecated in that environment's vocabulary
107
+ and it stops meaning anything there. Do it in two deploys: add the new key,
108
+ move people over in the Access tab, then delete the old key.
109
+
110
+ ## 4. Check
111
+
112
+ Run `pnpm lint && pnpm typecheck && pnpm test:run && pnpm build`, then
113
+ /qore-ship-checklist before a PR.
114
+
115
+ > `hasRole` from `@/lib/roles` still exists for cases that genuinely key off a
116
+ > raw role, but prefer permissions.
@@ -0,0 +1,56 @@
1
+ ---
2
+ name: qore-verify-user
3
+ description: Verify the calling end-user's identity (Better Auth JWT or Keycloak access token) on top of the connection check, using @quatronic/sdk/node/identity. Use when a backend endpoint must know which end-user is acting, not just which module.
4
+ allowed-tools: Bash(pnpm:*), Edit, Read
5
+ ---
6
+
7
+ # Verify the calling user
8
+
9
+ The connection token proves *a wired module is calling*. To also learn *which
10
+ end-user*, verify the forwarded Better Auth JWT or Keycloak access token with `requireUser` / `withUser`.
11
+
12
+ ## 1. Preconditions — set by the portal
13
+
14
+ The portal injects `CONN_<ALIAS>_ISSUER` when the calling module is a Better
15
+ Auth or a Keycloak frontend, and `QORE_MODULE` (this module's slug) on every
16
+ deploy. Without the issuer `requireUser` throws `missing_issuer` (500).
17
+ For a Keycloak caller, a missing `QORE_MODULE` is refused with 500: Keycloak
18
+ tokens are only accepted when their `aud` names this module (and `typ` is
19
+ `Bearer`).
20
+
21
+ ## 2. Guard an incoming request
22
+
23
+ Wrap your handler with `withUser` from `@quatronic/sdk/node/identity`. It returns
24
+ 401 (no/invalid connection token or JWT) or 500 (issuer not pinned) automatically,
25
+ and on success hands you `{ alias, user }`.
26
+
27
+ ## 3. Authorize on roles
28
+
29
+ `user.roles` holds the caller's roles in the calling app (Keycloak: only the
30
+ app client's roles, no realm roles; Better Auth: the `roles` claim). `user.app`
31
+ is the calling Keycloak app client. 403 the request yourself when a role is
32
+ missing.
33
+
34
+ ## 3b. Look up other users (Keycloak callers)
35
+
36
+ After `requireUser`/`withUser`, `userDirectoryFor(req)` from
37
+ `@quatronic/sdk/node/users` lists or resolves the users of the calling app —
38
+ never another app's, even when this backend serves several. Worked example:
39
+ `src/examples/user-directory.example.ts`. Jobs without a user cannot use it.
40
+
41
+ - Pass the *same* `Request` object that went through `requireUser`. A clone or
42
+ a new `Request` throws `DirectoryError("unauthorized")`, as does a token
43
+ without `azp`.
44
+ - A caller with no client role on its app gets `DirectoryError("forbidden")`.
45
+ - `withUser` does not map `DirectoryError`: catch it in the handler and return
46
+ `err.status` (401/403/400/413/503, safe to expose) with `err.code`, as the
47
+ example does. Otherwise a missing role or a Keycloak outage becomes a 500.
48
+
49
+ ## 4. Ship it
50
+
51
+ Dispatch the guarded handler from the request handler in `src/index.ts`;
52
+ tracing is automatic.
53
+
54
+ > Worked examples: `src/examples/user-auth.example.ts`, `src/examples/user-directory.example.ts`.
55
+ > The frontend side forwards the token via its `callBackend` helper (Keycloak and
56
+ > Better Auth templates both ship one).
@@ -1,31 +0,0 @@
1
- CREATE SCHEMA IF NOT EXISTS auth;
2
-
3
- CREATE TABLE IF NOT EXISTS auth.sessions (
4
- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
5
- user_sub varchar(64) NOT NULL,
6
- user_email varchar(320),
7
- organization_id varchar(64),
8
- organization_alias varchar(255),
9
- access_token text NOT NULL,
10
- access_expires_at timestamptz NOT NULL,
11
- refresh_ct bytea NOT NULL,
12
- refresh_iv bytea NOT NULL,
13
- refresh_expires_at timestamptz NOT NULL,
14
- created_at timestamptz NOT NULL DEFAULT now(),
15
- updated_at timestamptz NOT NULL DEFAULT now(),
16
- revoked_at timestamptz
17
- );
18
-
19
- CREATE INDEX IF NOT EXISTS ix_auth_sessions_user_sub
20
- ON auth.sessions(user_sub) WHERE revoked_at IS NULL;
21
-
22
- CREATE TABLE IF NOT EXISTS auth.login_state (
23
- state varchar(128) PRIMARY KEY,
24
- code_verifier varchar(255) NOT NULL,
25
- redirect_path text NOT NULL DEFAULT '/',
26
- created_at timestamptz NOT NULL DEFAULT now(),
27
- expires_at timestamptz NOT NULL
28
- );
29
-
30
- CREATE INDEX IF NOT EXISTS ix_auth_login_state_expires
31
- ON auth.login_state(expires_at);
@@ -1 +0,0 @@
1
- ALTER TABLE auth.sessions ADD COLUMN IF NOT EXISTS id_token text;