mirror of https://github.com/beemdevelopment/Aegis
commit
81d5915969
@ -0,0 +1,117 @@
|
||||
package com.beemdevelopment.aegis.importers;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
|
||||
import com.beemdevelopment.aegis.encoding.Base32;
|
||||
import com.beemdevelopment.aegis.encoding.EncodingException;
|
||||
import com.beemdevelopment.aegis.otp.GoogleAuthInfo;
|
||||
import com.beemdevelopment.aegis.otp.GoogleAuthInfoException;
|
||||
import com.beemdevelopment.aegis.otp.OtpInfoException;
|
||||
import com.beemdevelopment.aegis.otp.SteamInfo;
|
||||
import com.beemdevelopment.aegis.util.IOUtils;
|
||||
import com.beemdevelopment.aegis.vault.VaultEntry;
|
||||
import com.topjohnwu.superuser.io.SuFile;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.simpleflatmapper.csv.CsvParser;
|
||||
import org.simpleflatmapper.lightningcsv.Row;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class BitwardenImporter extends DatabaseImporter {
|
||||
public BitwardenImporter(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SuFile getAppPath() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected State read(InputStream stream, boolean isInternal) throws DatabaseImporterException {
|
||||
String fileString;
|
||||
try {
|
||||
fileString = new String(IOUtils.readAll(stream), StandardCharsets.UTF_8);
|
||||
} catch (IOException e) {
|
||||
throw new DatabaseImporterException(e);
|
||||
}
|
||||
try {
|
||||
JSONObject obj = new JSONObject(fileString);
|
||||
JSONArray array = obj.getJSONArray("items");
|
||||
|
||||
List<String> entries = new ArrayList<>();
|
||||
String entry;
|
||||
for (int i = 0; i < array.length(); i++) {
|
||||
entry = array.getJSONObject(i).getJSONObject("login").getString("totp");
|
||||
if (!entry.isEmpty()) {
|
||||
entries.add(entry);
|
||||
}
|
||||
}
|
||||
|
||||
return new BitwardenImporter.State(entries);
|
||||
} catch (JSONException e) {
|
||||
try {
|
||||
Iterator<Row> rowIterator = CsvParser.separator(',').rowIterator(fileString);
|
||||
List<String> entries = new ArrayList<>();
|
||||
rowIterator.forEachRemaining((row -> {
|
||||
String entry = row.get("login_totp");
|
||||
if (entry != null && !entry.isEmpty()) {
|
||||
entries.add(entry);
|
||||
}
|
||||
}));
|
||||
return new BitwardenImporter.State(entries);
|
||||
} catch (IOException e2) {
|
||||
throw new DatabaseImporterException(e2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class State extends DatabaseImporter.State {
|
||||
private final List<String> _entries;
|
||||
|
||||
public State(List<String> entries) {
|
||||
super(false);
|
||||
_entries = entries;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result convert() {
|
||||
Result result = new Result();
|
||||
|
||||
for (String obj : _entries) {
|
||||
try {
|
||||
VaultEntry entry = convertEntry(obj);
|
||||
result.addEntry(entry);
|
||||
} catch (DatabaseImporterEntryException e) {
|
||||
result.addError(e);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static VaultEntry convertEntry(String obj) throws DatabaseImporterEntryException {
|
||||
try {
|
||||
GoogleAuthInfo info = BitwardenImporter.parseUri(obj);
|
||||
return new VaultEntry(info);
|
||||
} catch (GoogleAuthInfoException | EncodingException | OtpInfoException | URISyntaxException e) {
|
||||
throw new DatabaseImporterEntryException(e, obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static GoogleAuthInfo parseUri(String obj) throws EncodingException, OtpInfoException, URISyntaxException, GoogleAuthInfoException {
|
||||
Uri uri = Uri.parse(obj);
|
||||
return uri.getScheme().equals("steam") ? new GoogleAuthInfo(new SteamInfo(Base32.decode(uri.getAuthority())), "Steam account", "Steam") : GoogleAuthInfo.parseUri(uri);
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
folder,favorite,type,name,notes,fields,login_uri,login_username,login_password,login_totp
|
||||
,,login,Test 1,,,,,,otpauth://totp/Deno:Mason?secret=4SJHB4GSD43FZBAI7C2HLRJGPQ&issuer=Deno&algorithm=SHA1&digits=6&period=30
|
||||
,,login,Test 2,,,,,,otpauth://totp/SPDX:James?secret=5OM4WOOGPLQEF6UGN3CPEOOLWU&issuer=SPDX&algorithm=SHA256&digits=7&period=20
|
||||
,,login,Test 3,,,,,,otpauth://totp/Airbnb:Elijah?secret=7ELGJSGXNCCTV3O6LKJWYFV2RA&issuer=Airbnb&algorithm=SHA512&digits=8&period=50
|
||||
,,login,Test 4,,,,,,steam://JRZCL47CMXVOQMNPZR2F7J4RGI
|
|
@ -0,0 +1,70 @@
|
||||
{
|
||||
"encrypted": false,
|
||||
"folders": [],
|
||||
"items": [
|
||||
{
|
||||
"id": "ffdea2cc-923d-47a0-be99-ae7800fd51e4",
|
||||
"organizationId": null,
|
||||
"folderId": null,
|
||||
"type": 1,
|
||||
"name": "Test 1",
|
||||
"notes": null,
|
||||
"favorite": false,
|
||||
"login": {
|
||||
"uris": null,
|
||||
"username": null,
|
||||
"password": null,
|
||||
"totp": "otpauth://totp/Deno:Mason?secret=4SJHB4GSD43FZBAI7C2HLRJGPQ&issuer=Deno&algorithm=SHA1&digits=6&period=30"
|
||||
},
|
||||
"collectionIds": null
|
||||
},
|
||||
{
|
||||
"id": "0f954b25-dbe9-4b38-b8ff-ae7800fd784d",
|
||||
"organizationId": null,
|
||||
"folderId": null,
|
||||
"type": 1,
|
||||
"name": "Test 2",
|
||||
"notes": null,
|
||||
"favorite": false,
|
||||
"login": {
|
||||
"uris": null,
|
||||
"username": null,
|
||||
"password": null,
|
||||
"totp": "otpauth://totp/SPDX:James?secret=5OM4WOOGPLQEF6UGN3CPEOOLWU&issuer=SPDX&algorithm=SHA256&digits=7&period=20"
|
||||
},
|
||||
"collectionIds": null
|
||||
},
|
||||
{
|
||||
"id": "b0f3aae6-f4da-49bc-b981-ae7800fd9467",
|
||||
"organizationId": null,
|
||||
"folderId": null,
|
||||
"type": 1,
|
||||
"name": "Test 3",
|
||||
"notes": null,
|
||||
"favorite": false,
|
||||
"login": {
|
||||
"uris": null,
|
||||
"username": null,
|
||||
"password": null,
|
||||
"totp": "otpauth://totp/Airbnb:Elijah?secret=7ELGJSGXNCCTV3O6LKJWYFV2RA&issuer=Airbnb&algorithm=SHA512&digits=8&period=50"
|
||||
},
|
||||
"collectionIds": null
|
||||
},
|
||||
{
|
||||
"id": "eb10632a-3bfd-40cd-9e4c-8c01c020b944",
|
||||
"organizationId": null,
|
||||
"folderId": null,
|
||||
"type": 1,
|
||||
"name": "Test Steam",
|
||||
"notes": null,
|
||||
"favorite": false,
|
||||
"login": {
|
||||
"uris": null,
|
||||
"username": null,
|
||||
"password": null,
|
||||
"totp": "steam://JRZCL47CMXVOQMNPZR2F7J4RGI"
|
||||
},
|
||||
"collectionIds": null
|
||||
}
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue