mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2025-01-25 08:05:55 +01:00
Delegate auth key validation to coordinator
This commit is contained in:
parent
216dc93986
commit
ba0ca1de75
@ -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;
|
||||
}
|
||||
|
@ -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"));
|
||||
}
|
||||
}
|
||||
|
@ -590,4 +590,6 @@ public interface DeviceCoordinator {
|
||||
* What LED patterns for notifications are supported by the device.
|
||||
*/
|
||||
AbstractNotificationPattern[] getNotificationLedPatterns();
|
||||
|
||||
boolean validateAuthKey(String authKey);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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));
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user