How to use the azure-iot-device.ConnectionString.createWithSharedAccessKey function in azure-iot-device

To help you get started, we’ve selected a few azure-iot-device 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-sdk-node / ts-e2e / src / device_twin.tests.ts View on Github external
describe('Over ' + transportCtor.name, () => {
      // tslint:disable:no-invalid-this
      this.timeout(60000);

      const testDevice = testUtils.createTestDevice();
      const scs = ServiceConnectionString.parse(process.env.IOTHUB_CONNECTION_STRING);
      const testDeviceCS = DeviceConnectionString.createWithSharedAccessKey(scs.HostName, testDevice.deviceId, testDevice.authentication.symmetricKey.primaryKey);

      beforeEach((beforeEachCallback) => {
        testUtils.addTestDeviceToRegistry(testDevice, beforeEachCallback);
      });

      afterEach((afterEachCallback) => {
        testUtils.removeTestDeviceFromRegistry(testDevice, afterEachCallback);
      });

      it('device can get its device twin and modify reported properties', (testCallback) => {
        const deviceClient = DeviceClient.fromConnectionString(testDeviceCS, transportCtor);
        const twinPatch = { twinKey: 'twinValue' };

        deviceClient.open((err) => {
          if (err) throw err;
          debug('Device Client: Opened');
github Azure / azure-iot-sdk-node / ts-e2e / src / device_methods.tests.ts View on Github external
describe('Over ' + transportCtor.name, () => {
      // tslint:disable:no-invalid-this
      this.timeout(60000);

      const testDevice = testUtils.createTestDevice();
      const scs = ServiceConnectionString.parse(process.env.IOTHUB_CONNECTION_STRING);
      const testDeviceCS = DeviceConnectionString.createWithSharedAccessKey(scs.HostName, testDevice.deviceId, testDevice.authentication.symmetricKey.primaryKey);

      beforeEach((beforeEachCallback) => {
        testUtils.addTestDeviceToRegistry(testDevice, beforeEachCallback);
      });

      afterEach((afterEachCallback) => {
        testUtils.removeTestDeviceFromRegistry(testDevice, afterEachCallback);
      });

      [null, '', 'foo', { k1: 'v1' }, {}].forEach((testPayload) => {
      it('device can receive a method call with a payload of ' + JSON.stringify(testPayload) + ' and send a response', (testCallback) => {
        const methodName = 'testMethod';
        const requestPayload = testPayload;
        const responsePayload = { responseKey: uuid.v4() };
        const deviceClient = DeviceClient.fromConnectionString(testDeviceCS, transportCtor);
        let sendOK = false;
github Azure / azure-iot-sdk-node / ts-e2e / src / c2d.tests.ts View on Github external
describe('C2D', () => {
  // tslint:disable:no-invalid-this
  this.timeout(60000);
  const testDevice2 = testUtils.createTestDevice();

  const hostName = ServiceConnectionString.parse(process.env.IOTHUB_CONNECTION_STRING).HostName;
  const testDeviceCS2 = DeviceConnectionString.createWithSharedAccessKey(hostName, testDevice2.deviceId, testDevice2.authentication.symmetricKey.primaryKey);

  before((beforeCallback) => {
    testUtils.addTestDeviceToRegistry(testDevice2, beforeCallback);
  });

  after((afterCallback) => {
    testUtils.removeTestDeviceFromRegistry(testDevice2, afterCallback);
  });

  [DeviceAmqp, DeviceAmqpWs, DeviceMqtt, DeviceMqttWs, DeviceHttp].forEach((transportCtor: any) => {
    describe('Over ' + transportCtor.name, () => {
      let deviceClient: DeviceClient;

      beforeEach((beforeEachCallback) => {
        deviceClient = DeviceClient.fromConnectionString(testDeviceCS2, transportCtor);
        deviceClient.open((err) => {
github Azure / azure-iot-sdk-node / ts-e2e / src / file_upload.tests.ts View on Github external
describe('File upload', () => {
  // tslint:disable:no-invalid-this
  this.timeout(60000);
  const testDevice = testUtils.createTestDevice();

  const hostName = ServiceConnectionString.parse(process.env.IOTHUB_CONNECTION_STRING).HostName;
  const testDeviceCS = DeviceConnectionString.createWithSharedAccessKey(hostName, testDevice.deviceId, testDevice.authentication.symmetricKey.primaryKey);

  const fileConfig = {
    fileName: 'smallFile',
    fileSizeInKb: 10,
  };

  before((beforeCallback) => {
    let fileContent = Buffer.alloc(fileConfig.fileSizeInKb * 1024);
    fileContent.fill(uuid.v4());
    fs.writeFileSync(fileConfig.fileName, fileContent);
    testUtils.addTestDeviceToRegistry(testDevice, beforeCallback);
  });

  after((afterCallback) => {
    fs.unlinkSync(fileConfig.fileName);
    testUtils.removeTestDeviceFromRegistry(testDevice, afterCallback);
github microsoft / vscode-azure-iot-toolkit / src / deviceExplorer.ts View on Github external
let result = "Fail";
                if (res.statusCode < 300) {
                    result = "Success";
                    if (op === "Create" || op === "Delete") {
                        // Workaround for https://github.com/microsoft/vscode-azure-iot-toolkit/issues/331
                        setTimeout(() => {
                            vscode.commands.executeCommand("azure-iot-toolkit.refresh");
                        }, 500);
                    }
                }
                TelemetryClient.sendEvent(eventName, { Result: result }, iotHubConnectionString);
                this.outputLine(label, `[${op}][${result}] status: ${res.statusCode} ${res.statusMessage}`, outputChannel);
            }
            if (deviceInfo) {
                if (deviceInfo.authentication.SymmetricKey.primaryKey != null) {
                    deviceInfo.connectionString = ConnectionString.createWithSharedAccessKey(hostName,
                        deviceInfo.deviceId, deviceInfo.authentication.SymmetricKey.primaryKey);
                }
                if (deviceInfo.authentication.x509Thumbprint.primaryThumbprint != null) {
                    deviceInfo.connectionString = ConnectionString.createWithX509Certificate(hostName, deviceInfo.deviceId);
                }
                this.outputLine(label, `[${op}] device info: ${JSON.stringify(deviceInfo, null, 2)}`, outputChannel);
                resolve(deviceInfo);
            }
            resolve();
        };
    }
github Azure / azure-iot-sdk-node / longhaultests / src / iothub_longhaul.ts View on Github external
timeout(registry.create.bind(registry), MAX_CREATE_TIME)({ deviceId: deviceId }, (err, deviceInfo) => {
    if (err) {
      debug('error creating device: ' + deviceId + ':' + err.toString());
      callback(err);
    } else {
      debug('device created: ' + deviceId);
      const cs = HubConnectionString.parse(hubConnectionString);
      callback(null, DeviceConnectionString.createWithSharedAccessKey(cs.HostName, deviceInfo.deviceId, deviceInfo.authentication.symmetricKey.primaryKey));
    }
  });
};
github formulahendry / vscode-azure-iot-toolkit / src / deviceTree.ts View on Github external
deviceList.forEach((device, index) => {
                        let image = device.connectionState.toString() === "Connected" ? "device-on.png" : "device-off.png";
                        let deviceConnectionString = "";
                        if (device.authentication.SymmetricKey.primaryKey != null) {
                            deviceConnectionString = ConnectionString.createWithSharedAccessKey(hostName, device.deviceId,
                                device.authentication.SymmetricKey.primaryKey);
                        } else if (device.authentication.x509Thumbprint.primaryThumbprint != null) {
                            deviceConnectionString = ConnectionString.createWithX509Certificate(hostName, device.deviceId);
                        }
                        devices.push(new DeviceItem(device.deviceId,
                            deviceConnectionString,
                            this.context.asAbsolutePath(path.join("resources", image)),
                            {
                                command: "azure-iot-toolkit.getDevice",
                                title: "",
                                arguments: [device.deviceId],
                            }));
                    });
                    resolve(devices);
github microsoft / vscode-azure-iot-toolkit / src / utility.ts View on Github external
deviceList.forEach((device, index) => {
                        let deviceConnectionString: string = "";
                        if (device.authentication.SymmetricKey.primaryKey != null) {
                            deviceConnectionString = DeviceConnectionString.createWithSharedAccessKey(hostName, device.deviceId,
                                device.authentication.SymmetricKey.primaryKey);
                        } else if (device.authentication.x509Thumbprint.primaryThumbprint != null) {
                            deviceConnectionString = DeviceConnectionString.createWithX509Certificate(hostName, device.deviceId);
                        }
                        devices.push(new DeviceItem(device.deviceId,
                            deviceConnectionString,
                            null,
                            device.connectionState.toString(),
                            null));
                    });
                    resolve(devices.sort((a: DeviceItem, b: DeviceItem) => { return a.deviceId.localeCompare(b.deviceId); }));