def test_sas_auth_provider_created_correctly_from_device_shared_access_signature_string(
):
    sas_string = create_sas_token_string()
    auth_provider = from_shared_access_signature(sas_string)

    assert isinstance(auth_provider,
                      SharedAccessSignatureAuthenticationProvider)
    assert auth_provider.device_id == device_id
    assert auth_provider.hostname == hostname
def test_sas_auth_provider_created_correctly_from_module_shared_access_signature_string_keyname(
):
    sas_string = create_sas_token_string(True, True)
    auth_provider = from_shared_access_signature(sas_string)

    assert isinstance(auth_provider,
                      SharedAccessSignatureAuthenticationProvider)
    assert auth_provider.device_id == device_id
    assert auth_provider.hostname == hostname
    assert auth_provider.module_id == module_id
    assert shared_access_key_name in auth_provider.get_current_sas_token()
def auth_provider(request):
    from azure.iot.hub.devicesdk.auth.authentication_provider_factory import (
        from_connection_string,
        from_shared_access_signature,
    )

    auth_type = request.param
    if auth_type == "SymmetricKey":
        return from_connection_string(
            connection_string_format.format(hostname, device_id,
                                            shared_access_key))
    elif auth_type == "SharedAccessSignature":
        uri = hostname + "/devices/" + device_id
        return from_shared_access_signature(
            sastoken_format.format(uri, signature, expiry))
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
import os
import logging
from azure.iot.hub.devicesdk.device_client import DeviceClient
from azure.iot.hub.devicesdk.auth.authentication_provider_factory import from_shared_access_signature

logging.basicConfig(level=logging.INFO)

sas_token_string = os.getenv("IOTHUB_DEVICE_SAS_STRING")

auth_provider = from_shared_access_signature(str(sas_token_string))
simpleDevice = DeviceClient.from_authentication_provider(auth_provider, "mqtt")


def connection_state_callback(status):
    print("connection status: " + status)
    if status == "connected":
        simpleDevice.send_event("payload from device authenticated with sas")


simpleDevice.on_connection_state = connection_state_callback
simpleDevice.connect()

while True:
    continue