Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[staff] Allow adding custom ott

Neeraj Gupta422a480975d5
diff --git a/infra/staff/src/components/AddOtt.tsx b/infra/staff/src/components/AddOtt.tsx
new file mode 100644
index 0000000000..b59867e028
--- /dev/null
+++ b/infra/staff/src/components/AddOtt.tsx
@@ -0,0 +1,214 @@
+import {
+ Alert,
+ Box,
+ Button,
+ Dialog,
+ DialogActions,
+ DialogContent,
+ DialogTitle,
+ FormControl,
+ InputLabel,
+ MenuItem,
+ Paper,
+ Select,
+ Typography,
+} from "@mui/material";
+import type { SelectChangeEvent } from "@mui/material/Select";
+import { useEffect, useState } from "react";
+import { getToken } from "../App";
+import { apiOrigin } from "../services/support";
+
+const APP_OPTIONS = [
+ { label: "Photos", value: "photos" },
+ { label: "Locker", value: "locker" },
+ { label: "Auth", value: "auth" },
+] as const;
+
+type AppOption = (typeof APP_OPTIONS)[number]["value"];
+
+const computeExpiryTimeMicros = () =>
+ Date.now() * 1000 + 7 * 24 * 60 * 60 * 1_000_000;
+
+const generateOtp = () =>
+ Math.floor(Math.random() * 1_000_000)
+ .toString()
+ .padStart(6, "0");
+
+interface AddOttProps {
+ open: boolean;
+ onClose: () => void;
+ userEmail: string;
+}
+
+const AddOtt = ({ open, onClose, userEmail }: AddOttProps) => {
+ const [selectedApp, setSelectedApp] = useState<AppOption>("photos");
+ const [submitting, setSubmitting] = useState(false);
+ const [error, setError] = useState<string | null>(null);
+ const [createdOtp, setCreatedOtp] = useState<string | null>(null);
+ const [expiryPreview, setExpiryPreview] = useState<number>(() =>
+ computeExpiryTimeMicros(),
+ );
+
+ useEffect(() => {
+ if (open) {
+ setSelectedApp("photos");
+ setError(null);
+ setCreatedOtp(null);
+ setExpiryPreview(computeExpiryTimeMicros());
+ }
+ }, [open]);
+
+ const handleClose = () => {
+ if (submitting) {
+ return;
+ }
+ onClose();
+ };
+
+ const handleSubmit = async () => {
+ try {
+ setSubmitting(true);
+ setError(null);
+
+ if (!userEmail || userEmail === "None") {
+ throw new Error("User email is unavailable");
+ }
+
+ const token = getToken();
+ if (!token) {
+ throw new Error("Auth token is unavailable");
+ }
+
+ const otp = generateOtp();
+ const expiryTime = computeExpiryTimeMicros();
+ setExpiryPreview(expiryTime);
+
+ const response = await fetch(`${apiOrigin}/admin/user/add-ott`, {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ "X-Auth-Token": token,
+ },
+ body: JSON.stringify({
+ email: userEmail,
+ code: otp,
+ app: selectedApp,
+ expiryTime: expiryTime,
+ }),
+ });
+
+ if (!response.ok) {
+ const message = await response.text();
+ throw new Error(message || "Failed to create OTT");
+ }
+
+ setCreatedOtp(otp);
+ } catch (err) {
+ if (err instanceof Error) {
+ setError(err.message);
+ } else {
+ setError("Failed to create OTT");
+ }
+ } finally {
+ setSubmitting(false);
+ }
+ };
+
+ return (
+ <Dialog
+ open={open}
+ onClose={handleClose}
+ PaperComponent={Paper}
+ maxWidth="xs"
+ fullWidth
+ >
+ {createdOtp ? (
+ <>
+ <DialogTitle>OTT created</DialogTitle>
+ <DialogContent>
+ <Typography gutterBottom>
+ Share this OTP with the user. It expires in 7 days.
+ </Typography>
+ <Box
+ sx={{
+ bgcolor: "#F5F5F5",
+ borderRadius: 1,
+ p: 2,
+ textAlign: "center",
+ fontSize: "1.5rem",
+ fontWeight: "bold",
+ letterSpacing: "0.2rem",
+ }}
+ >
+ {createdOtp}
+ </Box>
+ </DialogContent>
+ <DialogActions>
+ <Button onClick={handleClose} variant="contained">
+ Done
+ </Button>
+ </DialogActions>
+ </>
+ ) : (
+ <>
+ <DialogTitle>Add OTT</DialogTitle>
+ <DialogContent>
+ <Typography variant="body2" gutterBottom>
+ Generate a one-time token for{" "}
+ <strong>{userEmail || "this user"}</strong>. The OTP
+ expires 7 days from now.
+ </Typography>
+ <FormControl fullWidth margin="normal">
+ <InputLabel id="app-type-label">
+ App type
+ </InputLabel>
+ <Select
+ labelId="app-type-label"
+ value={selectedApp}
+ label="App type"
+ onChange={(
+ event: SelectChangeEvent<AppOption>,
+ ) =>
+ setSelectedApp(
+ event.target.value as AppOption,
+ )
+ }
+ >
+ {APP_OPTIONS.map((option) => (
+ <MenuItem
+ key={option.value}
+ value={option.value}
+ >
+ {option.label}
+ </MenuItem>
+ ))}
+ </Select>
+ </FormControl>
+ <Typography variant="caption" color="text.secondary">
+ Expiry (Β΅s): {expiryPreview}
+ </Typography>
+ {error && (
+ <Alert severity="error" sx={{ mt: 2 }}>
+ {error}
+ </Alert>
+ )}
+ </DialogContent>
+ <DialogActions>
+ <Button onClick={handleClose}>Cancel</Button>
+ <Button
+ variant="contained"
+ onClick={() => {
+ void handleSubmit();
+ }}
+ disabled={submitting}
+ >
+ {submitting ? "Creating..." : "Create"}
+ </Button>
+ </DialogActions>
+ </>
+ )}
+ </Dialog>
+ );
+};
+
+export default AddOtt;
diff --git a/infra/staff/src/components/UserComponent.tsx b/infra/staff/src/components/UserComponent.tsx
index be2b1afb11..b2c421d080 100644
--- a/infra/staff/src/components/UserComponent.tsx
+++ b/infra/staff/src/components/UserComponent.tsx
@@ -14,6 +14,7 @@ import TableRow from "@mui/material/TableRow";
import Typography from "@mui/material/Typography";
import * as React from "react";
import type { UserComponentProps } from "../types";
+import AddOtt from "./AddOtt";
import ChangeEmail from "./ChangeEmail";
import DeleteAccount from "./DeleteAccont";
import Disable2FA from "./Disable2FA";
@@ -32,6 +33,7 @@ const UserComponent: React.FC<UserComponentProps> = ({ userData }) => {
React.useState(false);
const [changeEmailOpen, setChangeEmailOpen] = React.useState(false);
const [disablePasskeysOpen, setDisablePasskeysOpen] = React.useState(false);
+ const [addOttOpen, setAddOttOpen] = React.useState(false);
React.useEffect(() => {
setTwoFactorEnabled(userData?.security["Two factor 2FA"] === "Enabled");
@@ -60,6 +62,7 @@ const UserComponent: React.FC<UserComponentProps> = ({ userData }) => {
onDeleteAccount={handleDeleteAccountClick}
onEditSubscription={handleEditSubscription}
onDisablePasskeys={handleDisablePasskeys}
+ onAddOtt={() => setAddOttOpen(true)}
canDisableEmailMFA={canDisableEmailMFA}
twoFactorEnabled={twoFactorEnabled}
setTwoFactorEnabled={setTwoFactorEnabled}
@@ -97,6 +100,11 @@ const UserComponent: React.FC<UserComponentProps> = ({ userData }) => {
handleClose={() => setDisablePasskeysOpen(false)}
handleDisablePasskeys={() => setDisablePasskeysOpen(false)}
/>
+ <AddOtt
+ open={addOttOpen}
+ onClose={() => setAddOttOpen(false)}
+ userEmail={userData.user.Email ?? ""}
+ />
</Grid>
);
};
@@ -108,6 +116,7 @@ interface DataTableProps {
onDeleteAccount: () => void;
onEditSubscription: () => void;
onDisablePasskeys: () => void;
+ onAddOtt: () => void;
canDisableEmailMFA: boolean;
twoFactorEnabled: boolean;
setTwoFactorEnabled: (enabled: boolean) => void;
@@ -123,6 +132,7 @@ const DataTable: React.FC<DataTableProps> = ({
onDeleteAccount,
onEditSubscription,
onDisablePasskeys,
+ onAddOtt,
canDisableEmailMFA,
twoFactorEnabled,
setTwoFactorEnabled,
@@ -237,6 +247,35 @@ const DataTable: React.FC<DataTableProps> = ({
</TableCell>
</TableRow>
))}
+ {title === "security" && (
+ <TableRow>
+ <TableCell
+ component="th"
+ scope="row"
+ style={{
+ padding: "16px",
+ borderBottom: "none",
+ }}
+ >
+ Add OTT
+ </TableCell>
+ <TableCell
+ align="right"
+ style={{
+ padding: "10px",
+ borderBottom: "none",
+ }}
+ >
+ <Button
+ variant="contained"
+ onClick={onAddOtt}
+ sx={{ textTransform: "none" }}
+ >
+ Add OTT
+ </Button>
+ </TableCell>
+ </TableRow>
+ )}
</TableBody>
</Table>
</TableContainer>