Search in sources :

Example 21 with UrlPathBuilder

use of com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.UrlPathBuilder in project azure-iot-sdk-java by Azure.

the class UrlPathBuilderTest method generateRequestUrlHttpThrowsOnEmptyRegID.

@Test(expected = IllegalArgumentException.class)
public void generateRequestUrlHttpThrowsOnEmptyRegID() throws IOException {
    // arrange
    UrlPathBuilder urlPathBuilder = new UrlPathBuilder(TEST_HOST_NAME, TEST_SCOPE, ProvisioningDeviceClientTransportProtocol.HTTPS);
    // act
    urlPathBuilder.generateRequestUrl("", TEST_OPERATION_ID);
}
Also used : UrlPathBuilder(com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.UrlPathBuilder) Test(org.junit.Test)

Example 22 with UrlPathBuilder

use of com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.UrlPathBuilder in project azure-iot-sdk-java by Azure.

the class UrlPathBuilderTest method constructorSucceeds.

// SRS_UrlPathBuilder_25_004: [ The constructor shall save the scope id or hostName string and protocol. ]
@Test
public void constructorSucceeds() throws IllegalArgumentException {
    // act
    UrlPathBuilder urlPathBuilder = new UrlPathBuilder(TEST_HOST_NAME, TEST_SCOPE, ProvisioningDeviceClientTransportProtocol.HTTPS);
    // assert
    assertEquals(TEST_SCOPE, Deencapsulation.getField(urlPathBuilder, "scope"));
    assertEquals(ProvisioningDeviceClientTransportProtocol.HTTPS, Deencapsulation.getField(urlPathBuilder, "provisioningDeviceClientTransportProtocol"));
}
Also used : UrlPathBuilder(com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.UrlPathBuilder) Test(org.junit.Test)

Example 23 with UrlPathBuilder

use of com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.UrlPathBuilder in project azure-iot-sdk-java by Azure.

the class UrlPathBuilderTest method constructorWithScopeOnlySucceeds.

// SRS_UrlPathBuilder_25_001: [ Constructor shall save scope id.]
@Test
public void constructorWithScopeOnlySucceeds() throws IllegalArgumentException {
    // act
    UrlPathBuilder urlPathBuilder = new UrlPathBuilder(TEST_SCOPE);
    // assert
    assertEquals(TEST_SCOPE, Deencapsulation.getField(urlPathBuilder, "scope"));
}
Also used : UrlPathBuilder(com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.UrlPathBuilder) Test(org.junit.Test)

Example 24 with UrlPathBuilder

use of com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.UrlPathBuilder in project azure-iot-sdk-java by Azure.

the class RegisterTask method constructSasToken.

private String constructSasToken() throws ProvisioningDeviceClientException, UnsupportedEncodingException, SecurityProviderException {
    if (RegisterTask.DEFAULT_EXPIRY_TIME_IN_SECS <= 0) {
        throw new IllegalArgumentException("expiry time cannot be negative or zero");
    }
    String registrationId = securityProvider.getRegistrationId();
    String tokenScope = new UrlPathBuilder(provisioningDeviceClientConfig.getIdScope()).generateSasTokenUrl(registrationId);
    if (tokenScope == null || tokenScope.isEmpty()) {
        throw new ProvisioningDeviceClientException("Could not construct token scope");
    }
    Long expiryTimeUTC = System.currentTimeMillis() / 1000 + RegisterTask.DEFAULT_EXPIRY_TIME_IN_SECS;
    String value = tokenScope.concat("\n" + expiryTimeUTC);
    byte[] token = null;
    if (securityProvider instanceof SecurityProviderTpm) {
        SecurityProviderTpm securityClientTpm = (SecurityProviderTpm) securityProvider;
        token = securityClientTpm.signWithIdentity(value.getBytes(StandardCharsets.UTF_8));
    } else if (securityProvider instanceof SecurityProviderSymmetricKey) {
        SecurityProviderSymmetricKey securityProviderSymmetricKey = (SecurityProviderSymmetricKey) securityProvider;
        token = securityProviderSymmetricKey.HMACSignData(value.getBytes(StandardCharsets.UTF_8.displayName()), decodeBase64(securityProviderSymmetricKey.getSymmetricKey()));
    }
    if (token == null || token.length == 0) {
        throw new ProvisioningDeviceSecurityException("Security client could not sign data successfully");
    }
    byte[] base64Signature = encodeBase64(token);
    String base64UrlEncodedSignature = URLEncoder.encode(new String(base64Signature, StandardCharsets.UTF_8), StandardCharsets.UTF_8.displayName());
    // SRS_RegisterTask_25_015: [ If the provided security client is for Key then, this method shall build the SasToken of the format SharedAccessSignature sr=<tokenScope>&sig=<signature>&se=<expiryTime>&skn= and save it to authorization]
    return String.format(SASTOKEN_FORMAT, tokenScope, base64UrlEncodedSignature, expiryTimeUTC);
}
Also used : ProvisioningDeviceSecurityException(com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceSecurityException) UrlPathBuilder(com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.UrlPathBuilder) SecurityProviderTpm(com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderTpm) SecurityProviderSymmetricKey(com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderSymmetricKey) ProvisioningDeviceClientException(com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientException)

Example 25 with UrlPathBuilder

use of com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.UrlPathBuilder in project azure-iot-sdk-java by Azure.

the class ContractAPIHttpTest method getRegistrationStatusWithAuthSucceeds.

// SRS_ContractAPIHttp_25_019: [This method shall retrieve the Url by calling generateRequestUrl on an object for UrlPathBuilder.]
// SRS_ContractAPIHttp_25_020: [This method shall prepare the GET request by setting following headers on a HttpRequest 1. User-Agent : User Agent String for the SDK 2. Accept : "application/json" 3. Content-Type: "application/json; charset=utf-8" 4. Authorization: specified sas token as authorization if a non null value is given.]
// SRS_ContractAPIHttp_25_021: [This method shall set the SSLContext for the Http Request.]
// SRS_ContractAPIHttp_25_022: [This method shall send http request and verify the status by calling 'ProvisioningDeviceClientExceptionManager.verifyHttpResponse'.]
// SRS_ContractAPIHttp_25_023: [If service return a status as < 300 then this method shall trigger the callback to the user with the response message.]
@Test
public void getRegistrationStatusWithAuthSucceeds() throws IOException, ProvisioningDeviceClientException {
    // arrange
    ContractAPIHttp contractAPIHttp = createContractClass();
    prepareRequestExpectations();
    new NonStrictExpectations() {

        {
            mockedRequestData.getOperationId();
            result = TEST_OPERATION_ID;
            mockedRequestData.getRegistrationId();
            result = TEST_REGISTRATION_ID;
            mockedRequestData.getSslContext();
            result = mockedSslContext;
            mockedRequestData.getSasToken();
            result = TEST_SAS_TOKEN;
            mockedHttpRequest.send();
            result = mockedHttpResponse;
        }
    };
    // act
    contractAPIHttp.getRegistrationStatus(mockedRequestData, mockedResponseCallback, null);
    // assert
    prepareRequestVerifications(HttpMethod.GET, 1);
    new Verifications() {

        {
            new UrlPathBuilder(TEST_HOST_NAME, TEST_SCOPE_ID, ProvisioningDeviceClientTransportProtocol.HTTPS);
            times = 1;
            mockedUrlPathBuilder.generateRequestUrl(TEST_REGISTRATION_ID, TEST_OPERATION_ID);
            times = 1;
            mockedHttpRequest.setSSLContext(mockedSslContext);
            times = 1;
            mockedResponseCallback.run((ResponseData) any, null);
            times = 1;
        }
    };
}
Also used : UrlPathBuilder(com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.UrlPathBuilder) ContractAPIHttp(com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.http.ContractAPIHttp) Test(org.junit.Test)

Aggregations

UrlPathBuilder (com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.UrlPathBuilder)29 Test (org.junit.Test)25 ContractAPIHttp (com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.http.ContractAPIHttp)12 DeviceRegistrationParser (com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.DeviceRegistrationParser)7 IOException (java.io.IOException)6 HttpRequest (com.microsoft.azure.sdk.iot.deps.transport.http.HttpRequest)3 HttpResponse (com.microsoft.azure.sdk.iot.deps.transport.http.HttpResponse)3 ResponseData (com.microsoft.azure.sdk.iot.provisioning.device.internal.task.ResponseData)3 URL (java.net.URL)3 ProvisioningDeviceClientException (com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientException)1 ProvisioningDeviceSecurityException (com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceSecurityException)1 TpmRegistrationResultParser (com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.TpmRegistrationResultParser)1 SecurityProviderSymmetricKey (com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderSymmetricKey)1 SecurityProviderTpm (com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderTpm)1 Base64.encodeBase64String (org.apache.commons.codec.binary.Base64.encodeBase64String)1