@marvalt/madapter 1.1.0 â 2.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/dist/client/mautic-client.d.ts +1 -8
- package/dist/client/mautic-client.d.ts.map +1 -1
- package/dist/generators/mautic-generator.d.ts.map +1 -1
- package/dist/index.cjs +73 -90
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +62 -17
- package/dist/index.d.ts.map +1 -1
- package/dist/index.esm.js +74 -92
- package/dist/index.esm.js.map +1 -1
- package/dist/react/components/TurnstileWidget.d.ts +47 -0
- package/dist/react/components/TurnstileWidget.d.ts.map +1 -0
- package/dist/server/index.d.ts +19 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/mautic-proxy.d.ts +28 -0
- package/dist/server/mautic-proxy.d.ts.map +1 -0
- package/dist/server/turnstile.d.ts +24 -0
- package/dist/server/turnstile.d.ts.map +1 -0
- package/dist/server.cjs +293 -0
- package/dist/server.cjs.map +1 -0
- package/dist/server.esm.js +290 -0
- package/dist/server.esm.js.map +1 -0
- package/dist/types/config.d.ts +12 -6
- package/dist/types/config.d.ts.map +1 -1
- package/dist/utils/config.d.ts +3 -3
- package/dist/utils/config.d.ts.map +1 -1
- package/package.json +9 -2
- package/scripts/postinstall.cjs +127 -0
- package/scripts/postinstall.js +114 -0
- package/templates/MAUTIC_SECURITY.md +308 -0
- package/templates/mautic-submit.ts +19 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license GPL-3.0-or-later
|
|
3
|
+
*
|
|
4
|
+
* This file is part of the MarVAlt Open SDK.
|
|
5
|
+
* Copyright (c) 2025 Vibune Pty Ltd.
|
|
6
|
+
*
|
|
7
|
+
* This program is free software: you can redistribute it and/or modify
|
|
8
|
+
* it under the terms of the GNU General Public License as published by
|
|
9
|
+
* the Free Software Foundation, either version 3 of the License, or
|
|
10
|
+
* (at your option) any later version.
|
|
11
|
+
*
|
|
12
|
+
* This program is distributed in the hope that it will be useful,
|
|
13
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
15
|
+
* See the GNU General Public License for more details.
|
|
16
|
+
*/
|
|
17
|
+
import React from 'react';
|
|
18
|
+
declare global {
|
|
19
|
+
interface Window {
|
|
20
|
+
turnstile?: {
|
|
21
|
+
render: (element: HTMLElement, options: TurnstileOptions) => string;
|
|
22
|
+
remove: (widgetId: string) => void;
|
|
23
|
+
reset: (widgetId: string) => void;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
interface TurnstileOptions {
|
|
28
|
+
sitekey: string;
|
|
29
|
+
callback?: (token: string) => void;
|
|
30
|
+
'error-callback'?: () => void;
|
|
31
|
+
'expired-callback'?: () => void;
|
|
32
|
+
theme?: 'light' | 'dark' | 'auto';
|
|
33
|
+
size?: 'normal' | 'compact';
|
|
34
|
+
tabindex?: number;
|
|
35
|
+
}
|
|
36
|
+
export interface TurnstileWidgetProps {
|
|
37
|
+
siteKey: string;
|
|
38
|
+
onSuccess: (token: string) => void;
|
|
39
|
+
onError?: () => void;
|
|
40
|
+
onExpire?: () => void;
|
|
41
|
+
theme?: 'light' | 'dark' | 'auto';
|
|
42
|
+
size?: 'normal' | 'compact';
|
|
43
|
+
tabIndex?: number;
|
|
44
|
+
}
|
|
45
|
+
export declare const TurnstileWidget: React.FC<TurnstileWidgetProps>;
|
|
46
|
+
export {};
|
|
47
|
+
//# sourceMappingURL=TurnstileWidget.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TurnstileWidget.d.ts","sourceRoot":"","sources":["../../../src/react/components/TurnstileWidget.tsx"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,KAA4B,MAAM,OAAO,CAAC;AAEjD,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,MAAM;QACd,SAA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program is free software: you can redistribute it and/or modify
|
|
8
|
+
* it under the terms of the GNU General Public License as published by
|
|
9
|
+
* the Free Software Foundation, either version 3 of the License, or
|
|
10
|
+
* (at your option) any later version.
|
|
11
|
+
*
|
|
12
|
+
* This program is distributed in the hope that it will be useful,
|
|
13
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
15
|
+
* See the GNU General Public License for more details.
|
|
16
|
+
*/
|
|
17
|
+
export * from './mautic-proxy';
|
|
18
|
+
export * from './turnstile';
|
|
19
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license GPL-3.0-or-later
|
|
3
|
+
*
|
|
4
|
+
* This file is part of the MarVAlt Open SDK.
|
|
5
|
+
* Copyright (c) 2025 Vibune Pty Ltd.
|
|
6
|
+
*
|
|
7
|
+
* This program is free software: you can redistribute it and/or modify
|
|
8
|
+
* it under the terms of the GNU General Public License as published by
|
|
9
|
+
* the Free Software Foundation, either version 3 of the License, or
|
|
10
|
+
* (at your option) any later version.
|
|
11
|
+
*
|
|
12
|
+
* This program is distributed in the hope that it will be useful,
|
|
13
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
15
|
+
* See the GNU General Public License for more details.
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* Cloudflare Pages Function handler for Mautic API proxy
|
|
19
|
+
*
|
|
20
|
+
* Environment variables required:
|
|
21
|
+
* - VITE_MAUTIC_URL or MAUTIC_URL: Mautic instance URL
|
|
22
|
+
* - VITE_MAUTIC_API_PUBLIC_KEY or MAUTIC_API_PUBLIC_KEY: OAuth2 client ID
|
|
23
|
+
* - VITE_MAUTIC_API_SECRET_KEY or MAUTIC_API_SECRET_KEY: OAuth2 client secret
|
|
24
|
+
* - VITE_CF_ACCESS_CLIENT_ID or CF_ACCESS_CLIENT_ID: (Optional) Cloudflare Access client ID
|
|
25
|
+
* - VITE_CF_ACCESS_CLIENT_SECRET or CF_ACCESS_CLIENT_SECRET: (Optional) Cloudflare Access client secret
|
|
26
|
+
*/
|
|
27
|
+
export declare function handleMauticProxy(context: any): Promise<Response>;
|
|
28
|
+
//# sourceMappingURL=mautic-proxy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mautic-proxy.d.ts","sourceRoot":"","sources":["../../src/server/mautic-proxy.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAkFH;;;;;;;;;GASG;AACH,wBAAsB,iBAAiB,CAAC,OAAO,EAAE,GAAG,qBAyMnD"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license GPL-3.0-or-later
|
|
3
|
+
*
|
|
4
|
+
* This file is part of the MarVAlt Open SDK.
|
|
5
|
+
* Copyright (c) 2025 Vibune Pty Ltd.
|
|
6
|
+
*
|
|
7
|
+
* This program is free software: you can redistribute it and/or modify
|
|
8
|
+
* it under the terms of the GNU General Public License as published by
|
|
9
|
+
* the Free Software Foundation, either version 3 of the License, or
|
|
10
|
+
* (at your option) any later version.
|
|
11
|
+
*
|
|
12
|
+
* This program is distributed in the hope that it will be useful,
|
|
13
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
15
|
+
* See the GNU General Public License for more details.
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* Verify Cloudflare Turnstile token server-side
|
|
19
|
+
* @param token - The Turnstile response token from the client
|
|
20
|
+
* @param secretKey - Your Turnstile secret key
|
|
21
|
+
* @returns Promise<boolean> - True if verification succeeds
|
|
22
|
+
*/
|
|
23
|
+
export declare function verifyTurnstile(token: string, secretKey: string): Promise<boolean>;
|
|
24
|
+
//# sourceMappingURL=turnstile.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"turnstile.d.ts","sourceRoot":"","sources":["../../src/server/turnstile.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AASH;;;;;GAKG;AACH,wBAAsB,eAAe,CACnC,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,OAAO,CAAC,CAoClB"}
|
package/dist/server.cjs
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @license GPL-3.0-or-later
|
|
5
|
+
*
|
|
6
|
+
* This file is part of the MarVAlt Open SDK.
|
|
7
|
+
* Copyright (c) 2025 Vibune Pty Ltd.
|
|
8
|
+
*
|
|
9
|
+
* This program is free software: you can redistribute it and/or modify
|
|
10
|
+
* it under the terms of the GNU General Public License as published by
|
|
11
|
+
* the Free Software Foundation, either version 3 of the License, or
|
|
12
|
+
* (at your option) any later version.
|
|
13
|
+
*
|
|
14
|
+
* This program is distributed in the hope that it will be useful,
|
|
15
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
17
|
+
* See the GNU General Public License for more details.
|
|
18
|
+
*/
|
|
19
|
+
/**
|
|
20
|
+
* Verify Cloudflare Turnstile token server-side
|
|
21
|
+
* @param token - The Turnstile response token from the client
|
|
22
|
+
* @param secretKey - Your Turnstile secret key
|
|
23
|
+
* @returns Promise<boolean> - True if verification succeeds
|
|
24
|
+
*/
|
|
25
|
+
async function verifyTurnstile(token, secretKey) {
|
|
26
|
+
if (!token || !secretKey) {
|
|
27
|
+
console.error('Missing Turnstile token or secret key');
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
try {
|
|
31
|
+
const response = await fetch('https://challenges.cloudflare.com/turnstile/v0/siteverify', {
|
|
32
|
+
method: 'POST',
|
|
33
|
+
headers: { 'Content-Type': 'application/json' },
|
|
34
|
+
body: JSON.stringify({
|
|
35
|
+
secret: secretKey,
|
|
36
|
+
response: token,
|
|
37
|
+
}),
|
|
38
|
+
});
|
|
39
|
+
if (!response.ok) {
|
|
40
|
+
console.error('Turnstile verification request failed:', response.status);
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
const data = await response.json();
|
|
44
|
+
if (!data.success) {
|
|
45
|
+
console.warn('Turnstile verification failed:', data['error-codes']);
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
console.error('Turnstile verification error:', error);
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @license GPL-3.0-or-later
|
|
58
|
+
*
|
|
59
|
+
* This file is part of the MarVAlt Open SDK.
|
|
60
|
+
* Copyright (c) 2025 Vibune Pty Ltd.
|
|
61
|
+
*
|
|
62
|
+
* This program is free software: you can redistribute it and/or modify
|
|
63
|
+
* it under the terms of the GNU General Public License as published by
|
|
64
|
+
* the Free Software Foundation, either version 3 of the License, or
|
|
65
|
+
* (at your option) any later version.
|
|
66
|
+
*
|
|
67
|
+
* This program is distributed in the hope that it will be useful,
|
|
68
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
69
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
70
|
+
* See the GNU General Public License for more details.
|
|
71
|
+
*/
|
|
72
|
+
// In-memory token cache (per worker instance)
|
|
73
|
+
let cachedToken = null;
|
|
74
|
+
async function getOAuth2Token(mauticUrl, clientId, clientSecret, cfAccessClientId, cfAccessClientSecret) {
|
|
75
|
+
// Check cache (with 5-minute buffer before expiry)
|
|
76
|
+
if (cachedToken && cachedToken.expires_at > Date.now() + 300000) {
|
|
77
|
+
console.log('đ Using cached OAuth2 token');
|
|
78
|
+
return cachedToken.access_token;
|
|
79
|
+
}
|
|
80
|
+
console.log('đ Fetching new OAuth2 token...');
|
|
81
|
+
const tokenUrl = `${mauticUrl}/oauth/v2/token`;
|
|
82
|
+
const body = new URLSearchParams({
|
|
83
|
+
grant_type: 'client_credentials',
|
|
84
|
+
client_id: clientId,
|
|
85
|
+
client_secret: clientSecret,
|
|
86
|
+
});
|
|
87
|
+
const headers = {
|
|
88
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
89
|
+
};
|
|
90
|
+
// Add CF Access headers if available
|
|
91
|
+
if (cfAccessClientId && cfAccessClientSecret) {
|
|
92
|
+
headers['CF-Access-Client-Id'] = cfAccessClientId;
|
|
93
|
+
headers['CF-Access-Client-Secret'] = cfAccessClientSecret;
|
|
94
|
+
console.log('đ Added CF Access headers to OAuth2 request');
|
|
95
|
+
}
|
|
96
|
+
const response = await fetch(tokenUrl, {
|
|
97
|
+
method: 'POST',
|
|
98
|
+
headers,
|
|
99
|
+
body: body.toString(),
|
|
100
|
+
});
|
|
101
|
+
if (!response.ok) {
|
|
102
|
+
const errorText = await response.text();
|
|
103
|
+
throw new Error(`OAuth2 token request failed: ${response.status} ${errorText}`);
|
|
104
|
+
}
|
|
105
|
+
const data = await response.json();
|
|
106
|
+
// Cache token
|
|
107
|
+
cachedToken = {
|
|
108
|
+
access_token: data.access_token,
|
|
109
|
+
expires_at: Date.now() + (data.expires_in * 1000),
|
|
110
|
+
};
|
|
111
|
+
console.log('â
OAuth2 token cached');
|
|
112
|
+
return cachedToken.access_token;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Cloudflare Pages Function handler for Mautic API proxy
|
|
116
|
+
*
|
|
117
|
+
* Environment variables required:
|
|
118
|
+
* - VITE_MAUTIC_URL or MAUTIC_URL: Mautic instance URL
|
|
119
|
+
* - VITE_MAUTIC_API_PUBLIC_KEY or MAUTIC_API_PUBLIC_KEY: OAuth2 client ID
|
|
120
|
+
* - VITE_MAUTIC_API_SECRET_KEY or MAUTIC_API_SECRET_KEY: OAuth2 client secret
|
|
121
|
+
* - VITE_CF_ACCESS_CLIENT_ID or CF_ACCESS_CLIENT_ID: (Optional) Cloudflare Access client ID
|
|
122
|
+
* - VITE_CF_ACCESS_CLIENT_SECRET or CF_ACCESS_CLIENT_SECRET: (Optional) Cloudflare Access client secret
|
|
123
|
+
*/
|
|
124
|
+
async function handleMauticProxy(context) {
|
|
125
|
+
const { request, env } = context;
|
|
126
|
+
try {
|
|
127
|
+
// Parse the endpoint from query parameter (proxy interface)
|
|
128
|
+
const url = new URL(request.url);
|
|
129
|
+
const endpoint = url.searchParams.get('endpoint');
|
|
130
|
+
if (!endpoint) {
|
|
131
|
+
return new Response('Missing endpoint parameter', { status: 400 });
|
|
132
|
+
}
|
|
133
|
+
// ============================================
|
|
134
|
+
// SECURITY LAYER 1: Origin Validation
|
|
135
|
+
// ============================================
|
|
136
|
+
const origin = request.headers.get('Origin');
|
|
137
|
+
const referer = request.headers.get('Referer');
|
|
138
|
+
// Get allowed origins from environment (comma-separated)
|
|
139
|
+
const allowedOriginsStr = env.ALLOWED_ORIGINS || env.VITE_ALLOWED_ORIGINS || '';
|
|
140
|
+
const allowedOrigins = allowedOriginsStr
|
|
141
|
+
.split(',')
|
|
142
|
+
.map((o) => o.trim())
|
|
143
|
+
.filter(Boolean);
|
|
144
|
+
// Default to localhost if no origins configured (development mode)
|
|
145
|
+
if (allowedOrigins.length === 0) {
|
|
146
|
+
allowedOrigins.push('http://localhost:8080', 'http://localhost:5173');
|
|
147
|
+
console.log('â ī¸ No ALLOWED_ORIGINS configured, defaulting to localhost');
|
|
148
|
+
}
|
|
149
|
+
const isAllowedOrigin = allowedOrigins.some((allowed) => origin?.startsWith(allowed) || referer?.startsWith(allowed));
|
|
150
|
+
// Block if origin/referer present but not allowed
|
|
151
|
+
if ((origin || referer) && !isAllowedOrigin) {
|
|
152
|
+
console.warn('đĢ Blocked request from unauthorized origin:', origin || referer);
|
|
153
|
+
return new Response(JSON.stringify({
|
|
154
|
+
error: 'Forbidden origin',
|
|
155
|
+
message: 'This endpoint can only be accessed from authorized domains'
|
|
156
|
+
}), {
|
|
157
|
+
status: 403,
|
|
158
|
+
headers: { 'Content-Type': 'application/json' }
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
// ============================================
|
|
162
|
+
// SECURITY LAYER 2: Endpoint Whitelisting
|
|
163
|
+
// ============================================
|
|
164
|
+
const allowedPatterns = [
|
|
165
|
+
/^\/form\/submit/, // Form submissions
|
|
166
|
+
/^\/forms\/\d+\/submit/, // Legacy form endpoint
|
|
167
|
+
/^\/mtc\.js$/, // Tracking script (optional)
|
|
168
|
+
/^\/mtc\//, // Tracking API (optional)
|
|
169
|
+
/^\/mtracking\.gif$/, // Tracking pixel (optional)
|
|
170
|
+
];
|
|
171
|
+
const isAllowedEndpoint = allowedPatterns.some(pattern => pattern.test(endpoint));
|
|
172
|
+
if (!isAllowedEndpoint) {
|
|
173
|
+
console.warn('đĢ Blocked unauthorized endpoint:', endpoint);
|
|
174
|
+
return new Response(JSON.stringify({
|
|
175
|
+
error: 'Forbidden endpoint',
|
|
176
|
+
message: 'Only form submission endpoints are allowed'
|
|
177
|
+
}), {
|
|
178
|
+
status: 403,
|
|
179
|
+
headers: { 'Content-Type': 'application/json' }
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
// ============================================
|
|
183
|
+
// SECURITY LAYER 3: Turnstile Verification
|
|
184
|
+
// ============================================
|
|
185
|
+
const turnstileSecretKey = env.TURNSTILE_SECRET_KEY || env.VITE_TURNSTILE_SECRET_KEY;
|
|
186
|
+
const turnstileEnabled = !!turnstileSecretKey;
|
|
187
|
+
// Only verify Turnstile for POST requests (form submissions)
|
|
188
|
+
if (turnstileEnabled && request.method === 'POST') {
|
|
189
|
+
const turnstileToken = request.headers.get('cf-turnstile-response');
|
|
190
|
+
if (!turnstileToken) {
|
|
191
|
+
console.warn('đĢ Missing Turnstile token for POST request');
|
|
192
|
+
return new Response(JSON.stringify({
|
|
193
|
+
error: 'Missing verification',
|
|
194
|
+
message: 'Bot verification required'
|
|
195
|
+
}), {
|
|
196
|
+
status: 403,
|
|
197
|
+
headers: { 'Content-Type': 'application/json' }
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
const isValid = await verifyTurnstile(turnstileToken, turnstileSecretKey);
|
|
201
|
+
if (!isValid) {
|
|
202
|
+
console.warn('đĢ Invalid Turnstile token');
|
|
203
|
+
return new Response(JSON.stringify({
|
|
204
|
+
error: 'Verification failed',
|
|
205
|
+
message: 'Bot verification failed'
|
|
206
|
+
}), {
|
|
207
|
+
status: 403,
|
|
208
|
+
headers: { 'Content-Type': 'application/json' }
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
console.log('â
Turnstile verification passed');
|
|
212
|
+
}
|
|
213
|
+
else if (turnstileEnabled) {
|
|
214
|
+
console.log('âšī¸ Turnstile enabled but skipped for non-POST request');
|
|
215
|
+
}
|
|
216
|
+
// Get server-side credentials (prefer non-VITE_ prefixed env vars)
|
|
217
|
+
const mauticUrl = env.MAUTIC_URL || env.VITE_MAUTIC_URL;
|
|
218
|
+
const clientId = env.MAUTIC_API_PUBLIC_KEY || env.VITE_MAUTIC_API_PUBLIC_KEY;
|
|
219
|
+
const clientSecret = env.MAUTIC_API_SECRET_KEY || env.VITE_MAUTIC_API_SECRET_KEY;
|
|
220
|
+
const cfAccessClientId = env.CF_ACCESS_CLIENT_ID || env.VITE_CF_ACCESS_CLIENT_ID;
|
|
221
|
+
const cfAccessClientSecret = env.CF_ACCESS_CLIENT_SECRET || env.VITE_CF_ACCESS_CLIENT_SECRET;
|
|
222
|
+
if (!mauticUrl || !clientId || !clientSecret) {
|
|
223
|
+
console.error('â Mautic credentials not configured', {
|
|
224
|
+
mauticUrl: !!mauticUrl,
|
|
225
|
+
clientId: !!clientId,
|
|
226
|
+
clientSecret: !!clientSecret
|
|
227
|
+
});
|
|
228
|
+
return new Response('Mautic credentials not configured', { status: 500 });
|
|
229
|
+
}
|
|
230
|
+
// Get OAuth2 token
|
|
231
|
+
const accessToken = await getOAuth2Token(mauticUrl, clientId, clientSecret, cfAccessClientId, cfAccessClientSecret);
|
|
232
|
+
// Determine if this is a form submission (goes to /form/submit)
|
|
233
|
+
// or an API call (goes to /api/endpoint)
|
|
234
|
+
const isFormSubmission = endpoint.startsWith('/form/submit');
|
|
235
|
+
const targetUrl = isFormSubmission
|
|
236
|
+
? `${mauticUrl}${endpoint}` // Form submissions don't use /api prefix
|
|
237
|
+
: `${mauticUrl}/api${endpoint}`; // API calls use /api prefix
|
|
238
|
+
// Prepare headers
|
|
239
|
+
const headers = {
|
|
240
|
+
'Authorization': `Bearer ${accessToken}`,
|
|
241
|
+
};
|
|
242
|
+
// Copy relevant headers from original request
|
|
243
|
+
const contentType = request.headers.get('Content-Type');
|
|
244
|
+
if (contentType) {
|
|
245
|
+
headers['Content-Type'] = contentType;
|
|
246
|
+
}
|
|
247
|
+
// Add CF Access headers if available
|
|
248
|
+
if (cfAccessClientId && cfAccessClientSecret) {
|
|
249
|
+
headers['CF-Access-Client-Id'] = cfAccessClientId;
|
|
250
|
+
headers['CF-Access-Client-Secret'] = cfAccessClientSecret;
|
|
251
|
+
}
|
|
252
|
+
// Prepare request init
|
|
253
|
+
const init = {
|
|
254
|
+
method: request.method,
|
|
255
|
+
headers,
|
|
256
|
+
};
|
|
257
|
+
// Include body for POST/PATCH/PUT requests
|
|
258
|
+
if (request.method !== 'GET' && request.method !== 'HEAD') {
|
|
259
|
+
init.body = await request.text();
|
|
260
|
+
}
|
|
261
|
+
console.log(`đ¤ Proxying ${request.method} request to Mautic:`, {
|
|
262
|
+
endpoint,
|
|
263
|
+
targetUrl,
|
|
264
|
+
isFormSubmission
|
|
265
|
+
});
|
|
266
|
+
// Forward request to Mautic
|
|
267
|
+
const response = await fetch(targetUrl, init);
|
|
268
|
+
console.log(`đĨ Mautic response: ${response.status} ${response.statusText}`);
|
|
269
|
+
// Return response to client
|
|
270
|
+
const responseBody = await response.text();
|
|
271
|
+
return new Response(responseBody, {
|
|
272
|
+
status: response.status,
|
|
273
|
+
statusText: response.statusText,
|
|
274
|
+
headers: {
|
|
275
|
+
'Content-Type': response.headers.get('Content-Type') || 'application/json',
|
|
276
|
+
},
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
catch (error) {
|
|
280
|
+
console.error('â Mautic proxy error:', error);
|
|
281
|
+
return new Response(JSON.stringify({
|
|
282
|
+
success: false,
|
|
283
|
+
error: error?.message || 'Unknown error',
|
|
284
|
+
}), {
|
|
285
|
+
status: 500,
|
|
286
|
+
headers: { 'Content-Type': 'application/json' },
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
exports.handleMauticProxy = handleMauticProxy;
|
|
292
|
+
exports.verifyTurnstile = verifyTurnstile;
|
|
293
|
+
//# sourceMappingURL=server.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.cjs","sources":["../src/server/turnstile.ts","../src/server/mautic-proxy.ts"],"sourcesContent":[null,null],"names":[],"mappings":";;AAAA;;;;;;;;;;;;;;;AAeG;AASH;;;;;AAKG;AACI,eAAe,eAAe,CACnC,KAAa,EACb,SAAiB,EAAA;AAEjB,IAAA,IAAI,CAAC,KAAK,IAAI,CAAC,SAAS,EAAE;AACxB,QAAA,OAAO,CAAC,KAAK,CAAC,uCAAuC,CAAC;AACtD,QAAA,OAAO,KAAK;IACd;AAEA,IAAA,IAAI;AACF,QAAA,MAAM,QAAQ,GAAG,MAAM,KAAK,CAC1B,2DAA2D,EAC3D;AACE,YAAA,MAAM,EAAE,MAAM;AACd,YAAA,OAAO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