Skip to content

Commit

Permalink
Merge pull request #40 from StagBIN/dev
Browse files Browse the repository at this point in the history
[WIP] Adding support for encrypted pastes and raw data viewing
  • Loading branch information
vjspranav committed Jun 21, 2023
2 parents e674ea9 + f630f15 commit acf0ff2
Show file tree
Hide file tree
Showing 14 changed files with 27,477 additions and 353 deletions.
27,102 changes: 26,934 additions & 168 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@
"@testing-library/react": "^11.2.7",
"@testing-library/user-event": "^12.8.3",
"@uiw/react-md-editor": "^3.6.0",
"ace-builds": "^1.14.0",
"axios": "^0.21.1",
"bootstrap": "^5.0.2",
"draft-js": "^0.11.7",
"node-sass": "^6.0.1",
"react": "^17.0.2",
"react-ace": "^9.4.3",
"react-ace": "^10.1.0",
"react-bootstrap": "^1.6.1",
"react-dom": "^17.0.2",
"react-ga": "^3.3.0",
"react-quill": "^1.3.5",
"react-responsive": "^9.0.0-beta.2",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.3",
"string-crypt": "^0.0.1",
"string-crypto": "^2.0.2",
"styled-components": "^5.3.0",
"sweetalert2": "^11.4.37",
"uuid": "^8.3.2",
Expand Down
88 changes: 83 additions & 5 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ import MEditor from "./components/MonacoEditor";
import PEditor from "./components/AceEditor";
import MobileTopAppBar from "./components/MobileTopAppBar";
import TopAppBar from "./components/TopAppBar";
import TopAppBarCompiler from "./components/TopAppBarCompiler";
import BottomAppBar from "./components/BottomAppBar";
import { API_URL } from "./Constants";
import MCompiler from "./components/MonacoCompiler";

// For Encryption
import StringCrypto from "string-crypto";
import PasswordDialog from "./components/PasswordDialog";

export const StagBinContext = createContext();

function CustomAlert(props) {
Expand Down Expand Up @@ -86,9 +91,12 @@ function App() {
id,
buid,
base_url,
encrypted,
oldEncrypted,
setSuccess,
setSizeWarning,
setDataEmptyError
setDataEmptyError,
setEncryptError
) => {
const headers = { buid };

Expand All @@ -105,6 +113,11 @@ function App() {
return;
}

if (!encrypted && oldEncrypted) {
setEncryptError(true);
return;
}

const res = await axios.patch(
API_URL + id,
{
Expand Down Expand Up @@ -135,6 +148,7 @@ function App() {
id,
buid,
base_url,
encyrpted,
setSuccess,
setSizeWarning,
setDataEmptyError
Expand All @@ -156,6 +170,7 @@ function App() {
data,
buid,
id,
isEncrypted: encyrpted,
});

if (res.status === 200) {
Expand Down Expand Up @@ -185,13 +200,44 @@ function App() {
const [success, setSuccess] = useState(false);
const [size_warning, setSizeWarning] = useState(false);
const [data_empty_error, setDataEmptyError] = useState(false);
const [encrypt_error, setEncryptError] = useState(false);
const [isMarkdownView, updateIsMarkdownView] = useState(false);
const [isSameContentbuid, setIsSameContentbuid] = useState("");
const [edited, setEdited] = useState(false);
const [isDiff, setIsDiff] = useState(false);
const [compileMode, setCompileMode] = useState(code ? true : false);
const [output, setOutput] = useState("Your output here!");

// Encryption
const [encrypted, setEncrypted] = useState(false);
const [oldEncrypted, setOldEncrypted] = useState(false);
const [encryptedReadOnly, setEncryptedReadOnly] = useState(false);
const [openPasswordDialog, setOpenPasswordDialog] = useState(false);

// For Encryption
const [password, setPassword] = useState("");
const { encryptString, decryptString } = new StringCrypto();

const handlePassWordClose = () => {
setOpenPasswordDialog(false);
if (password.length > 0) {
if (data.length <= 0) {
setDataEmptyError(true);
} else {
if (encrypted) {
console.log(decryptString(data, password));
setData(decryptString(data, password));
setEncrypted(false);
setEncryptedReadOnly(true);
} else {
setEncrypted(true);
setData(encryptString(data, password));
setEncryptedReadOnly(true);
}
}
}
};

// const themeToggler = () => {
// theme === "light" ? setTheme("dark") : setTheme("light");
// localStorage.setItem("stagbin_theme", theme === "light" ? "dark" : "light");
Expand Down Expand Up @@ -220,16 +266,20 @@ function App() {
url,
system_id,
base_url,
encrypted,
oldEncrypted,
setSuccess,
setSizeWarning,
setDataEmptyError
setDataEmptyError,
setEncryptError
);
} else {
post_save(
data,
url,
system_id,
base_url,
encrypted,
setSuccess,
setSizeWarning,
setDataEmptyError
Expand All @@ -245,6 +295,7 @@ function App() {
setSuccess(false);
setSizeWarning(false);
setDataEmptyError(false);
setEncryptError(false);
};

return (
Expand All @@ -259,20 +310,29 @@ function App() {
compileMode,
data,
edited,
encrypted,
encryptedReadOnly,
oldEncrypted,
setOldEncrypted,
language,
oldData,
openPasswordDialog,
output,
readOnly,
isDiff,
isMarkdownView,
isSameContentbuid,
setCompileMode,
setData,
setDataEmptyError,
setEdited,
setEncrypted,
setEncryptedReadOnly,
setIsDiff,
setIsSameContentbuid,
setLanguage,
setOldData,
setOpenPasswordDialog,
setOutput,
setReadOnly,
setUrl,
Expand All @@ -287,7 +347,7 @@ function App() {
<MobileTopAppBar />
</MediaQuery>
<MediaQuery minWidth={480}>
<TopAppBar />
{compileMode ? <TopAppBarCompiler /> : <TopAppBar />}
</MediaQuery>
</div>
<Switch>
Expand Down Expand Up @@ -329,19 +389,37 @@ function App() {
onClose={handleCloseSnackBar}
autoHideDuration={6000}
>
<CustomAlert onClose={handleCloseSnackBar} severity="Error">
<CustomAlert onClose={handleCloseSnackBar} severity="error">
Content cannot be empty
</CustomAlert>
</Snackbar>
<Snackbar
open={encrypt_error}
onClose={handleCloseSnackBar}
autoHideDuration={6000}
>
<CustomAlert onClose={handleCloseSnackBar} severity="error">
Content needs to be reencrypted
</CustomAlert>
</Snackbar>
<Snackbar
open={pageDown}
onClose={handleCloseSnackBar}
autoHideDuration={1000000}
>
<CustomAlert onClose={handleCloseSnackBar} severity="Error">
<CustomAlert onClose={handleCloseSnackBar} severity="error">
Internal Server Error, We are working on it
</CustomAlert>
</Snackbar>
<PasswordDialog
open={openPasswordDialog}
setOpen={setOpenPasswordDialog}
password={password}
setPassword={setPassword}
encrypted={encrypted}
handleClose={handlePassWordClose}
edited={edited}
/>
</div>
</StagBinContext.Provider>
</>
Expand Down
3 changes: 2 additions & 1 deletion src/Constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const API_URL = "https://api.stagbin.tk/dev/content/";
const RAW_URL = "https://raw.stagbin.tk/StagBIN-Raw/";

export { API_URL };
export { API_URL, RAW_URL };
23 changes: 21 additions & 2 deletions src/components/AceEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ const getData = async (
id,
base_url,
setIsSameContentbuid,
setEncrypted,
setEncryptedReadOnly,
setOpenPasswordDialog,
setLoading
) => {
// setLoading(true);
Expand All @@ -41,6 +44,9 @@ const getData = async (
reqData = res.data[0];
// console.log(reqData);
setIsSameContentbuid(reqData.edit);
setEncrypted(reqData.isEncrypted || false);
setEncryptedReadOnly(reqData.isEncrypted || false);
setOpenPasswordDialog(reqData.isEncrypted || false);
setData(reqData.data);
setLoading(false);
}
Expand All @@ -66,6 +72,10 @@ export default function PEditor() {
setUrl,
data,
setData,
encryptedReadOnly,
setEncrypted,
setEncryptedReadOnly,
setOpenPasswordDialog,
base_url,
setIsSameContentbuid,
edited,
Expand All @@ -86,8 +96,17 @@ export default function PEditor() {
}
if (!(!readOnly && edited)) setReadOnly(true);
setUrl(id);
if (!edited)
getData(setData, id, base_url, setIsSameContentbuid, setLoading);
if (!edited && !encryptedReadOnly)
getData(
setData,
id,
base_url,
setIsSameContentbuid,
setEncrypted,
setEncryptedReadOnly,
setOpenPasswordDialog,
setLoading
);
}
}
set_data_if_exists();
Expand Down
2 changes: 1 addition & 1 deletion src/components/BottomAppBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default function BottomAppBar(props) {
>
<Toolbar className={classes.toolbar}>
<a href="https://stagbin.tk" style={{ color: "inherit" }}>
<small>&copy; Copyright 2021, StagBIN</small>
<small>&copy; Copyright 2023, StagBIN</small>
</a>
<div style={{ margin: "8px" }}>
<a href="https://github.com/StagBin" style={{ color: "inherit" }}>
Expand Down
2 changes: 1 addition & 1 deletion src/components/BottomBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function BottomBar(props) {
>
<Link to="/" className="navbar-brand">
{" "}
<small>&copy; Copyright 2021, StagBIN</small>{" "}
<small>&copy; Copyright 2023, StagBIN</small>{" "}
</Link>
</nav>
</div>
Expand Down
35 changes: 35 additions & 0 deletions src/components/MobileTopAppBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import Zoom from "@material-ui/core/Zoom";
import AddIcon from "@material-ui/icons/Add";
import SaveIcon from "@material-ui/icons/Save";
import EditIcon from "@material-ui/icons/Edit";
import LockIcon from "@material-ui/icons/Lock";
import LockOpenIcon from "@material-ui/icons/LockOpen";
import FormControl from "@material-ui/core/FormControl";
import { Tooltip } from "@material-ui/core";
import IconButton from "@material-ui/core/IconButton";
Expand Down Expand Up @@ -80,8 +82,13 @@ export default function BackToTop(props) {
setReadOnly,
setEdited,
data,
encrypted,
setOpenPasswordDialog,
} = useContext(StagBinContext);

const showLockIcon = !readOnly && !encrypted;
const showUnlockIcon = readOnly && encrypted;

const classes = useStyles();
// console.log(readOnly);
return (
Expand Down Expand Up @@ -109,6 +116,34 @@ export default function BackToTop(props) {
}}
></FormControl>
<div style={{ display: "inline-flex" }}>
{showLockIcon && (
<Tooltip title="Encrypt">
<IconButton
edge="end"
color="inherit"
aria-label="Save"
onClick={() => {
setOpenPasswordDialog(true);
}}
>
<LockIcon />
</IconButton>
</Tooltip>
)}
{showUnlockIcon && (
<Tooltip title="Decrypt">
<IconButton
edge="end"
color="inherit"
aria-label="Save"
onClick={() => {
setOpenPasswordDialog(true);
}}
>
<LockOpenIcon />
</IconButton>
</Tooltip>
)}
{readOnly ? (
isSameContentbuid ? (
<Tooltip title="Edit">
Expand Down
Loading

0 comments on commit acf0ff2

Please sign in to comment.