use of com.microsoft.azure.sdk.iot.service.exceptions.IotHubUnathorizedException in project azure-iot-sdk-java by Azure.
the class GetTwinTests method serviceClientTokenRenewalWithAzureSasCredential.
@Test
@StandardTierHubOnlyTest
public void serviceClientTokenRenewalWithAzureSasCredential() throws Exception {
if (testInstance.protocol != IotHubClientProtocol.AMQPS || testInstance.clientType != ClientType.DEVICE_CLIENT || testInstance.authenticationType != AuthenticationType.SAS) {
// This test is for the service client, so no need to rerun it for all the different client types or device protocols
return;
}
super.setUpNewDeviceAndModule();
IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(iotHubConnectionString);
IotHubServiceSasToken serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
String sasToken = serviceSasToken.toString();
AzureSasCredential sasCredential = new AzureSasCredential(sasToken);
this.testInstance.twinServiceClient = new DeviceTwin(iotHubConnectionStringObj.getHostName(), sasCredential, DeviceTwinClientOptions.builder().httpReadTimeout(HTTP_READ_TIMEOUT).build());
// add first device just to make sure that the first credential update worked
super.testGetDeviceTwin();
// deliberately expire the SAS token to provoke a 401 to ensure that the twin client is using the shared
// access signature that is set here.
sasCredential.update(SasTokenTools.makeSasTokenExpired(sasToken));
try {
super.testGetDeviceTwin();
fail("Expected get twin call to throw unauthorized exception since an expired SAS token was used, but no exception was thrown");
} catch (IotHubUnathorizedException e) {
log.debug("IotHubUnauthorizedException was thrown as expected, continuing test");
}
// Renew the expired shared access signature
serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
sasCredential.update(serviceSasToken.toString());
// adding third device should succeed since the shared access signature has been renewed
super.testGetDeviceTwin();
}
Aggregations