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);
}
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"));
}
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"));
}
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);
}
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;
}
};
}
Aggregations