1 import { c } from 'ttag';
3 import { readKasperskyData } from '@proton/pass/lib/import/providers/kaspersky/kaspersky.reader';
4 import { readProtonPassCSV } from '@proton/pass/lib/import/providers/protonpass/protonpass.csv.reader';
5 import { transferableToFile } from '@proton/pass/utils/file/transferable-file';
6 import { PASS_APP_NAME } from '@proton/shared/lib/constants';
8 import { read1Password1PifData } from './providers/1password/1pif.reader';
9 import { read1Password1PuxData } from './providers/1password/1pux.reader';
10 import { readBitwardenData } from './providers/bitwarden/bitwarden.reader';
11 import { readChromiumData } from './providers/chromium/chromium.reader';
12 import { readDashlaneDataCSV } from './providers/dashlane/dashlane.csv.reader';
13 import { readDashlaneDataZIP } from './providers/dashlane/dashlane.zip.reader';
14 import { readEnpassData } from './providers/enpass/enpass.reader';
15 import { readFirefoxData } from './providers/firefox/firefox.reader';
16 import { readKeePassData } from './providers/keepass/keepass.reader';
17 import { readKeeperData } from './providers/keeper/keeper.reader';
18 import { readLastPassData } from './providers/lastpass/lastpass.reader';
19 import { readNordPassData } from './providers/nordpass/nordpass.reader';
20 import { decryptProtonPassImport, readProtonPassZIP } from './providers/protonpass/protonpass.zip.reader';
21 import { readRoboformData } from './providers/roboform/roboform.reader';
22 import { readSafariData } from './providers/safari/safari.reader';
23 import { type ImportPayload, ImportProvider, type ImportReaderPayload } from './types';
25 export const extractFileExtension = (fileName: string): string => {
26 const parts = fileName.split('.');
27 return parts[parts.length - 1];
30 export const isProtonPassEncryptedImport = (payload: ImportReaderPayload): boolean =>
31 payload.provider === ImportProvider.PROTONPASS && extractFileExtension(payload.file.name) === 'pgp';
33 export const prepareImport = async (payload: ImportReaderPayload): Promise<ImportReaderPayload> =>
34 isProtonPassEncryptedImport(payload) ? { ...payload, file: await decryptProtonPassImport(payload) } : payload;
36 export const fileReader = async (payload: ImportReaderPayload): Promise<ImportPayload> => {
37 const file = transferableToFile(payload.file);
38 const fileExtension = extractFileExtension(payload.file.name);
40 switch (payload.provider) {
41 case ImportProvider.BITWARDEN: {
42 return readBitwardenData({ data: await file.text() });
45 case ImportProvider.BRAVE:
46 case ImportProvider.CHROME:
47 case ImportProvider.EDGE: {
48 return readChromiumData({ data: await file.text() });
51 case ImportProvider.FIREFOX: {
52 return readFirefoxData({ data: await file.text() });
55 case ImportProvider.KEEPASS: {
56 return readKeePassData({ data: await file.text() });
59 case ImportProvider.LASTPASS: {
60 return readLastPassData({ data: await file.text() });
63 case ImportProvider.ONEPASSWORD: {
64 switch (fileExtension) {
66 return read1Password1PifData({ data: await file.text() });
68 return read1Password1PuxData({ data: await file.arrayBuffer() });
70 throw new Error(c('Error').t`Unsupported 1Password file format`);
74 case ImportProvider.PROTONPASS: {
75 switch (fileExtension) {
77 return readProtonPassCSV({ data: await file.text() });
80 return readProtonPassZIP({
81 data: await file.arrayBuffer(),
82 userId: payload.userId,
83 currentAliases: payload.options?.currentAliases ?? [],
86 throw new Error(c('Error').t`Unsupported ${PASS_APP_NAME} file format`);
90 case ImportProvider.CSV: {
91 return readProtonPassCSV({ data: await file.text(), isGenericCSV: true });
94 case ImportProvider.DASHLANE: {
95 switch (fileExtension) {
97 return readDashlaneDataCSV({ data: await file.text() });
99 return readDashlaneDataZIP({ data: await file.arrayBuffer() });
101 throw new Error(c('Error').t`Unsupported ${PASS_APP_NAME} file format`);
105 case ImportProvider.SAFARI: {
106 return readSafariData({ data: await file.text() });
109 case ImportProvider.KEEPER: {
110 return readKeeperData({ data: await file.text() });
113 case ImportProvider.ROBOFORM: {
114 return readRoboformData({ data: await file.text() });
117 case ImportProvider.NORDPASS: {
118 return readNordPassData({ data: await file.text() });
121 case ImportProvider.ENPASS: {
122 return readEnpassData({ data: await file.text() });
125 case ImportProvider.KASPERSKY: {
126 return readKasperskyData({ data: await file.text() });
130 throw new Error(c('Error').t`Invalid password manager`);