@chidchanun/bcp 0.2.4 → 0.2.6

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
@@ -1,14 +1,14 @@
1
1
  # BCP Framework
2
2
 
3
- BCP Framework is a React full-stack framework for file-based routing, SSR, SPA navigation, server data loading, guarded application flows, API routes, authentication, database access, validation, uploads, storage and standalone Node.js production deployment.
3
+ BCP Framework is a React full-stack framework for file-based routing, SSR, SPA navigation, server data loading, guarded application flows, API routes, authentication, authorization, database access, validation, uploads, storage and standalone Node.js production deployment.
4
4
 
5
- > **Development target:** `0.2.4Application Packaging`
5
+ > **Development target:** `0.2.6Authorization & Security v2`
6
6
  >
7
- > `0.2.4` is an unreleased development target until local validation, RC checks, tagging and npm publication complete.
7
+ > `0.2.6` is an unreleased development target until local validation, RC checks, tagging and npm publication complete.
8
8
 
9
9
  ## 0.2 platform
10
10
 
11
- `0.2.0` established the Framework Platform baseline, `0.2.1` added the Documentation Platform, `0.2.2` added Configuration & Environment v2, `0.2.3` added Database Platform v2, and `0.2.4` adds deployment-oriented application packaging without intentionally removing the existing public application model.
11
+ `0.2.0` established the Framework Platform baseline, `0.2.1` added the Documentation Platform, `0.2.2` added Configuration & Environment v2, `0.2.3` added Database Platform v2, `0.2.4` added Application Packaging, `0.2.5` added Authentication Platform v2, and `0.2.6` adds permission/policy authorization plus CSRF and same-origin request protection without intentionally removing the existing public application model.
12
12
 
13
13
  Machine-readable platform contracts:
14
14
 
@@ -31,8 +31,9 @@ docs/api-manifest.json
31
31
  | Routing | Static, dynamic, catch-all, optional catch-all and route groups |
32
32
  | Server data | Route `loader.ts`, request-scoped server APIs |
33
33
  | Mutations | Route-owned `actions.ts` and `<Form>` |
34
- | Authorization | `guard.ts`, `requireAuth()`, `requireRole()` |
35
- | Authentication | JWT cookie sessions and auth helpers |
34
+ | Authentication | JWT cookie sessions, optional server-side session stores, revocation, logout-all, idle timeout and rotation |
35
+ | Authorization | Auth/guest/role/permission route guards, flat permissions and resource-aware policies |
36
+ | Request security | Same-origin validation and signed CSRF tokens for unsafe mutations |
36
37
  | Middleware | Middleware System v2 with onion execution |
37
38
  | Validation | Typed validators and structured validation errors |
38
39
  | Error handling | HTTP error helpers and consistent error responses |
@@ -155,8 +156,6 @@ await db.connect();
155
156
  await db.disconnect();
156
157
  ```
157
158
 
158
- `db.close()` remains available for backward-compatible shutdown handling.
159
-
160
159
  Migration CLI:
161
160
 
162
161
  ```bash
@@ -166,8 +165,6 @@ bcp db status
166
165
  bcp db rollback
167
166
  ```
168
167
 
169
- BCP makes framework migration bookkeeping provider-aware. Application migration SQL itself is not automatically translated between SQL dialects.
170
-
171
168
  Read more:
172
169
 
173
170
  - [Database](docs/database.md)
@@ -175,18 +172,12 @@ Read more:
175
172
 
176
173
  ## Application Packaging — 0.2.4
177
174
 
178
- Create a fresh production build and convert it into a deployment-oriented package:
175
+ Create a fresh production build and deployment package:
179
176
 
180
177
  ```bash
181
178
  bcp package
182
179
  ```
183
180
 
184
- Framework-repository development can also run:
185
-
186
- ```bash
187
- npm run package
188
- ```
189
-
190
181
  Output:
191
182
 
192
183
  ```text
@@ -206,30 +197,179 @@ Output:
206
197
  └─ README.md
207
198
  ```
208
199
 
209
- The package layer:
200
+ Read more:
210
201
 
211
- - runs a fresh `bcp build` before packaging,
212
- - strips application `devDependencies` from the deployment package,
213
- - derives a production-only npm v3 lock graph when safe,
214
- - records `npm ci --omit=dev` when the production lock is available,
215
- - falls back explicitly to `npm install --omit=dev` when a safe lock cannot be produced,
216
- - writes deployment/runtime metadata,
217
- - records environment variable names without copying `.env` values,
218
- - creates SHA-256 file integrity metadata,
219
- - generates a Node 24 Alpine Docker starter.
202
+ - [Application Packaging](docs/application-packaging.md)
203
+ - [Deployment](docs/deployment.md)
220
204
 
221
- The current package target is:
205
+ ## Authentication Platform v2 — 0.2.5
222
206
 
223
- ```text
224
- standalone-node
207
+ Stateless signed JWT-cookie authentication remains supported:
208
+
209
+ ```ts
210
+ import {
211
+ auth,
212
+ login,
213
+ logout,
214
+ } from "bcp/auth";
225
215
  ```
226
216
 
227
- Native executable, desktop, Android and iOS packaging remain later roadmap work.
217
+ Applications that need centralized revocation can add a server-side session store:
218
+
219
+ ```ts
220
+ import {
221
+ createAuth,
222
+ createMemoryAuthSessionStore,
223
+ } from "bcp/auth";
224
+
225
+ const sessionStore =
226
+ createMemoryAuthSessionStore();
227
+
228
+ export const appAuth =
229
+ createAuth({
230
+ store: sessionStore,
231
+ idleTimeout: 60 * 30,
232
+ });
233
+ ```
234
+
235
+ With a store configured, authentication requires both a valid signed JWT cookie and an active `sid` record. The built-in memory store is intended for development/tests; production multi-instance deployments should implement `AuthSessionStore` with shared storage.
228
236
 
229
237
  Read more:
230
238
 
231
- - [Application Packaging](docs/application-packaging.md)
232
- - [Deployment](docs/deployment.md)
239
+ - [Authentication](docs/authentication.md)
240
+ - [Auth Session Stores](docs/auth-session-store.md)
241
+ - [Auth Route Guards](docs/auth-route-guards.md)
242
+
243
+ ## Authorization & Security v2 — 0.2.6
244
+
245
+ ### Permissions
246
+
247
+ Use flat permissions directly from `bcp/auth`:
248
+
249
+ ```ts
250
+ import {
251
+ hasPermission,
252
+ requirePermission,
253
+ } from "bcp/auth";
254
+
255
+ hasPermission(
256
+ user,
257
+ "users.read"
258
+ );
259
+
260
+ await requirePermission(
261
+ [
262
+ "users.read",
263
+ "users.write",
264
+ ],
265
+ {
266
+ match: "all",
267
+ }
268
+ );
269
+ ```
270
+
271
+ Route trees can use a permission guard:
272
+
273
+ ```ts
274
+ import {
275
+ createPermissionGuard,
276
+ } from "bcp/auth";
277
+
278
+ export const guard =
279
+ createPermissionGuard(
280
+ "admin.access"
281
+ );
282
+ ```
283
+
284
+ The default permission field is `permissions`. Applications can select another field such as `scopes`.
285
+
286
+ ### Resource policies
287
+
288
+ For ownership, tenant or resource-state rules:
289
+
290
+ ```ts
291
+ import {
292
+ authorize,
293
+ defineAuthorizationPolicy,
294
+ } from "bcp/auth";
295
+
296
+ const updateProject =
297
+ defineAuthorizationPolicy(
298
+ ({
299
+ user,
300
+ resource,
301
+ }) =>
302
+ resource.ownerId ===
303
+ user.id
304
+ );
305
+
306
+ await authorize(
307
+ updateProject,
308
+ {
309
+ user,
310
+ resource: project,
311
+ }
312
+ );
313
+ ```
314
+
315
+ Available policy helpers:
316
+
317
+ ```text
318
+ can()
319
+ cannot()
320
+ authorize()
321
+ AuthorizationError
322
+ ```
323
+
324
+ ### Same-origin and CSRF protection
325
+
326
+ Request-security helpers are exposed from `bcp/server`:
327
+
328
+ ```ts
329
+ import {
330
+ createCsrfToken,
331
+ requireCsrfRequest,
332
+ requireSameOriginRequest,
333
+ } from "bcp/server";
334
+ ```
335
+
336
+ Protect an unsafe API/action mutation with origin validation:
337
+
338
+ ```ts
339
+ await requireSameOriginRequest();
340
+ ```
341
+
342
+ For signed CSRF protection:
343
+
344
+ ```ts
345
+ const csrfToken =
346
+ await createCsrfToken();
347
+
348
+ // Render/send csrfToken through the trusted application UI.
349
+
350
+ await requireCsrfRequest({
351
+ token: submittedToken,
352
+ });
353
+ ```
354
+
355
+ Default CSRF header/cookie:
356
+
357
+ ```text
358
+ X-BCP-CSRF
359
+ bcp_csrf
360
+ ```
361
+
362
+ Secret resolution:
363
+
364
+ ```text
365
+ explicit options.secret
366
+
367
+ BCP_CSRF_SECRET
368
+
369
+ BCP_SESSION_SECRET
370
+ ```
371
+
372
+ Read more: [Authorization & Security v2](docs/authorization-security.md)
233
373
 
234
374
  ## Public entrypoints
235
375
 
@@ -292,16 +432,14 @@ bcp generate middleware
292
432
  bcp generate migration create_users
293
433
  ```
294
434
 
295
- Page/API/middleware generators require `--force` before replacing an existing scaffold target.
296
-
297
435
  ## Application model
298
436
 
299
437
  ```text
300
438
  Browser
301
439
 
302
- Security / middleware / cache
440
+ Origin / CSRF / middleware / cache
303
441
 
304
- Route guard
442
+ Authentication + authorization guard
305
443
 
306
444
  Loader / action / API route
307
445
 
@@ -310,24 +448,6 @@ React SSR
310
448
  Hydration / SPA navigation
311
449
  ```
312
450
 
313
- Typical route structure:
314
-
315
- ```text
316
- app/
317
- ├─ layout.tsx
318
- ├─ page.tsx
319
- ├─ dashboard/
320
- │ ├─ guard.ts
321
- │ └─ users/
322
- │ └─ [id]/
323
- │ ├─ loader.ts
324
- │ ├─ actions.ts
325
- │ └─ page.tsx
326
- └─ api/
327
- └─ upload/
328
- └─ route.ts
329
- ```
330
-
331
451
  ## Production build
332
452
 
333
453
  Raw standalone build:
@@ -337,23 +457,13 @@ npm run build
337
457
  npm run start
338
458
  ```
339
459
 
340
- Output:
341
-
342
- ```text
343
- .bcp-framework/build/
344
- ├─ client/
345
- ├─ public/
346
- └─ server/
347
- └─ server.mjs
348
- ```
349
-
350
460
  Deployment package:
351
461
 
352
462
  ```bash
353
463
  bcp package
354
464
  ```
355
465
 
356
- Both current targets remain Node.js `standalone-node` applications. Production hardening includes configurable request/header/keep-alive/shutdown timeouts, trusted-proxy handling and graceful `SIGTERM` / `SIGINT` shutdown.
466
+ Both current targets remain Node.js `standalone-node` applications.
357
467
 
358
468
  ## Documentation Platform
359
469
 
@@ -367,14 +477,6 @@ docs/platform-manifest.json
367
477
  docs/api-manifest.json
368
478
  ```
369
479
 
370
- The manifests provide navigation order, routes, Markdown sources, version/release state, public entrypoints and API guide ownership.
371
-
372
- Read more:
373
-
374
- - [Documentation Source Map](docs/README.md)
375
- - [Documentation Platform](docs/documentation-platform.md)
376
- - [Application Packaging](docs/application-packaging.md)
377
-
378
480
  ## Release validation
379
481
 
380
482
  Framework releases must pass:
@@ -389,7 +491,7 @@ npm run test:e2e
389
491
  npm run rc:check
390
492
  ```
391
493
 
392
- `0.2.4` adds Application Packaging unit and prepared-package smoke checks covering production dependency metadata, production lock pruning, environment-value exclusion, deployment metadata and the `bcp package` CLI surface.
494
+ `0.2.6` adds Authorization & Security v2 unit and prepared-package smoke checks covering permission matching, policy authorization, permission route guards, same-origin mutation validation and signed CSRF tokens.
393
495
 
394
496
  Do not tag or publish until the final release commit passes the complete RC sequence.
395
497
 
@@ -408,12 +510,14 @@ Do not tag or publish until the final release commit passes the complete RC sequ
408
510
  | `0.2.2` | Configuration & Environment v2 |
409
511
  | `0.2.3` | Database Platform v2 |
410
512
  | `0.2.4` | Application Packaging |
513
+ | `0.2.5` | Authentication Platform v2 |
514
+ | `0.2.6` | Authorization & Security v2 |
411
515
 
412
516
  ## Roadmap
413
517
 
414
- `0.2.4Application Packaging` establishes the deployment artifact contract for the existing standalone Node.js runtime.
518
+ `0.2.6Authorization & Security v2` establishes the permission/policy and browser-mutation security layer on top of Authentication Platform v2.
415
519
 
416
- The next planned framework milestone can build on this package contract without changing the current Node.js deployment model. Native `.exe`, desktop and mobile compilation remain later roadmap work.
520
+ The next `0.2.x` milestone can build on the existing runtime, database, auth, security and packaging contracts. Native `.exe`, desktop and mobile compilation remain later roadmap work.
417
521
 
418
522
  ## License
419
523
 
package/docs/README.md CHANGED
@@ -2,9 +2,9 @@
2
2
 
3
3
  The `docs/` directory is the documentation source of truth for BCP Framework and is organized for **`bcp-docs-web`**.
4
4
 
5
- > **Documentation target:** BCP Framework `0.2.2`
5
+ > **Documentation target:** BCP Framework `0.2.6 — Authorization & Security v2`
6
6
  >
7
- > **Release state:** unreleased development target. Do not label `0.2.2` as published until RC validation, tagging and npm publication complete.
7
+ > **Release state:** unreleased development target until RC validation, tagging and npm publication complete.
8
8
 
9
9
  ## Documentation architecture
10
10
 
@@ -41,32 +41,78 @@ manifest-driven sync
41
41
 
42
42
  Framework source and tests remain authoritative for runtime behavior.
43
43
 
44
- ## 0.2.2 — Configuration & Environment v2
44
+ ## Current 0.2.x milestones
45
45
 
46
- `0.2.2` adds an optional typed application environment schema while preserving the established framework configuration precedence.
46
+ | Version | Milestone |
47
+ | --- | --- |
48
+ | `0.2.0` | Framework Platform |
49
+ | `0.2.1` | Documentation Platform |
50
+ | `0.2.2` | Configuration & Environment v2 |
51
+ | `0.2.3` | Database Platform v2 |
52
+ | `0.2.4` | Application Packaging |
53
+ | `0.2.5` | Authentication Platform v2 |
54
+ | `0.2.6` | Authorization & Security v2 |
55
+
56
+ ## 0.2.6 — Authorization & Security v2
57
+
58
+ `0.2.6` adds server-side permission checks, resource-aware authorization policies, permission route guards, same-origin mutation validation and signed CSRF protection.
47
59
 
48
60
  New/updated documentation sources:
49
61
 
50
62
  | Source | Purpose |
51
63
  | --- | --- |
52
- | `environment-validation.md` | `bcp.environment.*`, validation rules, CLI checks and secret/public boundaries |
53
- | `configuration.md` | Framework configuration precedence and diagnostics workflow |
54
- | `platform-manifest.json` | Declares typed environment/config diagnostics capabilities |
55
- | `api-manifest.json` | Maps `bcp/config` to configuration and environment guides |
56
- | `releases/0.2.2.md` | Configuration & Environment v2 release notes |
64
+ | `authorization-security.md` | Permissions, policies, permission guards, same-origin validation and CSRF APIs |
65
+ | `auth-route-guards.md` | Auth/guest/role/permission route guard guidance |
66
+ | `api-reference.md` | Public authorization and request-security exports |
67
+ | `platform-manifest.json` | Authorization/security capability flags |
68
+ | `api-manifest.json` | `bcp/auth` and `bcp/server` guide ownership |
69
+ | `releases/0.2.6.md` | Authorization & Security v2 release notes |
57
70
 
58
- Primary command:
71
+ Primary authorization APIs:
59
72
 
60
- ```bash
61
- bcp config check
73
+ ```ts
74
+ import {
75
+ authorize,
76
+ can,
77
+ createPermissionGuard,
78
+ defineAuthorizationPolicy,
79
+ hasPermission,
80
+ requirePermission,
81
+ } from "bcp/auth";
62
82
  ```
63
83
 
64
- JSON diagnostics:
84
+ Primary browser-mutation security APIs:
65
85
 
66
- ```bash
67
- bcp config check --json
86
+ ```ts
87
+ import {
88
+ createCsrfToken,
89
+ requireCsrfRequest,
90
+ requireSameOriginRequest,
91
+ } from "bcp/server";
92
+ ```
93
+
94
+ ## Security model
95
+
96
+ The `0.2.6` server model separates concerns:
97
+
98
+ ```text
99
+ authentication
100
+ -> identify a signed/revocable session
101
+
102
+ authorization
103
+ -> permissions + policies + route guards
104
+
105
+ request security
106
+ -> Origin/Referer validation + signed CSRF token
107
+
108
+ input validation
109
+ -> application schema/domain checks
68
110
  ```
69
111
 
112
+ All authorization decisions must remain server-side. Client UI permission checks may improve presentation but do not replace route/action/API enforcement.
113
+
114
+ CSRF tokens use `BCP_CSRF_SECRET` when configured and otherwise fall back to `BCP_SESSION_SECRET`. Security secrets must contain at least 32 UTF-8 bytes.
115
+
70
116
  ## Update rule
71
117
 
72
118
  When framework behavior or public surface changes:
@@ -89,7 +135,7 @@ Current sections:
89
135
  ```text
90
136
  Getting Started
91
137
  Routing & Data
92
- Authentication
138
+ Authentication & Authorization
93
139
  Database
94
140
  Runtime & Infrastructure
95
141
  Storage & Uploads
@@ -103,76 +149,25 @@ Important current routes:
103
149
 
104
150
  | Website route | Markdown source |
105
151
  | --- | --- |
106
- | `/docs/configuration` | `configuration.md` |
107
- | `/docs/environment-validation` | `environment-validation.md` |
108
- | `/docs/platform-contract` | `platform-contract.md` |
109
- | `/docs/documentation-platform` | `documentation-platform.md` |
110
- | `/docs/migration-0.2` | `migration-0.2.md` |
152
+ | `/docs/authentication` | `authentication.md` |
153
+ | `/docs/auth-session-store` | `auth-session-store.md` |
154
+ | `/docs/auth-route-guards` | `auth-route-guards.md` |
155
+ | `/docs/authorization-security` | `authorization-security.md` |
156
+ | `/docs/application-packaging` | `application-packaging.md` |
157
+ | `/docs/database` | `database.md` |
111
158
  | `/docs/api-reference` | `api-reference.md` |
112
- | `/releases/0.2.2` | `releases/0.2.2.md` |
159
+ | `/releases/0.2.6` | `releases/0.2.6.md` |
113
160
 
114
161
  Every route/source pair is validated by unit tests.
115
162
 
116
- ## Configuration source model
117
-
118
- Framework settings:
119
-
120
- ```text
121
- bcp.config.ts
122
- bcp.config.mts
123
- bcp.config.js
124
- bcp.config.mjs
125
- ```
126
-
127
- Application environment schema:
128
-
129
- ```text
130
- bcp.environment.ts
131
- bcp.environment.mts
132
- bcp.environment.js
133
- bcp.environment.mjs
134
- ```
135
-
136
- Only one file from each convention may exist at a time.
137
-
138
- The environment schema is optional. Existing applications without `bcp.environment.*` continue to use the previous environment-loading behavior.
139
-
140
- Configuration precedence remains:
141
-
142
- ```text
143
- CLI override
144
-
145
- BCP_* environment
146
-
147
- bcp.config.*
148
-
149
- framework defaults
150
- ```
151
-
152
- See [Environment Validation](environment-validation.md).
153
-
154
- ## Environment security boundary
155
-
156
- `BCP_PUBLIC_*` values may be embedded into browser bundles. They must never contain credentials/private secrets.
157
-
158
- A variable declared with:
159
-
160
- ```ts
161
- secret: true
162
- ```
163
-
164
- must not use the `BCP_PUBLIC_` prefix. The configuration validator treats that combination as an error.
165
-
166
- `secret: true` is validation/tooling metadata and does not encrypt a value.
167
-
168
163
  ## Platform manifest
169
164
 
170
- `docs/platform-manifest.json` describes the supported framework baseline, including:
165
+ `docs/platform-manifest.json` describes:
171
166
 
172
167
  ```text
173
168
  framework version/release state
174
169
  Node/React/runtime baseline
175
- production build target
170
+ production build/package target
176
171
  public package entrypoints
177
172
  CLI command families
178
173
  capability flags
@@ -180,14 +175,6 @@ previous-baseline compatibility intent
180
175
  documentation contract files
181
176
  ```
182
177
 
183
- `0.2.2` declares the additional capabilities:
184
-
185
- ```text
186
- typedEnvironmentSchema
187
- configurationDiagnostics
188
- configCheckCli
189
- ```
190
-
191
178
  The supported production target remains:
192
179
 
193
180
  ```text
@@ -196,7 +183,7 @@ standalone-node
196
183
 
197
184
  ## API manifest
198
185
 
199
- `docs/api-manifest.json` describes the public package entrypoints documentation tooling may present as supported API surfaces.
186
+ `docs/api-manifest.json` describes public package entrypoints documentation tooling may present as supported APIs.
200
187
 
201
188
  Current entrypoints:
202
189
 
@@ -214,13 +201,6 @@ bcp/server-only
214
201
  bcp/middleware
215
202
  ```
216
203
 
217
- `bcp/config` owns both:
218
-
219
- ```text
220
- /docs/configuration
221
- /docs/environment-validation
222
- ```
223
-
224
204
  The API-manifest entrypoint set must match the platform public-entrypoint set exactly.
225
205
 
226
206
  ## bcp-docs-web synchronization
@@ -241,14 +221,6 @@ load referenced Markdown
241
221
  synchronize CMS/search/navigation
242
222
  ```
243
223
 
244
- Examples from the docs-web project:
245
-
246
- ```powershell
247
- npm run docs:sync -- --dry-run
248
- npm run docs:sync -- --publish-new
249
- npm run docs:sync -- --ref=v0.2.1
250
- ```
251
-
252
224
  ## Source conventions
253
225
 
254
226
  - one H1 per Markdown page,
@@ -263,7 +235,7 @@ npm run docs:sync -- --ref=v0.2.1
263
235
 
264
236
  ## Release validation
265
237
 
266
- Before publishing `0.2.2`:
238
+ Before publishing `0.2.6`:
267
239
 
268
240
  ```bash
269
241
  npm run typecheck
@@ -274,16 +246,17 @@ npm run test:e2e
274
246
  npm run rc:check
275
247
  ```
276
248
 
277
- Configuration & Environment v2 validation covers:
278
-
279
- - typed environment rules/defaults,
280
- - project `bcp.environment.*` loading,
281
- - public/secret safety boundary,
282
- - production diagnostics,
283
- - `bcp config check` CLI parsing,
284
- - dev schema watching,
285
- - public `bcp/config` exports,
286
- - prepared npm package files,
249
+ Authorization & Security v2 validation covers:
250
+
251
+ - permission normalization and any/all matching,
252
+ - permission route guards,
253
+ - sync/async authorization policies,
254
+ - `AuthorizationError` denial behavior,
255
+ - same-origin mutation checks,
256
+ - signed CSRF tokens,
257
+ - malformed/cross-origin rejection,
258
+ - public `bcp/auth` and `bcp/server` exports,
259
+ - prepared npm package contents,
287
260
  - docs/platform/API version parity.
288
261
 
289
262
  The final release tag must point to the exact commit that passed the complete RC sequence.
@@ -304,19 +277,3 @@ release notes
304
277
  ```
305
278
 
306
279
  `bcp-docs-web` remains the presentation/search/navigation layer for this content.
307
-
308
- ## Next direction
309
-
310
- Planned next milestone:
311
-
312
- ```text
313
- 0.2.3 — Database Platform v2
314
- ```
315
-
316
- Focus:
317
-
318
- - database adapter contract,
319
- - PostgreSQL support,
320
- - SQLite support,
321
- - connection lifecycle improvements,
322
- - provider-consistent migration workflows.