Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Add item list long press TOTP copy option (#1804)
Leendert de Borsta6bf9d45b3fb
diff --git a/apps/mobile-app/components/items/ItemCard.tsx b/apps/mobile-app/components/items/ItemCard.tsxindex af962a52a..78b09fb81 100644--- a/apps/mobile-app/components/items/ItemCard.tsx+++ b/apps/mobile-app/components/items/ItemCard.tsx@@ -10,7 +10,9 @@ import Toast from 'react-native-toast-message';import { ItemIcon } from '@/components/items/ItemIcon';import { useDialog } from '@/context/DialogContext';+import { useDb } from '@/context/DbContext';import { LocalPreferencesService } from '@/services/LocalPreferencesService';+import { generateTotpCode } from '@/utils/TotpUtility';import { useColors } from '@/hooks/useColorScheme';import { useNavigationDebounce } from '@/hooks/useNavigationDebounce';import { copyToClipboardWithExpiration } from '@/utils/ClipboardUtility';@@ -30,6 +32,7 @@ export function ItemCard({ item, onItemDelete, showFolderPath = false }: ItemCarconst colors = useColors();const { t } = useTranslation();const { showConfirm } = useDialog();+ const dbContext = useDb();const navigate = useNavigationDebounce();/**@@ -155,6 +158,31 @@ export function ItemCard({ item, onItemDelete, showFolderPath = false }: ItemCar}}break;+ case t('items.contextMenu.copyTotpCode'):+ {+ if (dbContext?.sqliteClient) {+ try {+ const totpCodes = await dbContext.sqliteClient.settings.getTotpCodesForItem(item.Id);+ const activeTotp = totpCodes.find(tc => !tc.IsDeleted);+ if (activeTotp) {+ const code = generateTotpCode(activeTotp.SecretKey);+ if (code) {+ await copyToClipboard(code);+ if (Platform.OS === 'ios') {+ Toast.show({+ type: 'success',+ text1: t('items.toasts.totpCodeCopied'),+ position: 'bottom',+ });+ }+ }+ }+ } catch (error) {+ console.error('Failed to copy TOTP code:', error);+ }+ }+ }+ break;}};@@ -223,6 +251,17 @@ export function ItemCard({ item, onItemDelete, showFolderPath = false }: ItemCar});}+ if (item.HasTotp) {+ actions.push({+ title: t('items.contextMenu.copyTotpCode'),+ systemIcon: Platform.select({+ ios: 'number',+ android: 'baseline_pin',+ default: 'number',+ }),+ });+ }+return actions;};diff --git a/apps/mobile-app/components/items/details/TotpSection.tsx b/apps/mobile-app/components/items/details/TotpSection.tsxindex 769c5c2d6..d21ae4278 100644--- a/apps/mobile-app/components/items/details/TotpSection.tsx+++ b/apps/mobile-app/components/items/details/TotpSection.tsx@@ -1,5 +1,7 @@import * as OTPAuth from 'otpauth';import React, { useState, useEffect } from 'react';++import { generateTotpCode } from '@/utils/TotpUtility';import { useTranslation } from 'react-i18next';import { View, StyleSheet, TouchableOpacity, Platform } from 'react-native';import Toast from 'react-native-toast-message';@@ -61,24 +63,6 @@ export const TotpSection: React.FC<TotpSectionProps> = ({ item }) : React.ReactNreturn Math.floor(((30.0 - remaining) / 30.0) * 100);};- /**- * Generate the totp code.- */- const generateTotpCode = (secretKey: string): string => {- try {- const totp = new OTPAuth.TOTP({- secret: secretKey,- algorithm: 'SHA1',- digits: 6,- period: 30- });- return totp.generate();- } catch (error) {- console.error('Error generating TOTP code:', error);- return 'Error';- }- };-/*** Copy the totp code to the clipboard.*/@@ -132,7 +116,7 @@ export const TotpSection: React.FC<TotpSectionProps> = ({ item }) : React.ReactNconst newCodes: Record<string, string> = {};totpCodes.forEach(code => {const generatedCode = generateTotpCode(code.SecretKey);- if (generatedCode !== 'Error') {+ if (generatedCode) {newCodes[code.Id] = generatedCode;} else {newCodes[code.Id] = prevCodes[code.Id] ?? 'Error';@@ -143,7 +127,8 @@ export const TotpSection: React.FC<TotpSectionProps> = ({ item }) : React.ReactNconst initialCodes: Record<string, string> = {};totpCodes.forEach(code => {- initialCodes[code.Id] = generateTotpCode(code.SecretKey);+ const codeStr = generateTotpCode(code.SecretKey);+ initialCodes[code.Id] = codeStr || 'Error';});setCurrentCodes(initialCodes);diff --git a/apps/mobile-app/i18n/locales/en.json b/apps/mobile-app/i18n/locales/en.jsonindex 248f5c160..2098e5f2d 100644--- a/apps/mobile-app/i18n/locales/en.json+++ b/apps/mobile-app/i18n/locales/en.json@@ -475,6 +475,7 @@"usernameCopied": "Username copied to clipboard","emailCopied": "Email copied to clipboard","passwordCopied": "Password copied to clipboard",+ "totpCodeCopied": "TOTP code copied to clipboard","urlCopied": "URL copied to clipboard"},"createNewAliasFor": "Create new alias for",@@ -484,7 +485,8 @@"delete": "Delete","copyUsername": "Copy Username","copyEmail": "Copy Email",- "copyPassword": "Copy Password"+ "copyPassword": "Copy Password",+ "copyTotpCode": "Copy TOTP Code"},"urlContextMenu": {"title": "URL Options",diff --git a/apps/mobile-app/utils/TotpUtility.ts b/apps/mobile-app/utils/TotpUtility.tsnew file mode 100644index 000000000..0c9a275b7--- /dev/null+++ b/apps/mobile-app/utils/TotpUtility.ts@@ -0,0 +1,23 @@+import * as OTPAuth from 'otpauth';++/**+ * Generates the current TOTP code for the given secret key.+ * Uses standard TOTP settings: SHA1, 6 digits, 30-second period.+ *+ * @param secretKey - Base32-encoded TOTP secret+ * @returns The current 6-digit TOTP code, or empty string on error+ */+export function generateTotpCode(secretKey: string): string {+ try {+ const totp = new OTPAuth.TOTP({+ secret: secretKey,+ algorithm: 'SHA1',+ digits: 6,+ period: 30+ });+ return totp.generate();+ } catch (error) {+ console.error('Error generating TOTP code:', error);+ return '';+ }+}