Search in sources :

Example 11 with Base64.encodeBase64String

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);
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Base64.encodeBase64String(org.apache.commons.codec.binary.Base64.encodeBase64String) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) Mac(javax.crypto.Mac)

Example 12 with Base64.encodeBase64String

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"));
}
Also used : IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Base64.encodeBase64String(org.apache.commons.codec.binary.Base64.encodeBase64String) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Test(org.junit.Test)

Example 13 with Base64.encodeBase64String

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);
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) Base64.encodeBase64String(org.apache.commons.codec.binary.Base64.encodeBase64String) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Mac(javax.crypto.Mac)

Example 14 with Base64.encodeBase64String

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);
}
Also used : ProvisioningConnectionString(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningConnectionString) ProvisioningSasToken(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken) Base64.encodeBase64String(org.apache.commons.codec.binary.Base64.encodeBase64String) ProvisioningConnectionString(com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningConnectionString) Test(org.junit.Test)

Aggregations

Base64.encodeBase64String (org.apache.commons.codec.binary.Base64.encodeBase64String)14 Test (org.junit.Test)8 Mac (javax.crypto.Mac)4 SecretKeySpec (javax.crypto.spec.SecretKeySpec)4 ProvisioningConnectionString (com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningConnectionString)3 ProvisioningSasToken (com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken)3 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)3 IotHubServiceSasToken (com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken)2 URLEncoder (java.net.URLEncoder)2 InvalidKeyException (java.security.InvalidKeyException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 Expectations (mockit.Expectations)2 NonStrictExpectations (mockit.NonStrictExpectations)2 ResponseEntity (org.springframework.http.ResponseEntity)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 MessageProperty (com.microsoft.azure.sdk.iot.device.MessageProperty)1 SignRequest (com.microsoft.azure.sdk.iot.device.hsm.parser.SignRequest)1 HttpsBatchMessage (com.microsoft.azure.sdk.iot.device.transport.https.HttpsBatchMessage)1 HttpsSingleMessage (com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage)1 UrlPathBuilder (com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.UrlPathBuilder)1