Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

6.1 KB
Raw Blame
import { generateTotpCode, getTotpElapsedPercentage, getTotpRemainingSeconds } from '@aliasvault/client/items/TotpUtility';
import React, { useState, useEffect } from 'react';
import { useTranslation } from 'react-i18next';

import { useDb } from '@/entrypoints/popup/context/DbContext';
import { useRecordItemCopy } from '@/entrypoints/popup/hooks/useRecordItemCopy';

import { logExpected, logFailure } from '@/utils/Diagnostics';
import { sendMessage } from '@/utils/messaging/ExtensionMessaging';

import type { TotpCode } from '@aliasvault/models/vault';

/**
 * Formats a TOTP code with a space in the middle for better readability, e.g. "XXX XXX" or "XXXX XXXX".
 */
const formatTotpCode = (code: string | undefined): string => {
  if (!code) {
    return '';
  }
  if (code.length % 2 === 0) {
    const half = code.length / 2;
    return `${code.slice(0, half)} ${code.slice(half)}`;
  }
  return code;
};

type TotpBlockProps = {
  itemId: string;
  manifestId: string;
}

/**
 * This component shows TOTP codes for an item.
 */
const TotpBlock: React.FC<TotpBlockProps> = ({ itemId, manifestId }) => {
  const { t } = useTranslation();
  const [totpCodes, setTotpCodes] = useState<TotpCode[]>([]);
  const [loading, setLoading] = useState(true);
  const [currentCodes, setCurrentCodes] = useState<Record<string, string>>({});
  const [copiedId, setCopiedId] = useState<string | null>(null);
  const dbContext = useDb();
  const recordItemCopy = useRecordItemCopy();

  /**
   * Copies a TOTP code to the clipboard.
   */
  const copyToClipboard = async (code: string, id: string): Promise<void> => {
    try {
      await navigator.clipboard.writeText(code);
      setCopiedId(id);
      
      // Notify background script that clipboard was copied
      await sendMessage('CLIPBOARD_COPIED');

      /*
       * Record the use against the item. The auto-copy that follows an autofill deliberately does not go
       * through here: that interaction is already counted as an autofill, and would otherwise count twice.
       */
      recordItemCopy(itemId, manifestId);

      // Reset copied state after 2 seconds
      setTimeout(() => {
        setCopiedId(null);
      }, 2000);
    } catch (error) {
      logExpected('[Clipboard] Copying the TOTP code failed', error);
    }
  };

  useEffect(() => {
    /**
     * Loads the TOTP codes for the item.
     */
    const loadTotpCodes = async (): Promise<void> => {
      if (!dbContext?.sqliteClient || !itemId) {
        return;
      }

      try {
        const codes = dbContext.sqliteClient.items.getTotpCodesForItem({ Id: itemId, ManifestId: manifestId });
        setTotpCodes(codes);
      } catch (error) {
        logFailure('Error loading TOTP codes', error);
      } finally {
        setLoading(false);
      }
    };

    loadTotpCodes();
  }, [itemId, manifestId, dbContext?.sqliteClient]);

  useEffect(() => {
    let cancelled = false;

    /**
     * Regenerates the codes; a failed code keeps its previous value so a single bad tick does not empty the displayed value.
     */
    const refreshCodes = async (): Promise<void> => {
      const generated = await Promise.all(totpCodes.map(async code => [code.Id, await generateTotpCode(code.SecretKey, code)] as const));
      if (cancelled) {
        return;
      }
      setCurrentCodes(prevCodes => Object.fromEntries(generated.map(([id, code]) => [id, code ?? prevCodes[id] ?? 'Error'])));
    };

    void refreshCodes();
    const intervalId = setInterval(() => void refreshCodes(), 1000);

    return () : void => {
      cancelled = true;
      clearInterval(intervalId);
    };
  }, [totpCodes]);

  if (loading) {
    return (
      <div className="text-gray-500 dark:text-gray-400 mb-4">
        <h2 className="text-lg font-semibold text-gray-900 dark:text-white mb-2">{t('common.twoFactorAuthentication')}</h2>
        {t('common.loadingTotpCodes')}
      </div>
    );
  }

  if (totpCodes.length === 0) {
    return null;
  }

  return (
    <div className="mb-4">
      <div className="space-y-2">
        <h2 className="text-lg font-semibold text-gray-900 dark:text-white">{t('common.twoFactorAuthentication')}</h2>
        <div className="grid grid-cols-1 gap-2">
          {totpCodes.map(totpCode => (
            <button
              key={totpCode.Id}
              className={`w-full text-left p-2 ps-3 pe-3 rounded bg-white dark:bg-gray-800 shadow hover:shadow-md transition-all border border-gray-200 dark:border-gray-700 hover:bg-gray-50 dark:hover:bg-gray-700`}
              onClick={() => copyToClipboard(currentCodes[totpCode.Id], totpCode.Id)}
              aria-label={`Copy ${totpCode.Name || t('totp.defaultName')} code`}
            >
              <div className="flex justify-between items-center gap-2">
                <div className="flex items-center flex-1">
                  <h4 className="text-sm font-medium text-gray-900 dark:text-white">{totpCode.Name || t('totp.defaultName')}</h4>
                </div>
                <div className="flex items-center gap-2">
                  <div className="flex flex-col items-end">
                    <span className="text-lg font-bold text-gray-900 dark:text-white">
                      {formatTotpCode(currentCodes[totpCode.Id])}
                    </span>
                    <div className="text-xs">
                      {copiedId === totpCode.Id ? (
                        <span className="text-green-600 dark:text-green-400">{t('common.copiedShort')}</span>
                      ) : (
                        <span className="text-gray-500 dark:text-gray-400">{getTotpRemainingSeconds(totpCode)}s</span>
                      )}
                    </div>
                  </div>
                  <div className="w-1 h-6 bg-gray-200 rounded-full dark:bg-gray-600">
                    <div
                      className="bg-primary-500 rounded-full transition-all"
                      style={{ height: `${getTotpElapsedPercentage(totpCode)}%`, width: '100%' }}
                    />
                  </div>
                </div>
              </div>
            </button>
          ))}
        </div>
      </div>
    </div>
  );
};

export default TotpBlock;