Skip to content

Commit

Permalink
adapt to core StringUtils (openhab#15784)
Browse files Browse the repository at this point in the history
Signed-off-by: Leo Siepel <[email protected]>
  • Loading branch information
lsiepel authored and d.kumar committed Oct 30, 2023
1 parent 0d214d4 commit 0e6dbd9
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.openhab.binding.mybmw.internal.utils.HTTPConstants;
import org.openhab.binding.mybmw.internal.utils.ImageProperties;
import org.openhab.core.io.net.http.HttpClientFactory;
import org.openhab.core.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -323,12 +324,12 @@ public synchronized boolean updateToken() {
/*
* Step 2) Calculate values for base parameters
*/
String verfifierBytes = Converter.getRandomString(64);
String verfifierBytes = StringUtils.getRandomAlphabetic(64).toLowerCase();
String codeVerifier = Base64.getUrlEncoder().withoutPadding().encodeToString(verfifierBytes.getBytes());
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(codeVerifier.getBytes(StandardCharsets.UTF_8));
String codeChallange = Base64.getUrlEncoder().withoutPadding().encodeToString(hash);
String stateBytes = Converter.getRandomString(16);
String stateBytes = StringUtils.getRandomAlphabetic(16).toLowerCase();
String state = Base64.getUrlEncoder().withoutPadding().encodeToString(stateBytes.getBytes());

MultiMap<String> baseParams = new MultiMap<String>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
package org.openhab.binding.mybmw.internal.utils;

import java.lang.reflect.Type;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.time.ZoneId;
Expand All @@ -24,7 +23,6 @@
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import java.util.TimeZone;

import org.eclipse.jdt.annotation.NonNullByDefault;
Expand Down Expand Up @@ -249,15 +247,6 @@ public static Vehicle getConsistentVehcile(Vehicle v) {
return v;
}

public static String getRandomString(int size) {
int leftLimit = 97; // letter 'a'
int rightLimit = 122; // letter 'z'
Random random = new SecureRandom();

return random.ints(leftLimit, rightLimit + 1).limit(size)
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append).toString();
}

public static State getLockState(boolean lock) {
if (lock) {
return StringType.valueOf(Constants.LOCKED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.openhab.binding.mybmw.internal.utils.Constants;
import org.openhab.binding.mybmw.internal.utils.Converter;
import org.openhab.core.io.net.http.HttpClientFactory;
import org.openhab.core.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -78,16 +79,14 @@ void testAuth() {
AuthQueryResponse aqr = Converter.getGson().fromJson(firstResponse.getContentAsString(),
AuthQueryResponse.class);

// String verifier_bytes = RandomStringUtils.randomAlphanumeric(64);
String verifierBytes = Converter.getRandomString(64);
String verifierBytes = StringUtils.getRandomAlphabetic(64).toLowerCase();
String codeVerifier = Base64.getUrlEncoder().withoutPadding().encodeToString(verifierBytes.getBytes());

MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(codeVerifier.getBytes(StandardCharsets.UTF_8));
String codeChallenge = Base64.getUrlEncoder().withoutPadding().encodeToString(hash);

// String state_bytes = RandomStringUtils.randomAlphanumeric(16);
String stateBytes = Converter.getRandomString(16);
String stateBytes = StringUtils.getRandomAlphabetic(16).toLowerCase();
String state = Base64.getUrlEncoder().withoutPadding().encodeToString(stateBytes.getBytes());

String authUrl = aqr.gcdmBaseUrl + BimmerConstants.OAUTH_ENDPOINT;
Expand Down

0 comments on commit 0e6dbd9

Please sign in to comment.