How to use the azure-iot-device.SharedAccessSignature.create 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 / digitaltwins / e2e / digitaltwin_telemetry.js View on Github external
it('can send "imploded" telemetry and it is received on the Event Hubs endpoint', function (done) {
    this.timeout(60000);

    const testTelemetryBody = {
      firstTelemetryProperty: 1,
      thirdTelemetryProperty: 'end'
    };
    const startAfterTime = Date.now() - 5000;
    let ehClient;

    // test device client
    const deviceSasExpiry = Math.floor(new Date() / 1000) + 3600;
    const deviceSas = DeviceSas.create(hubHostName, createdDevice.deviceId, createdDevice.authentication.symmetricKey.primaryKey, deviceSasExpiry);
    const deviceClient = DeviceClient.fromSharedAccessSignature(deviceSas, Mqtt);
    const digitalTwinClient = new DigitalTwinDeviceClient(capabilityModelDocument['@id'], deviceClient);
    const testInterfaceInstance = new TestInterfaceInstance('testInterfaceInstance', function () {}, function () {});
    digitalTwinClient.addInterfaceInstance(testInterfaceInstance);

    const onEventHubMessage = function (eventData) {
      if (eventData.annotations['iothub-connection-device-id'] === createdDevice.deviceId) {
        debug('received a message from the test device: ');
        debug(JSON.stringify(eventData.body));
        if ((eventData.body.firstTelemetryProperty && eventData.body.firstTelemetryProperty === testTelemetryBody.firstTelemetryProperty) &&
            (eventData.body.thirdTelemetryProperty && eventData.body.thirdTelemetryProperty === testTelemetryBody.thirdTelemetryProperty) &&
            (Object.keys(eventData.body).length === 2)) {
          debug('found telemetry message from test device. test successful.');
          closeClients(deviceClient, ehClient, done);
        }
      } else {
github Azure / azure-iot-sdk-node / digitaltwins / e2e / digitaltwin_properties.js View on Github external
it('service client gets the digital twin, updates a property using a full patch, the device accepts the property update and the service is updated again.', function (done) {
    this.timeout(60000);

    // test device client
    const deviceSasExpiry = Math.floor(new Date() / 1000) + 3600;
    const deviceSas = DeviceSas.create(hubHostName, createdDevice.deviceId, createdDevice.authentication.symmetricKey.primaryKey, deviceSasExpiry);
    const deviceClient = DeviceClient.fromSharedAccessSignature(deviceSas, Mqtt);
    const digitalTwinClient = new DigitalTwinDeviceClient(capabilityModelDocument['@id'], deviceClient);
    const desiredPropertyValue = uuid.v4();

    const closeClients = function (deviceClient, err) {
      debug('closing device and event hubs clients');
      deviceClient.close()
        .then(() => {
          debug('device client closed');
          done(err);
        }).catch((closeErr)=> {
          debug('error closing clients: ' + closeErr.toString());
          done(err || closeErr);
        });
    };
github Azure / azure-iot-sdk-node / digitaltwins / e2e / digitaltwin_commands.js View on Github external
it('service invokes an async command and it is acknowledged then updated by the device client', function (done) {
    this.timeout(60000); // eslint-disable-line no-invalid-this

    // test device client
    const deviceSasExpiry = Math.floor(new Date() / 1000) + 3600;
    const deviceSas = DeviceSas.create(hubHostName, createdDevice.deviceId, createdDevice.authentication.symmetricKey.primaryKey, deviceSasExpiry);
    const deviceClient = DeviceClient.fromSharedAccessSignature(deviceSas, Mqtt);
    const digitalTwinClient = new DigitalTwinDeviceClient(capabilityModelDocument['@id'], deviceClient);
    const digitalTwinServiceClient = new DigitalTwinServiceClient(credentials);
    const startAfterTime = Date.now() - 5000;

    let ehClient;
    let requestId;

    const onEventHubMessage = function (eventData) {
      if (eventData.annotations['iothub-connection-device-id'] === createdDevice.deviceId) {
        debug('received a message from the test device: ');
        if (eventData.body.modelInformation) {
          debug('device registered its interfaces');
        }
        if (eventData.applicationProperties && eventData.applicationProperties['iothub-command-name'] === asyncCommandName) {
          assert.strictEqual(eventData.body, invokeCommandResponse);
github Azure / azure-iot-sdks / node / device / transport / mqtt / lib / mqtt_simulated.js View on Github external
this.handleRequest = function (done) {
    var sig = SharedAccessSignature.parse(config.sharedAccessSignature);

    if (config.host === 'bad') {                      // bad host
      done(new Error('Invalid host address'));
    }
    else if (config.deviceId === 'bad') {             // bad policy
      done(makeError('Connection Refused'));
    }
    else if (config.gatewayHostName === 'bad') {      // bad gateway url
      done(makeError('Invalid gateway address'));
    }
    else {
      var cmpSig = (SharedAccessSignature.create(config.host, config.deviceId, 'bad', sig.se)).toString();
      if (config.sharedAccessSignature === cmpSig) {  // bad key
        done(makeError('Connection Refused'));
      }
      else {
        done(null, 'OK');
      }
    }
  };
}
github Azure / iothub-explorer / iothub-explorer-sas-token.js View on Github external
registry.get(deviceId, function (err, device) {
  if (err)
    serviceError(err);
  else {
    var key = device.authentication.symmetricKey.primaryKey || device.authentication.symmetricKey.secondaryKey;
    if (!key) {
      inputError('Cannot create a SAS for this device. It does not use symmetric key authentication.');
    }
    var host = getHostFromSas(serviceSas);
    var sas = deviceSas.create(host, deviceId, key, expiry);
    if (program.raw) {
      console.log(sas.toString());
    } else {
      printSuccess('Shared Access Key for ' + deviceId + ':');
      console.log(sas.toString());
      printSuccess('is valid until: ' + new Date(expiry * 1000).toString());
    }
  }
});
github microsoft / vscode-azure-iot-toolkit / src / utility.ts View on Github external
public static generateSasTokenForDevice(deviceConnectionString: string, expiryInHours = 1): string {
        const connectionString = DeviceConnectionString.parse(deviceConnectionString);
        const expiry = Math.floor(Date.now() / 1000) + expiryInHours * 60 * 60;
        return DeviceSharedAccessSignature.create(connectionString.HostName, connectionString.DeviceId, connectionString.SharedAccessKey, expiry).toString();
    }