How to use the azure-iot-common.ConnectionString.parse function in azure-iot-common

To help you get started, we’ve selected a few azure-iot-common examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Azure / azure-iot-sdks / node / device / transport / amqp-ws / lib / _client_test_integration.js View on Github external
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

'use strict';

var ConnectionString = require('azure-iot-common').ConnectionString;
var runTests = require('azure-iot-device-amqp/lib/_client_test_integration.js');
var AmqpWs = require('./amqp_ws.js');

var host = ConnectionString.parse(process.env.IOTHUB_CONNECTION_STRING).HostName;
var deviceId = process.env.IOTHUB_DEVICE_ID;
var key = process.env.IOTHUB_DEVICE_KEY;

function makeConnectionString(host, device, key) {
  return 'HostName='+host+';DeviceId='+device+';SharedAccessKey='+key;
}

var connectionString = makeConnectionString(host, deviceId, key);

var badConnStrings = [
  makeConnectionString('bad', 'device', 'key'),
  makeConnectionString('host', 'bad', 'key'),
  makeConnectionString('host', 'device', 'bad')
];

describe('Over AMQP/WS', function () {
github Azure / azure-iot-sdk-node / device / transport / amqp / lib / _client_test_integration.js View on Github external
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

'use strict';

var assert = require('chai').assert;

var Client = require('azure-iot-device').Client;
var ConnectionString = require('azure-iot-common').ConnectionString;
var Message = require('azure-iot-common').Message;
var Amqp = require('./amqp.js');

var host = ConnectionString.parse(process.env.IOTHUB_CONNECTION_STRING).HostName;
var deviceId = process.env.IOTHUB_DEVICE_ID;
var key = process.env.IOTHUB_DEVICE_KEY;

function makeConnectionString(host, device, key) {
  return 'HostName='+host+';DeviceId='+device+';SharedAccessKey='+key;
}

var connectionString = makeConnectionString(host, deviceId, key);

var badConnStrings = [
  makeConnectionString('bad', deviceId, key),
  makeConnectionString(host, 'bad', key),
  makeConnectionString(host, deviceId, 'bad')
];

function badConfigTests(opName, badConnStrings, Transport, requestFn) {
github Azure / azure-iot-sdks / node / e2etests / eh / eventhubclient.js View on Github external
function createConfig(connectionString, entityPath) {
  var cn = ConnectionString.parse(connectionString);
  var host = cn.HostName || (cn.Endpoint || '').slice('sb://'.length);
  return {
    host: host,
    namespace: host.split('.')[0],
    keyName: cn.SharedAccessKeyName,
    key: cn.SharedAccessKey,
    eventHubName: entityPath
  };
}
github Azure / iothub-explorer / lib / eventhubclient.js View on Github external
function createConfig(connectionString, entityPath) {
  var cn = ConnectionString.parse(connectionString);
  var host = cn.HostName || (cn.Endpoint || '').slice('sb://'.length);
  return {
    host: host,
    namespace: host.split('.')[0],
    keyName: cn.SharedAccessKeyName,
    key: cn.SharedAccessKey,
    eventHubName: entityPath
  };
}
github Azure / azure-iot-sdks / node / service / lib / connection_string.js View on Github external
parse: function parse(source) {
    /*Codes_SRS_NODE_IOTHUB_CONNSTR_05_001: [The parse method shall return the result of calling azure-iot-common.ConnectionString.parse.]*/
    /*Codes_SRS_NODE_IOTHUB_CONNSTR_05_002: [It shall throw ArgumentError if any of 'HostName', 'SharedAccessKeyName', or 'SharedAccessKey' fields are not found in the source argument.]*/
    return Base.parse(source, ['HostName', 'SharedAccessKeyName', 'SharedAccessKey']);
  }
};
github Azure / azure-iot-sdk-node / digitaltwins / e2e / model_repository_credentials.js View on Github external
constructor(connectionString) {
    this._connectionString = ConnectionString.parse(connectionString, ['HostName', 'SharedAccessKeyName', 'SharedAccessKey', 'RepositoryId']);
  }
github Azure / azure-iot-sdk-node / device / core / src / module_client.ts View on Github external
static fromConnectionString(connStr: string, transportCtor: any): ModuleClient {
    /*Codes_SRS_NODE_MODULE_CLIENT_05_003: [The fromConnectionString method shall throw ReferenceError if the connStr argument is falsy.]*/
    if (!connStr) throw new ReferenceError('connStr is \'' + connStr + '\'');

    const cn = ConnectionString.parse(connStr);

    /*Codes_SRS_NODE_MODULE_CLIENT_16_087: [The `fromConnectionString` method shall create a new `SharedAccessKeyAuthorizationProvider` object with the connection string passed as argument if it contains a SharedAccessKey parameter and pass this object to the transport constructor.]*/
    let authenticationProvider: AuthenticationProvider;

    if (cn.SharedAccessKey) {
      authenticationProvider = SharedAccessKeyAuthenticationProvider.fromConnectionString(connStr);
    } else {
      /*Codes_SRS_NODE_MODULE_CLIENT_16_001: [The `fromConnectionString` method shall throw a `NotImplementedError` if the connection string does not contain a `SharedAccessKey` field because x509 authentication is not supported yet for modules.]*/
      throw new errors.NotImplementedError('ModuleClient only supports SAS Token authentication');
    }

    /*Codes_SRS_NODE_MODULE_CLIENT_05_006: [The fromConnectionString method shall return a new instance of the Client object, as by a call to new Client(new transportCtor(...)).]*/
    return new ModuleClient(new transportCtor(authenticationProvider), null);
  }
github Azure / azure-iot-sdk-node / provisioning / service / src / provisioningserviceclient.ts View on Github external
static fromConnectionString(value: string): ProvisioningServiceClient {
     /*Codes_SRS_NODE_PROVISIONING_SERVICE_CLIENT_06_005: [The `fromConnectionString` method shall throw `ReferenceError` if the `value` argument is falsy.]*/
     if (!value) throw new ReferenceError('value is \'' + value + '\'');

    const cn = ConnectionString.parse(value);

    const config: RestApiClient.TransportConfig = {
      host: cn.HostName,
      sharedAccessSignature: SharedAccessSignature.create(cn.HostName, cn.SharedAccessKeyName, cn.SharedAccessKey, Date.now())
    };
    /*Codes_SRS_NODE_PROVISIONING_SERVICE_CLIENT_06_006: [`fromConnectionString` method shall derive and transform the needed parts from the connection string in order to create a `config` object for the constructor (see `SRS_NODE_PROVISIONING_SERVICE_CLIENT_06_002`).] */
    /*Codes_SRS_NODE_PROVISIONING_SERVICE_CLIENT_06_007: [The `fromConnectionString` method shall return a new instance of the `ProvisioningServiceClient` object.] */
    return new ProvisioningServiceClient(config);
  }
github Azure / azure-iot-sdks / node / device / core / lib / connection_string.js View on Github external
parse: function parse(source) {
    /*Codes_SRS_NODE_DEVICE_CONNSTR_05_001: [The parse method shall return the result of calling azure-iot-common.ConnectionString.parse.]*/
    /*Codes_SRS_NODE_DEVICE_CONNSTR_05_002: [It shall throw ArgumentError if any of 'HostName' or 'DeviceId' fields are not found in the source argument.]*/
    var connectionString = Base.parse(source, ['HostName', 'DeviceId']);
    /*Codes_SRS_NODE_DEVICE_CONNSTR_16_001: [It shall throw `ArgumentError` if `SharedAccessKey` and `x509` are present at the same time or if none of them are present.]*/
    if((connectionString.SharedAccessKey && connectionString.x509) || (!connectionString.SharedAccessKey && !connectionString.x509)) {
      throw new errors.ArgumentError('The connection string must contain either a SharedAccessKey or x509=true');
    }

    return connectionString;
  }
};
github Azure / azure-iot-sdk-node / device / core / src / sak_authentication_provider.ts View on Github external
static fromConnectionString(connectionString: string, tokenValidTimeInSeconds?: number, tokenRenewalMarginInSeconds?: number): SharedAccessKeyAuthenticationProvider {
    if (!connectionString) {
      /*Codes_SRS_NODE_SAK_AUTH_PROVIDER_16_006: [The `fromConnectionString` method shall throw a `ReferenceError` if the `connectionString` parameter is falsy.]*/
      throw new ReferenceError('connectionString cannot be \'' + connectionString + '\'');
    }

    /*Codes_SRS_NODE_SAK_AUTH_PROVIDER_16_007: [The `fromConnectionString` method shall throw an `errors.ArgumentError` if the `connectionString` does not have a SharedAccessKey parameter.]*/
    const cs: ConnectionString = ConnectionString.parse(connectionString, ['DeviceId', 'HostName', 'SharedAccessKey']);

    /*Codes_SRS_NODE_SAK_AUTH_PROVIDER_16_008: [The `fromConnectionString` method shall extract the credentials from the `connectionString` argument and create a new `SharedAccessKeyAuthenticationProvider` that uses these credentials to generate security tokens.]*/
    const credentials: TransportConfig = {
      host: cs.HostName,
      gatewayHostName: cs.GatewayHostName,
      deviceId: cs.DeviceId,
      moduleId: cs.ModuleId,
      sharedAccessKeyName: cs.SharedAccessKeyName,
      sharedAccessKey: cs.SharedAccessKey
    };

    return new SharedAccessKeyAuthenticationProvider(credentials, tokenValidTimeInSeconds, tokenRenewalMarginInSeconds);
  }
}