Add support for importing plain text andOTP databases

This commit is contained in:
Alexander Bakker 2018-05-13 19:42:59 +02:00
parent fa607a7856
commit 7422b0cf53
4 changed files with 92 additions and 5 deletions

View file

@ -155,7 +155,7 @@ public class KeyInfo implements Serializable {
if (!isTypeValid(type)) {
throw new KeyInfoException(String.format("unsupported otp type: %s", type));
}
_type = type.toLowerCase();
_type = type;
}
public void setSecret(char[] base32) throws KeyInfoException {

View file

@ -35,10 +35,10 @@ public class AegisImporter extends DatabaseImporter {
public List<DatabaseEntry> convert() throws DatabaseImporterException {
try {
JSONObject obj;
if (!_file.isEncrypted()) {
obj = _file.getContent();
} else {
if (_file.isEncrypted() && _key != null) {
obj = _file.getContent(_key);
} else {
obj = _file.getContent();
}
Database db = new Database();

View file

@ -0,0 +1,83 @@
package me.impy.aegis.importers;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import me.impy.aegis.crypto.KeyInfo;
import me.impy.aegis.crypto.KeyInfoException;
import me.impy.aegis.db.DatabaseEntry;
import me.impy.aegis.encoding.Base32;
import me.impy.aegis.encoding.Base32Exception;
import me.impy.aegis.util.ByteInputStream;
public class AndOTPImporter extends DatabaseImporter {
private JSONArray _obj;
public AndOTPImporter(ByteInputStream stream) {
super(stream);
}
@Override
public void parse() throws DatabaseImporterException {
try {
_obj = new JSONArray(new String(_stream.getBytes(), "UTF-8"));
} catch (JSONException e) {
throw new DatabaseImporterException(e);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
@Override
public List<DatabaseEntry> convert() throws DatabaseImporterException {
List<DatabaseEntry> entries = new ArrayList<>();
try {
for (int i = 0; i < _obj.length(); i++) {
JSONObject obj = _obj.getJSONObject(i);
KeyInfo key = new KeyInfo();
key.setAlgorithm(obj.getString("algorithm"));
key.setDigits(obj.getInt("digits"));
key.setPeriod(obj.getInt("period"));
key.setType(obj.getString("type"));
if (key.getType().equals("hotp")) {
key.setCounter(obj.getLong("counter"));
}
String[] parts = obj.getString("label").split(" - ");
if (parts.length > 1) {
key.setIssuer(parts[0]);
key.setAccountName(parts[1]);
} else {
key.setAccountName(parts[0]);
}
byte[] secret = Base32.decode(obj.getString("secret").toCharArray());
key.setSecret(secret);
DatabaseEntry entry = new DatabaseEntry(key);
entries.add(entry);
}
} catch (Base32Exception | KeyInfoException | JSONException e) {
throw new DatabaseImporterException(e);
}
return entries;
}
@Override
public boolean isEncrypted() {
return false;
}
@Override
public String getName() {
return "andOTP";
}
}

View file

@ -11,7 +11,11 @@ import me.impy.aegis.util.ByteInputStream;
public abstract class DatabaseImporter {
private static List<Class<? extends DatabaseImporter>> _converters = Collections.unmodifiableList(
new ArrayList<>(Arrays.asList(AegisImporter.class, FreeOTPImporter.class))
new ArrayList<>(Arrays.asList(
AegisImporter.class,
FreeOTPImporter.class,
AndOTPImporter.class
))
);
protected ByteInputStream _stream;