@auth-craft/pages-auth-proxy 1.0.0 → 1.1.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/README.md +23 -13
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/pages.d.ts +17 -7
- package/dist/pages.d.ts.map +1 -1
- package/dist/pages.js +8 -5
- package/dist/pages.js.map +1 -1
- package/dist/proxy.d.ts +21 -2
- package/dist/proxy.d.ts.map +1 -1
- package/dist/proxy.js +15 -5
- package/dist/proxy.js.map +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -6,9 +6,15 @@ Thin reverse-proxy so an app on **Cloudflare Pages** can serve `/{scope}/auth/*`
|
|
|
6
6
|
Why: when the browser only ever sees the app's own origin, the refresh cookie the
|
|
7
7
|
backend sets is **first-party** (same-site) — `SameSite=Lax/Strict` works, there is **no
|
|
8
8
|
cross-site CORS**, and it is **immune to Safari ITP**. This proxy is intentionally
|
|
9
|
-
**thin**: it never holds backend secrets
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
**thin**: it never holds backend secrets. All trust-critical logic (`X-Gateway-Secret`,
|
|
10
|
+
JWT verify, rate-limit, stealth 404) stays in the gateway.
|
|
11
|
+
|
|
12
|
+
The one thing it *can* enforce is **scope**: lock the proxy to a single scope (`scope:
|
|
13
|
+
'customer'`) and the frontend calls scope-less `/auth/...` while the proxy prepends the
|
|
14
|
+
scope segment. The client never picks the scope, so a customer app **cannot** reach
|
|
15
|
+
system/tenant context through it — the lock holds even against a single shared gateway
|
|
16
|
+
worker that derives scope from the path. (Leave `scope` unset for the legacy behaviour
|
|
17
|
+
where the client carries its own `/{scope}/...` segment.)
|
|
12
18
|
|
|
13
19
|
> Optional: use this only for apps you deploy on Cloudflare Pages. Apps that can't (e.g.
|
|
14
20
|
> a store you don't host) keep calling the gateway cross-origin and use **body** token
|
|
@@ -22,13 +28,13 @@ pnpm add @auth-craft/pages-auth-proxy
|
|
|
22
28
|
|
|
23
29
|
## Cloudflare Pages usage
|
|
24
30
|
|
|
25
|
-
Add a catch-all Pages Function
|
|
31
|
+
Add a catch-all Pages Function under `/auth` and lock it to this app's scope:
|
|
26
32
|
|
|
27
33
|
```ts
|
|
28
|
-
// functions/
|
|
34
|
+
// functions/auth/[[path]].ts
|
|
29
35
|
import { createPagesAuthProxy } from '@auth-craft/pages-auth-proxy/pages';
|
|
30
36
|
|
|
31
|
-
export const onRequest = createPagesAuthProxy({
|
|
37
|
+
export const onRequest = createPagesAuthProxy({ scope: 'customer' });
|
|
32
38
|
```
|
|
33
39
|
|
|
34
40
|
Configure the Pages project bindings:
|
|
@@ -39,9 +45,9 @@ Configure the Pages project bindings:
|
|
|
39
45
|
| `AUTH_GATEWAY_URL` | Gateway public URL (fallback when no Service Binding) |
|
|
40
46
|
| `EDGE_GATE_SECRET` | Client→gateway gate, sent as `X-Edge-Gate` (Pages **secret**) |
|
|
41
47
|
|
|
42
|
-
The frontend then calls auth on its **own origin**, e.g.
|
|
43
|
-
`fetch('/
|
|
44
|
-
The cookie set by the backend is first-party to the app.
|
|
48
|
+
The frontend then calls auth on its **own origin**, scope-less, e.g.
|
|
49
|
+
`fetch('/auth/authenticate', { method: 'POST', credentials: 'include', ... })`.
|
|
50
|
+
The proxy stamps the scope; the cookie set by the backend is first-party to the app.
|
|
45
51
|
|
|
46
52
|
### Pairs with `@auth-craft/client` cookie mode
|
|
47
53
|
|
|
@@ -52,9 +58,9 @@ by the `HttpOnly` cookie:
|
|
|
52
58
|
import { createCookieProvider } from '@auth-craft/client';
|
|
53
59
|
|
|
54
60
|
const provider = createCookieProvider({
|
|
55
|
-
loginUrl: '/
|
|
56
|
-
refreshUrl: '/
|
|
57
|
-
logoutUrl: '/
|
|
61
|
+
loginUrl: '/auth/authenticate',
|
|
62
|
+
refreshUrl: '/auth/refresh',
|
|
63
|
+
logoutUrl: '/auth/logout',
|
|
58
64
|
});
|
|
59
65
|
```
|
|
60
66
|
|
|
@@ -64,7 +70,7 @@ const provider = createCookieProvider({
|
|
|
64
70
|
import { createAuthProxy } from '@auth-craft/pages-auth-proxy';
|
|
65
71
|
|
|
66
72
|
const proxy = createAuthProxy({
|
|
67
|
-
|
|
73
|
+
scope: 'customer',
|
|
68
74
|
gatewayUrl: 'https://auth-craft-prod-customer.acme.workers.dev',
|
|
69
75
|
edgeGateSecret: env.EDGE_GATE_SECRET,
|
|
70
76
|
});
|
|
@@ -77,6 +83,10 @@ export default { fetch: (req: Request) => proxy(req) };
|
|
|
77
83
|
- **Not a CORS replacement — better.** Same-origin requests have no CORS at all. Site
|
|
78
84
|
isolation comes from first-party `SameSite` cookies + `HttpOnly` + the backend's CSRF
|
|
79
85
|
Origin check (`trustedOrigins`), not from CORS.
|
|
86
|
+
- **Scope is enforced, not trusted.** With `scope` set, the prepended segment is the
|
|
87
|
+
lock's value; the client's path is normalized (`..` collapsed) before matching, so it
|
|
88
|
+
can't smuggle another scope. A customer app physically cannot reach system/tenant —
|
|
89
|
+
even against one shared gateway worker. Use one locked proxy per app/scope.
|
|
80
90
|
- Keep `EDGE_GATE_SECRET` in a Pages **secret**. With a Service Binding you can configure
|
|
81
91
|
the gateway to trust the binding and avoid exposing the gate entirely.
|
|
82
92
|
- The proxy preserves method, body, headers and cookies; it adds only `X-Edge-Gate`.
|
package/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,6 @@
|
|
|
8
8
|
* Framework-agnostic core: `createAuthProxy`. For Cloudflare Pages Functions use the
|
|
9
9
|
* `@auth-craft/pages-auth-proxy/pages` subpath.
|
|
10
10
|
*/
|
|
11
|
-
export {
|
|
12
|
-
export
|
|
11
|
+
export type { AuthProxyOptions, AuthScope, ProxyFetcher } from './proxy.js';
|
|
12
|
+
export { createAuthProxy } from './proxy.js';
|
|
13
13
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,YAAY,EAAE,gBAAgB,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAC5E,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -8,5 +8,5 @@
|
|
|
8
8
|
* Framework-agnostic core: `createAuthProxy`. For Cloudflare Pages Functions use the
|
|
9
9
|
* `@auth-craft/pages-auth-proxy/pages` subpath.
|
|
10
10
|
*/
|
|
11
|
-
export { createAuthProxy } from './proxy';
|
|
11
|
+
export { createAuthProxy } from './proxy.js';
|
|
12
12
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC"}
|
package/dist/pages.d.ts
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Cloudflare Pages Functions adapter for the auth reverse-proxy.
|
|
3
3
|
*
|
|
4
|
-
* Drop this into a catch-all Pages Function so `/
|
|
5
|
-
*
|
|
4
|
+
* Drop this into a catch-all Pages Function so `/auth/*` is served on the app's own
|
|
5
|
+
* origin and forwarded to the gateway. Lock it to the scope this app serves — the
|
|
6
|
+
* frontend then calls scope-less `/auth/...` and the proxy stamps the scope, so a
|
|
7
|
+
* customer app can never reach system/tenant context:
|
|
6
8
|
*
|
|
7
9
|
* ```ts
|
|
8
|
-
* // functions/
|
|
10
|
+
* // functions/auth/[[path]].ts
|
|
9
11
|
* import { createPagesAuthProxy } from '@auth-craft/pages-auth-proxy/pages';
|
|
10
12
|
*
|
|
11
|
-
* export const onRequest = createPagesAuthProxy({
|
|
13
|
+
* export const onRequest = createPagesAuthProxy({ scope: 'customer' });
|
|
12
14
|
* ```
|
|
13
15
|
*
|
|
14
16
|
* Bindings read from `env` (configure in the Pages project):
|
|
@@ -18,7 +20,7 @@
|
|
|
18
20
|
*
|
|
19
21
|
* Anything outside `prefix` falls through to the rest of the Pages app via `next()`.
|
|
20
22
|
*/
|
|
21
|
-
import { type ProxyFetcher } from './proxy';
|
|
23
|
+
import { type AuthScope, type ProxyFetcher } from './proxy.js';
|
|
22
24
|
/** Env bindings this adapter reads from the Pages project. */
|
|
23
25
|
export interface PagesProxyEnv {
|
|
24
26
|
/** Service Binding to the gateway worker (preferred). */
|
|
@@ -36,8 +38,16 @@ interface PagesContext<Env extends PagesProxyEnv> {
|
|
|
36
38
|
}
|
|
37
39
|
export interface CreatePagesAuthProxyOptions {
|
|
38
40
|
/**
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
+
* Lock this proxy to a single auth scope (recommended). The frontend then calls
|
|
42
|
+
* scope-less paths (e.g. `/auth/...`) and the proxy prepends the scope before
|
|
43
|
+
* forwarding, so a customer app can never reach system/tenant context through it.
|
|
44
|
+
* See {@link AuthScope}.
|
|
45
|
+
*/
|
|
46
|
+
scope?: AuthScope;
|
|
47
|
+
/**
|
|
48
|
+
* Path prefix to proxy. Requests outside it fall through to the rest of the Pages app.
|
|
49
|
+
* Defaults to '/auth' when `scope` is set (the client calls a scope-less path), else
|
|
50
|
+
* '' (proxies everything the Function matches).
|
|
41
51
|
*/
|
|
42
52
|
prefix?: string;
|
|
43
53
|
}
|
package/dist/pages.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pages.d.ts","sourceRoot":"","sources":["../src/pages.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"pages.d.ts","sourceRoot":"","sources":["../src/pages.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,EAAE,KAAK,SAAS,EAAmB,KAAK,YAAY,EAAE,MAAM,YAAY,CAAC;AAEhF,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;;;;;OAKG;IACH,KAAK,CAAC,EAAE,SAAS,CAAC;IAElB;;;;OAIG;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,CAsBnD"}
|
package/dist/pages.js
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Cloudflare Pages Functions adapter for the auth reverse-proxy.
|
|
3
3
|
*
|
|
4
|
-
* Drop this into a catch-all Pages Function so `/
|
|
5
|
-
*
|
|
4
|
+
* Drop this into a catch-all Pages Function so `/auth/*` is served on the app's own
|
|
5
|
+
* origin and forwarded to the gateway. Lock it to the scope this app serves — the
|
|
6
|
+
* frontend then calls scope-less `/auth/...` and the proxy stamps the scope, so a
|
|
7
|
+
* customer app can never reach system/tenant context:
|
|
6
8
|
*
|
|
7
9
|
* ```ts
|
|
8
|
-
* // functions/
|
|
10
|
+
* // functions/auth/[[path]].ts
|
|
9
11
|
* import { createPagesAuthProxy } from '@auth-craft/pages-auth-proxy/pages';
|
|
10
12
|
*
|
|
11
|
-
* export const onRequest = createPagesAuthProxy({
|
|
13
|
+
* export const onRequest = createPagesAuthProxy({ scope: 'customer' });
|
|
12
14
|
* ```
|
|
13
15
|
*
|
|
14
16
|
* Bindings read from `env` (configure in the Pages project):
|
|
@@ -18,7 +20,7 @@
|
|
|
18
20
|
*
|
|
19
21
|
* Anything outside `prefix` falls through to the rest of the Pages app via `next()`.
|
|
20
22
|
*/
|
|
21
|
-
import { createAuthProxy } from './proxy';
|
|
23
|
+
import { createAuthProxy } from './proxy.js';
|
|
22
24
|
/**
|
|
23
25
|
* Build a Pages Functions `onRequest` handler that proxies auth traffic to the gateway.
|
|
24
26
|
* Requests outside `prefix` are handed back to the Pages pipeline via `next()`.
|
|
@@ -30,6 +32,7 @@ export function createPagesAuthProxy(options = {}) {
|
|
|
30
32
|
throw new Error('pages-auth-proxy: bind AUTH_GATEWAY (Service Binding) or set AUTH_GATEWAY_URL in the Pages project.');
|
|
31
33
|
}
|
|
32
34
|
const proxy = createAuthProxy({
|
|
35
|
+
...(options.scope !== undefined ? { scope: options.scope } : {}),
|
|
33
36
|
...(options.prefix !== undefined ? { prefix: options.prefix } : {}),
|
|
34
37
|
...(env.AUTH_GATEWAY ? { gateway: env.AUTH_GATEWAY } : {}),
|
|
35
38
|
...(env.AUTH_GATEWAY_URL ? { gatewayUrl: env.AUTH_GATEWAY_URL } : {}),
|
package/dist/pages.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pages.js","sourceRoot":"","sources":["../src/pages.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"pages.js","sourceRoot":"","sources":["../src/pages.ts"],"names":[],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,OAAO,CAAC,CAAC;IACxB,CAAC,CAAC;AACJ,CAAC"}
|
package/dist/proxy.d.ts
CHANGED
|
@@ -21,13 +21,32 @@
|
|
|
21
21
|
export interface ProxyFetcher {
|
|
22
22
|
fetch(request: Request): Promise<Response>;
|
|
23
23
|
}
|
|
24
|
+
/** The auth scope this proxy is locked to. The gateway reads it from the path segment. */
|
|
25
|
+
export type AuthScope = 'system' | 'tenant' | 'customer';
|
|
24
26
|
export interface AuthProxyOptions {
|
|
27
|
+
/**
|
|
28
|
+
* Lock this proxy to a single auth scope. When set, the frontend calls scope-LESS
|
|
29
|
+
* paths on its own origin (e.g. `/auth/authenticate`) and the proxy PREPENDS the
|
|
30
|
+
* scope segment before forwarding, so the gateway always receives `/{scope}/auth/...`.
|
|
31
|
+
*
|
|
32
|
+
* This is the security guarantee: the client never picks the scope, so a customer app
|
|
33
|
+
* physically cannot reach `system`/`tenant` context through this proxy — even against
|
|
34
|
+
* a single shared gateway worker that derives scope from the path. Set it to the scope
|
|
35
|
+
* this app serves.
|
|
36
|
+
*
|
|
37
|
+
* When omitted, the proxy forwards the path as-is (the frontend includes the scope
|
|
38
|
+
* segment itself, e.g. `/customer/auth/...`) — the legacy, un-enforced behaviour.
|
|
39
|
+
*/
|
|
40
|
+
scope?: AuthScope;
|
|
25
41
|
/**
|
|
26
42
|
* Path prefix this proxy serves, e.g. '/auth' or '/customer'. Requests whose path
|
|
27
43
|
* starts with this prefix are forwarded; everything else returns `passthrough`
|
|
28
44
|
* (default: a 404) so the proxy can be mounted alongside the app's own routes.
|
|
29
|
-
*
|
|
30
|
-
*
|
|
45
|
+
*
|
|
46
|
+
* With `scope` set, this is the scope-LESS prefix the frontend calls (default '/auth');
|
|
47
|
+
* the scope segment is added by the proxy, not the client. Without `scope`, the prefix
|
|
48
|
+
* is forwarded as-is and the gateway strips the scope from it.
|
|
49
|
+
* @default '/auth' when `scope` is set, otherwise '' (proxy every request)
|
|
31
50
|
*/
|
|
32
51
|
prefix?: string;
|
|
33
52
|
/**
|
package/dist/proxy.d.ts.map
CHANGED
|
@@ -1 +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
|
|
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,0FAA0F;AAC1F,MAAM,MAAM,SAAS,GAAG,QAAQ,GAAG,QAAQ,GAAG,UAAU,CAAC;AAEzD,MAAM,WAAW,gBAAgB;IAC/B;;;;;;;;;;;;OAYG;IACH,KAAK,CAAC,EAAE,SAAS,CAAC;IAElB;;;;;;;;;OASG;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,CAsClG"}
|
package/dist/proxy.js
CHANGED
|
@@ -54,7 +54,10 @@ function buildGatewayRequest(request, edgeGateSecret, targetUrl) {
|
|
|
54
54
|
* `(request) => Response` runtime (Pages Functions, Workers, etc.).
|
|
55
55
|
*/
|
|
56
56
|
export function createAuthProxy(options) {
|
|
57
|
-
|
|
57
|
+
// With a locked scope the client calls a scope-less prefix (default '/auth') and the
|
|
58
|
+
// proxy prepends the scope. Without one, default to '' (proxy everything it matches).
|
|
59
|
+
const scope = options.scope;
|
|
60
|
+
const prefix = normalizePrefix(options.prefix ?? (scope ? '/auth' : ''));
|
|
58
61
|
const passthrough = options.passthrough ?? (() => new Response(null, { status: 404 }));
|
|
59
62
|
if (!options.gateway && !options.gatewayUrl) {
|
|
60
63
|
throw new Error('createAuthProxy: provide either `gateway` (Service Binding) or `gatewayUrl`.');
|
|
@@ -64,14 +67,21 @@ export function createAuthProxy(options) {
|
|
|
64
67
|
if (!matchesPrefix(url.pathname, prefix)) {
|
|
65
68
|
return passthrough(request);
|
|
66
69
|
}
|
|
67
|
-
//
|
|
70
|
+
// Scope enforcement: prepend the locked scope segment so the gateway always sees
|
|
71
|
+
// `/{scope}{path}`. `url.pathname` is already normalized (`..` collapsed) by URL, so
|
|
72
|
+
// the client can't smuggle a different scope past this. When no scope is locked the
|
|
73
|
+
// path is forwarded unchanged (the client carries its own scope segment — legacy).
|
|
74
|
+
const forwardPath = scope ? `/${scope}${url.pathname}` : url.pathname;
|
|
75
|
+
// Service Binding path — forward to the bound worker on the (rewritten) path.
|
|
68
76
|
if (options.gateway) {
|
|
69
|
-
const
|
|
77
|
+
const target = new URL(url);
|
|
78
|
+
target.pathname = forwardPath;
|
|
79
|
+
const forwarded = buildGatewayRequest(request, options.edgeGateSecret, target.toString());
|
|
70
80
|
return options.gateway.fetch(forwarded);
|
|
71
81
|
}
|
|
72
|
-
// HTTP path — rebuild against the gateway origin, preserving path + query.
|
|
82
|
+
// HTTP path — rebuild against the gateway origin, preserving the (rewritten) path + query.
|
|
73
83
|
const base = options.gatewayUrl.endsWith('/') ? options.gatewayUrl.slice(0, -1) : options.gatewayUrl;
|
|
74
|
-
const target = new URL(`${base}${
|
|
84
|
+
const target = new URL(`${base}${forwardPath}`);
|
|
75
85
|
target.search = url.search;
|
|
76
86
|
const forwarded = buildGatewayRequest(request, options.edgeGateSecret, target.toString());
|
|
77
87
|
return fetch(forwarded);
|
package/dist/proxy.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"proxy.js","sourceRoot":"","sources":["../src/proxy.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;
|
|
1
|
+
{"version":3,"file":"proxy.js","sourceRoot":"","sources":["../src/proxy.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAiEH,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,CAAC,GAAG,MAAM,GAAG,CAAC,CAAC;AAClE,CAAC;AAED;;;GAGG;AACH,SAAS,mBAAmB,CAAC,OAAgB,EAAE,cAAkC,EAAE,SAAkB;IACnG,MAAM,OAAO,GAAG,IAAI,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAC7C,IAAI,cAAc;QAAE,OAAO,CAAC,GAAG,CAAC,aAAa,EAAE,cAAc,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
|
2
2
|
"name": "@auth-craft/pages-auth-proxy",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
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
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -10,9 +10,9 @@
|
|
|
10
10
|
"dist"
|
|
11
11
|
],
|
|
12
12
|
"devDependencies": {
|
|
13
|
-
"@cloudflare/workers-types": "^
|
|
14
|
-
"typescript": "^
|
|
15
|
-
"vitest": "^4.1.
|
|
13
|
+
"@cloudflare/workers-types": "^5.20260814.1",
|
|
14
|
+
"typescript": "^7.0.2",
|
|
15
|
+
"vitest": "^4.1.10",
|
|
16
16
|
"@auth-craft/tsconfig": "0.1.0"
|
|
17
17
|
},
|
|
18
18
|
"keywords": [
|