Skip to content

armgit / ente

Public Imported from GitHub
0

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

Rearrange

Manav Rathi51dc8d1de6bc
diff --git a/web/apps/auth/src/pages/auth.tsx b/web/apps/auth/src/pages/auth.tsx
index a76b687fc3..c5c328a425 100644
--- a/web/apps/auth/src/pages/auth.tsx
+++ b/web/apps/auth/src/pages/auth.tsx
@@ -122,7 +122,7 @@ const AuthenticatorCodesPage = () => {
</div>
) : (
filteredCodes.map((code) => (
- <OTPDisplay codeInfo={code} key={code.id} />
+ <CodeDisplay codeInfo={code} key={code.id} />
))
)}
</div>
@@ -162,28 +162,110 @@ const AuthNavbar: React.FC = () => {
);
};
-const AuthFooter: React.FC = () => {
+interface CodeDisplay {
+ codeInfo: Code;
+}
+
+const CodeDisplay: React.FC<CodeDisplay> = ({ codeInfo }) => {
+ const [otp, setOTP] = useState("");
+ const [nextOTP, setNextOTP] = useState("");
+ const [codeErr, setCodeErr] = useState("");
+ const [hasCopied, setHasCopied] = useState(false);
+
+ const generateCodes = () => {
+ try {
+ const currentTime = new Date().getTime();
+ if (codeInfo.type.toLowerCase() === "totp") {
+ const totp = new TOTP({
+ secret: codeInfo.secret,
+ algorithm: codeInfo.algorithm ?? Code.defaultAlgo,
+ period: codeInfo.period ?? Code.defaultPeriod,
+ digits: codeInfo.digits,
+ });
+ setOTP(totp.generate());
+ setNextOTP(
+ totp.generate({
+ timestamp: currentTime + codeInfo.period * 1000,
+ }),
+ );
+ } else if (codeInfo.type.toLowerCase() === "hotp") {
+ const hotp = new HOTP({
+ secret: codeInfo.secret,
+ counter: 0,
+ algorithm: codeInfo.algorithm,
+ });
+ setOTP(hotp.generate());
+ setNextOTP(hotp.generate({ counter: 1 }));
+ }
+ } catch (err) {
+ setCodeErr(err.message);
+ }
+ };
+
+ const copyCode = () => {
+ navigator.clipboard.writeText(otp);
+ setHasCopied(true);
+ setTimeout(() => {
+ setHasCopied(false);
+ }, 2000);
+ };
+
+ useEffect(() => {
+ // this is to set the initial code and nextCode on component mount
+ generateCodes();
+ const codeType = codeInfo.type;
+ const codePeriodInMs = codeInfo.period * 1000;
+ const timeToNextCode =
+ codePeriodInMs - (new Date().getTime() % codePeriodInMs);
+ const intervalId = null;
+ // wait until we are at the start of the next code period,
+ // and then start the interval loop
+ setTimeout(() => {
+ // we need to call generateCodes() once before the interval loop
+ // to set the initial code and nextCode
+ generateCodes();
+ codeType.toLowerCase() === "totp" ||
+ codeType.toLowerCase() === "hotp"
+ ? setInterval(() => {
+ generateCodes();
+ }, codePeriodInMs)
+ : null;
+ }, timeToNextCode);
+
+ return () => {
+ if (intervalId) clearInterval(intervalId);
+ };
+ }, [codeInfo]);
+
return (
- <div
- style={{
- display: "flex",
- flexDirection: "column",
- alignItems: "center",
- justifyContent: "center",
- }}
- >
- <p>{t("AUTH_DOWNLOAD_MOBILE_APP")}</p>
- <a
- href="https://github.com/ente-io/ente/tree/main/auth#-download"
- download
- >
- <Button color="accent">{t("DOWNLOAD")}</Button>
- </a>
+ <div style={{ padding: "8px" }}>
+ {codeErr === "" ? (
+ <ButtonBase
+ component="div"
+ onClick={() => {
+ copyCode();
+ }}
+ >
+ <OTPDisplay code={codeInfo} otp={otp} nextOTP={nextOTP} />
+ <Snackbar
+ open={hasCopied}
+ message="Code copied to clipboard"
+ />
+ </ButtonBase>
+ ) : (
+ <BadCodeInfo codeInfo={codeInfo} codeErr={codeErr} />
+ )}
</div>
);
};
-const TOTPDisplay = ({ issuer, account, code, nextCode, period }) => {
+interface OTPDisplayProps {
+ code: Code;
+ otp: string;
+ nextOTP: string;
+}
+
+const OTPDisplay: React.FC<OTPDisplayProps> = ({ code, otp, nextOTP }) => {
return (
<div
style={{
@@ -192,7 +274,7 @@ const TOTPDisplay = ({ issuer, account, code, nextCode, period }) => {
overflow: "hidden",
}}
>
- <TimerProgress period={period ?? Code.defaultPeriod} />
+ <TimerProgress period={code.period ?? Code.defaultPeriod} />
<div
style={{
padding: "12px 20px 0px 20px",
@@ -219,7 +301,7 @@ const TOTPDisplay = ({ issuer, account, code, nextCode, period }) => {
textAlign: "left",
}}
>
- {issuer}
+ {code.issuer}
</p>
<p
style={{
@@ -232,7 +314,7 @@ const TOTPDisplay = ({ issuer, account, code, nextCode, period }) => {
color: "grey",
}}
>
- {account}
+ {code.account}
</p>
<p
style={{
@@ -243,7 +325,7 @@ const TOTPDisplay = ({ issuer, account, code, nextCode, period }) => {
textAlign: "left",
}}
>
- {code}
+ {otp}
</p>
</div>
<div style={{ flex: 1 }} />
@@ -280,7 +362,7 @@ const TOTPDisplay = ({ issuer, account, code, nextCode, period }) => {
color: "grey",
}}
>
- {nextCode}
+ {nextOTP}
</p>
</div>
</div>
@@ -288,154 +370,26 @@ const TOTPDisplay = ({ issuer, account, code, nextCode, period }) => {
);
};
-function BadCodeInfo({ codeInfo, codeErr }) {
- const [showRawData, setShowRawData] = useState(false);
-
- return (
- <div className="code-info">
- <div>{codeInfo.title}</div>
- <div>{codeErr}</div>
- <div>
- {showRawData ? (
- <div onClick={() => setShowRawData(false)}>
- {codeInfo.rawData ?? "no raw data"}
- </div>
- ) : (
- <div onClick={() => setShowRawData(true)}>Show rawData</div>
- )}
- </div>
- </div>
- );
-}
-
-interface OTPDisplayProps {
- codeInfo: Code;
-}
-
-const OTPDisplay: React.FC<OTPDisplayProps> = ({ codeInfo }) => {
- const [code, setCode] = useState("");
- const [nextCode, setNextCode] = useState("");
- const [codeErr, setCodeErr] = useState("");
- const [hasCopied, setHasCopied] = useState(false);
-
- const generateCodes = () => {
- try {
- const currentTime = new Date().getTime();
- if (codeInfo.type.toLowerCase() === "totp") {
- const totp = new TOTP({
- secret: codeInfo.secret,
- algorithm: codeInfo.algorithm ?? Code.defaultAlgo,
- period: codeInfo.period ?? Code.defaultPeriod,
- digits: codeInfo.digits,
- });
- setCode(totp.generate());
- setNextCode(
- totp.generate({
- timestamp: currentTime + codeInfo.period * 1000,
- }),
- );
- } else if (codeInfo.type.toLowerCase() === "hotp") {
- const hotp = new HOTP({
- secret: codeInfo.secret,
- counter: 0,
- algorithm: codeInfo.algorithm,
- });
- setCode(hotp.generate());
- setNextCode(hotp.generate({ counter: 1 }));
- }
- } catch (err) {
- setCodeErr(err.message);
- }
- };
-
- const copyCode = () => {
- navigator.clipboard.writeText(code);
- setHasCopied(true);
- setTimeout(() => {
- setHasCopied(false);
- }, 2000);
- };
-
- useEffect(() => {
- // this is to set the initial code and nextCode on component mount
- generateCodes();
- const codeType = codeInfo.type;
- const codePeriodInMs = codeInfo.period * 1000;
- const timeToNextCode =
- codePeriodInMs - (new Date().getTime() % codePeriodInMs);
- const intervalId = null;
- // wait until we are at the start of the next code period,
- // and then start the interval loop
- setTimeout(() => {
- // we need to call generateCodes() once before the interval loop
- // to set the initial code and nextCode
- generateCodes();
- codeType.toLowerCase() === "totp" ||
- codeType.toLowerCase() === "hotp"
- ? setInterval(() => {
- generateCodes();
- }, codePeriodInMs)
- : null;
- }, timeToNextCode);
-
- return () => {
- if (intervalId) clearInterval(intervalId);
- };
- }, [codeInfo]);
-
- return (
- <div style={{ padding: "8px" }}>
- {codeErr === "" ? (
- <ButtonBase
- component="div"
- onClick={() => {
- copyCode();
- }}
- >
- <TOTPDisplay
- period={codeInfo.period}
- issuer={codeInfo.issuer}
- account={codeInfo.account}
- code={code}
- nextCode={nextCode}
- />
- <Snackbar
- open={hasCopied}
- message="Code copied to clipboard"
- />
- </ButtonBase>
- ) : (
- <BadCodeInfo codeInfo={codeInfo} codeErr={codeErr} />
- )}
- </div>
- );
-};
-
interface TimerProgressProps {
period: number;
}
const TimerProgress: React.FC<TimerProgressProps> = ({ period }) => {
const [progress, setProgress] = useState(0);
- const [ticker, setTicker] = useState(null);
const microSecondsInPeriod = period * 1000000;
- const startTicker = () => {
+ useEffect(() => {
+ const updateTimeRemaining = () => {
+ const timeRemaining =
+ microSecondsInPeriod -
+ ((new Date().getTime() * 1000) % microSecondsInPeriod);
+ setProgress(timeRemaining / microSecondsInPeriod);
+ };
+
const ticker = setInterval(() => {
updateTimeRemaining();
}, 10);
- setTicker(ticker);
- };
- const updateTimeRemaining = () => {
- const timeRemaining =
- microSecondsInPeriod -
- ((new Date().getTime() * 1000) % microSecondsInPeriod);
- setProgress(timeRemaining / microSecondsInPeriod);
- };
-
- useEffect(() => {
- startTicker();
return () => clearInterval(ticker);
}, []);
@@ -452,3 +406,44 @@ const TimerProgress: React.FC<TimerProgressProps> = ({ period }) => {
/>
);
};
+
+function BadCodeInfo({ codeInfo, codeErr }) {
+ const [showRawData, setShowRawData] = useState(false);
+
+ return (
+ <div className="code-info">
+ <div>{codeInfo.title}</div>
+ <div>{codeErr}</div>
+ <div>
+ {showRawData ? (
+ <div onClick={() => setShowRawData(false)}>
+ {codeInfo.rawData ?? "no raw data"}
+ </div>
+ ) : (
+ <div onClick={() => setShowRawData(true)}>Show rawData</div>
+ )}
+ </div>
+ </div>
+ );
+}
+
+const AuthFooter: React.FC = () => {
+ return (
+ <div
+ style={{
+ display: "flex",
+ flexDirection: "column",
+ alignItems: "center",
+ justifyContent: "center",
+ }}
+ >
+ <p>{t("AUTH_DOWNLOAD_MOBILE_APP")}</p>
+ <a
+ href="https://github.com/ente-io/ente/tree/main/auth#-download"
+ download
+ >
+ <Button color="accent">{t("DOWNLOAD")}</Button>
+ </a>
+ </div>
+ );
+};