コード例 #1
0
    def create_from_symmetric_key(cls, symmetric_key, hostname, device_id,
                                  **kwargs):
        """
        Instantiate a client using symmetric key authentication.

        :param symmetric_key: The symmetric key.
        :param str hostname: Host running the IotHub.
            Can be found in the Azure portal in the Overview tab as the string hostname.
        :param device_id: The device ID

        :param bool websockets: Configuration Option. Default is False. Set to true if using MQTT over websockets.
        :param str product_info: Configuration Option. Default is empty string. The string contains arbitrary product info which is appended to the user agent string.
        :return: An instance of an IoTHub client that uses a symmetric key for authentication.
        """

        authentication_provider = auth.SymmetricKeyAuthenticationProvider(
            hostname=hostname,
            device_id=device_id,
            module_id=None,
            shared_access_key=symmetric_key)
        pipeline_configuration = IoTHubPipelineConfig(**kwargs)

        pipeline_configuration.blob_upload = True  # Blob Upload is a feature on Device Clients
        http_pipeline = pipeline.HTTPPipeline(authentication_provider,
                                              pipeline_configuration)

        iothub_pipeline = pipeline.IoTHubPipeline(authentication_provider,
                                                  pipeline_configuration)

        return cls(iothub_pipeline, http_pipeline)
コード例 #2
0
    def create_from_connection_string(cls,
                                      connection_string,
                                      server_verification_cert=None,
                                      **kwargs):
        """
        Instantiate the client from a IoTHub device or module connection string.

        :param str connection_string: The connection string for the IoTHub you wish to connect to.
        :param str server_verification_cert: The trusted certificate chain. Necessary when
            using connecting to an endpoint which has a non-standard root of trust, such as a
            protocol gateway.
        :param bool websockets: Configuration Option. Default is False. Set to true if using MQTT over websockets.
        :param str product_info: Configuration Option. Default is empty string. The string contains arbitrary product info which is appended to the user agent string.

        :raises: ValueError if given an invalid connection_string.

        :returns: An instance of an IoTHub client that uses a connection string for authentication.
        """
        # TODO: Make this device/module specific and reject non-matching connection strings.
        # This will require refactoring of the auth package to use common objects (e.g. ConnectionString)
        # in order to differentiate types of connection strings.
        pipeline_configuration = IoTHubPipelineConfig(**kwargs)
        if cls.__name__ == "IoTHubDeviceClient":
            pipeline_configuration.blob_upload = True
        authentication_provider = auth.SymmetricKeyAuthenticationProvider.parse(
            connection_string)
        authentication_provider.server_verification_cert = server_verification_cert
        http_pipeline = pipeline.HTTPPipeline(authentication_provider,
                                              pipeline_configuration)
        iothub_pipeline = pipeline.IoTHubPipeline(authentication_provider,
                                                  pipeline_configuration)
        return cls(iothub_pipeline, http_pipeline)
コード例 #3
0
    def create_from_x509_certificate(cls, x509, hostname, device_id, **kwargs):
        """
        Instantiate a client which using X509 certificate authentication.

        :param str hostname: Host running the IotHub.
            Can be found in the Azure portal in the Overview tab as the string hostname.
        :param x509: The complete x509 certificate object.
            To use the certificate the enrollment object needs to contain cert
            (either the root certificate or one of the intermediate CA certificates).
            If the cert comes from a CER file, it needs to be base64 encoded.
        :type x509: :class:`azure.iot.device.X509`
        :param str device_id: The ID used to uniquely identify a device in the IoTHub

        :param bool websockets: Configuration Option. Default is False. Set to true if using MQTT over websockets.
        :param str product_info: Configuration Option. Default is empty string. The string contains arbitrary product info which is appended to the user agent string.

        :returns: An instance of an IoTHub client that uses an X509 certificate for authentication.
        """
        authentication_provider = auth.X509AuthenticationProvider(
            x509=x509, hostname=hostname, device_id=device_id)
        pipeline_configuration = IoTHubPipelineConfig(**kwargs)

        pipeline_configuration.blob_upload = True  # Blob Upload is a feature on Device Clients
        http_pipeline = pipeline.HTTPPipeline(authentication_provider,
                                              pipeline_configuration)

        iothub_pipeline = pipeline.IoTHubPipeline(authentication_provider,
                                                  pipeline_configuration)
        return cls(iothub_pipeline, http_pipeline)