Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# HAP-Java 2.0.7
* Add overloads to characteristics so that the username can be passed through.

# HAP-Java 2.0.6
* Several methods allowing library users to manipulate characteristics themselves
* Allow library users to provider their own implementation of AccessoryInformationService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,14 @@ public interface Characteristic {
* @param jsonValue the JSON serialized value to set.
*/
void setValue(JsonValue jsonValue);

/**
* Invoked by the remote client, this updates the current value of the characteristic.
*
* @param jsonValue the JSON serialized value to set.
* @param username the authenticated username making the request
*/
default void setValue(JsonValue jsonValue, String username) {
setValue(jsonValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

public interface ExceptionalConsumer<T> {
void accept(T t) throws Exception;

default void accept(T t, String username) throws Exception {
accept(t);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,20 @@ protected CompletableFuture<JsonObjectBuilder> makeBuilder(int instanceId) {
/** {@inheritDoc} */
@Override
public final void setValue(JsonValue jsonValue) {
setValue(jsonValue, null);
}

/** {@inheritDoc} */
@Override
public final void setValue(JsonValue jsonValue, String username) {
try {
setValue(convert(jsonValue));
setValue(convert(jsonValue), username);
} catch (Exception e) {
logger.warn(
"Error while setting JSON value {} for characteristic {}",
"Error while setting JSON value {} for characteristic {} from user {}",
jsonValue,
getClass().getName(),
username,
e);
}
}
Expand Down Expand Up @@ -185,6 +192,17 @@ public void unsubscribe() {
*/
public abstract void setValue(T value) throws Exception;

/**
* Update the characteristic value using a new value supplied by the connected client.
*
* @param value the new value to set.
* @param username the authenticated username making the request
* @throws Exception if the value cannot be set.
*/
public void setValue(T value, String username) throws Exception {
setValue(value);
}

/**
* Retrieves the current value of the characteristic.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ public CompletableFuture<Boolean> getValue() {

@Override
public void setValue(Boolean value) throws Exception {
if (setter.isPresent()) setter.get().accept(value);
setValue(value, null);
}

@Override
public void setValue(Boolean value, String username) throws Exception {
if (setter.isPresent()) setter.get().accept(value, username);
}

/** {@inheritDoc} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,24 @@ public CompletableFuture<Integer> getValue() {
}

public void setValue(T value) throws Exception {
setValue(value, null);
}

public void setValue(T value, String username) throws Exception {
if (!setter.isPresent()) {
return;
}

setter.get().accept(value);
setter.get().accept(value, username);
}

@Override
public void setValue(Integer value) throws Exception {
setValue(value, null);
}

@Override
public void setValue(Integer value, String username) throws Exception {
if (!setter.isPresent()) {
return;
}
Expand All @@ -130,7 +139,7 @@ public void setValue(Integer value) throws Exception {
if (validValues != null && value != null) {
for (T valid : validValues) {
if (valid.getCode() == value) {
setValue(valid);
setValue(valid, username);
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,12 @@ public final CompletableFuture<Double> getValue() {

@Override
public void setValue(Double value) throws Exception {
if (setter.isPresent()) setter.get().accept(value);
setValue(value, null);
}

@Override
public void setValue(Double value, String username) throws Exception {
if (setter.isPresent()) setter.get().accept(value, username);
}

/** {@inheritDoc} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ public CompletableFuture<Integer> getValue() {

@Override
public void setValue(Integer value) throws Exception {
if (setter.isPresent()) setter.get().accept(value);
setValue(value, null);
}

@Override
public void setValue(Integer value, String username) throws Exception {
if (setter.isPresent()) setter.get().accept(value, username);
}

/** {@inheritDoc} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ public String convert(JsonValue jsonValue) {
/** {@inheritDoc} */
@Override
public void setValue(String value) throws Exception {
if (setter.isPresent()) setter.get().accept(value);
setValue(value, null);
}

/** {@inheritDoc} */
@Override
public void setValue(String value, String username) throws Exception {
if (setter.isPresent()) setter.get().accept(value, username);
}

/** {@inheritDoc} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,15 @@ public HomekitStandaloneAccessoryServer createStandaloneAccessory(
return new HomekitStandaloneAccessoryServer(accessory, http, localAddress, authInfo);
}
}

public HomekitStandaloneAccessoryServer createStandaloneAccessory(
HomekitAuthInfo authInfo, HomekitAccessory accessory, int category)
throws IOException, ExecutionException, InterruptedException {
if (jmdns != null) {
return new HomekitStandaloneAccessoryServer(accessory, http, jmdns, authInfo, category);
} else {
return new HomekitStandaloneAccessoryServer(accessory, http, localAddress, authInfo, category);
return new HomekitStandaloneAccessoryServer(
accessory, http, localAddress, authInfo, category);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class HomekitStandaloneAccessoryServer {
root = new HomekitRoot(accessory.getName().get(), webHandler, jmdns, authInfo);
root.addAccessory(accessory);
}

HomekitStandaloneAccessoryServer(
HomekitAccessory accessory,
HomekitWebHandler webHandler,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class ConnectionImpl implements HomekitClientConnection {
private int outboundBinaryMessageCount = 0;
private byte[] readKey;
private byte[] writeKey;
private String username;
private boolean isUpgraded = false;
private final Consumer<HttpResponse> outOfBandMessageCallback;
private final SubscriptionManager subscriptions;
Expand Down Expand Up @@ -58,6 +59,7 @@ private HttpResponse doHandleRequest(HttpRequest request) throws IOException {
isUpgraded = true;
readKey = ((UpgradeResponse) response).getReadKey().array();
writeKey = ((UpgradeResponse) response).getWriteKey().array();
username = ((UpgradeResponse) response).getUsername();
}
LOGGER.trace("{} {} {}", response.getStatusCode(), request.getMethod(), request.getUri());
return response;
Expand Down Expand Up @@ -145,4 +147,9 @@ public void close() {
public void outOfBand(HttpResponse message) {
outOfBandMessageCallback.accept(message);
}

@Override
public String getUsername() {
return username;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ public interface HomekitClientConnection {
void close();

void outOfBand(HttpResponse message);

default String getUsername() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public HttpResponse put(HttpRequest request, HomekitClientConnection connection)
}

if (jsonCharacteristic.containsKey("value")) {
characteristic.setValue(jsonCharacteristic.get("value"));
characteristic.setValue(jsonCharacteristic.get("value"), connection.getUsername());
}
if (jsonCharacteristic.containsKey("ev")
&& characteristic instanceof EventableCharacteristic) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,10 @@ private HttpResponse handleVerifyFinishRequest(VerifyFinishRequest request) thro

byte[] material = ByteUtils.joinBytes(clientPublicKey, clientUsername, publicKey);

byte[] clientLtpk =
authInfo.getUserPublicKey(
authInfo.getMac() + new String(clientUsername, StandardCharsets.UTF_8));
String username = new String(clientUsername, StandardCharsets.UTF_8);
byte[] clientLtpk = authInfo.getUserPublicKey(authInfo.getMac() + username);
if (clientLtpk == null) {
logger.warn("Unknown user: {}", new String(clientUsername, StandardCharsets.UTF_8));
logger.warn("Unknown user: {}", username);
return new PairingResponse(4, ErrorCode.AUTHENTICATION);
}

Expand All @@ -120,7 +119,8 @@ private HttpResponse handleVerifyFinishRequest(VerifyFinishRequest request) thro
return new UpgradeResponse(
encoder.toByteArray(),
createKey("Control-Write-Encryption-Key"),
createKey("Control-Read-Encryption-Key"));
createKey("Control-Read-Encryption-Key"),
username);
} else {
logger.warn("Invalid signature. Could not pair " + registry.getLabel());
return new PairingResponse(4, ErrorCode.AUTHENTICATION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ public class UpgradeResponse extends PairingResponse {

private final byte[] readKey;
private final byte[] writeKey;
private final String username;

UpgradeResponse(byte[] body, byte[] readKey, byte[] writeKey) {
UpgradeResponse(byte[] body, byte[] readKey, byte[] writeKey, String username) {
super(body);
this.readKey = readKey;
this.writeKey = writeKey;
this.username = username;
}

@Override
Expand All @@ -25,4 +27,8 @@ public ByteBuffer getReadKey() {
public ByteBuffer getWriteKey() {
return ByteBuffer.wrap(writeKey);
}

public String getUsername() {
return username;
}
}