|
|
|
@ -26,11 +26,13 @@ import java.nio.file.DirectoryStream; |
|
|
|
import java.nio.file.Files; |
|
|
|
import java.nio.file.Path; |
|
|
|
import java.nio.file.Paths; |
|
|
|
import java.time.Clock; |
|
|
|
import java.util.Base64; |
|
|
|
import java.util.Iterator; |
|
|
|
|
|
|
|
@Slf4j |
|
|
|
public final class AzureIotHubUtil { |
|
|
|
|
|
|
|
private static final String BASE_DIR_PATH = System.getProperty("user.dir"); |
|
|
|
private static final String APP_DIR = "application"; |
|
|
|
private static final String SRC_DIR = "src"; |
|
|
|
@ -52,41 +54,37 @@ public final class AzureIotHubUtil { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private static final long SAS_TOKEN_VALID_SECS = 365 * 24 * 60 * 60; |
|
|
|
private static final long ONE_SECOND_IN_MILLISECONDS = 1000; |
|
|
|
private static final long SAS_TOKEN_VALID_SECS = 365 * 24 * 60 * 60; // one year
|
|
|
|
|
|
|
|
private static final String SAS_TOKEN_FORMAT = "SharedAccessSignature sr=%s&sig=%s&se=%s"; |
|
|
|
|
|
|
|
private static final String USERNAME_FORMAT = "%s/%s/?api-version=2018-06-30"; |
|
|
|
|
|
|
|
private AzureIotHubUtil() { |
|
|
|
} |
|
|
|
private AzureIotHubUtil() {} |
|
|
|
|
|
|
|
public static String buildUsername(String host, String deviceId) { |
|
|
|
return String.format(USERNAME_FORMAT, host, deviceId); |
|
|
|
} |
|
|
|
|
|
|
|
public static String buildSasToken(String host, String sasKey) { |
|
|
|
public static String buildSasToken(String host, String sasKey, Clock clock) { |
|
|
|
try { |
|
|
|
final String targetUri = URLEncoder.encode(host.toLowerCase(), "UTF-8"); |
|
|
|
final long expiryTime = buildExpiresOn(); |
|
|
|
final String targetUri = URLEncoder.encode(host.toLowerCase(), StandardCharsets.UTF_8); |
|
|
|
final long expiryTime = buildExpiresOn(clock); |
|
|
|
String toSign = targetUri + "\n" + expiryTime; |
|
|
|
byte[] keyBytes = Base64.getDecoder().decode(sasKey.getBytes(StandardCharsets.UTF_8)); |
|
|
|
SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA256"); |
|
|
|
Mac mac = Mac.getInstance("HmacSHA256"); |
|
|
|
mac.init(signingKey); |
|
|
|
byte[] rawHmac = mac.doFinal(toSign.getBytes(StandardCharsets.UTF_8)); |
|
|
|
String signature = URLEncoder.encode(Base64.getEncoder().encodeToString(rawHmac), "UTF-8"); |
|
|
|
String signature = URLEncoder.encode(Base64.getEncoder().encodeToString(rawHmac), StandardCharsets.UTF_8); |
|
|
|
return String.format(SAS_TOKEN_FORMAT, targetUri, signature, expiryTime); |
|
|
|
} catch (Exception e) { |
|
|
|
throw new RuntimeException("Failed to build SAS token!!!", e); |
|
|
|
throw new RuntimeException("Failed to build SAS token!", e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private static long buildExpiresOn() { |
|
|
|
long expiresOnDate = System.currentTimeMillis(); |
|
|
|
expiresOnDate += SAS_TOKEN_VALID_SECS * ONE_SECOND_IN_MILLISECONDS; |
|
|
|
return expiresOnDate / ONE_SECOND_IN_MILLISECONDS; |
|
|
|
private static long buildExpiresOn(Clock clock) { |
|
|
|
return clock.instant().plusSeconds(SAS_TOKEN_VALID_SECS).getEpochSecond(); |
|
|
|
} |
|
|
|
|
|
|
|
public static String getDefaultCaCert() { |
|
|
|
|