Search in sources :

Example 1 with AzureSasCredential

use of com.azure.core.credential.AzureSasCredential in project azure-iot-sdk-java by Azure.

the class AzureSasCredentialSample method main.

public static void main(String[] args) {
    SamplesArguments parsedArguments = new SamplesArguments(args);
    String sharedAccessSignature = parsedArguments.getSharedAccessSignature();
    // Shared access signatures can be generated by the SDK for you by using the service client constructors
    // that take in your IoT Hub's connection string. This sample will demonstrate how to construct service clients
    // without giving them the connection string by giving them shared access signatures instead.
    // This sample's readme includes a helper function that shows how to build shared access signatures below.
    // 
    // More details on how to build this shared access signature can be found here:
    // https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-security#security-token-structure
    AzureSasCredential credential = new AzureSasCredential(sharedAccessSignature);
    // "my-azure-iot-hub.azure-devices.net" for example
    String iotHubHostName = parsedArguments.getIotHubHostName();
    String newDeviceId = runRegistryManagerSample(iotHubHostName, credential);
    runTwinClientSample(iotHubHostName, credential, newDeviceId);
    runServiceClientSample(iotHubHostName, credential, newDeviceId);
    // The AzureSasCredential object supports updating the shared access signature which is important to do
    // once the previous signature has expired, or is close to expiring. This sample assumes that no renewal will
    // be necessary since the sample completes in a few seconds. After updating the credential object, any
    // clients that were constructed with that credential will start using the updated signature
    // String updatedSignature = "some new shared access signature";
    // credential.update(updatedSignature);
    runJobClientSample(iotHubHostName, credential);
    runDeviceMethodClientSample(iotHubHostName, credential, newDeviceId);
}
Also used : AzureSasCredential(com.azure.core.credential.AzureSasCredential)

Example 2 with AzureSasCredential

use of com.azure.core.credential.AzureSasCredential in project azure-iot-sdk-java by Azure.

the class RegistryManagerTests method buildRegistryManagerWithAzureSasCredential.

private static RegistryManager buildRegistryManagerWithAzureSasCredential() {
    IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(iotHubConnectionString);
    IotHubServiceSasToken serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
    AzureSasCredential azureSasCredential = new AzureSasCredential(serviceSasToken.toString());
    RegistryManagerOptions options = RegistryManagerOptions.builder().httpReadTimeout(HTTP_READ_TIMEOUT).build();
    return new RegistryManager(iotHubConnectionStringObj.getHostName(), azureSasCredential, options);
}
Also used : IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) AzureSasCredential(com.azure.core.credential.AzureSasCredential) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) RegistryManagerOptions(com.microsoft.azure.sdk.iot.service.RegistryManagerOptions)

Example 3 with AzureSasCredential

use of com.azure.core.credential.AzureSasCredential in project azure-iot-sdk-java by Azure.

the class ServiceClientTests method buildServiceClientWithAzureSasCredential.

private static ServiceClient buildServiceClientWithAzureSasCredential(IotHubServiceClientProtocol protocol, ServiceClientOptions options) {
    IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(iotHubConnectionString);
    IotHubServiceSasToken serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
    AzureSasCredential azureSasCredential = new AzureSasCredential(serviceSasToken.toString());
    return new ServiceClient(iotHubConnectionStringObj.getHostName(), azureSasCredential, protocol, options);
}
Also used : IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) AzureSasCredential(com.azure.core.credential.AzureSasCredential) ServiceClient(com.microsoft.azure.sdk.iot.service.ServiceClient) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString)

Example 4 with AzureSasCredential

use of com.azure.core.credential.AzureSasCredential in project azure-iot-sdk-java by Azure.

the class ServiceClientTests method feedbackMessageReceiverWithAzureSasCredential.

@Test
@StandardTierHubOnlyTest
public void feedbackMessageReceiverWithAzureSasCredential() throws Exception {
    RegistryManager registryManager = RegistryManager.createFromConnectionString(iotHubConnectionString, RegistryManagerOptions.builder().httpReadTimeout(HTTP_READ_TIMEOUT).build());
    IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(iotHubConnectionString);
    IotHubServiceSasToken serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
    AzureSasCredential sasTokenProvider = new AzureSasCredential(serviceSasToken.toString());
    ServiceClient serviceClient = new ServiceClient(iotHubConnectionStringObj.getHostName(), sasTokenProvider, testInstance.protocol);
    FeedbackReceiver feedbackReceiver = serviceClient.getFeedbackReceiver();
    feedbackReceiver.open();
    // received feedback message can be ignored since we no longer have any tests that need to consume them
    // All this test cares about is that this API doesn't result in an unauthorized exception
    feedbackReceiver.receive(2 * 1000);
    feedbackReceiver.close();
    serviceClient.close();
    registryManager.close();
}
Also used : IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) AzureSasCredential(com.azure.core.credential.AzureSasCredential) ServiceClient(com.microsoft.azure.sdk.iot.service.ServiceClient) FeedbackReceiver(com.microsoft.azure.sdk.iot.service.FeedbackReceiver) RegistryManager(com.microsoft.azure.sdk.iot.service.RegistryManager) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest) IotHubTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) Test(org.junit.Test)

Example 5 with AzureSasCredential

use of com.azure.core.credential.AzureSasCredential in project azure-iot-sdk-java by Azure.

the class DeviceMethodCommon method buildDeviceMethodClientWithAzureSasCredential.

protected static DeviceMethod buildDeviceMethodClientWithAzureSasCredential() {
    IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(iotHubConnectionString);
    IotHubServiceSasToken serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
    AzureSasCredential azureSasCredential = new AzureSasCredential(serviceSasToken.toString());
    DeviceMethodClientOptions options = DeviceMethodClientOptions.builder().httpReadTimeout(HTTP_READ_TIMEOUT).build();
    return new DeviceMethod(iotHubConnectionStringObj.getHostName(), azureSasCredential, options);
}
Also used : IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) DeviceMethodClientOptions(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethodClientOptions) AzureSasCredential(com.azure.core.credential.AzureSasCredential) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) DeviceMethod(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod)

Aggregations

AzureSasCredential (com.azure.core.credential.AzureSasCredential)17 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)16 IotHubServiceSasToken (com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken)16 Test (org.junit.Test)10 IotHubTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest)9 StandardTierHubOnlyTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest)9 IntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest)8 ContinuousIntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)8 IotHubUnathorizedException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubUnathorizedException)6 RegistryManager (com.microsoft.azure.sdk.iot.service.RegistryManager)5 ServiceClient (com.microsoft.azure.sdk.iot.service.ServiceClient)4 DeviceTwin (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)4 Device (com.microsoft.azure.sdk.iot.service.Device)2 DeviceMethod (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod)2 DigitalTwinClient (com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClient)2 JobClient (com.microsoft.azure.sdk.iot.service.jobs.JobClient)2 FeedbackReceiver (com.microsoft.azure.sdk.iot.service.FeedbackReceiver)1 FileUploadNotificationReceiver (com.microsoft.azure.sdk.iot.service.FileUploadNotificationReceiver)1 Message (com.microsoft.azure.sdk.iot.service.Message)1 RegistryManagerOptions (com.microsoft.azure.sdk.iot.service.RegistryManagerOptions)1