@pikku/cli 0.12.40 → 0.12.42
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/dist/.pikku/agent/pikku-agent-types.gen.d.ts +1 -1
- package/dist/.pikku/channel/pikku-channel-types.gen.d.ts +3 -3
- package/dist/.pikku/channel/pikku-channel-types.gen.js +1 -1
- package/dist/.pikku/cli/pikku-cli-channel.d.ts +6 -6
- package/dist/.pikku/cli/pikku-cli-channel.js +21 -1
- package/dist/.pikku/cli/pikku-cli-types.gen.d.ts +1 -1
- package/dist/.pikku/cli/pikku-cli-types.gen.js +1 -1
- package/dist/.pikku/cli/pikku-cli-wirings-meta.gen.js +1 -1
- package/dist/.pikku/cli/pikku-cli-wirings-meta.gen.json +68 -0
- package/dist/.pikku/cli/pikku-cli-wirings.gen.d.ts +1 -1
- package/dist/.pikku/cli/pikku-cli-wirings.gen.js +1 -1
- package/dist/.pikku/cli/pikku-cli.gen.d.ts +1 -1
- package/dist/.pikku/cli/pikku-cli.gen.js +1 -1
- package/dist/.pikku/console/pikku-node-types.gen.d.ts +1 -1
- package/dist/.pikku/function/pikku-function-types.gen.d.ts +2 -2
- package/dist/.pikku/function/pikku-function-types.gen.js +1 -1
- package/dist/.pikku/function/pikku-functions-meta.gen.js +1 -1
- package/dist/.pikku/function/pikku-functions-meta.gen.json +152 -82
- package/dist/.pikku/function/pikku-functions.gen.js +1 -1
- package/dist/.pikku/http/pikku-http-types.gen.d.ts +1 -1
- package/dist/.pikku/http/pikku-http-types.gen.js +1 -1
- package/dist/.pikku/http/pikku-http-wirings-meta.gen.js +1 -1
- package/dist/.pikku/http/pikku-http-wirings.gen.d.ts +1 -1
- package/dist/.pikku/http/pikku-http-wirings.gen.js +1 -1
- package/dist/.pikku/mcp/pikku-mcp-types.gen.d.ts +1 -1
- package/dist/.pikku/mcp/pikku-mcp-types.gen.js +1 -1
- package/dist/.pikku/pikku-bootstrap.gen.d.ts +1 -1
- package/dist/.pikku/pikku-bootstrap.gen.js +1 -1
- package/dist/.pikku/pikku-meta-service.gen.d.ts +1 -1
- package/dist/.pikku/pikku-meta-service.gen.js +1 -1
- package/dist/.pikku/pikku-services.gen.d.ts +1 -1
- package/dist/.pikku/pikku-types.gen.d.ts +1 -1
- package/dist/.pikku/pikku-types.gen.js +1 -1
- package/dist/.pikku/queue/pikku-queue-types.gen.d.ts +1 -1
- package/dist/.pikku/queue/pikku-queue-types.gen.js +1 -1
- package/dist/.pikku/queue/pikku-queue-workers-wirings-meta.gen.js +1 -1
- package/dist/.pikku/queue/pikku-queue-workers-wirings.gen.d.ts +1 -1
- package/dist/.pikku/queue/pikku-queue-workers-wirings.gen.js +1 -1
- package/dist/.pikku/rpc/pikku-rpc-wirings-meta.internal.gen.js +1 -1
- package/dist/.pikku/rpc/pikku-rpc-wirings-meta.internal.gen.json +4 -0
- package/dist/.pikku/scheduler/pikku-scheduler-types.gen.d.ts +1 -1
- package/dist/.pikku/scheduler/pikku-scheduler-types.gen.js +1 -1
- package/dist/.pikku/schemas/register.gen.js +13 -5
- package/dist/.pikku/schemas/schemas/FabricLinkOutput.schema.json +1 -1
- package/dist/.pikku/schemas/schemas/FabricSmokeInput.schema.json +1 -0
- package/dist/.pikku/schemas/schemas/FabricSmokeOutput.schema.json +1 -0
- package/dist/.pikku/schemas/schemas/LogoutInput.schema.json +1 -0
- package/dist/.pikku/schemas/schemas/WhoamiInput.schema.json +1 -0
- package/dist/.pikku/secrets/pikku-secret-types.gen.d.ts +1 -1
- package/dist/.pikku/secrets/pikku-secret-types.gen.js +1 -1
- package/dist/.pikku/secrets/pikku-secrets.gen.d.ts +1 -1
- package/dist/.pikku/secrets/pikku-secrets.gen.js +1 -1
- package/dist/.pikku/trigger/pikku-trigger-types.gen.d.ts +1 -1
- package/dist/.pikku/trigger/pikku-trigger-types.gen.js +1 -1
- package/dist/.pikku/variables/pikku-variable-types.gen.d.ts +1 -1
- package/dist/.pikku/variables/pikku-variable-types.gen.js +1 -1
- package/dist/.pikku/variables/pikku-variables.gen.d.ts +1 -1
- package/dist/.pikku/variables/pikku-variables.gen.js +1 -1
- package/dist/.pikku/workflow/pikku-workflow-types.gen.d.ts +1 -1
- package/dist/.pikku/workflow/pikku-workflow-types.gen.js +1 -1
- package/dist/.pikku/workflow/pikku-workflow-wirings-meta.gen.js +1 -1
- package/dist/.pikku/workflow/pikku-workflow-wirings.gen.js +1 -1
- package/dist/bin/pikku-bin.mjs +2 -2
- package/dist/src/cli.wiring.js +45 -0
- package/dist/src/fabric/fabric-commands.d.ts +130 -63
- package/dist/src/fabric/fabric-commands.js +21 -0
- package/dist/src/fabric/functions/db-schema.function.d.ts +3 -3
- package/dist/src/fabric/functions/deploy-list.function.d.ts +3 -3
- package/dist/src/fabric/functions/deploy-units.function.d.ts +3 -3
- package/dist/src/fabric/functions/deploy.function.d.ts +6 -6
- package/dist/src/fabric/functions/domains-add.function.d.ts +3 -3
- package/dist/src/fabric/functions/domains-list.function.d.ts +3 -3
- package/dist/src/fabric/functions/domains-remove.function.d.ts +3 -3
- package/dist/src/fabric/functions/errors.function.d.ts +3 -3
- package/dist/src/fabric/functions/init.function.d.ts +3 -3
- package/dist/src/fabric/functions/link.function.d.ts +7 -3
- package/dist/src/fabric/functions/link.function.js +3 -1
- package/dist/src/fabric/functions/llm-key.function.d.ts +3 -3
- package/dist/src/fabric/functions/login.function.d.ts +3 -3
- package/dist/src/fabric/functions/logs.function.d.ts +3 -3
- package/dist/src/fabric/functions/metrics.function.d.ts +3 -3
- package/dist/src/fabric/functions/rollback.function.d.ts +3 -3
- package/dist/src/fabric/functions/secrets-list.function.d.ts +3 -3
- package/dist/src/fabric/functions/secrets-set.function.d.ts +3 -3
- package/dist/src/fabric/functions/smoke.function.d.ts +93 -0
- package/dist/src/fabric/functions/smoke.function.js +860 -0
- package/dist/src/fabric/functions/status.function.d.ts +3 -3
- package/dist/src/fabric/functions/trace.function.d.ts +3 -3
- package/dist/src/fabric/functions/validate.function.d.ts +3 -3
- package/dist/src/functions/commands/all.d.ts +1 -1
- package/dist/src/functions/commands/binary.d.ts +3 -3
- package/dist/src/functions/commands/bootstrap.d.ts +1 -1
- package/dist/src/functions/commands/console.d.ts +3 -3
- package/dist/src/functions/commands/db-audit.d.ts +1 -1
- package/dist/src/functions/commands/db-generate.d.ts +1 -1
- package/dist/src/functions/commands/db-migrate.d.ts +1 -1
- package/dist/src/functions/commands/db-reset.d.ts +1 -1
- package/dist/src/functions/commands/db-seed.d.ts +1 -1
- package/dist/src/functions/commands/deploy-apply.d.ts +3 -3
- package/dist/src/functions/commands/deploy-info.d.ts +1 -1
- package/dist/src/functions/commands/deploy-plan.d.ts +3 -3
- package/dist/src/functions/commands/dev.d.ts +3 -3
- package/dist/src/functions/commands/emails-init.d.ts +1 -1
- package/dist/src/functions/commands/enable.d.ts +5 -5
- package/dist/src/functions/commands/info.d.ts +4 -4
- package/dist/src/functions/commands/login.d.ts +37 -0
- package/dist/src/functions/commands/login.js +85 -0
- package/dist/src/functions/commands/meta.d.ts +31 -31
- package/dist/src/functions/commands/new-addon.d.ts +3 -3
- package/dist/src/functions/commands/new-function.d.ts +3 -3
- package/dist/src/functions/commands/new-middleware.d.ts +3 -3
- package/dist/src/functions/commands/new-permission.d.ts +3 -3
- package/dist/src/functions/commands/new-wiring.d.ts +3 -3
- package/dist/src/functions/commands/pikku-command-bootstrap.d.ts +1 -1
- package/dist/src/functions/commands/pikku-command-summary.d.ts +1 -1
- package/dist/src/functions/commands/skills.d.ts +6 -6
- package/dist/src/functions/commands/tests-coverage.d.ts +3 -3
- package/dist/src/functions/commands/tests-init.d.ts +3 -3
- package/dist/src/functions/commands/versions-check.d.ts +1 -1
- package/dist/src/functions/commands/versions-init.d.ts +3 -3
- package/dist/src/functions/commands/versions-update.d.ts +1 -1
- package/dist/src/functions/commands/watch.d.ts +3 -3
- package/dist/src/functions/commands/workspace-validate.d.ts +3 -3
- package/dist/src/functions/db/local-db.js +20 -75
- package/dist/src/functions/runtimes/fetch/index.d.ts +1 -1
- package/dist/src/functions/runtimes/nextjs/pikku-command-nextjs.d.ts +1 -1
- package/dist/src/functions/runtimes/tanstack-start/pikku-command-tanstack-start.d.ts +1 -1
- package/dist/src/functions/runtimes/websocket/pikku-command-websocket-typed.d.ts +1 -1
- package/dist/src/functions/wirings/ai-agent/pikku-command-ai-agent-types.d.ts +1 -1
- package/dist/src/functions/wirings/ai-agent/pikku-command-ai-agent.d.ts +1 -1
- package/dist/src/functions/wirings/ai-agent/pikku-command-public-agent.d.ts +1 -1
- package/dist/src/functions/wirings/auth/pikku-command-auth.d.ts +1 -1
- package/dist/src/functions/wirings/channels/pikku-channels.d.ts +1 -1
- package/dist/src/functions/wirings/channels/pikku-command-channel-types.d.ts +1 -1
- package/dist/src/functions/wirings/channels/pikku-command-channels-map.d.ts +1 -1
- package/dist/src/functions/wirings/channels/pikku-command-channels.d.ts +1 -1
- package/dist/src/functions/wirings/cli/pikku-command-cli-entry.d.ts +1 -1
- package/dist/src/functions/wirings/cli/pikku-command-cli-types.d.ts +1 -1
- package/dist/src/functions/wirings/cli/pikku-command-cli.d.ts +1 -1
- package/dist/src/functions/wirings/cli/serialize-channel-cli-client.js +38 -7
- package/dist/src/functions/wirings/console/pikku-command-console-functions.d.ts +1 -1
- package/dist/src/functions/wirings/console/pikku-command-node-types.d.ts +1 -1
- package/dist/src/functions/wirings/console/pikku-command-nodes-meta.d.ts +1 -1
- package/dist/src/functions/wirings/credentials/pikku-command-credentials.d.ts +1 -1
- package/dist/src/functions/wirings/emails/pikku-command-emails.d.ts +1 -1
- package/dist/src/functions/wirings/functions/pikku-command-addon-types.d.ts +1 -1
- package/dist/src/functions/wirings/functions/pikku-command-function-types-split.d.ts +3 -3
- package/dist/src/functions/wirings/functions/pikku-command-function-types.d.ts +3 -3
- package/dist/src/functions/wirings/functions/pikku-command-functions.d.ts +1 -1
- package/dist/src/functions/wirings/functions/pikku-command-services.d.ts +1 -1
- package/dist/src/functions/wirings/functions/schemas.d.ts +1 -1
- package/dist/src/functions/wirings/gateway/pikku-command-gateway.d.ts +1 -1
- package/dist/src/functions/wirings/http/pikku-command-http-map.d.ts +1 -1
- package/dist/src/functions/wirings/http/pikku-command-http-routes.d.ts +1 -1
- package/dist/src/functions/wirings/http/pikku-command-http-types.d.ts +1 -1
- package/dist/src/functions/wirings/http/pikku-command-openapi.d.ts +1 -1
- package/dist/src/functions/wirings/http/pikku-http-routes.d.ts +1 -1
- package/dist/src/functions/wirings/mcp/pikku-command-mcp-json.d.ts +1 -1
- package/dist/src/functions/wirings/mcp/pikku-command-mcp-types.d.ts +1 -1
- package/dist/src/functions/wirings/mcp/pikku-command-mcp.d.ts +1 -1
- package/dist/src/functions/wirings/middleware/pikku-command-middleware.d.ts +1 -1
- package/dist/src/functions/wirings/package/pikku-command-package-types.d.ts +2 -2
- package/dist/src/functions/wirings/package/pikku-command-package.d.ts +1 -1
- package/dist/src/functions/wirings/permissions/pikku-command-permissions.d.ts +1 -1
- package/dist/src/functions/wirings/queue/pikku-command-queue-map.d.ts +1 -1
- package/dist/src/functions/wirings/queue/pikku-command-queue-service.d.ts +1 -1
- package/dist/src/functions/wirings/queue/pikku-command-queue-types.d.ts +1 -1
- package/dist/src/functions/wirings/queue/pikku-command-queue.d.ts +1 -1
- package/dist/src/functions/wirings/queue/pikku-queue-map.d.ts +1 -1
- package/dist/src/functions/wirings/queue/pikku-queue.d.ts +1 -1
- package/dist/src/functions/wirings/realtime/pikku-command-events-scaffold.d.ts +1 -1
- package/dist/src/functions/wirings/realtime/pikku-command-realtime.d.ts +1 -1
- package/dist/src/functions/wirings/rpc/pikku-command-public-rpc.d.ts +1 -1
- package/dist/src/functions/wirings/rpc/pikku-command-react-query.d.ts +1 -1
- package/dist/src/functions/wirings/rpc/pikku-command-remote-rpc.d.ts +1 -1
- package/dist/src/functions/wirings/rpc/pikku-command-rpc-client.d.ts +1 -1
- package/dist/src/functions/wirings/rpc/pikku-command-rpc-map.d.ts +2 -2
- package/dist/src/functions/wirings/rpc/pikku-command-rpc.d.ts +1 -1
- package/dist/src/functions/wirings/scheduler/pikku-command-scheduler-types.d.ts +1 -1
- package/dist/src/functions/wirings/scheduler/pikku-command-scheduler.d.ts +1 -1
- package/dist/src/functions/wirings/secrets/pikku-command-secrets.d.ts +1 -1
- package/dist/src/functions/wirings/triggers/pikku-command-trigger-types.d.ts +3 -3
- package/dist/src/functions/wirings/triggers/pikku-command-trigger.d.ts +1 -1
- package/dist/src/functions/wirings/variables/pikku-command-variables.d.ts +1 -1
- package/dist/src/functions/wirings/workflow/pikku-command-workflow-routes.d.ts +1 -1
- package/dist/src/functions/wirings/workflow/pikku-command-workflow.d.ts +1 -1
- package/dist/src/scaffold/rpc-remote.gen.d.ts +3 -3
- package/dist/src/scaffold/rpc-remote.gen.js +1 -1
- package/dist/src/utils/cli-session.d.ts +40 -0
- package/dist/src/utils/cli-session.js +73 -0
- package/dist/src/utils/device-auth.d.ts +28 -0
- package/dist/src/utils/device-auth.js +111 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
- package/skills/pikku-machine-auth/SKILL.md +177 -0
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: pikku-machine-auth
|
|
3
|
+
description: 'Use when authenticating a CLI/agent/service against a Pikku server, adding machine-to-machine (M2M) auth, issuing scoped API keys for sandboxes/agents/workers, or wiring better-auth sessions into Pikku middleware. Covers `pikku login` (device-authorization), the better-auth API Key plugin, machine identities, and `betterAuthSession` with the api-key branch.
|
|
4
|
+
TRIGGER when: user asks about CLI login, `pikku login`, machine agents, service-to-service auth, API keys, client credentials, sandbox/worker tokens, or resolving a better-auth session in a Pikku function.
|
|
5
|
+
DO NOT TRIGGER when: user asks about end-user HTTP session/cookie auth only (use pikku-http + the app betterAuth config) or about WebSocket channel mechanics (use pikku-websocket).'
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
# Pikku Machine Auth
|
|
9
|
+
|
|
10
|
+
Unified authentication for humans **and** machines against a Pikku + better-auth
|
|
11
|
+
server. Two paths, two headers, one resolver:
|
|
12
|
+
|
|
13
|
+
| Caller | Credential | Header | Obtained by |
|
|
14
|
+
|---|---|---|---|
|
|
15
|
+
| **Human** (CLI, dev) | better-auth session token | `Authorization: Bearer <token>` | `pikku login` (device flow) → `~/.pikku/session.json` |
|
|
16
|
+
| **Machine** (agent, sandbox, worker) | scoped API key | `x-api-key: <key>` | `createApiKey` (server-side, at provision/spawn) |
|
|
17
|
+
|
|
18
|
+
Both resolve to a Pikku `UserSession` through one middleware:
|
|
19
|
+
`betterAuthSession({ mapSession, apiKey: { mapKey } })`.
|
|
20
|
+
|
|
21
|
+
> The literal OAuth `client_credentials` grant is **not** implemented in
|
|
22
|
+
> better-auth's oidc-provider. The API Key plugin gives the same capability (a
|
|
23
|
+
> baked secret a service presents for scoped access), not the wire protocol.
|
|
24
|
+
|
|
25
|
+
## Agent Operating Procedure
|
|
26
|
+
|
|
27
|
+
1. Discover before editing — inspect the app's `betterAuth({ plugins: [...] })`
|
|
28
|
+
config and existing middleware wiring before adding anything.
|
|
29
|
+
2. Server changes go in the auth factory + a middleware wiring file; never put
|
|
30
|
+
auth checks in a function body (use `permissions`).
|
|
31
|
+
3. The API Key plugin contributes an `apikey` table — add the matching SQL
|
|
32
|
+
migration and regenerate DB types before relying on it.
|
|
33
|
+
4. Validate with the narrowest command, then `pikku all`.
|
|
34
|
+
|
|
35
|
+
## Human path — `pikku login`
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
pikku login --url https://app.example.com # device-authorization flow
|
|
39
|
+
pikku whoami # show current session + expiry
|
|
40
|
+
pikku logout # remove stored session
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
`pikku login` runs the RFC 8628 device flow: it requests a code, opens the
|
|
44
|
+
browser to the verification URL, polls until you approve, then stores the
|
|
45
|
+
session token (keyed by base URL) at `~/.pikku/session.json` with its expiry.
|
|
46
|
+
|
|
47
|
+
**Server requirement** — enable the `deviceAuthorization` and `bearer` plugins:
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
import { deviceAuthorization, bearer } from 'better-auth/plugins'
|
|
51
|
+
|
|
52
|
+
betterAuth({
|
|
53
|
+
// ...
|
|
54
|
+
plugins: [
|
|
55
|
+
deviceAuthorization({ expiresIn: '5min', interval: '5s', schema: {} }),
|
|
56
|
+
bearer(), // lets `Authorization: Bearer <session-token>` resolve a session
|
|
57
|
+
],
|
|
58
|
+
})
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
The browser approval is two steps the user's browser does automatically:
|
|
62
|
+
`GET /auth/device?user_code=XXXX` (claims the code while signed in) then
|
|
63
|
+
`POST /auth/device/approve`. The CLI only requests the code and polls
|
|
64
|
+
`POST /auth/device/token`.
|
|
65
|
+
|
|
66
|
+
## Machine path — API keys
|
|
67
|
+
|
|
68
|
+
Install the plugin (separate official package) and enable it:
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
yarn add @better-auth/api-key # peer: better-auth ^1.6.19
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
import { apiKey } from '@better-auth/api-key'
|
|
76
|
+
|
|
77
|
+
betterAuth({
|
|
78
|
+
plugins: [
|
|
79
|
+
apiKey({
|
|
80
|
+
enableMetadata: true, // REQUIRED to store scope on the key
|
|
81
|
+
enableSessionForAPIKeys: true, // lets a key resolve via getSession too
|
|
82
|
+
}),
|
|
83
|
+
],
|
|
84
|
+
})
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Identity model
|
|
88
|
+
|
|
89
|
+
A **machine is an API key, not a throwaway user.** Keys are owned by a small set
|
|
90
|
+
of stable **service-user** identities you provision once (e.g. `orchestrator`,
|
|
91
|
+
`machine-agent`, `builder`, `sandbox-runtime`). Per-machine scope rides on the
|
|
92
|
+
key's `metadata`/`permissions`. A key requires a real owning user row — minting
|
|
93
|
+
one for a non-existent `userId` is created but will not resolve.
|
|
94
|
+
|
|
95
|
+
### Mint a scoped key (server-side, at spawn/provision)
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
// `auth` is the better-auth instance (injected service)
|
|
99
|
+
const { key } = await auth.api.createApiKey({
|
|
100
|
+
body: {
|
|
101
|
+
userId: sandboxRuntimeUserId, // a stable service user
|
|
102
|
+
name: `sandbox:${sandboxId}`,
|
|
103
|
+
expiresIn: 60 * 60, // seconds
|
|
104
|
+
metadata: { sandboxId }, // keep only STABLE ids here
|
|
105
|
+
permissions: { sandbox: ['read', 'write'] },
|
|
106
|
+
},
|
|
107
|
+
})
|
|
108
|
+
// inject `key` into the machine's env; it sends it as `x-api-key`.
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Rotate by minting a new key and expiring/deleting the old (`deleteApiKey`);
|
|
112
|
+
multiple active keys per identity allow zero-downtime rotation.
|
|
113
|
+
|
|
114
|
+
### Resolve scope — `verifyApiKey`, not `getSession`
|
|
115
|
+
|
|
116
|
+
`getSession(x-api-key)` returns only a bare mock session **without** the
|
|
117
|
+
metadata. Scope must come from `verifyApiKey`, which returns
|
|
118
|
+
`{ valid, key: { userId, metadata, permissions } }`. The
|
|
119
|
+
`betterAuthSession` api-key branch does this for you:
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
import { betterAuthSession } from '@pikku/better-auth'
|
|
123
|
+
import { addHTTPMiddleware } from '@pikku/core/http'
|
|
124
|
+
|
|
125
|
+
addHTTPMiddleware([
|
|
126
|
+
betterAuthSession({
|
|
127
|
+
// human path: getSession result -> app session
|
|
128
|
+
mapSession: ({ user }) => ({ userId: user.id }),
|
|
129
|
+
// machine path: verified key -> app session. `services` lets you resolve
|
|
130
|
+
// CURRENT scope (e.g. look up the owning row) instead of trusting only the
|
|
131
|
+
// baked metadata.
|
|
132
|
+
apiKey: {
|
|
133
|
+
header: 'x-api-key', // default
|
|
134
|
+
mapKey: async (key, services) => {
|
|
135
|
+
const sandboxId = key.metadata?.sandboxId
|
|
136
|
+
if (!sandboxId) return null // reject
|
|
137
|
+
const row = await services.kysely
|
|
138
|
+
.selectFrom('sandboxInstance')
|
|
139
|
+
.innerJoin('sandbox', 'sandbox.id', 'sandboxInstance.sandboxId')
|
|
140
|
+
.select(['sandbox.orgId', 'sandbox.projectId'])
|
|
141
|
+
.where('sandboxInstance.sandboxId', '=', sandboxId)
|
|
142
|
+
.where('sandboxInstance.stoppedAt', 'is', null)
|
|
143
|
+
.executeTakeFirst()
|
|
144
|
+
if (!row) return null
|
|
145
|
+
return { userId: sandboxId, orgId: row.orgId, role: 'sandbox' }
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
}),
|
|
149
|
+
])
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
When the api-key header is present it is authoritative — the middleware never
|
|
153
|
+
falls through to `getSession` (a bare mock session would shadow the scoped one).
|
|
154
|
+
When it is absent, the human `getSession` path runs as normal.
|
|
155
|
+
|
|
156
|
+
### WebSocket channels authenticate on the upgrade handshake
|
|
157
|
+
|
|
158
|
+
Generated channel CLI clients attach the credential as a connection header
|
|
159
|
+
(`x-api-key` for `PIKKU_API_KEY`, else `Authorization: Bearer` from
|
|
160
|
+
`~/.pikku/session.json`). The `@pikku/ws` server copies the upgrade-request
|
|
161
|
+
headers into the channel's `http.request` and runs the inherited HTTP `*`
|
|
162
|
+
middleware during `runUpgradeMiddleware`, so `betterAuthSession` resolves the
|
|
163
|
+
session before the channel opens. For this to work the app must register
|
|
164
|
+
`betterAuthSession` via `addHTTPMiddleware([...])` (the `*` group) — not only on
|
|
165
|
+
specific routes — so it is inherited into the channel upgrade. Browser clients
|
|
166
|
+
cannot set WebSocket headers, so header-auth only covers the Node CLI path; a
|
|
167
|
+
browser channel needs a query-param/subprotocol vector instead.
|
|
168
|
+
|
|
169
|
+
## Gotchas
|
|
170
|
+
|
|
171
|
+
- `apiKey()` rejects `metadata` unless `enableMetadata: true`.
|
|
172
|
+
- `deviceAuthorization()` requires a `schema` option (pass `schema: {}`).
|
|
173
|
+
- Keep the two paths on **different headers** — `x-api-key` (machine) vs
|
|
174
|
+
`Authorization: Bearer` (human). One header for both reintroduces ambiguity.
|
|
175
|
+
- The `apikey` table is plugin-contributed — add the SQL migration + regen types.
|
|
176
|
+
- `~/.pikku/session.json` is written `0600` and stores the token + expiry; the
|
|
177
|
+
CLI uses the expiry to detect when a re-login is needed.
|