Delegate auth key validation to coordinator

This commit is contained in:
José Rebelo 2023-12-02 10:59:34 +00:00
parent 216dc93986
commit ba0ca1de75
8 changed files with 37 additions and 3 deletions

View File

@ -607,7 +607,7 @@ public class DiscoveryActivityV2 extends AbstractGBActivity implements AdapterVi
if (authKey == null || authKey.isEmpty()) {
toast(DiscoveryActivityV2.this, getString(R.string.discovery_need_to_enter_authkey), Toast.LENGTH_LONG, GB.WARN);
return;
} else if (authKey.getBytes().length < 34 || !authKey.startsWith("0x")) {
} else if (!coordinator.validateAuthKey(authKey)) {
toast(DiscoveryActivityV2.this, getString(R.string.discovery_entered_invalid_authkey), Toast.LENGTH_LONG, GB.WARN);
return;
}

View File

@ -607,27 +607,38 @@ public abstract class AbstractDeviceCoordinator implements DeviceCoordinator {
return R.drawable.ic_device_default_disabled;
}
@Override
public boolean supportsNotificationVibrationPatterns() {
return false;
}
@Override
public boolean supportsNotificationVibrationRepetitionPatterns() {
return false;
}
@Override
public boolean supportsNotificationLedPatterns() {
return false;
}
@Override
public AbstractNotificationPattern[] getNotificationVibrationPatterns() {
return new AbstractNotificationPattern[0];
}
@Override
public AbstractNotificationPattern[] getNotificationVibrationRepetitionPatterns() {
return new AbstractNotificationPattern[0];
}
@Override
public AbstractNotificationPattern[] getNotificationLedPatterns() {
return new AbstractNotificationPattern[0];
}
@Override
public boolean validateAuthKey(final String authKey) {
return !(authKey.getBytes().length < 34 || !authKey.startsWith("0x"));
}
}

View File

@ -590,4 +590,6 @@ public interface DeviceCoordinator {
* What LED patterns for notifications are supported by the device.
*/
AbstractNotificationPattern[] getNotificationLedPatterns();
boolean validateAuthKey(String authKey);
}

View File

@ -581,4 +581,10 @@ public abstract class Huami2021Coordinator extends HuamiCoordinator {
public static boolean experimentalFeatures(final GBDevice device) {
return getPrefs(device).getBoolean("zepp_os_experimental_features", false);
}
@Override
public boolean validateAuthKey(final String authKey) {
final byte[] authKeyBytes = authKey.trim().getBytes();
return authKeyBytes.length == 32 || (authKey.trim().startsWith("0x") && authKeyBytes.length == 34);
}
}

View File

@ -41,4 +41,10 @@ public abstract class XiaomiEncryptedCoordinator extends XiaomiCoordinator {
public Class<? extends DeviceSupport> getDeviceSupportClass() {
return XiaomiEncryptedSupport.class;
}
@Override
public boolean validateAuthKey(final String authKey) {
final byte[] authKeyBytes = authKey.trim().getBytes();
return authKeyBytes.length == 32 || (authKey.startsWith("0x") && authKeyBytes.length == 34);
}
}

View File

@ -23,11 +23,15 @@ import androidx.annotation.NonNull;
import java.util.Collection;
import java.util.Collections;
import java.util.regex.Pattern;
import nodomain.freeyourgadget.gadgetbridge.service.DeviceSupport;
import nodomain.freeyourgadget.gadgetbridge.service.devices.xiaomi.XiaomiPlaintextSupport;
public abstract class XiaomiPlaintextCoordinator extends XiaomiCoordinator {
// user id is used as auth key - numeric
private static final Pattern AUTH_KEY_PATTERN = Pattern.compile("^[0-9]+$");
@NonNull
@Override
public Collection<? extends ScanFilter> createBLEScanFilters() {
@ -41,4 +45,9 @@ public abstract class XiaomiPlaintextCoordinator extends XiaomiCoordinator {
public Class<? extends DeviceSupport> getDeviceSupportClass() {
return XiaomiPlaintextSupport.class;
}
@Override
public boolean validateAuthKey(final String authKey) {
return AUTH_KEY_PATTERN.matcher(authKey.trim()).matches();
}
}

View File

@ -95,7 +95,7 @@ public class InitOperation extends AbstractBTLEOperation<HuamiSupport> {
String authKey = sharedPrefs.getString("authkey", null);
if (authKey != null && !authKey.isEmpty()) {
byte[] srcBytes = authKey.trim().getBytes();
if (authKey.length() == 34 && authKey.substring(0, 2).equals("0x")) {
if (authKey.length() == 34 && authKey.startsWith("0x")) {
srcBytes = GB.hexStringToByteArray(authKey.substring(2));
}
System.arraycopy(srcBytes, 0, authKeyBytes, 0, Math.min(srcBytes.length, 16));

View File

@ -286,7 +286,7 @@ public class XiaomiAuthService extends AbstractXiaomiService {
final SharedPreferences sharedPrefs = GBApplication.getDeviceSpecificSharedPrefs(device.getAddress());
final String authKey = sharedPrefs.getString("authkey", null);
final String authKey = sharedPrefs.getString("authkey", "").trim();
if (StringUtils.isNotBlank(authKey)) {
final byte[] srcBytes;
// Allow both with and without 0x, to avoid user mistakes