use of com.google.api.client.util.Base64.encodeBase64String in project azure-iot-sdk-java by Azure.
the class ProvisioningSasToken method buildToken.
/**
* Helper function to build the token string
*
* @return Valid token string
*/
private String buildToken() {
String targetUri;
try {
// Codes_SRS_PROVISIONING_SERVICE_SASTOKEN_12_002: [The constructor shall create a target uri from the url encoded host name)]
targetUri = URLEncoder.encode(this.resourceUri.toLowerCase(), StandardCharsets.UTF_8.name());
// Codes_SRS_PROVISIONING_SERVICE_SASTOKEN_12_003: [The constructor shall create a string to sign by concatenating the target uri and the expiry time string (one year)]
String toSign = targetUri + "\n" + this.expiryTime;
// Codes_SRS_PROVISIONING_SERVICE_SASTOKEN_12_004: [The constructor shall create a key from the shared access key signing with HmacSHA256]
// Get an hmac_sha1 key from the raw key bytes
byte[] keyBytes = decodeBase64(this.keyValue.getBytes(StandardCharsets.UTF_8));
SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA256");
// Get an hmac_sha1 Mac instance and initialize with the signing key
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(signingKey);
// Codes_SRS_PROVISIONING_SERVICE_SASTOKEN_12_005: [The constructor shall compute the final signature by url encoding the signed key]
// Compute the hmac on input data bytes
byte[] rawHmac = mac.doFinal(toSign.getBytes(StandardCharsets.UTF_8));
// Convert raw bytes to Hex
String signature = URLEncoder.encode(encodeBase64String(rawHmac), StandardCharsets.UTF_8.name());
// Codes_SRS_PROVISIONING_SERVICE_SASTOKEN_12_006: [The constructor shall concatenate the target uri, the signature, the expiry time and the key name using the format: "SharedAccessSignature sr=%s&sig=%s&se=%s&skn=%s"]
return String.format(TOKEN_FORMAT, targetUri, signature, this.expiryTime, this.keyName);
} catch (UnsupportedEncodingException | NoSuchAlgorithmException | InvalidKeyException e) {
// Codes_SRS_PROVISIONING_SERVICE_SASTOKEN_12_007: [The constructor shall throw Exception if building the token failed]
throw new RuntimeException(e);
}
}
use of com.google.api.client.util.Base64.encodeBase64String in project azure-iot-sdk-java by Azure.
the class IotHubServiceSasTokenTest method constructor_good_case_format_check.
// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBSERVICESASTOKEN_12_002: [The constructor shall create a target uri from the url encoded host name)]
// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBSERVICESASTOKEN_12_003: [The constructor shall create a string to sign by concatenating the target uri and the expiry time string (one year)]
// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBSERVICESASTOKEN_12_004: [The constructor shall create a key from the shared access key signing with HmacSHA256]
// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBSERVICESASTOKEN_12_005: [The constructor shall compute the final signature by url encoding the signed key]
// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBSERVICESASTOKEN_12_006: [The constructor shall concatenate the target uri, the signature, the expiry time and the key name using the format: "SharedAccessSignature sr=%s&sig=%s&se=%s&skn=%s"]
// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBSERVICESASTOKEN_12_007: [The function shall return with the generated token]
@Test
public void constructor_good_case_format_check() throws Exception {
// Arrange
String iotHubName = "b.c.d";
String hostName = "HOSTNAME." + iotHubName;
String sharedAccessKeyName = "ACCESSKEYNAME";
String policyName = "SharedAccessKey";
String sharedAccessKey = encodeBase64String("1234567890abcdefghijklmnopqrstvwxyz=".getBytes(StandardCharsets.UTF_8));
String connectionString = "HostName=" + hostName + ";SharedAccessKeyName=" + sharedAccessKeyName + ";" + policyName + "=" + sharedAccessKey;
IotHubConnectionString iotHubConnectionString = IotHubConnectionStringBuilder.createConnectionString(connectionString);
// Act
IotHubServiceSasToken iotHubServiceSasToken = new IotHubServiceSasToken(iotHubConnectionString);
String token = iotHubServiceSasToken.toString();
// Assert
assertTrue(token.contains("SharedAccessSignature sr=hostname.b.c.d&sig="));
assertTrue(token.contains("&se="));
assertTrue(token.contains("&skn=ACCESSKEYNAME"));
}
use of com.google.api.client.util.Base64.encodeBase64String in project azure-iot-sdk-java by Azure.
the class IotHubServiceSasToken method buildToken.
/**
* Helper function to build the token string
*
* @return Valid token string
*/
private String buildToken() {
String targetUri;
try {
targetUri = URLEncoder.encode(this.resourceUri.toLowerCase(), StandardCharsets.UTF_8.name());
String toSign = targetUri + "\n" + this.expiryTimeSeconds;
byte[] keyBytes = decodeBase64(this.keyValue.getBytes(StandardCharsets.UTF_8));
SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA256");
// Get an hmac_sha1 Mac instance and initialize with the signing key
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(signingKey);
// Compute the hmac on input data bytes
byte[] rawHmac = mac.doFinal(toSign.getBytes(StandardCharsets.UTF_8));
// Convert raw bytes to Hex
String signature = URLEncoder.encode(encodeBase64String(rawHmac), StandardCharsets.UTF_8.name());
return String.format(TOKEN_FORMAT, targetUri, signature, this.expiryTimeSeconds, this.keyName);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of com.google.api.client.util.Base64.encodeBase64String in project azure-iot-sdk-java by Azure.
the class ProvisioningServiceSasTokenTest method constructorThrowsOnBuildToken.
// Tests_SRS_PROVISIONING_SERVICE_SASTOKEN_12_007: [The constructor shall throw Exception if building the token failed]
@Test(expected = Exception.class)
public void constructorThrowsOnBuildToken() throws Exception {
// Arrange
String deviceProvisioningServiceName = "b.c.d";
String hostName = "HOSTNAME." + deviceProvisioningServiceName;
String sharedAccessKeyName = "ACCESSKEYNAME";
String policyName = "SharedAccessKey";
String sharedAccessKey = encodeBase64String("key".getBytes(StandardCharsets.UTF_8));
String connectionString = "HostName=" + hostName + ";SharedAccessKeyName=" + sharedAccessKeyName + ";" + policyName + "=" + sharedAccessKey;
// Act
ProvisioningConnectionString provisioningConnectionString = ProvisioningConnectionStringBuilder.createConnectionString(connectionString);
Deencapsulation.setField(provisioningConnectionString, "hostName", null);
ProvisioningSasToken provisioningServiceSasToken = new ProvisioningSasToken(provisioningConnectionString);
}
Aggregations