Fix a couple of bugs in the new profile edit activity

This commit is contained in:
Alexander Bakker 2017-12-27 23:01:23 +01:00
parent 418f5aed29
commit db54d38c14
6 changed files with 39 additions and 6 deletions

View file

@ -67,7 +67,7 @@ public class AddProfileActivity extends AegisActivity {
private void initializeForm() {
KeyInfo info = _keyProfile.getEntry().getInfo();
_profileName.setText(info.getAccountName());
_textAlgorithm.setText(info.getAlgorithm());
_textAlgorithm.setText(info.getAlgorithm(false));
_textIssuer.setText(info.getIssuer());
_textPeriod.setText(info.getPeriod() + " seconds");

View file

@ -2,6 +2,7 @@ package me.impy.aegis;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.ArrayRes;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
@ -65,18 +66,24 @@ public class EditProfileActivity extends AegisActivity {
_textSecret.setText(Base32.encodeOriginal(entry.getInfo().getSecret()));
_textSecret.addTextChangedListener(watcher);
String type = entry.getInfo().getType();
_spinnerType = findViewById(R.id.spinner_type);
SpinnerHelper.fillSpinner(this, _spinnerType, R.array.otp_types_array);
_spinnerType.setSelection(getStringResourceIndex(R.array.otp_types_array, type), false);
_spinnerType.setOnTouchListener(_selectedListener);
_spinnerType.setOnItemSelectedListener(_selectedListener);
String algo = entry.getInfo().getAlgorithm(false);
_spinnerAlgo = findViewById(R.id.spinner_algo);
SpinnerHelper.fillSpinner(this, _spinnerAlgo, R.array.otp_algo_array);
_spinnerAlgo.setSelection(getStringResourceIndex(R.array.otp_algo_array, algo), false);
_spinnerAlgo.setOnTouchListener(_selectedListener);
_spinnerAlgo.setOnItemSelectedListener(_selectedListener);
String digits = Integer.toString(entry.getInfo().getDigits());
_spinnerDigits = findViewById(R.id.spinner_digits);
SpinnerHelper.fillSpinner(this, _spinnerDigits, R.array.otp_digits_array);
_spinnerDigits.setSelection(getStringResourceIndex(R.array.otp_digits_array, digits), false);
_spinnerDigits.setOnTouchListener(_selectedListener);
_spinnerDigits.setOnItemSelectedListener(_selectedListener);
}
@ -217,4 +224,14 @@ public class EditProfileActivity extends AegisActivity {
}
}
private int getStringResourceIndex(@ArrayRes int id, String string) {
String[] res = getResources().getStringArray(id);
for (int i = 0; i < res.length; i++) {
if (res[i].equals(string)) {
return i;
}
}
return -1;
}
}

View file

@ -21,6 +21,7 @@ public class KeyInfo implements Serializable {
builder.scheme("otpauth");
builder.authority(_type);
builder.appendQueryParameter("digits", Integer.toString(_digits));
builder.appendQueryParameter("period", Integer.toString(_period));
builder.appendQueryParameter("algorithm", _algorithm);
builder.appendQueryParameter("secret", Base32.encodeOriginal(_secret));
@ -125,8 +126,11 @@ public class KeyInfo implements Serializable {
public String getIssuer() {
return _issuer;
}
public String getAlgorithm() {
return "Hmac" + _algorithm;
public String getAlgorithm(boolean java) {
if (java) {
return "Hmac" + _algorithm;
}
return _algorithm;
}
public int getDigits() {
return _digits;

View file

@ -15,7 +15,7 @@ public class OTP {
switch (info.getType()) {
case "totp":
String time = Long.toHexString(System.currentTimeMillis() / 1000 / info.getPeriod());
otp = TOTP.generateTOTP(info.getSecret(), time, info.getDigits(), info.getAlgorithm());
otp = TOTP.generateTOTP(info.getSecret(), time, info.getDigits(), info.getAlgorithm(true));
break;
case "hotp":
otp = HOTP.generateOTP(info.getSecret(), info.getCounter(), info.getDigits(), false, -1);

View file

@ -27,6 +27,10 @@ public class Database {
}
public void deserialize(byte[] data) throws Exception {
deserialize(data, true);
}
public void deserialize(byte[] data, boolean incCount) throws Exception {
JSONObject obj = new JSONObject(new String(data, "UTF-8"));
// TODO: support different VERSION deserialization providers
@ -39,7 +43,15 @@ public class Database {
for (int i = 0; i < array.length(); i++) {
DatabaseEntry entry = new DatabaseEntry(null);
entry.deserialize(array.getJSONObject(i));
addKey(entry);
// if incCount is false, don't increment the counter and don't set an ID
// this is used by the database importer to prevent an exception down the line
// TODO: find a better solution for this ugly hack
if (incCount) {
addKey(entry);
} else {
_entries.add(entry);
}
}
}

View file

@ -16,7 +16,7 @@ public class AegisImporter extends DatabaseImporter {
public List<DatabaseEntry> convert() throws Exception {
byte[] bytes = _stream.getBytes();
Database db = new Database();
db.deserialize(bytes);
db.deserialize(bytes, false);
return db.getKeys();
}