-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathvalidateFaustEnvVars.ts
More file actions
81 lines (72 loc) · 2.46 KB
/
validateFaustEnvVars.ts
File metadata and controls
81 lines (72 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { getWpSecret, getWpUrl } from '../utils/index.js';
import { errorLog, infoLog, warnLog } from '../stdout/index.js';
import { validateNextWordPressUrl } from './validateNextWordPressUrl.js';
export function isWPEngineComSubdomain(url: string) {
const regex = /\b\w+\.wpengine\.com\b/;
return regex.test(url);
}
/**
* Validates that the appropriate Faust related environment variables are set.
*/
export const validateFaustEnvVars = async () => {
const secretWp = getWpSecret();
if (!process.env.NEXT_PUBLIC_WORDPRESS_URL) {
errorLog('Could not find NEXT_PUBLIC_WORDPRESS_URL environment variable.');
process.exit(1);
}
if (isWPEngineComSubdomain(process.env.NEXT_PUBLIC_WORDPRESS_URL)) {
infoLog(`Found NEXT_PUBLIC_WORDPRESS_URL using wpengine.com TLD.`);
infoLog(`It is recommended to use the wpenginepowered.com TLD instead.`);
infoLog(
`Ex: https://example.wpengine.com -> https://example.wpenginepowered.com`,
);
infoLog(
`This will leverage WP Engine's Advanced Network CDN. See: https://wpengine.com/support/network/`,
);
}
if (!secretWp) {
warnLog('Could not find FAUST_SECRET_KEY environment variable.');
warnLog('Some functionality may be limited.');
}
if (process.env.NEXT_PUBLIC_WORDPRESS_URL.startsWith('http://') && secretWp) {
warnLog('Your WordPress site is not running on https!');
warnLog(
'This is a security concern as all traffic with your secret key is in plain text.',
);
warnLog(
'Please make sure your production Faust app runs with a WordPress instance on https!',
);
}
if (secretWp) {
// send secret key
const apiUrl = `${getWpUrl()}/?rest_route=/faustwp/v1/validate_secret_key`;
const headers = {
'x-faustwp-secret': secretWp,
};
try {
const response = await fetch(apiUrl, {
headers,
method: 'POST',
});
if (response.status === 401) {
const wwwAuth = response.headers.get('www-authenticate') || '';
if (wwwAuth.toLowerCase().includes('basic')) {
errorLog(
'Your WordPress site appears to be protected with HTTP Basic Authentication.',
);
errorLog(
'Faust cannot validate the secret key until Basic Auth credentials are provided or the protection is removed.',
);
} else {
errorLog(
'Ensure your FAUST_SECRET_KEY environment variable matches your Secret Key in the Faust WordPress plugin settings',
);
}
process.exit(1);
}
await validateNextWordPressUrl();
} catch (error) {
console.log('error', error);
}
}
};