[homekit] add setting to block homekit user/pairing deletion (#11731)

* add setting to block homekit user deletion and unpairing
* add logging
* remove . from settings label

Signed-off-by: Eugen Freiter <freiter@gmx.de>
This commit is contained in:
eugen
2021-12-08 12:14:44 +01:00
committed by GitHub
parent 8a8384e4e6
commit 1d65e10bba
5 changed files with 31 additions and 11 deletions

View File

@@ -46,22 +46,24 @@ public class HomekitAuthInfoImpl implements HomekitAuthInfo {
private byte[] privateKey;
private String pin;
private String setupId;
private boolean blockUserDeletion;
public HomekitAuthInfoImpl(Storage<String> storage, String pin, String setupId)
public HomekitAuthInfoImpl(Storage<String> storage, String pin, String setupId, boolean blockUserDeletion)
throws InvalidAlgorithmParameterException {
this.storage = storage;
this.pin = pin;
this.setupId = setupId;
this.blockUserDeletion = blockUserDeletion;
initializeStorage();
}
@Override
public void createUser(String username, byte[] publicKey) {
logger.trace("Create user {}", username);
logger.trace("create user {}", username);
final String userKey = createUserKey(username);
final String encodedPublicKey = Base64.getEncoder().encodeToString(publicKey);
storage.put(userKey, encodedPublicKey);
logger.trace("Stored user key {} with value {}", userKey, encodedPublicKey);
logger.trace("stored user key {} with value {}", userKey, encodedPublicKey);
}
@Override
@@ -113,8 +115,12 @@ public class HomekitAuthInfoImpl implements HomekitAuthInfo {
@Override
public void removeUser(String username) {
logger.trace("Remove user {}", username);
storage.remove(createUserKey(username));
logger.trace("remove user {}", username);
if (!this.blockUserDeletion) {
storage.remove(createUserKey(username));
} else {
logger.debug("deletion of the user was blocked by binding settings");
}
}
@Override
@@ -124,11 +130,15 @@ public class HomekitAuthInfoImpl implements HomekitAuthInfo {
}
public void clear() {
logger.trace("Clear all users");
for (String key : new HashSet<>(storage.getKeys())) {
if (isUserKey(key)) {
storage.remove(key);
logger.trace("clear all users");
if (!this.blockUserDeletion) {
for (String key : new HashSet<>(storage.getKeys())) {
if (isUserKey(key)) {
storage.remove(key);
}
}
} else {
logger.debug("deletion of users information was blocked by binding settings");
}
}
@@ -146,7 +156,7 @@ public class HomekitAuthInfoImpl implements HomekitAuthInfo {
final @Nullable Object privateKeyConfig = storage.get(STORAGE_PRIVATE_KEY);
if (mac == null) {
logger.warn(
"Could not find existing MAC in {}. Generating new MAC. This will require re-pairing of iOS devices.",
"could not find existing MAC in {}. Generating new MAC. This will require re-pairing of iOS devices.",
storage.getClass().getName());
mac = HomekitServer.generateMac();
storage.put(STORAGE_MAC, mac);

View File

@@ -94,7 +94,7 @@ public class HomekitImpl implements Homekit, NetworkAddressChangeListener {
this.changeListener = new HomekitChangeListener(itemRegistry, settings, metadataRegistry, storageService);
try {
authInfo = new HomekitAuthInfoImpl(storageService.getStorage(HomekitAuthInfoImpl.STORAGE_KEY), settings.pin,
settings.setupId);
settings.setupId, settings.blockUserDeletion);
startHomekitServer();
} catch (IOException | InvalidAlgorithmParameterException e) {
logger.warn("cannot activate HomeKit binding. {}", e.getMessage());

View File

@@ -32,6 +32,7 @@ public class HomekitSettings {
public int startDelay = 30;
public boolean useFahrenheitTemperature = false;
public boolean useOHmDNS = false;
public boolean blockUserDeletion = false;
public String thermostatTargetModeHeat = "HeatOn";
public String thermostatTargetModeCool = "CoolOn";
public String thermostatTargetModeAuto = "Auto";
@@ -81,6 +82,8 @@ public class HomekitSettings {
}
} else if (!useOHmDNS != other.useOHmDNS) {
return false;
} else if (!blockUserDeletion != other.blockUserDeletion) {
return false;
} else if (!pin.equals(other.pin)) {
return false;
} else if (!setupId.equals(other.setupId)) {

View File

@@ -116,5 +116,10 @@
<description>Defines whether mDNS service of openHAB or a separate instance of mDNS should be used.</description>
<default>false</default>
</parameter>
<parameter name="blockUserDeletion" type="boolean" required="false" groupName="network">
<label>Block deletion of the HomeKit user</label>
<description>Block deletion of the HomeKit user information from openHAB and the unpairing of devices</description>
<default>false</default>
</parameter>
</config-description>
</config-description:config-descriptions>