@porulle/core 0.39.0 → 0.40.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/dist/auth/middleware.d.ts +21 -0
- package/dist/auth/middleware.d.ts.map +1 -1
- package/dist/auth/middleware.js +59 -1
- package/dist/kernel/hooks/executor.d.ts +1 -1
- package/dist/kernel/hooks/executor.d.ts.map +1 -1
- package/dist/kernel/hooks/types.d.ts +11 -2
- package/dist/kernel/hooks/types.d.ts.map +1 -1
- package/dist/modules/orders/service.d.ts.map +1 -1
- package/dist/modules/orders/service.js +1 -1
- package/dist/runtime/server.d.ts.map +1 -1
- package/dist/runtime/server.js +7 -2
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/auth/middleware.ts +60 -1
- package/src/kernel/hooks/executor.ts +5 -5
- package/src/kernel/hooks/types.ts +11 -2
- package/src/modules/orders/service.ts +9 -2
- package/src/runtime/server.ts +7 -2
package/package.json
CHANGED
package/src/auth/middleware.ts
CHANGED
|
@@ -17,6 +17,33 @@ function emptyToNull(value: string | null | undefined): string | null {
|
|
|
17
17
|
// `catalog:read` here would 401 every public storefront read.
|
|
18
18
|
export { DEFAULT_CUSTOMER_PERMISSIONS } from "./actor.js";
|
|
19
19
|
|
|
20
|
+
/**
|
|
21
|
+
* The challenge RFC 9110 §15.5.2 requires on a 401: it "MUST include a WWW-Authenticate header
|
|
22
|
+
* field containing at least one challenge applicable to the target resource". Without it a 401
|
|
23
|
+
* names no scheme, so it is advice a generic HTTP client cannot act on.
|
|
24
|
+
*
|
|
25
|
+
* `Bearer` and nothing else. Both credentials this API accepts — the session cookie the mobile
|
|
26
|
+
* client holds and the `x-api-key` a machine holds — are presented as bearer-style tokens, and a
|
|
27
|
+
* `Basic` challenge would make a browser render a native credential prompt over a JSON API.
|
|
28
|
+
*
|
|
29
|
+
* The realm is a constant. It must never carry the organization or vendor, which would disclose
|
|
30
|
+
* tenancy to a caller that has not authenticated.
|
|
31
|
+
*/
|
|
32
|
+
export const AUTHENTICATE_CHALLENGE = 'Bearer realm="api"';
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Attach the challenge to a 401, and to nothing else.
|
|
36
|
+
*
|
|
37
|
+
* NOT unconditional: §15.5.4 asks no challenge of a 403, and offering one there tells a caller who
|
|
38
|
+
* IS authenticated to try authenticating again. An existing header is left alone so a plugin or a
|
|
39
|
+
* route can answer with a more specific challenge than this default.
|
|
40
|
+
*/
|
|
41
|
+
export function applyAuthenticateChallenge(response: Response): void {
|
|
42
|
+
if (response.status !== 401) return;
|
|
43
|
+
if (response.headers.has("www-authenticate")) return;
|
|
44
|
+
response.headers.set("www-authenticate", AUTHENTICATE_CHALLENGE);
|
|
45
|
+
}
|
|
46
|
+
|
|
20
47
|
const LEGACY_STORE_RESOLVER_WARN_COOLDOWN_MS = 60_000;
|
|
21
48
|
let lastLegacyStoreResolverWarnAt = 0;
|
|
22
49
|
|
|
@@ -24,7 +51,15 @@ export function authMiddleware(
|
|
|
24
51
|
auth: AuthInstance,
|
|
25
52
|
config: CommerceConfig,
|
|
26
53
|
): MiddlewareHandler {
|
|
27
|
-
|
|
54
|
+
/**
|
|
55
|
+
* The resolution body. It has FIVE `next()` call sites and four of them `return` immediately
|
|
56
|
+
* after — a signed-in caller leaves at the `if (actor)` branch, an API-key caller at its own,
|
|
57
|
+
* and only an anonymous caller reaches the last one. Anything that must run for EVERY request
|
|
58
|
+
* therefore cannot live at the bottom of this function: it would fire for one caller class and
|
|
59
|
+
* silently skip the rest. Measured, not assumed — a header set at the bottom reached an
|
|
60
|
+
* anonymous 401 and never reached a signed-in 403.
|
|
61
|
+
*/
|
|
62
|
+
const resolve: MiddlewareHandler = async (c, next) => {
|
|
28
63
|
if (isIdentityFreeRoute(c.req.method, c.req.path, config)) {
|
|
29
64
|
c.set("actor", null);
|
|
30
65
|
await next();
|
|
@@ -238,4 +273,28 @@ export function authMiddleware(
|
|
|
238
273
|
}
|
|
239
274
|
await next();
|
|
240
275
|
};
|
|
276
|
+
|
|
277
|
+
// ONE boundary around all five of the body's exits. A 401 leaves this server by five routes —
|
|
278
|
+
// `requirePerm` and `requireAnyPerm` in interfaces/rest/utils.ts, the plugin router's inline
|
|
279
|
+
// refusal, the customer portal's own, and a thrown `CommerceUnauthorizedError` shaped by
|
|
280
|
+
// `mapErrorToResponse` — and every one of them unwinds through here whichever way the body
|
|
281
|
+
// returned. So one line covers all five, and any sixth refusal added later, which five copies of
|
|
282
|
+
// the rule could not.
|
|
283
|
+
//
|
|
284
|
+
// The only 401 that does NOT reach this point is one produced by `app.onError`: by then every
|
|
285
|
+
// `await next()` in the chain has already rejected. runtime/server.ts calls the same helper there.
|
|
286
|
+
return async (c, next) => {
|
|
287
|
+
// The body RETURNS a Response on one path — the strict-org-resolution 503 — and Hono assigns
|
|
288
|
+
// that return value over `c.res` after this handler finishes. Dropping it on the floor turned
|
|
289
|
+
// two `ORG_RESOLUTION_FAILED` rows red, and challenging `c.res` instead of the returned object
|
|
290
|
+
// would have mutated a response that was about to be replaced. Both cases are handled by
|
|
291
|
+
// challenging whichever object is actually going to be sent.
|
|
292
|
+
const returned = await resolve(c, next);
|
|
293
|
+
if (returned instanceof Response) {
|
|
294
|
+
applyAuthenticateChallenge(returned);
|
|
295
|
+
return returned;
|
|
296
|
+
}
|
|
297
|
+
applyAuthenticateChallenge(c.res);
|
|
298
|
+
return;
|
|
299
|
+
};
|
|
241
300
|
}
|
|
@@ -104,13 +104,13 @@ export async function runBeforeHooks<T>(
|
|
|
104
104
|
return current;
|
|
105
105
|
}
|
|
106
106
|
|
|
107
|
-
export async function runAfterHooks<
|
|
108
|
-
hooks: AfterHook<
|
|
109
|
-
originalData:
|
|
110
|
-
committedResult:
|
|
107
|
+
export async function runAfterHooks<TResult, TData = TResult>(
|
|
108
|
+
hooks: AfterHook<TResult, TData>[],
|
|
109
|
+
originalData: TData | null,
|
|
110
|
+
committedResult: TResult,
|
|
111
111
|
operation: HookOperation,
|
|
112
112
|
context: HookContext,
|
|
113
|
-
runsInTransaction: (hook: AfterHook<
|
|
113
|
+
runsInTransaction: (hook: AfterHook<TResult, TData>) => boolean = () => false,
|
|
114
114
|
): Promise<HookReport> {
|
|
115
115
|
const errors: HookError[] = [];
|
|
116
116
|
for (const hook of hooks) {
|
|
@@ -50,9 +50,18 @@ export type BeforeHook<TData> = (args: {
|
|
|
50
50
|
context: HookContext;
|
|
51
51
|
}) => Promise<TData> | TData;
|
|
52
52
|
|
|
53
|
-
|
|
53
|
+
/**
|
|
54
|
+
* `data` is what went in, `result` is what was committed. For most operations
|
|
55
|
+
* those are the same shape, so `TData` defaults to `TResult` and a single type
|
|
56
|
+
* argument keeps its old meaning.
|
|
57
|
+
*
|
|
58
|
+
* They differ where the committed entity is not the input: a status change
|
|
59
|
+
* commits a hydrated order but its input is the transition itself, and a hook
|
|
60
|
+
* that cannot see which transition occurred cannot act on one.
|
|
61
|
+
*/
|
|
62
|
+
export type AfterHook<TResult, TData = TResult> = (args: {
|
|
54
63
|
data: TData | null;
|
|
55
|
-
result:
|
|
64
|
+
result: TResult;
|
|
56
65
|
operation: HookOperation;
|
|
57
66
|
context: HookContext;
|
|
58
67
|
}) => Promise<void> | void;
|
|
@@ -134,7 +134,14 @@ type StatusChangeHookInput = {
|
|
|
134
134
|
reason?: string;
|
|
135
135
|
};
|
|
136
136
|
type BeforeStatusChangeHook = BeforeHook<StatusChangeHookInput>;
|
|
137
|
-
|
|
137
|
+
/**
|
|
138
|
+
* `result` is the committed, hydrated order; `data` is the transition that
|
|
139
|
+
* produced it. A subscriber that must act on *which* transition occurred — the
|
|
140
|
+
* channel connector pushes an order to its merchant only when it leaves
|
|
141
|
+
* `pending_payment` — cannot read that from the order alone, because the order
|
|
142
|
+
* carries only the status it now has.
|
|
143
|
+
*/
|
|
144
|
+
type AfterStatusChangeHook = AfterHook<HydratedOrder, StatusChangeHookInput>;
|
|
138
145
|
|
|
139
146
|
function context(
|
|
140
147
|
actor: Actor | null,
|
|
@@ -1254,7 +1261,7 @@ export class OrderService {
|
|
|
1254
1261
|
const hydrated = await this.hydrateOrder(cas, ctx);
|
|
1255
1262
|
const report = await runAfterHooks(
|
|
1256
1263
|
afterHooks,
|
|
1257
|
-
|
|
1264
|
+
statusHookInput,
|
|
1258
1265
|
hydrated,
|
|
1259
1266
|
"statusChange",
|
|
1260
1267
|
hookCtx,
|
package/src/runtime/server.ts
CHANGED
|
@@ -10,7 +10,7 @@ import { createClientIpResolver } from "./client-ip.js";
|
|
|
10
10
|
import type { Actor } from "../auth/types.js";
|
|
11
11
|
import type { AuthInstance } from "../auth/setup.js";
|
|
12
12
|
import type { CommerceConfig } from "../config/types.js";
|
|
13
|
-
import { authMiddleware } from "../auth/middleware.js";
|
|
13
|
+
import { applyAuthenticateChallenge, authMiddleware } from "../auth/middleware.js";
|
|
14
14
|
import { resolveOrgIdForCommerce } from "../auth/org.js";
|
|
15
15
|
import { organizationGuard } from "../auth/organization-guard.js";
|
|
16
16
|
import type { DrizzleDatabase } from "../kernel/database/drizzle-db.js";
|
|
@@ -371,7 +371,12 @@ export async function createServer(config: CommerceConfig) {
|
|
|
371
371
|
const isProd = process.env.NODE_ENV === "production";
|
|
372
372
|
app.onError((err, c) => {
|
|
373
373
|
const { body, status } = mapErrorToResponse(err, isProd, logger);
|
|
374
|
-
|
|
374
|
+
const response = c.json(body, status);
|
|
375
|
+
// The one 401 the auth middleware cannot reach: by the time onError runs, every `await next()`
|
|
376
|
+
// in the chain has already rejected, so its post-next step never executes. Same helper, so the
|
|
377
|
+
// rule lives in one place even though it is applied in two.
|
|
378
|
+
applyAuthenticateChallenge(response);
|
|
379
|
+
return response;
|
|
375
380
|
});
|
|
376
381
|
|
|
377
382
|
// ─── Routes ──────────────────────────────────────────────────────────
|