use of com.azure.core.credential.AzureSasCredential in project azure-iot-sdk-java by Azure.
the class QueryTwinTests method rawQueryTokenRenewalWithAzureSasCredential.
@Test
@StandardTierHubOnlyTest
public void rawQueryTokenRenewalWithAzureSasCredential() throws IOException, InterruptedException, IotHubException, GeneralSecurityException, URISyntaxException, ModuleClientException {
IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(iotHubConnectionString);
IotHubServiceSasToken serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
AzureSasCredential sasCredential = new AzureSasCredential(serviceSasToken.toString());
testInstance.twinServiceClient = new DeviceTwin(iotHubConnectionStringObj.getHostName(), sasCredential);
// test that the service client can query before the shared access signature expires
testRawQueryTwin();
// deliberately expire the SAS token to provoke a 401 to ensure that the registry manager is using the shared
// access signature that is set here.
sasCredential.update(SasTokenTools.makeSasTokenExpired(serviceSasToken.toString()));
try {
testRawQueryTwin();
fail("Expected query 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());
// test that the service client can query after renewing the shared access signature expires
testRawQueryTwin();
}
use of com.azure.core.credential.AzureSasCredential in project azure-iot-sdk-java by Azure.
the class DigitalTwinClientTests method digitalTwinClientTokenRenewalWithAzureSasCredential.
@Test
@StandardTierHubOnlyTest
public void digitalTwinClientTokenRenewalWithAzureSasCredential() {
if (protocol != MQTT) {
// This test is for the service client, so no need to rerun it for all the different device protocols
return;
}
IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(IOTHUB_CONNECTION_STRING);
IotHubServiceSasToken serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
AzureSasCredential sasCredential = new AzureSasCredential(serviceSasToken.toString());
digitalTwinClient = new DigitalTwinClient(iotHubConnectionStringObj.getHostName(), sasCredential);
// get a digital twin with a valid SAS token in the AzureSasCredential instance
// don't care about the return value, just checking that the request isn't unauthorized
digitalTwinClient.getDigitalTwin(deviceId, BasicDigitalTwin.class);
// deliberately expire the SAS token to provoke a 401 to ensure that the digital twin client is using the shared
// access signature that is set here.
sasCredential.update(SasTokenTools.makeSasTokenExpired(serviceSasToken.toString()));
try {
digitalTwinClient.getDigitalTwin(deviceId, BasicDigitalTwin.class);
fail("Expected get digital twin call to throw unauthorized exception since an expired SAS token was used, but no exception was thrown");
} catch (RestException e) {
if (e.response().code() == 401) {
log.debug("IotHubUnauthorizedException was thrown as expected, continuing test");
} else {
throw e;
}
}
// Renew the expired shared access signature
serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
sasCredential.update(serviceSasToken.toString());
// get a digital twin using the renewed shared access signature
// don't care about the return value, just checking that the request isn't unauthorized
digitalTwinClient.getDigitalTwin(deviceId, BasicDigitalTwin.class);
}
use of com.azure.core.credential.AzureSasCredential in project azure-iot-sdk-java by Azure.
the class DeviceMethodTests 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;
}
IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(iotHubConnectionString);
IotHubServiceSasToken serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
AzureSasCredential sasCredential = new AzureSasCredential(serviceSasToken.toString());
this.testInstance.methodServiceClient = new DeviceMethod(iotHubConnectionStringObj.getHostName(), sasCredential, DeviceMethodClientOptions.builder().httpReadTimeout(HTTP_READ_TIMEOUT).build());
super.openDeviceClientAndSubscribeToMethods();
// add first device just to make sure that the first credential update worked
super.invokeMethodSucceed();
// deliberately expire the SAS token to provoke a 401 to ensure that the method client is using the shared
// access signature that is set here.
sasCredential.update(SasTokenTools.makeSasTokenExpired(serviceSasToken.toString()));
try {
super.invokeMethodSucceed();
fail("Expected invoke method 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());
// final method invocation should succeed since the shared access signature has been renewed
super.invokeMethodSucceed();
}
use of com.azure.core.credential.AzureSasCredential in project azure-iot-sdk-java by Azure.
the class DigitalTwinClientTests method buildDigitalTwinClientWithAzureSasCredential.
private static DigitalTwinClient buildDigitalTwinClientWithAzureSasCredential() {
IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(IOTHUB_CONNECTION_STRING);
IotHubServiceSasToken serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
AzureSasCredential azureSasCredential = new AzureSasCredential(serviceSasToken.toString());
DigitalTwinClientOptions options = DigitalTwinClientOptions.builder().httpReadTimeout(HTTP_READ_TIMEOUT).build();
return new DigitalTwinClient(iotHubConnectionStringObj.getHostName(), azureSasCredential, options);
}
use of com.azure.core.credential.AzureSasCredential in project azure-iot-sdk-java by Azure.
the class DeviceTwinCommon method buildDeviceTwinClientWithAzureSasCredential.
protected static DeviceTwin buildDeviceTwinClientWithAzureSasCredential() {
IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(iotHubConnectionString);
IotHubServiceSasToken serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
AzureSasCredential azureSasCredential = new AzureSasCredential(serviceSasToken.toString());
DeviceTwinClientOptions options = DeviceTwinClientOptions.builder().httpReadTimeout(HTTP_READ_TIMEOUT).build();
return new DeviceTwin(iotHubConnectionStringObj.getHostName(), azureSasCredential, options);
}
Aggregations