@porulle/core 0.40.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/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/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
|
}
|
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 ──────────────────────────────────────────────────────────
|