How to use the azure-iothub.Registry.fromSharedAccessSignature function in azure-iothub

To help you get started, we’ve selected a few azure-iothub 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 / iothub-explorer / iothub-explorer-get-twin.js View on Github external
showDeprecationText('az iot hub device-twin show');

program
  .description('Get the twin of the specified device')
  .usage('[options] <device-id>')
  .option('-l, --login <connection-string>', 'use the connection string provided as argument to use to authenticate with your IoT Hub instance')
  .option('-r, --raw', 'use this flag to return raw output instead of pretty-printed output')
  .parse(process.argv);

var deviceId = program.args[0];
if(!deviceId) inputError('You must specify a deviceId.');

var sas = getSas(program.login);

var registry = Registry.fromSharedAccessSignature(sas);
registry.getTwin(deviceId, function (err, twin) {
  if (err) serviceError(err);
  else {
    // The _registry property that shouldn't be printed and make prettyjson crash.
    delete twin._registry;
    var output = program.raw ? JSON.stringify(twin) : prettyjson.render(twin);
    console.log(output);
  }
});
github Azure / iothub-explorer / iothub-explorer-import-devices.js View on Github external
.usage('[options] --storage <storage-connection-string> --output <file-path>')
  .option('-i, --input <file-path>', 'path to the input file containing device descriptions')
  .option('-o, --output <file-path>', 'path indicating where to download the import log file generated by the service (empty in case of success)')
  .option('-s, --storage <storage-connection-string>', 'Azure blob storage connection string to be used during bulk import')
  .option('-l, --login <iothub-connection-string>', 'use the connection string provided as argument to use to authenticate with your IoT hub')
  .parse(process.argv);

if(!program.storage) inputError('A storage connection string with permissions to create a blob is required.');
if(!program.input) inputError('An input file path is required.');

var inputFile = program.input;
var outputFile = program.output;
var storageConnectionString = program.storage;
var sas = getSas(program.login);

var registry = Registry.fromSharedAccessSignature(sas);
var blobSvc = azureStorage.createBlobService(storageConnectionString);

var startDate = new Date();
var expiryDate = new Date(startDate);
// Use a large interval to account for clock errors and time to run the job.
expiryDate.setMinutes(startDate.getMinutes() + 100);
startDate.setMinutes(startDate.getMinutes() - 100);

var inputSharedAccessPolicy = {
  AccessPolicy: {
    Permissions: 'rl',
    Start: startDate,
    Expiry: expiryDate
  },
};
github Azure / iothub-explorer / iothub-explorer-sas-token.js View on Github external
.option('-l, --login <connectionString>', 'use the connection string provided as argument to use to authenticate with your IoT hub')
  .option('-d, --duration <durationInSeconds>', 'expiration time (in seconds): if not specified, the default is one hour', parseInt)
  .parse(process.argv);

if(!program.args[0]) inputError('You must specify a device id.');

var deviceId = program.args[0];
var nowInSeconds = Math.floor(Date.now() / 1000);
var expiry = program.duration ? nowInSeconds + program.duration : nowInSeconds + 3600;

if (isNaN(new Date(expiry * 1000))) {
  inputError('Invalid duration');
}

var serviceSas = getSas(program.login);
var registry = Registry.fromSharedAccessSignature(serviceSas);

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());
github Azure / iothub-explorer / iothub-explorer-query-twin.js View on Github external
var showDeprecationText = require('./common.js').showDeprecationText;

showDeprecationText('az iot hub query');

program
  .description('Query the registry for twins matching the SQL query passed as argument')
  .usage('[options] <device-id>')
  .option('-l, --login <connection-string>', 'use the connection string provided as argument to use to authenticate with your IoT Hub instance')
  .option('-r, --raw', 'use this flag to return raw output instead of pretty-printed output')
  .parse(process.argv);

if(!program.args[0]) inputError('You must specify a SQL query.');
var sqlQuery = program.args[0];
var sas = getSas(program.login);

var registry = Registry.fromSharedAccessSignature(sas);
var query = registry.createQuery(sqlQuery);
var onNewResults = function(err, results) {
  if (err) {
    serviceError(err);
  } else {
    results.forEach(function(twin) {
      delete twin._registry;
      var output = program.raw ? JSON.stringify(twin) : prettyjson.render(twin);
      console.log(output);
    });

    if(query.hasMoreResults) {
      query.nextAsTwin(onNewResults);
    }
  }
};
github Azure / iothub-explorer / iothub-explorer-export-devices.js View on Github external
.usage('[options] --storage <storage-connection-string> --output <file-path>')
  .option('-l, --login <connection-string>', 'connection string to use to authenticate with your IoT Hub instance')
  .option('-o, --output <file-path>', 'output file containing device descriptions')
  .option('-s, --storage <storage-connection-string>', 'Azure blob storage connection string to be used during bulk export')
  .option('-x, --exclude-keys', 'exclude symmetric keys in output (keys are included by default)')
  .parse(process.argv);

if(!program.storage) inputError('A storage connection string with permissions to create a blob is required');
if(!program.output) inputError('An output file path is required.');

var outputFile = program.output;
var storageConnectionString = program.storage;
var excludeKeys = program.excludeKeys || false;
var sas = getSas(program.login);

var registry = Registry.fromSharedAccessSignature(sas);
var blobSvc = azureStorage.createBlobService(storageConnectionString);

var startDate = new Date();
var expiryDate = new Date(startDate);
expiryDate.setMinutes(startDate.getMinutes() + 100);
startDate.setMinutes(startDate.getMinutes() - 100);

var outputSharedAccessPolicy = {
  AccessPolicy: {
    Permissions: 'rwd',
    Start: startDate,
    Expiry: expiryDate
  },
};

var outputContainerName = 'exportcontainer';
github Azure / iothub-explorer / iothub-explorer-delete.js View on Github external
showDeprecationText('az iot hub device-identity delete');

program
  .description('Delete a device identity from your IoT hub device registry')
  .usage('[options] <device-id>')
  .option('-l, --login <connection-string>', 'connection string to use to authenticate with your IoT hub')
  .parse(process.argv);

if(!program.args[0]) {
  inputError('You must specify a deviceId');
}

var deviceId = program.args[0];
var sas = getSas(program.login);

var registry = Registry.fromSharedAccessSignature(sas);
registry.delete(deviceId, function (err) {
  if (err) serviceError(err);
  else printSuccess('Deleted device ' + deviceId);
});
github Azure / iothub-explorer / iothub-explorer-simulate-device.js View on Github external
var sendCount = program.sendCount || Number.MAX_SAFE_INTEGER;
var receiveCount = program.receiveCount || Number.MAX_SAFE_INTEGER;
var uploadFilePath = program.uploadFile;

var deviceConnectionString;
var deviceId = program.args[0];
if (!deviceId) {
  deviceConnectionString = program.deviceConnectionString;
  if(!deviceConnectionString) {
    inputError('You must specify either a device connection string (--device-connection-string) or the IoT Hub connection string and a device id as first argument');
  } else {
    deviceId = DeviceConnectionString.parse(deviceConnectionString).DeviceId;
    simulateDevice();
  }
} else {
  var registry = Registry.fromSharedAccessSignature(sas.toString());
  registry.get(deviceId, function(err, deviceInfo) {
    if (err) serviceError(err);
    else {
      var host = getHostFromSas(sas.toString());
      deviceConnectionString = createDeviceConnectionString(deviceInfo, host);
    }

    simulateDevice();
  });
}

function simulateDevice() {
  if (!deviceConnectionString) throw new Error('Couldn\'t figure out device connection string');
  if (!Protocol) throw new Error('Couldn\'t figure out protocol to connect to IoT Hub');

  var sendRunning = !!program.send;
github Azure / iothub-explorer / iothub-explorer-create.js View on Github external
function createDevice(deviceInfo) {
  var sas = getSas(program.login);

  var registry = Registry.fromSharedAccessSignature(sas);
  registry.create(deviceInfo, function (err, createdDeviceInfo) {
    if (err) serviceError(err);
    else {
      printSuccess('Created device ' + deviceInfo.deviceId);
      var host = getHostFromSas(sas);
      printDevice(createdDeviceInfo, host, program.display, program.raw);
    }
  });
}
github Azure / iothub-explorer / iothub-explorer-get.js View on Github external
showDeprecationText('az iot hub device-identity show');

program
  .description('Get a device identity from your IoT hub device registry')
  .usage('[options] <device-id>')
  .option('-c, --connection-string', '[deprecated] The connection string is now displayed by default')
  .option('-d, --display <property-filter>', 'filter the properties of the device that should be displayed using a comma-separated list of property names')
  .option('-l, --login <connection-string>', 'use the connection string provided as argument to use to authenticate with your IoT Hub instance')
  .option('-r, --raw', 'use this flag to return raw output instead of pretty-printed output')
  .parse(process.argv);

if(!program.args[0]) inputError('You must specify a deviceId.');
var sas = getSas(program.login);
var deviceId = program.args[0];

var registry = Registry.fromSharedAccessSignature(sas);
registry.get(deviceId, function (err, device) {
  if (err) serviceError(err);
  else {
    var host = getHostFromSas(sas);
    printDevice(device, host, program.display, program.raw);
  }
});