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() { private void initializeForm() {
KeyInfo info = _keyProfile.getEntry().getInfo(); KeyInfo info = _keyProfile.getEntry().getInfo();
_profileName.setText(info.getAccountName()); _profileName.setText(info.getAccountName());
_textAlgorithm.setText(info.getAlgorithm()); _textAlgorithm.setText(info.getAlgorithm(false));
_textIssuer.setText(info.getIssuer()); _textIssuer.setText(info.getIssuer());
_textPeriod.setText(info.getPeriod() + " seconds"); _textPeriod.setText(info.getPeriod() + " seconds");

View file

@ -2,6 +2,7 @@ package me.impy.aegis;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.ArrayRes;
import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.text.Editable; import android.text.Editable;
@ -65,18 +66,24 @@ public class EditProfileActivity extends AegisActivity {
_textSecret.setText(Base32.encodeOriginal(entry.getInfo().getSecret())); _textSecret.setText(Base32.encodeOriginal(entry.getInfo().getSecret()));
_textSecret.addTextChangedListener(watcher); _textSecret.addTextChangedListener(watcher);
String type = entry.getInfo().getType();
_spinnerType = findViewById(R.id.spinner_type); _spinnerType = findViewById(R.id.spinner_type);
SpinnerHelper.fillSpinner(this, _spinnerType, R.array.otp_types_array); SpinnerHelper.fillSpinner(this, _spinnerType, R.array.otp_types_array);
_spinnerType.setSelection(getStringResourceIndex(R.array.otp_types_array, type), false);
_spinnerType.setOnTouchListener(_selectedListener); _spinnerType.setOnTouchListener(_selectedListener);
_spinnerType.setOnItemSelectedListener(_selectedListener); _spinnerType.setOnItemSelectedListener(_selectedListener);
String algo = entry.getInfo().getAlgorithm(false);
_spinnerAlgo = findViewById(R.id.spinner_algo); _spinnerAlgo = findViewById(R.id.spinner_algo);
SpinnerHelper.fillSpinner(this, _spinnerAlgo, R.array.otp_algo_array); SpinnerHelper.fillSpinner(this, _spinnerAlgo, R.array.otp_algo_array);
_spinnerAlgo.setSelection(getStringResourceIndex(R.array.otp_algo_array, algo), false);
_spinnerAlgo.setOnTouchListener(_selectedListener); _spinnerAlgo.setOnTouchListener(_selectedListener);
_spinnerAlgo.setOnItemSelectedListener(_selectedListener); _spinnerAlgo.setOnItemSelectedListener(_selectedListener);
String digits = Integer.toString(entry.getInfo().getDigits());
_spinnerDigits = findViewById(R.id.spinner_digits); _spinnerDigits = findViewById(R.id.spinner_digits);
SpinnerHelper.fillSpinner(this, _spinnerDigits, R.array.otp_digits_array); SpinnerHelper.fillSpinner(this, _spinnerDigits, R.array.otp_digits_array);
_spinnerDigits.setSelection(getStringResourceIndex(R.array.otp_digits_array, digits), false);
_spinnerDigits.setOnTouchListener(_selectedListener); _spinnerDigits.setOnTouchListener(_selectedListener);
_spinnerDigits.setOnItemSelectedListener(_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.scheme("otpauth");
builder.authority(_type); builder.authority(_type);
builder.appendQueryParameter("digits", Integer.toString(_digits));
builder.appendQueryParameter("period", Integer.toString(_period)); builder.appendQueryParameter("period", Integer.toString(_period));
builder.appendQueryParameter("algorithm", _algorithm); builder.appendQueryParameter("algorithm", _algorithm);
builder.appendQueryParameter("secret", Base32.encodeOriginal(_secret)); builder.appendQueryParameter("secret", Base32.encodeOriginal(_secret));
@ -125,9 +126,12 @@ public class KeyInfo implements Serializable {
public String getIssuer() { public String getIssuer() {
return _issuer; return _issuer;
} }
public String getAlgorithm() { public String getAlgorithm(boolean java) {
if (java) {
return "Hmac" + _algorithm; return "Hmac" + _algorithm;
} }
return _algorithm;
}
public int getDigits() { public int getDigits() {
return _digits; return _digits;
} }

View file

@ -15,7 +15,7 @@ public class OTP {
switch (info.getType()) { switch (info.getType()) {
case "totp": case "totp":
String time = Long.toHexString(System.currentTimeMillis() / 1000 / info.getPeriod()); 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; break;
case "hotp": case "hotp":
otp = HOTP.generateOTP(info.getSecret(), info.getCounter(), info.getDigits(), false, -1); 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 { 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")); JSONObject obj = new JSONObject(new String(data, "UTF-8"));
// TODO: support different VERSION deserialization providers // TODO: support different VERSION deserialization providers
@ -39,7 +43,15 @@ public class Database {
for (int i = 0; i < array.length(); i++) { for (int i = 0; i < array.length(); i++) {
DatabaseEntry entry = new DatabaseEntry(null); DatabaseEntry entry = new DatabaseEntry(null);
entry.deserialize(array.getJSONObject(i)); entry.deserialize(array.getJSONObject(i));
// 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); addKey(entry);
} else {
_entries.add(entry);
}
} }
} }

View file

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