Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bitwarden import #348

Merged
merged 7 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/l10n/arb/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"importSelectJsonFile": "Select JSON file",
"importEnteEncGuide": "Select the encrypted JSON file exported from ente",
"importRaivoGuide": "Use the \"Export OTPs to Zip archive\" option in Raivo's Settings.\n\nExtract the zip file and import the JSON file.",
"importBitwardenGuide": "Use the \"Export vault\" option within Bitwarden Tools and import the unencrypted JSON file.",
"importAegisGuide": "Use the \"Export the vault\" option in Aegis's Settings.\n\nIf your vault is encrypted, you will need to enter vault password to decrypt the vault.",
"exportCodes": "Export codes",
"importLabel": "Import",
Expand Down
103 changes: 103 additions & 0 deletions lib/ui/settings/data/import/bitwarden_import.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:ente_auth/l10n/l10n.dart';
import 'package:ente_auth/models/code.dart';
import 'package:ente_auth/services/authenticator_service.dart';
import 'package:ente_auth/store/code_store.dart';
import 'package:ente_auth/ui/components/buttons/button_widget.dart';
import 'package:ente_auth/ui/components/dialog_widget.dart';
import 'package:ente_auth/ui/components/models/button_type.dart';
import 'package:ente_auth/ui/settings/data/import/import_success.dart';
import 'package:ente_auth/utils/dialog_util.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

Future<void> showBitwardenImportInstruction(BuildContext context) async {
final l10n = context.l10n;
final result = await showDialogWidget(
context: context,
title: l10n.importFromApp("Bitwarden"),
body: l10n.importBitwardenGuide,
buttons: [
ButtonWidget(
buttonType: ButtonType.primary,
labelText: l10n.importSelectJsonFile,
isInAlert: true,
buttonSize: ButtonSize.large,
buttonAction: ButtonAction.first,
),
ButtonWidget(
buttonType: ButtonType.secondary,
labelText: context.l10n.cancel,
buttonSize: ButtonSize.large,
isInAlert: true,
buttonAction: ButtonAction.second,
),
],
);
if (result?.action != null && result!.action != ButtonAction.cancel) {
if (result.action == ButtonAction.first) {
await _pickBitwardenJsonFile(context);
}
}
}

Future<void> _pickBitwardenJsonFile(BuildContext context) async {
final l10n = context.l10n;
FilePickerResult? result = await FilePicker.platform.pickFiles();
if (result == null) {
return;
}
final progressDialog = createProgressDialog(context, l10n.pleaseWait);
await progressDialog.show();
try {
String path = result.files.single.path!;
int? count = await _processBitwardenExportFile(context, path);
await progressDialog.hide();
if (count != null) {
await importSuccessDialog(context, count);
}
} catch (e) {
await progressDialog.hide();
await showErrorDialog(
context,
context.l10n.sorry,
context.l10n.importFailureDesc,
);
}
}

Future<int?> _processBitwardenExportFile(
BuildContext context,
String path,
) async {
File file = File(path);
final jsonString = await file.readAsString();
final data = jsonDecode(jsonString);
List<dynamic> jsonArray = data['items'];
final parsedCodes = [];
for (var item in jsonArray) {
if (item['login']['totp'] != null) {
var issuer = item['name'];
var account = item['login']['username'];
var secret = item['login']['totp'];

parsedCodes.add(
Code.fromAccountAndSecret(
account,
issuer,
secret,
),
);
}
}

for (final code in parsedCodes) {
await CodeStore.instance.addCode(code, shouldSync: false);
}
unawaited(AuthenticatorService.instance.onlineSync());
return parsedCodes.length;
}
4 changes: 4 additions & 0 deletions lib/ui/settings/data/import/import_service.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:ente_auth/ui/settings/data/import/aegis_import.dart';
import 'package:ente_auth/ui/settings/data/import/bitwarden_import.dart';
import 'package:ente_auth/ui/settings/data/import/encrypted_ente_import.dart';
import 'package:ente_auth/ui/settings/data/import/google_auth_import.dart';
import 'package:ente_auth/ui/settings/data/import/plain_text_import.dart';
Expand Down Expand Up @@ -31,6 +32,9 @@ class ImportService {
case ImportType.aegis:
showAegisImportInstruction(context);
break;
case ImportType.bitwarden:
showBitwardenImportInstruction(context);
break;
}
}
}
4 changes: 4 additions & 0 deletions lib/ui/settings/data/import_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ enum ImportType {
ravio,
googleAuthenticator,
aegis,
bitwarden,
}

class ImportCodePage extends StatelessWidget {
Expand All @@ -24,6 +25,7 @@ class ImportCodePage extends StatelessWidget {
ImportType.ravio,
ImportType.aegis,
ImportType.googleAuthenticator,
ImportType.bitwarden,
];

ImportCodePage({super.key});
Expand All @@ -40,6 +42,8 @@ class ImportCodePage extends StatelessWidget {
return 'Google Authenticator';
case ImportType.aegis:
return 'Aegis Authenticator';
case ImportType.bitwarden:
return 'Bitwarden';
}
}

Expand Down