Improve code import, Fix UnlockActivity

This commit is contained in:
MrLetsplay 2023-09-18 21:34:56 +02:00
parent ffba642fa0
commit 7d8a38fad7
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
6 changed files with 40 additions and 25 deletions

View File

@ -50,19 +50,6 @@ public class MainActivity extends BaseActivity {
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
/*try {
byte[] salt = Crypto.generateSalt();
SecretKey key = Crypto.generateKey("HELLO", salt);
Log.i("UWUSECRET", key.toString());
KeyStore ks = KeyStore.getInstance("AndroidKeyStore");
ks.load(null);
ks.setEntry("", new KeyStore.SecretKeyEntry(key), null);
} catch (CryptoException | KeyStoreException | CertificateException | IOException |
NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}*/
//getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE); TODO: enable secure flag //getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE); TODO: enable secure flag
ThemeUtil.loadTheme(this); ThemeUtil.loadTheme(this);

View File

@ -145,12 +145,12 @@ public class GroupFragment extends NamedFragment {
}, null); }, null);
} }
public void addOTP(OTPData data) { public void addOTP(OTPData... data) {
OTPDatabase.promptLoadDatabase(requireActivity(), () -> { OTPDatabase.promptLoadDatabase(requireActivity(), () -> {
try { try {
OTPDatabase.getLoadedDatabase().addOTP(groupID, data); for(OTPData d : data) OTPDatabase.getLoadedDatabase().addOTP(groupID, d);
OTPDatabase.saveDatabase(requireContext(), SettingsUtil.getCryptoParameters(requireContext())); OTPDatabase.saveDatabase(requireContext(), SettingsUtil.getCryptoParameters(requireContext()));
otpListAdapter.add(data); for(OTPData d : data) otpListAdapter.add(d);
} catch (OTPDatabaseException | CryptoException e) { } catch (OTPDatabaseException | CryptoException e) {
DialogUtil.showErrorDialog(requireContext(), "Failed to save database: " + e); DialogUtil.showErrorDialog(requireContext(), "Failed to save database: " + e);
} }

View File

@ -117,13 +117,11 @@ public class SettingsFragment extends NamedFragment {
binding.settingsBiometricLock.setOnCheckedChangeListener((view, checked) -> { binding.settingsBiometricLock.setOnCheckedChangeListener((view, checked) -> {
if(checked) { if(checked) {
OTPDatabase.promptLoadDatabase(requireActivity(), () -> { OTPDatabase.promptLoadDatabase(requireActivity(), () -> {
Log.i("PROMPT", "§BERIIO");
BiometricUtil.promptBiometricAuth(requireActivity(), () -> { BiometricUtil.promptBiometricAuth(requireActivity(), () -> {
try { try {
BiometricKey biometricKey = Crypto.createBiometricKey(SettingsUtil.getCryptoParameters(requireContext())); BiometricKey biometricKey = Crypto.createBiometricKey(SettingsUtil.getCryptoParameters(requireContext()));
SettingsUtil.enableBiometricEncryption(requireContext(), biometricKey); SettingsUtil.enableBiometricEncryption(requireContext(), biometricKey);
} catch (CryptoException e) { } catch (CryptoException e) {
e.printStackTrace();
DialogUtil.showErrorDialog(requireContext(), "Failed to enable: " + e); DialogUtil.showErrorDialog(requireContext(), "Failed to enable: " + e);
} }
}, () -> view.setChecked(false)); }, () -> view.setChecked(false));

View File

@ -8,6 +8,7 @@ import android.media.Image;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.WindowManager; import android.view.WindowManager;
import android.widget.Toast; import android.widget.Toast;
@ -200,6 +201,7 @@ public class QRScannerActivity extends AppCompatActivity {
if(part.getBatchIndex() == part.getBatchSize() - 1) { if(part.getBatchIndex() == part.getBatchSize() - 1) {
success(currentCodes.toArray(new OTPData[0])); success(currentCodes.toArray(new OTPData[0]));
}else { }else {
lastPart = part;
process = true; process = true;
} }
} }

View File

@ -61,8 +61,7 @@ public class UnlockActivity extends AppCompatActivity {
setContentView(binding.getRoot()); setContentView(binding.getRoot());
if(SettingsUtil.isBiometricEncryption(this) && BiometricUtil.isSupported(this)) { if(SettingsUtil.isBiometricEncryption(this) && BiometricUtil.isSupported(this)) {
binding.unlockBiometrics.setOnClickListener(view -> BiometricUtil.promptBiometricAuth(this, this::success, () -> {})); Runnable onSuccess = () -> {
BiometricUtil.promptBiometricAuth(this, () -> {
BiometricKey biometricKey = SettingsUtil.getBiometricKey(this); BiometricKey biometricKey = SettingsUtil.getBiometricKey(this);
try { try {
SecretKey biometricSecretKey = Crypto.getBiometricKey(biometricKey); SecretKey biometricSecretKey = Crypto.getBiometricKey(biometricKey);
@ -73,7 +72,10 @@ public class UnlockActivity extends AppCompatActivity {
} catch (CryptoException | OTPDatabaseException e) { } catch (CryptoException | OTPDatabaseException e) {
DialogUtil.showErrorDialog(this, "Failed to load database: " + e); DialogUtil.showErrorDialog(this, "Failed to load database: " + e);
} }
}, () -> {}); };
binding.unlockBiometrics.setOnClickListener(view -> BiometricUtil.promptBiometricAuth(this, onSuccess, () -> {}));
BiometricUtil.promptBiometricAuth(this, onSuccess, () -> {});
} }
binding.unlockButton.setOnClickListener(view -> { binding.unlockButton.setOnClickListener(view -> {

View File

@ -41,6 +41,14 @@ public class OTPParser {
String name = params.getName(); String name = params.getName();
String issuer = params.getIssuer(); String issuer = params.getIssuer();
if(name.contains(":")) { // Name possibly contains issuer prefix
String[] spl = name.split(":");
if(spl.length == 2) { // Otherwise it's not a valid prefix, or too many ':'s, just use everything as the account name
if(issuer == null || issuer.isEmpty()) issuer = spl[0];
name = spl[1];
}
}
OTPType type; OTPType type;
switch(params.getType()) { switch(params.getType()) {
case OTP_TYPE_UNSPECIFIED: case OTP_TYPE_UNSPECIFIED:
@ -115,8 +123,28 @@ public class OTPParser {
} }
String type = uri.getHost(); String type = uri.getHost();
String accountName = uri.getPath(); String path = uri.getPath();
if(path == null || path.length() < 2) {
throw new IllegalArgumentException("Missing required parameters");
}
path = path.substring(1);
String issuer = uri.getQueryParameter("issuer"); String issuer = uri.getQueryParameter("issuer");
String accountName;
if(path.contains(":")) { // Possibly contains issuer prefix
String[] spl = path.split(":");
if(spl.length != 2) { // Either not a valid prefix, or too many ':'s, just use everything as the account name
accountName = path;
}else {
if(issuer == null || issuer.isEmpty()) issuer = spl[0];
accountName = spl[1];
}
}else {
accountName = path;
}
String secret = uri.getQueryParameter("secret"); String secret = uri.getQueryParameter("secret");
String algorithm = uri.getQueryParameter("algorithm"); String algorithm = uri.getQueryParameter("algorithm");
String digits = uri.getQueryParameter("digits"); String digits = uri.getQueryParameter("digits");
@ -139,12 +167,10 @@ public class OTPParser {
throw new IllegalArgumentException("Missing required parameters"); throw new IllegalArgumentException("Missing required parameters");
} }
if(accountName == null || accountName.length() < 2 /* Because path is /accName, so 2 letters for acc with 1 letter name */) { if(accountName.length() == 0 || (issuer != null && issuer.length() == 0)) {
throw new IllegalArgumentException("Missing required parameters"); throw new IllegalArgumentException("Missing required parameters");
} }
accountName = accountName.substring(1);
try { try {
// 0 or null for defaults (handled by Cringe-Authenticator-Library) // 0 or null for defaults (handled by Cringe-Authenticator-Library)
OTPAlgorithm fAlgorithm = algorithm == null ? null : OTPAlgorithm.valueOf(algorithm.toUpperCase()); OTPAlgorithm fAlgorithm = algorithm == null ? null : OTPAlgorithm.valueOf(algorithm.toUpperCase());