From 69cea6786dbccc180908627c5ccac2e4345d3351 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Thu, 23 May 2024 18:54:55 +0530 Subject: [PATCH 1/9] Redistr --- web/apps/auth/src/services/code.ts | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/web/apps/auth/src/services/code.ts b/web/apps/auth/src/services/code.ts index ca9ba16427..87e8887ef1 100644 --- a/web/apps/auth/src/services/code.ts +++ b/web/apps/auth/src/services/code.ts @@ -41,19 +41,12 @@ export interface Code { * otpauth://totp/account:user@example.org?algorithm=SHA1&digits=6&issuer=issuer&period=30&secret=ALPHANUM */ export const codeFromURIString = (id: string, uriString: string): Code => { - let santizedRawData = uriString - .replace(/\+/g, "%2B") - .replace(/:/g, "%3A") - .replaceAll("\r", ""); - if (santizedRawData.startsWith('"')) { - santizedRawData = santizedRawData.substring(1); - } - if (santizedRawData.endsWith('"')) { - santizedRawData = santizedRawData.substring( - 0, - santizedRawData.length - 1, - ); - } + const santizedRawData = uriString + .replaceAll("+", "%2B") + .replaceAll(":", "%3A") + .replaceAll("\r", "") + // trim quotes + .replace(/^"|"$/g, ""); const uriParams = {}; const searchParamsString = From 30a8691c7f90fca2fffc847967d02aef7a458588 Mon Sep 17 00:00:00 2001 From: Ashil <77285023+ashilkn@users.noreply.github.com> Date: Thu, 23 May 2024 18:59:36 +0530 Subject: [PATCH 2/9] [mob][photos] Fix infinite loading on searching (#1830) ## Description Search was infinitely loading even after all search results are ready. --- mobile/lib/ui/viewer/search/search_widget.dart | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mobile/lib/ui/viewer/search/search_widget.dart b/mobile/lib/ui/viewer/search/search_widget.dart index c917d60e91..89002b1e14 100644 --- a/mobile/lib/ui/viewer/search/search_widget.dart +++ b/mobile/lib/ui/viewer/search/search_widget.dart @@ -203,7 +203,7 @@ class SearchWidgetState extends State { String query, ) { int resultCount = 0; - final maxResultCount = _isYearValid(query) ? 13 : 12; + final maxResultCount = _isYearValid(query) ? 12 : 11; final streamController = StreamController>(); if (query.isEmpty) { @@ -260,10 +260,11 @@ class SearchWidgetState extends State { onResultsReceived(locationResult); }, ); + _searchService.getAllFace(null).then( - (locationResult) { + (faceResult) { final List filteredResults = []; - for (final result in locationResult) { + for (final result in faceResult) { if (result.name().toLowerCase().contains(query.toLowerCase())) { filteredResults.add(result); } From 90d0196d471e04f0ec7b2dfc47117b1baf144449 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Thu, 23 May 2024 19:06:06 +0530 Subject: [PATCH 3/9] Extract logic --- web/apps/auth/src/pages/auth.tsx | 55 +++++++++--------------------- web/apps/auth/src/services/code.ts | 41 ++++++++++++++++++++++ 2 files changed, 58 insertions(+), 38 deletions(-) diff --git a/web/apps/auth/src/pages/auth.tsx b/web/apps/auth/src/pages/auth.tsx index e628050ea2..4ad1b99207 100644 --- a/web/apps/auth/src/pages/auth.tsx +++ b/web/apps/auth/src/pages/auth.tsx @@ -15,10 +15,9 @@ import MoreHoriz from "@mui/icons-material/MoreHoriz"; import { Button, ButtonBase, Snackbar, TextField } from "@mui/material"; import { t } from "i18next"; import { useRouter } from "next/router"; -import { HOTP, TOTP } from "otpauth"; import { AppContext } from "pages/_app"; import React, { useContext, useEffect, useState } from "react"; -import { Code } from "services/code"; +import { generateOTPs, type Code } from "services/code"; import { getAuthCodes } from "services/remote"; const AuthenticatorCodesPage = () => { @@ -172,33 +171,13 @@ const CodeDisplay: React.FC = ({ codeInfo }) => { const [codeErr, setCodeErr] = useState(""); const [hasCopied, setHasCopied] = useState(false); - const generateCodes = () => { + const regen = () => { try { - const currentTime = new Date().getTime(); - if (codeInfo.type === "totp") { - const totp = new TOTP({ - secret: codeInfo.secret, - algorithm: codeInfo.algorithm, - period: codeInfo.period, - digits: codeInfo.digits, - }); - setOTP(totp.generate()); - setNextOTP( - totp.generate({ - timestamp: currentTime + codeInfo.period * 1000, - }), - ); - } else if (codeInfo.type === "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 [m, n] = generateOTPs(codeInfo); + setOTP(m); + setNextOTP(n); + } catch (e) { + setCodeErr(e.message); } }; @@ -211,29 +190,29 @@ const CodeDisplay: React.FC = ({ codeInfo }) => { }; useEffect(() => { - // this is to set the initial code and nextCode on component mount - generateCodes(); + // Generate to set the initial otp and nextOTP on component mount. + regen(); 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 + const interval = 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(); + // We need to call regen() once before the interval loop to set the + // initial otp and nextOTP. + regen(); codeType.toLowerCase() === "totp" || codeType.toLowerCase() === "hotp" ? setInterval(() => { - generateCodes(); + regen(); }, codePeriodInMs) : null; }, timeToNextCode); return () => { - if (intervalId) clearInterval(intervalId); + if (interval) clearInterval(interval); }; }, [codeInfo]); diff --git a/web/apps/auth/src/services/code.ts b/web/apps/auth/src/services/code.ts index 87e8887ef1..15eb15aaf6 100644 --- a/web/apps/auth/src/services/code.ts +++ b/web/apps/auth/src/services/code.ts @@ -1,3 +1,4 @@ +import { HOTP, TOTP } from "otpauth"; import { URI } from "vscode-uri"; /** @@ -145,3 +146,43 @@ const _getType = (uriPath: string): Code["type"] => { const getSanitizedSecret = (uriParams): string => { return uriParams["secret"].replace(/ /g, "").toUpperCase(); }; + +/** + * Generate a pair of OTPs (one time passwords) from the given {@link code}. + * + * @param code The parsed code data, including the secret and code type. + * + * @returns a pair of OTPs, the current one and the next one, using the given + * {@link code}. + */ +export const generateOTPs = (code: Code): [otp: string, nextOTP: string] => { + let otp: string; + let nextOTP: string; + switch (code.type) { + case "totp": { + const totp = new TOTP({ + secret: code.secret, + algorithm: code.algorithm, + period: code.period, + digits: code.digits, + }); + otp = totp.generate(); + nextOTP = totp.generate({ + timestamp: new Date().getTime() + code.period * 1000, + }); + break; + } + + case "hotp": { + const hotp = new HOTP({ + secret: code.secret, + counter: 0, + algorithm: code.algorithm, + }); + otp = hotp.generate(); + nextOTP = hotp.generate({ counter: 1 }); + break; + } + } + return [otp, nextOTP]; +}; From 48fcbdc98c119a1b81c299d1f8fde86678122587 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Thu, 23 May 2024 19:10:42 +0530 Subject: [PATCH 4/9] Reword --- web/apps/auth/src/pages/auth.tsx | 39 +++++++++++++------------------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/web/apps/auth/src/pages/auth.tsx b/web/apps/auth/src/pages/auth.tsx index 4ad1b99207..38b254c546 100644 --- a/web/apps/auth/src/pages/auth.tsx +++ b/web/apps/auth/src/pages/auth.tsx @@ -121,7 +121,7 @@ const AuthenticatorCodesPage = () => { ) : ( filteredCodes.map((code) => ( - + )) )} @@ -162,38 +162,36 @@ const AuthNavbar: React.FC = () => { }; interface CodeDisplay { - codeInfo: Code; + code: Code; } -const CodeDisplay: React.FC = ({ codeInfo }) => { +const CodeDisplay: React.FC = ({ code }) => { const [otp, setOTP] = useState(""); const [nextOTP, setNextOTP] = useState(""); - const [codeErr, setCodeErr] = useState(""); + const [errorMessage, setErrorMessage] = useState(""); const [hasCopied, setHasCopied] = useState(false); const regen = () => { try { - const [m, n] = generateOTPs(codeInfo); + const [m, n] = generateOTPs(code); setOTP(m); setNextOTP(n); } catch (e) { - setCodeErr(e.message); + setErrorMessage(e instanceof Error ? e.message : String(e)); } }; const copyCode = () => { navigator.clipboard.writeText(otp); setHasCopied(true); - setTimeout(() => { - setHasCopied(false); - }, 2000); + setTimeout(() => setHasCopied(false), 2000); }; useEffect(() => { // Generate to set the initial otp and nextOTP on component mount. regen(); - const codeType = codeInfo.type; - const codePeriodInMs = codeInfo.period * 1000; + const codeType = code.type; + const codePeriodInMs = code.period * 1000; const timeToNextCode = codePeriodInMs - (new Date().getTime() % codePeriodInMs); const interval = null; @@ -214,25 +212,20 @@ const CodeDisplay: React.FC = ({ codeInfo }) => { return () => { if (interval) clearInterval(interval); }; - }, [codeInfo]); + }, [code]); return (
- {codeErr === "" ? ( - { - copyCode(); - }} - > - + {errorMessage ? ( + + ) : ( + + - ) : ( - )}
); @@ -396,7 +389,7 @@ function BadCodeInfo({ codeInfo, codeErr }) {
{showRawData ? (
setShowRawData(false)}> - {codeInfo.uriString ?? "(no raw data)"} + {codeInfo.uriString}
) : (
setShowRawData(true)}>Show rawData
From 0f1c98d0d012439ca44e584d5c5bdc3cbd463a24 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Thu, 23 May 2024 19:17:57 +0530 Subject: [PATCH 5/9] Reword --- web/apps/auth/src/pages/auth.tsx | 47 ++++++++++++++++---------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/web/apps/auth/src/pages/auth.tsx b/web/apps/auth/src/pages/auth.tsx index 38b254c546..4006cc9e19 100644 --- a/web/apps/auth/src/pages/auth.tsx +++ b/web/apps/auth/src/pages/auth.tsx @@ -42,7 +42,7 @@ const AuthenticatorCodesPage = () => { } setHasFetched(true); }; - fetchCodes(); + void fetchCodes(); appContext.showNavBar(false); }, []); @@ -126,7 +126,7 @@ const AuthenticatorCodesPage = () => { )}
- +