Skip to content

Commit

Permalink
adapt to core StringUtils (openhab#15785)
Browse files Browse the repository at this point in the history
Signed-off-by: Leo Siepel <leosiepel@gmail.com>
  • Loading branch information
lsiepel authored and querdenker2k committed Oct 21, 2023
1 parent efe48f6 commit 806ec90
Showing 1 changed file with 2 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -73,6 +71,7 @@
import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.types.Command;
import org.openhab.core.util.HexUtils;
import org.openhab.core.util.StringUtils;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -93,7 +92,6 @@ public class MillheatAccountHandler extends BaseBridgeHandler {
private static final int NUM_NONCE_CHARS = 16;
private static final String CONTENT_TYPE = "application/x-zc-object";
private static final String ALLOWED_NONCE_CHARACTERS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static final int ALLOWED_NONCE_CHARACTERS_LENGTH = ALLOWED_NONCE_CHARACTERS.length();
private static final String REQUEST_TIMEOUT = "300";
public static String authEndpoint = "https://eurouter.ablecloud.cn:9005/zc-account/v1/";
public static String serviceEndpoint = "https://eurouter.ablecloud.cn:9005/millService/v1/";
Expand All @@ -107,15 +105,6 @@ public class MillheatAccountHandler extends BaseBridgeHandler {
private @Nullable ScheduledFuture<?> statusFuture;
private @NonNullByDefault({}) MillheatAccountConfiguration config;

private static String getRandomString(final int sizeOfRandomString) {
final Random random = new SecureRandom();
final StringBuilder sb = new StringBuilder(sizeOfRandomString);
for (int i = 0; i < sizeOfRandomString; ++i) {
sb.append(ALLOWED_NONCE_CHARACTERS.charAt(random.nextInt(ALLOWED_NONCE_CHARACTERS_LENGTH)));
}
return sb.toString();
}

public MillheatAccountHandler(final Bridge bridge, final HttpClient httpClient, final BundleContext context) {
super(bridge);
this.httpClient = httpClient;
Expand Down Expand Up @@ -343,7 +332,7 @@ private void updateThingStatuses() {
}

private Request buildLoggedInRequest(final AbstractRequest req) throws NoSuchAlgorithmException {
final String nonce = getRandomString(NUM_NONCE_CHARS);
final String nonce = StringUtils.getRandomString(NUM_NONCE_CHARS, ALLOWED_NONCE_CHARACTERS);
final String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
final String signatureBasis = REQUEST_TIMEOUT + timestamp + nonce + token;
MessageDigest md = MessageDigest.getInstance(SHA_1_ALGORITHM);
Expand Down

0 comments on commit 806ec90

Please sign in to comment.