mirror of
https://github.com/Qortal/Qortal-Hub.git
synced 2025-07-23 04:36:52 +00:00
tighten up csp
This commit is contained in:
@@ -23,15 +23,14 @@ import { set } from "lodash";
|
||||
import { cleanUrl, isUsingLocal } from "../background";
|
||||
|
||||
const manifestData = {
|
||||
version: '0.2.0'
|
||||
}
|
||||
version: "0.2.0",
|
||||
};
|
||||
|
||||
export const NotAuthenticated = ({
|
||||
getRootProps,
|
||||
getInputProps,
|
||||
setExtstate,
|
||||
|
||||
|
||||
apiKey,
|
||||
setApiKey,
|
||||
globalApiKey,
|
||||
@@ -54,9 +53,9 @@ export const NotAuthenticated = ({
|
||||
const [customApikey, setCustomApiKey] = React.useState("");
|
||||
const [customNodeToSaveIndex, setCustomNodeToSaveIndex] =
|
||||
React.useState(null);
|
||||
const importedApiKeyRef = useRef(null)
|
||||
const currentNodeRef = useRef(null)
|
||||
const hasLocalNodeRef = useRef(null)
|
||||
const importedApiKeyRef = useRef(null);
|
||||
const currentNodeRef = useRef(null);
|
||||
const hasLocalNodeRef = useRef(null);
|
||||
const isLocal = cleanUrl(currentNode?.url) === "127.0.0.1:12391";
|
||||
const handleFileChangeApiKey = (event) => {
|
||||
const file = event.target.files[0]; // Get the selected file
|
||||
@@ -71,7 +70,6 @@ export const NotAuthenticated = ({
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const checkIfUserHasLocalNode = useCallback(async () => {
|
||||
try {
|
||||
const url = `http://127.0.0.1:12391/admin/status`;
|
||||
@@ -93,43 +91,48 @@ export const NotAuthenticated = ({
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
window.sendMessage("getCustomNodesFromStorage")
|
||||
.then((response) => {
|
||||
if (response) {
|
||||
setCustomNodes(response || []);
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Failed to get custom nodes from storage:", error.message || "An error occurred");
|
||||
});
|
||||
|
||||
window
|
||||
.sendMessage("getCustomNodesFromStorage")
|
||||
.then((response) => {
|
||||
if (response) {
|
||||
setCustomNodes(response || []);
|
||||
window.electronAPI.setAllowedDomains(response?.map((node)=> node.url))
|
||||
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(
|
||||
"Failed to get custom nodes from storage:",
|
||||
error.message || "An error occurred"
|
||||
);
|
||||
});
|
||||
}, []);
|
||||
|
||||
useEffect(()=> {
|
||||
importedApiKeyRef.current = importedApiKey
|
||||
}, [importedApiKey])
|
||||
useEffect(()=> {
|
||||
currentNodeRef.current = currentNode
|
||||
}, [currentNode])
|
||||
useEffect(() => {
|
||||
importedApiKeyRef.current = importedApiKey;
|
||||
}, [importedApiKey]);
|
||||
useEffect(() => {
|
||||
currentNodeRef.current = currentNode;
|
||||
}, [currentNode]);
|
||||
|
||||
useEffect(()=> {
|
||||
hasLocalNodeRef.current = hasLocalNode
|
||||
}, [hasLocalNode])
|
||||
useEffect(() => {
|
||||
hasLocalNodeRef.current = hasLocalNode;
|
||||
}, [hasLocalNode]);
|
||||
|
||||
const validateApiKey = useCallback(async (key, fromStartUp) => {
|
||||
try {
|
||||
if(!currentNodeRef.current) return
|
||||
const isLocalKey = cleanUrl(key?.url) === "127.0.0.1:12391";
|
||||
if(isLocalKey && !hasLocalNodeRef.current && !fromStartUp){
|
||||
throw new Error('Please turn on your local node')
|
||||
|
||||
}
|
||||
const isCurrentNodeLocal = cleanUrl(currentNodeRef.current?.url) === "127.0.0.1:12391";
|
||||
if(isLocalKey && !isCurrentNodeLocal) {
|
||||
setIsValidApiKey(false);
|
||||
setUseLocalNode(false);
|
||||
return
|
||||
}
|
||||
if (!currentNodeRef.current) return;
|
||||
const isLocalKey = cleanUrl(key?.url) === "127.0.0.1:12391";
|
||||
if (isLocalKey && !hasLocalNodeRef.current && !fromStartUp) {
|
||||
throw new Error("Please turn on your local node");
|
||||
}
|
||||
const isCurrentNodeLocal =
|
||||
cleanUrl(currentNodeRef.current?.url) === "127.0.0.1:12391";
|
||||
if (isLocalKey && !isCurrentNodeLocal) {
|
||||
setIsValidApiKey(false);
|
||||
setUseLocalNode(false);
|
||||
return;
|
||||
}
|
||||
let payload = {};
|
||||
|
||||
if (currentNodeRef.current?.url === "http://127.0.0.1:12391") {
|
||||
@@ -137,7 +140,7 @@ export const NotAuthenticated = ({
|
||||
apikey: importedApiKeyRef.current || key?.apikey,
|
||||
url: currentNodeRef.current?.url,
|
||||
};
|
||||
} else if(currentNodeRef.current) {
|
||||
} else if (currentNodeRef.current) {
|
||||
payload = currentNodeRef.current;
|
||||
}
|
||||
const url = `${payload?.url}/admin/apikey/test`;
|
||||
@@ -152,21 +155,24 @@ export const NotAuthenticated = ({
|
||||
// Assuming the response is in plain text and will be 'true' or 'false'
|
||||
const data = await response.text();
|
||||
if (data === "true") {
|
||||
window.sendMessage("setApiKey", payload)
|
||||
.then((response) => {
|
||||
if (response) {
|
||||
handleSetGlobalApikey(payload);
|
||||
setIsValidApiKey(true);
|
||||
setUseLocalNode(true);
|
||||
if (!fromStartUp) {
|
||||
setApiKey(payload);
|
||||
window
|
||||
.sendMessage("setApiKey", payload)
|
||||
.then((response) => {
|
||||
if (response) {
|
||||
handleSetGlobalApikey(payload);
|
||||
setIsValidApiKey(true);
|
||||
setUseLocalNode(true);
|
||||
if (!fromStartUp) {
|
||||
setApiKey(payload);
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Failed to set API key:", error.message || "An error occurred");
|
||||
});
|
||||
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(
|
||||
"Failed to set API key:",
|
||||
error.message || "An error occurred"
|
||||
);
|
||||
});
|
||||
} else {
|
||||
setIsValidApiKey(false);
|
||||
setUseLocalNode(false);
|
||||
@@ -213,24 +219,28 @@ export const NotAuthenticated = ({
|
||||
}
|
||||
|
||||
setCustomNodes(nodes);
|
||||
window.electronAPI.setAllowedDomains(nodes?.map((node)=> node.url))
|
||||
|
||||
setCustomNodeToSaveIndex(null);
|
||||
if (!nodes) return;
|
||||
window.sendMessage("setCustomNodes", nodes)
|
||||
.then((response) => {
|
||||
if (response) {
|
||||
setMode("list");
|
||||
setUrl("http://");
|
||||
setCustomApiKey("");
|
||||
// add alert if needed
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Failed to set custom nodes:", error.message || "An error occurred");
|
||||
});
|
||||
|
||||
window
|
||||
.sendMessage("setCustomNodes", nodes)
|
||||
.then((response) => {
|
||||
if (response) {
|
||||
setMode("list");
|
||||
setUrl("http://");
|
||||
setCustomApiKey("");
|
||||
// add alert if needed
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(
|
||||
"Failed to set custom nodes:",
|
||||
error.message || "An error occurred"
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
return (
|
||||
<>
|
||||
<Spacer height="35px" />
|
||||
@@ -296,16 +306,16 @@ export const NotAuthenticated = ({
|
||||
}}
|
||||
/>
|
||||
</Box>
|
||||
<Spacer height="15px" />
|
||||
|
||||
<Typography
|
||||
sx={{
|
||||
fontSize: "12px",
|
||||
visibility: !useLocalNode && 'hidden'
|
||||
}}
|
||||
>
|
||||
{"Using node: "} {currentNode?.url}
|
||||
</Typography>
|
||||
<Spacer height="15px" />
|
||||
|
||||
<Typography
|
||||
sx={{
|
||||
fontSize: "12px",
|
||||
visibility: !useLocalNode && "hidden",
|
||||
}}
|
||||
>
|
||||
{"Using node: "} {currentNode?.url}
|
||||
</Typography>
|
||||
<>
|
||||
<Spacer height="15px" />
|
||||
<Box
|
||||
@@ -344,28 +354,30 @@ export const NotAuthenticated = ({
|
||||
validateApiKey(currentNode);
|
||||
} else {
|
||||
setCurrentNode({
|
||||
url: "http://127.0.0.1:12391",
|
||||
url: "http://127.0.0.1:12391",
|
||||
});
|
||||
setUseLocalNode(false);
|
||||
window
|
||||
.sendMessage("setApiKey", null)
|
||||
.then((response) => {
|
||||
if (response) {
|
||||
setApiKey(null);
|
||||
handleSetGlobalApikey(null);
|
||||
}
|
||||
})
|
||||
setUseLocalNode(false)
|
||||
window.sendMessage("setApiKey", null)
|
||||
.then((response) => {
|
||||
if (response) {
|
||||
setApiKey(null);
|
||||
handleSetGlobalApikey(null);
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Failed to set API key:", error.message || "An error occurred");
|
||||
});
|
||||
|
||||
.catch((error) => {
|
||||
console.error(
|
||||
"Failed to set API key:",
|
||||
error.message || "An error occurred"
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
}}
|
||||
disabled={false}
|
||||
defaultChecked
|
||||
/>
|
||||
}
|
||||
label={`Use ${isLocal ? 'Local' : 'Custom'} Node`}
|
||||
label={`Use ${isLocal ? "Local" : "Custom"} Node`}
|
||||
/>
|
||||
</Box>
|
||||
{currentNode?.url === "http://127.0.0.1:12391" && (
|
||||
@@ -379,31 +391,33 @@ export const NotAuthenticated = ({
|
||||
onChange={handleFileChangeApiKey} // File input handler
|
||||
/>
|
||||
</Button>
|
||||
<Typography sx={{
|
||||
fontSize: '12px',
|
||||
visibility: importedApiKey ? 'visible' : 'hidden'
|
||||
}}>{`api key : ${importedApiKey}`}</Typography>
|
||||
|
||||
|
||||
|
||||
|
||||
<Typography
|
||||
sx={{
|
||||
fontSize: "12px",
|
||||
visibility: importedApiKey ? "visible" : "hidden",
|
||||
}}
|
||||
>{`api key : ${importedApiKey}`}</Typography>
|
||||
</>
|
||||
)}
|
||||
<Button
|
||||
size="small"
|
||||
onClick={() => {
|
||||
setShow(true);
|
||||
}}
|
||||
variant="contained"
|
||||
component="label"
|
||||
>
|
||||
Choose custom node
|
||||
</Button>
|
||||
<Button
|
||||
size="small"
|
||||
onClick={() => {
|
||||
setShow(true);
|
||||
}}
|
||||
variant="contained"
|
||||
component="label"
|
||||
>
|
||||
Choose custom node
|
||||
</Button>
|
||||
</>
|
||||
<Typography sx={{
|
||||
color: "white",
|
||||
fontSize: '12px'
|
||||
}}>Build version: {manifestData?.version}</Typography>
|
||||
<Typography
|
||||
sx={{
|
||||
color: "white",
|
||||
fontSize: "12px",
|
||||
}}
|
||||
>
|
||||
Build version: {manifestData?.version}
|
||||
</Typography>
|
||||
</Box>
|
||||
</>
|
||||
<CustomizedSnackbars
|
||||
@@ -430,7 +444,6 @@ export const NotAuthenticated = ({
|
||||
flexDirection: "column",
|
||||
}}
|
||||
>
|
||||
|
||||
{mode === "list" && (
|
||||
<Box
|
||||
sx={{
|
||||
@@ -472,17 +485,20 @@ export const NotAuthenticated = ({
|
||||
setMode("list");
|
||||
setShow(false);
|
||||
setUseLocalNode(false);
|
||||
window.sendMessage("setApiKey", null)
|
||||
.then((response) => {
|
||||
if (response) {
|
||||
setApiKey(null);
|
||||
handleSetGlobalApikey(null);
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Failed to set API key:", error.message || "An error occurred");
|
||||
});
|
||||
|
||||
window
|
||||
.sendMessage("setApiKey", null)
|
||||
.then((response) => {
|
||||
if (response) {
|
||||
setApiKey(null);
|
||||
handleSetGlobalApikey(null);
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(
|
||||
"Failed to set API key:",
|
||||
error.message || "An error occurred"
|
||||
);
|
||||
});
|
||||
}}
|
||||
variant="contained"
|
||||
>
|
||||
@@ -527,18 +543,21 @@ export const NotAuthenticated = ({
|
||||
setMode("list");
|
||||
setShow(false);
|
||||
setIsValidApiKey(false);
|
||||
setUseLocalNode(false);
|
||||
window.sendMessage("setApiKey", null)
|
||||
.then((response) => {
|
||||
if (response) {
|
||||
setApiKey(null);
|
||||
handleSetGlobalApikey(null);
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Failed to set API key:", error.message || "An error occurred");
|
||||
});
|
||||
|
||||
setUseLocalNode(false);
|
||||
window
|
||||
.sendMessage("setApiKey", null)
|
||||
.then((response) => {
|
||||
if (response) {
|
||||
setApiKey(null);
|
||||
handleSetGlobalApikey(null);
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(
|
||||
"Failed to set API key:",
|
||||
error.message || "An error occurred"
|
||||
);
|
||||
});
|
||||
}}
|
||||
variant="contained"
|
||||
>
|
||||
@@ -562,7 +581,6 @@ export const NotAuthenticated = ({
|
||||
const nodesToSave = [
|
||||
...(customNodes || []),
|
||||
].filter((item) => item?.url !== node?.url);
|
||||
|
||||
|
||||
saveCustomNodes(nodesToSave);
|
||||
}}
|
||||
@@ -601,9 +619,7 @@ export const NotAuthenticated = ({
|
||||
/>
|
||||
</Box>
|
||||
)}
|
||||
|
||||
</Box>
|
||||
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
{mode === "list" && (
|
||||
|
Reference in New Issue
Block a user