mirror of
https://github.com/Nezumi-2711/cloudflare-gateway-pihole-scripts.git
synced 2026-09-22 13:38:37 +00:00
use API Token in favour of API Key for better security
This commit is contained in:
+25
-11
@@ -1,4 +1,10 @@
|
||||
import { ACCOUNT_EMAIL, ACCOUNT_ID, API_HOST, API_TOKEN } from "./constants.js";
|
||||
import {
|
||||
ACCOUNT_EMAIL,
|
||||
ACCOUNT_ID,
|
||||
API_HOST,
|
||||
API_KEY,
|
||||
API_TOKEN,
|
||||
} from "./constants.js";
|
||||
|
||||
if (!globalThis.fetch) {
|
||||
console.warn(
|
||||
@@ -20,20 +26,30 @@ if (!globalThis.fetch) {
|
||||
* @returns {Promise}
|
||||
*/
|
||||
const request = async (url, options) => {
|
||||
if (!API_TOKEN || !ACCOUNT_ID || !ACCOUNT_EMAIL) {
|
||||
if (!(API_TOKEN || API_KEY) || !ACCOUNT_ID) {
|
||||
throw new Error(
|
||||
"One or more required secrets have not been added: CLOUDFLARE_API_KEY, CLOUDFLARE_ACCOUNT_ID, and CLOUDFLARE_ACCOUNT_EMAIL"
|
||||
"The following secrets are required: CLOUDFLARE_API_TOKEN, CLOUDFLARE_ACCOUNT_ID"
|
||||
);
|
||||
}
|
||||
|
||||
const headers = API_TOKEN
|
||||
? {
|
||||
Authorization: `Bearer ${API_TOKEN}`,
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
: {
|
||||
Authorization: `Bearer ${API_KEY}`,
|
||||
"Content-Type": "application/json",
|
||||
"X-Auth-Email": ACCOUNT_EMAIL,
|
||||
"X-Auth-Key": API_KEY,
|
||||
};
|
||||
|
||||
const response = await fetch(url, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${API_TOKEN}`,
|
||||
"Content-Type": "application/json",
|
||||
"X-Auth-Email": ACCOUNT_EMAIL,
|
||||
"X-Auth-Key": API_TOKEN,
|
||||
},
|
||||
...options,
|
||||
headers: {
|
||||
...options.headers,
|
||||
...headers,
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
@@ -42,8 +58,6 @@ const request = async (url, options) => {
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
console.log(`HTTP request succeeded: ${data.success}`);
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user