@auth-craft/pages-auth-proxy 1.0.0
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 +82 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/pages.d.ts +50 -0
- package/dist/pages.d.ts.map +1 -0
- package/dist/pages.js +43 -0
- package/dist/pages.js.map +1 -0
- package/dist/proxy.d.ts +61 -0
- package/dist/proxy.d.ts.map +1 -0
- package/dist/proxy.js +80 -0
- package/dist/proxy.js.map +1 -0
- package/package.json +46 -0
package/README.md
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# @auth-craft/pages-auth-proxy
|
|
2
|
+
|
|
3
|
+
Thin reverse-proxy so an app on **Cloudflare Pages** can serve `/{scope}/auth/*` on its
|
|
4
|
+
**own origin**, then forward to the Auth Craft **gateway worker**.
|
|
5
|
+
|
|
6
|
+
Why: when the browser only ever sees the app's own origin, the refresh cookie the
|
|
7
|
+
backend sets is **first-party** (same-site) — `SameSite=Lax/Strict` works, there is **no
|
|
8
|
+
cross-site CORS**, and it is **immune to Safari ITP**. This proxy is intentionally
|
|
9
|
+
**thin**: it never holds backend secrets and never decides the auth scope. All
|
|
10
|
+
trust-critical logic (scope stamping `X-Auth-Context`, `X-Gateway-Secret`, JWT verify,
|
|
11
|
+
rate-limit, stealth 404) stays in the gateway.
|
|
12
|
+
|
|
13
|
+
> Optional: use this only for apps you deploy on Cloudflare Pages. Apps that can't (e.g.
|
|
14
|
+
> a store you don't host) keep calling the gateway cross-origin and use **body** token
|
|
15
|
+
> transport instead — both models work against the same gateway + backend.
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pnpm add @auth-craft/pages-auth-proxy
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Cloudflare Pages usage
|
|
24
|
+
|
|
25
|
+
Add a catch-all Pages Function. Scope it to a prefix so it coexists with your app routes:
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
// functions/customer/[[path]].ts
|
|
29
|
+
import { createPagesAuthProxy } from '@auth-craft/pages-auth-proxy/pages';
|
|
30
|
+
|
|
31
|
+
export const onRequest = createPagesAuthProxy({ prefix: '/customer' });
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Configure the Pages project bindings:
|
|
35
|
+
|
|
36
|
+
| Binding | Purpose |
|
|
37
|
+
|---------|---------|
|
|
38
|
+
| `AUTH_GATEWAY` | **Service Binding** to the gateway worker (preferred — in-network, lowest latency) |
|
|
39
|
+
| `AUTH_GATEWAY_URL` | Gateway public URL (fallback when no Service Binding) |
|
|
40
|
+
| `EDGE_GATE_SECRET` | Client→gateway gate, sent as `X-Edge-Gate` (Pages **secret**) |
|
|
41
|
+
|
|
42
|
+
The frontend then calls auth on its **own origin**, e.g.
|
|
43
|
+
`fetch('/customer/auth/authenticate', { method: 'POST', credentials: 'include', ... })`.
|
|
44
|
+
The cookie set by the backend is first-party to the app.
|
|
45
|
+
|
|
46
|
+
### Pairs with `@auth-craft/client` cookie mode
|
|
47
|
+
|
|
48
|
+
Because cookies are first-party, use the cookie provider — no IndexedDB, refresh handled
|
|
49
|
+
by the `HttpOnly` cookie:
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
import { createCookieProvider } from '@auth-craft/client';
|
|
53
|
+
|
|
54
|
+
const provider = createCookieProvider({
|
|
55
|
+
loginUrl: '/customer/auth/authenticate',
|
|
56
|
+
refreshUrl: '/customer/auth/refresh',
|
|
57
|
+
logoutUrl: '/customer/auth/logout',
|
|
58
|
+
});
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Framework-agnostic core
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
import { createAuthProxy } from '@auth-craft/pages-auth-proxy';
|
|
65
|
+
|
|
66
|
+
const proxy = createAuthProxy({
|
|
67
|
+
prefix: '/customer',
|
|
68
|
+
gatewayUrl: 'https://auth-craft-prod-customer.acme.workers.dev',
|
|
69
|
+
edgeGateSecret: env.EDGE_GATE_SECRET,
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
export default { fetch: (req: Request) => proxy(req) };
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Security notes
|
|
76
|
+
|
|
77
|
+
- **Not a CORS replacement — better.** Same-origin requests have no CORS at all. Site
|
|
78
|
+
isolation comes from first-party `SameSite` cookies + `HttpOnly` + the backend's CSRF
|
|
79
|
+
Origin check (`trustedOrigins`), not from CORS.
|
|
80
|
+
- Keep `EDGE_GATE_SECRET` in a Pages **secret**. With a Service Binding you can configure
|
|
81
|
+
the gateway to trust the binding and avoid exposing the gate entirely.
|
|
82
|
+
- The proxy preserves method, body, headers and cookies; it adds only `X-Edge-Gate`.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @auth-craft/pages-auth-proxy
|
|
3
|
+
*
|
|
4
|
+
* Thin reverse-proxy so an app on Cloudflare Pages can serve `/{scope}/auth/*` on its
|
|
5
|
+
* OWN origin (first-party cookies, no cross-site CORS, immune to Safari ITP) while all
|
|
6
|
+
* trust-critical auth logic stays in the Auth Craft gateway worker.
|
|
7
|
+
*
|
|
8
|
+
* Framework-agnostic core: `createAuthProxy`. For Cloudflare Pages Functions use the
|
|
9
|
+
* `@auth-craft/pages-auth-proxy/pages` subpath.
|
|
10
|
+
*/
|
|
11
|
+
export { createAuthProxy } from './proxy';
|
|
12
|
+
export type { AuthProxyOptions, ProxyFetcher } from './proxy';
|
|
13
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAC1C,YAAY,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @auth-craft/pages-auth-proxy
|
|
3
|
+
*
|
|
4
|
+
* Thin reverse-proxy so an app on Cloudflare Pages can serve `/{scope}/auth/*` on its
|
|
5
|
+
* OWN origin (first-party cookies, no cross-site CORS, immune to Safari ITP) while all
|
|
6
|
+
* trust-critical auth logic stays in the Auth Craft gateway worker.
|
|
7
|
+
*
|
|
8
|
+
* Framework-agnostic core: `createAuthProxy`. For Cloudflare Pages Functions use the
|
|
9
|
+
* `@auth-craft/pages-auth-proxy/pages` subpath.
|
|
10
|
+
*/
|
|
11
|
+
export { createAuthProxy } from './proxy';
|
|
12
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC"}
|
package/dist/pages.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cloudflare Pages Functions adapter for the auth reverse-proxy.
|
|
3
|
+
*
|
|
4
|
+
* Drop this into a catch-all Pages Function so `/{scope}/auth/*` is served on the app's
|
|
5
|
+
* own origin and forwarded to the gateway:
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* // functions/[[path]].ts (or functions/auth/[[path]].ts to scope to /auth)
|
|
9
|
+
* import { createPagesAuthProxy } from '@auth-craft/pages-auth-proxy/pages';
|
|
10
|
+
*
|
|
11
|
+
* export const onRequest = createPagesAuthProxy({ prefix: '/customer' });
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* Bindings read from `env` (configure in the Pages project):
|
|
15
|
+
* - `AUTH_GATEWAY` — Service Binding to the gateway worker (preferred), OR
|
|
16
|
+
* - `AUTH_GATEWAY_URL` — gateway public URL (fallback when no Service Binding)
|
|
17
|
+
* - `EDGE_GATE_SECRET` — client→gateway gate (Pages secret)
|
|
18
|
+
*
|
|
19
|
+
* Anything outside `prefix` falls through to the rest of the Pages app via `next()`.
|
|
20
|
+
*/
|
|
21
|
+
import { type ProxyFetcher } from './proxy';
|
|
22
|
+
/** Env bindings this adapter reads from the Pages project. */
|
|
23
|
+
export interface PagesProxyEnv {
|
|
24
|
+
/** Service Binding to the gateway worker (preferred). */
|
|
25
|
+
AUTH_GATEWAY?: ProxyFetcher;
|
|
26
|
+
/** Gateway public URL (used when AUTH_GATEWAY is not bound). */
|
|
27
|
+
AUTH_GATEWAY_URL?: string;
|
|
28
|
+
/** Client→gateway gate secret, sent as X-Edge-Gate. */
|
|
29
|
+
EDGE_GATE_SECRET?: string;
|
|
30
|
+
}
|
|
31
|
+
/** Minimal shape of a Pages Functions event context (avoids a hard dep on @cloudflare/workers-types consumers). */
|
|
32
|
+
interface PagesContext<Env extends PagesProxyEnv> {
|
|
33
|
+
request: Request;
|
|
34
|
+
env: Env;
|
|
35
|
+
next: () => Promise<Response>;
|
|
36
|
+
}
|
|
37
|
+
export interface CreatePagesAuthProxyOptions {
|
|
38
|
+
/**
|
|
39
|
+
* Path prefix to proxy (e.g. '/customer', '/auth'). Requests outside it fall through
|
|
40
|
+
* to the rest of the Pages app. Default '' proxies everything the Function matches.
|
|
41
|
+
*/
|
|
42
|
+
prefix?: string;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Build a Pages Functions `onRequest` handler that proxies auth traffic to the gateway.
|
|
46
|
+
* Requests outside `prefix` are handed back to the Pages pipeline via `next()`.
|
|
47
|
+
*/
|
|
48
|
+
export declare function createPagesAuthProxy<Env extends PagesProxyEnv = PagesProxyEnv>(options?: CreatePagesAuthProxyOptions): (context: PagesContext<Env>) => Promise<Response>;
|
|
49
|
+
export {};
|
|
50
|
+
//# sourceMappingURL=pages.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pages.d.ts","sourceRoot":"","sources":["../src/pages.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EAAmB,KAAK,YAAY,EAAE,MAAM,SAAS,CAAC;AAE7D,8DAA8D;AAC9D,MAAM,WAAW,aAAa;IAC5B,yDAAyD;IACzD,YAAY,CAAC,EAAE,YAAY,CAAC;IAC5B,gEAAgE;IAChE,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,uDAAuD;IACvD,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC3B;AAED,mHAAmH;AACnH,UAAU,YAAY,CAAC,GAAG,SAAS,aAAa;IAC9C,OAAO,EAAE,OAAO,CAAC;IACjB,GAAG,EAAE,GAAG,CAAC;IACT,IAAI,EAAE,MAAM,OAAO,CAAC,QAAQ,CAAC,CAAC;CAC/B;AAED,MAAM,WAAW,2BAA2B;IAC1C;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,SAAS,aAAa,GAAG,aAAa,EAC5E,OAAO,GAAE,2BAAgC,GACxC,CAAC,OAAO,EAAE,YAAY,CAAC,GAAG,CAAC,KAAK,OAAO,CAAC,QAAQ,CAAC,CAqBnD"}
|
package/dist/pages.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cloudflare Pages Functions adapter for the auth reverse-proxy.
|
|
3
|
+
*
|
|
4
|
+
* Drop this into a catch-all Pages Function so `/{scope}/auth/*` is served on the app's
|
|
5
|
+
* own origin and forwarded to the gateway:
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* // functions/[[path]].ts (or functions/auth/[[path]].ts to scope to /auth)
|
|
9
|
+
* import { createPagesAuthProxy } from '@auth-craft/pages-auth-proxy/pages';
|
|
10
|
+
*
|
|
11
|
+
* export const onRequest = createPagesAuthProxy({ prefix: '/customer' });
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* Bindings read from `env` (configure in the Pages project):
|
|
15
|
+
* - `AUTH_GATEWAY` — Service Binding to the gateway worker (preferred), OR
|
|
16
|
+
* - `AUTH_GATEWAY_URL` — gateway public URL (fallback when no Service Binding)
|
|
17
|
+
* - `EDGE_GATE_SECRET` — client→gateway gate (Pages secret)
|
|
18
|
+
*
|
|
19
|
+
* Anything outside `prefix` falls through to the rest of the Pages app via `next()`.
|
|
20
|
+
*/
|
|
21
|
+
import { createAuthProxy } from './proxy';
|
|
22
|
+
/**
|
|
23
|
+
* Build a Pages Functions `onRequest` handler that proxies auth traffic to the gateway.
|
|
24
|
+
* Requests outside `prefix` are handed back to the Pages pipeline via `next()`.
|
|
25
|
+
*/
|
|
26
|
+
export function createPagesAuthProxy(options = {}) {
|
|
27
|
+
return async (context) => {
|
|
28
|
+
const { request, env, next } = context;
|
|
29
|
+
if (!env.AUTH_GATEWAY && !env.AUTH_GATEWAY_URL) {
|
|
30
|
+
throw new Error('pages-auth-proxy: bind AUTH_GATEWAY (Service Binding) or set AUTH_GATEWAY_URL in the Pages project.');
|
|
31
|
+
}
|
|
32
|
+
const proxy = createAuthProxy({
|
|
33
|
+
...(options.prefix !== undefined ? { prefix: options.prefix } : {}),
|
|
34
|
+
...(env.AUTH_GATEWAY ? { gateway: env.AUTH_GATEWAY } : {}),
|
|
35
|
+
...(env.AUTH_GATEWAY_URL ? { gatewayUrl: env.AUTH_GATEWAY_URL } : {}),
|
|
36
|
+
...(env.EDGE_GATE_SECRET ? { edgeGateSecret: env.EDGE_GATE_SECRET } : {}),
|
|
37
|
+
// Outside the prefix → let the rest of the Pages app handle it.
|
|
38
|
+
passthrough: () => next(),
|
|
39
|
+
});
|
|
40
|
+
return proxy(request);
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=pages.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+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 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Framework-agnostic auth reverse-proxy core.
|
|
3
|
+
*
|
|
4
|
+
* The store/app frontend calls `/{prefix}/...` on its OWN origin (e.g.
|
|
5
|
+
* `https://shop.brand.com/customer/auth/authenticate`). This proxy forwards that to
|
|
6
|
+
* the Auth Craft gateway worker, which derives the scope, stamps `X-Auth-Context`,
|
|
7
|
+
* verifies JWTs and reaches the backend.
|
|
8
|
+
*
|
|
9
|
+
* Because the browser only ever sees the app's own origin, the refresh cookie the
|
|
10
|
+
* gateway/backend sets is FIRST-PARTY (same-site) — `SameSite=Lax/Strict` works, no
|
|
11
|
+
* cross-site CORS, immune to Safari ITP. All trust-critical logic stays in the
|
|
12
|
+
* gateway; this proxy only attaches the client→gateway gate (`X-Edge-Gate`) and
|
|
13
|
+
* never holds backend secrets or decides scope.
|
|
14
|
+
*
|
|
15
|
+
* Two ways to reach the gateway (pick one):
|
|
16
|
+
* - Service Binding (preferred): `gateway` is a Fetcher bound to the gateway worker.
|
|
17
|
+
* In-network, lowest latency, and `X-Edge-Gate` need not even be exposed.
|
|
18
|
+
* - HTTP: `gatewayUrl` is the gateway's public URL; the proxy fetches it directly.
|
|
19
|
+
*/
|
|
20
|
+
/** A minimal fetcher — a Cloudflare Service Binding satisfies this. */
|
|
21
|
+
export interface ProxyFetcher {
|
|
22
|
+
fetch(request: Request): Promise<Response>;
|
|
23
|
+
}
|
|
24
|
+
export interface AuthProxyOptions {
|
|
25
|
+
/**
|
|
26
|
+
* Path prefix this proxy serves, e.g. '/auth' or '/customer'. Requests whose path
|
|
27
|
+
* starts with this prefix are forwarded; everything else returns `passthrough`
|
|
28
|
+
* (default: a 404) so the proxy can be mounted alongside the app's own routes.
|
|
29
|
+
* The prefix is FORWARDED as-is — the gateway expects `/{scope}/...` and strips it.
|
|
30
|
+
* @default '' (proxy every request)
|
|
31
|
+
*/
|
|
32
|
+
prefix?: string;
|
|
33
|
+
/**
|
|
34
|
+
* Service Binding to the gateway worker (preferred). Mutually exclusive with
|
|
35
|
+
* `gatewayUrl`; if both are set, `gateway` wins.
|
|
36
|
+
*/
|
|
37
|
+
gateway?: ProxyFetcher;
|
|
38
|
+
/**
|
|
39
|
+
* Public URL of the gateway worker (used when no Service Binding is available).
|
|
40
|
+
* The forwarded path + query is appended to this origin.
|
|
41
|
+
* @example 'https://auth-craft-prod-customer.acme.workers.dev'
|
|
42
|
+
*/
|
|
43
|
+
gatewayUrl?: string;
|
|
44
|
+
/**
|
|
45
|
+
* Client→gateway gate secret, sent as `X-Edge-Gate`. Required by the gateway unless
|
|
46
|
+
* it is reached via a Service Binding configured to skip the gate. Keep it in a
|
|
47
|
+
* Pages secret/env binding — never in client code.
|
|
48
|
+
*/
|
|
49
|
+
edgeGateSecret?: string;
|
|
50
|
+
/**
|
|
51
|
+
* Response returned for requests outside `prefix`. Lets the proxy coexist with the
|
|
52
|
+
* app's own handlers. Defaults to a bare 404.
|
|
53
|
+
*/
|
|
54
|
+
passthrough?: (request: Request) => Promise<Response> | Response;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Create the proxy handler. Returns a function suitable for any
|
|
58
|
+
* `(request) => Response` runtime (Pages Functions, Workers, etc.).
|
|
59
|
+
*/
|
|
60
|
+
export declare function createAuthProxy(options: AuthProxyOptions): (request: Request) => Promise<Response>;
|
|
61
|
+
//# sourceMappingURL=proxy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"proxy.d.ts","sourceRoot":"","sources":["../src/proxy.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,uEAAuE;AACvE,MAAM,WAAW,YAAY;IAC3B,KAAK,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;CAC5C;AAED,MAAM,WAAW,gBAAgB;IAC/B;;;;;;OAMG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB;;;OAGG;IACH,OAAO,CAAC,EAAE,YAAY,CAAC;IAEvB;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;;;OAIG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;IAExB;;;OAGG;IACH,WAAW,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,GAAG,QAAQ,CAAC;CAClE;AAoCD;;;GAGG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,gBAAgB,GAAG,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,CA2BlG"}
|
package/dist/proxy.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Framework-agnostic auth reverse-proxy core.
|
|
3
|
+
*
|
|
4
|
+
* The store/app frontend calls `/{prefix}/...` on its OWN origin (e.g.
|
|
5
|
+
* `https://shop.brand.com/customer/auth/authenticate`). This proxy forwards that to
|
|
6
|
+
* the Auth Craft gateway worker, which derives the scope, stamps `X-Auth-Context`,
|
|
7
|
+
* verifies JWTs and reaches the backend.
|
|
8
|
+
*
|
|
9
|
+
* Because the browser only ever sees the app's own origin, the refresh cookie the
|
|
10
|
+
* gateway/backend sets is FIRST-PARTY (same-site) — `SameSite=Lax/Strict` works, no
|
|
11
|
+
* cross-site CORS, immune to Safari ITP. All trust-critical logic stays in the
|
|
12
|
+
* gateway; this proxy only attaches the client→gateway gate (`X-Edge-Gate`) and
|
|
13
|
+
* never holds backend secrets or decides scope.
|
|
14
|
+
*
|
|
15
|
+
* Two ways to reach the gateway (pick one):
|
|
16
|
+
* - Service Binding (preferred): `gateway` is a Fetcher bound to the gateway worker.
|
|
17
|
+
* In-network, lowest latency, and `X-Edge-Gate` need not even be exposed.
|
|
18
|
+
* - HTTP: `gatewayUrl` is the gateway's public URL; the proxy fetches it directly.
|
|
19
|
+
*/
|
|
20
|
+
function normalizePrefix(prefix) {
|
|
21
|
+
if (!prefix)
|
|
22
|
+
return '';
|
|
23
|
+
const p = prefix.startsWith('/') ? prefix : `/${prefix}`;
|
|
24
|
+
return p.endsWith('/') ? p.slice(0, -1) : p;
|
|
25
|
+
}
|
|
26
|
+
function matchesPrefix(pathname, prefix) {
|
|
27
|
+
if (!prefix)
|
|
28
|
+
return true;
|
|
29
|
+
return pathname === prefix || pathname.startsWith(`${prefix}/`);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Build the request forwarded to the gateway: same method/body/headers, with
|
|
33
|
+
* `X-Edge-Gate` attached. The path is preserved (the gateway derives + strips scope).
|
|
34
|
+
*/
|
|
35
|
+
function buildGatewayRequest(request, edgeGateSecret, targetUrl) {
|
|
36
|
+
const headers = new Headers(request.headers);
|
|
37
|
+
if (edgeGateSecret)
|
|
38
|
+
headers.set('X-Edge-Gate', edgeGateSecret);
|
|
39
|
+
const init = {
|
|
40
|
+
method: request.method,
|
|
41
|
+
headers,
|
|
42
|
+
redirect: 'manual',
|
|
43
|
+
};
|
|
44
|
+
const hasBody = request.method !== 'GET' && request.method !== 'HEAD' && request.body !== null;
|
|
45
|
+
if (hasBody) {
|
|
46
|
+
init.body = request.body;
|
|
47
|
+
// Streaming bodies require `duplex: 'half'` on the Workers runtime.
|
|
48
|
+
init.duplex = 'half';
|
|
49
|
+
}
|
|
50
|
+
return new Request(targetUrl ?? request.url, init);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Create the proxy handler. Returns a function suitable for any
|
|
54
|
+
* `(request) => Response` runtime (Pages Functions, Workers, etc.).
|
|
55
|
+
*/
|
|
56
|
+
export function createAuthProxy(options) {
|
|
57
|
+
const prefix = normalizePrefix(options.prefix);
|
|
58
|
+
const passthrough = options.passthrough ?? (() => new Response(null, { status: 404 }));
|
|
59
|
+
if (!options.gateway && !options.gatewayUrl) {
|
|
60
|
+
throw new Error('createAuthProxy: provide either `gateway` (Service Binding) or `gatewayUrl`.');
|
|
61
|
+
}
|
|
62
|
+
return async (request) => {
|
|
63
|
+
const url = new URL(request.url);
|
|
64
|
+
if (!matchesPrefix(url.pathname, prefix)) {
|
|
65
|
+
return passthrough(request);
|
|
66
|
+
}
|
|
67
|
+
// Service Binding path — forward the same-origin URL; the bound worker handles it.
|
|
68
|
+
if (options.gateway) {
|
|
69
|
+
const forwarded = buildGatewayRequest(request, options.edgeGateSecret);
|
|
70
|
+
return options.gateway.fetch(forwarded);
|
|
71
|
+
}
|
|
72
|
+
// HTTP path — rebuild against the gateway origin, preserving path + query.
|
|
73
|
+
const base = options.gatewayUrl.endsWith('/') ? options.gatewayUrl.slice(0, -1) : options.gatewayUrl;
|
|
74
|
+
const target = new URL(`${base}${url.pathname}`);
|
|
75
|
+
target.search = url.search;
|
|
76
|
+
const forwarded = buildGatewayRequest(request, options.edgeGateSecret, target.toString());
|
|
77
|
+
return fetch(forwarded);
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=proxy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"proxy.js","sourceRoot":"","sources":["../src/proxy.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AA4CH,SAAS,eAAe,CAAC,MAA0B;IACjD,IAAI,CAAC,MAAM;QAAE,OAAO,EAAE,CAAC;IACvB,MAAM,CAAC,GAAG,MAAM,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,MAAM,EAAE,CAAC;IACzD,OAAO,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAC9C,CAAC;AAED,SAAS,aAAa,CAAC,QAAgB,EAAE,MAAc;IACrD,IAAI,CAAC,MAAM;QAAE,OAAO,IAAI,CAAC;IACzB,OAAO,QAAQ,KAAK,MAAM,IAAI,QAAQ,CAAC,UAAU,CA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AG,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QACjC,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,QAAQ,EAAE,MAAM,CAAC,EAAE,CAAC;YACzC,OAAO,WAAW,CAAC,OAAO,CAAC,CAAC;QAC9B,CAAC;QAED,mFAAmF;QACnF,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;YACpB,MAAM,SAAS,GAAG,mBAAmB,CAAC,OAAO,EAAE,OAAO,CAAC,cAAc,CAAC,CAAC;YACvE,OAAO,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;QAC1C,CAAC;QAED,2EAA2E;QAC3E,MAAM,IAAI,GAAG,OAAO,CAAC,UAAW,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,UAAW,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,UAAW,CAAC;QACxG,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC,GAAG,IAAI,GAAG,GAAG,CAAC,QAAQ,EAAE,CAAC,CAAC;QACjD,MAAM,CAAC,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC;QAC3B,MAAM,SAAS,GAAG,mBAAmB,CAAC,OAAO,EAAE,OAAO,CAAC,cAAc,EAAE,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC;QAC1F,OAAO,KAAK,CAAC,SAAS,CAAC,CAAC;IAC1B,CAAC,CAAC;AACJ,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@auth-craft/pages-auth-proxy",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Thin Cloudflare Pages reverse-proxy for Auth Craft — serves /{scope}/auth/* same-origin (first-party cookies) and forwards to the gateway worker (Service Binding or HTTP + X-Edge-Gate). Keeps all trust-critical logic in the gateway.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"@cloudflare/workers-types": "^4.20260610.1",
|
|
14
|
+
"typescript": "^6.0.3",
|
|
15
|
+
"vitest": "^4.1.8",
|
|
16
|
+
"@auth-craft/tsconfig": "0.1.0"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"auth-craft",
|
|
20
|
+
"cloudflare",
|
|
21
|
+
"pages",
|
|
22
|
+
"proxy",
|
|
23
|
+
"bff",
|
|
24
|
+
"first-party-cookies"
|
|
25
|
+
],
|
|
26
|
+
"author": "Auth Craft Contributors",
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"import": "./dist/index.js"
|
|
32
|
+
},
|
|
33
|
+
"./pages": {
|
|
34
|
+
"types": "./dist/pages.d.ts",
|
|
35
|
+
"import": "./dist/pages.js"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "tsc",
|
|
40
|
+
"dev": "tsc --watch",
|
|
41
|
+
"type-check": "tsc --noEmit",
|
|
42
|
+
"test": "vitest run",
|
|
43
|
+
"test:watch": "vitest",
|
|
44
|
+
"clean": "rm -rf dist"
|
|
45
|
+
}
|
|
46
|
+
}
|