mirror of
https://github.com/beemdevelopment/Aegis.git
synced 2025-06-11 00:49:37 +00:00
Add support for importing plain text andOTP databases
This commit is contained in:
parent
fa607a7856
commit
7422b0cf53
4 changed files with 92 additions and 5 deletions
|
@ -155,7 +155,7 @@ public class KeyInfo implements Serializable {
|
||||||
if (!isTypeValid(type)) {
|
if (!isTypeValid(type)) {
|
||||||
throw new KeyInfoException(String.format("unsupported otp type: %s", type));
|
throw new KeyInfoException(String.format("unsupported otp type: %s", type));
|
||||||
}
|
}
|
||||||
_type = type.toLowerCase();
|
_type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSecret(char[] base32) throws KeyInfoException {
|
public void setSecret(char[] base32) throws KeyInfoException {
|
||||||
|
|
|
@ -35,10 +35,10 @@ public class AegisImporter extends DatabaseImporter {
|
||||||
public List<DatabaseEntry> convert() throws DatabaseImporterException {
|
public List<DatabaseEntry> convert() throws DatabaseImporterException {
|
||||||
try {
|
try {
|
||||||
JSONObject obj;
|
JSONObject obj;
|
||||||
if (!_file.isEncrypted()) {
|
if (_file.isEncrypted() && _key != null) {
|
||||||
obj = _file.getContent();
|
|
||||||
} else {
|
|
||||||
obj = _file.getContent(_key);
|
obj = _file.getContent(_key);
|
||||||
|
} else {
|
||||||
|
obj = _file.getContent();
|
||||||
}
|
}
|
||||||
|
|
||||||
Database db = new Database();
|
Database db = new Database();
|
||||||
|
|
|
@ -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";
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,7 +11,11 @@ import me.impy.aegis.util.ByteInputStream;
|
||||||
|
|
||||||
public abstract class DatabaseImporter {
|
public abstract class DatabaseImporter {
|
||||||
private static List<Class<? extends DatabaseImporter>> _converters = Collections.unmodifiableList(
|
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;
|
protected ByteInputStream _stream;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue