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;
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;
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/";
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;
}
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);