How to use the azure-iothub.SharedAccessSignature.create 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.

github Azure / azure-iot-sdks / node / e2etests / eh / eventhubclient.js View on Github external
function EventHubClient(connString, path) {
  this.config = createConfig(connString, path);
  this.connected = false;
  this.connectPromise = null;

  if (!this.config.eventHubName) {
    throw new Error('No event hub name specified');
  }

  var token = SharedAccessSignature.create(this.config.host, this.config.keyName, this.config.key, anHourFromNow()).toString();

  this.uri = 'amqps://' +
  encodeURIComponent(this.config.keyName + '@sas.root.' + this.config.namespace) + ':' +
  encodeURIComponent(token) + '@' +
  this.config.host;

  this.amqpClient = new amqp10.Client(amqp10.Policy.EventHub);
}
github Azure / iothub-explorer / iothub-explorer-login.js View on Github external
var showDeprecationText = require('./common.js').showDeprecationText;

showDeprecationText('az login');

program
  .description('Create a temporary session on your IoT hub')
  .option('-d, --duration <duration-in-seconds>', 'time to keep the session open for (in seconds): if not specified, the default is one hour', parseInt)
  .parse(process.argv);

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

var connString = program.args[0];
var nowInSeconds = Math.floor(Date.now() / 1000);
var expiry = program.duration ? nowInSeconds + program.duration : nowInSeconds + 3600;
var cn = ConnectionString.parse(connString);
var sas = SharedAccessSignature.create(cn.HostName, cn.SharedAccessKeyName, cn.SharedAccessKey, expiry);
var loc = configLoc();

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

fs.mkdir(loc.dir, function () {
  var sessionFilePath = path.join(loc.dir, loc.file);
  fs.writeFile(sessionFilePath, sas.toString(), function (err) {
    if (err) inputError(err.toString());
    else {
      printSuccess('Session started, expires on ' + new Date(expiry * 1000).toString());
      printSuccess('Session file: ' + sessionFilePath);
    }
  });
});
github microsoft / vscode-azure-iot-toolkit / src / utility.ts View on Github external
public static generateSasTokenForService(iotHubConnectionString: string, expiryInHours = 1): string {
        const connectionString = ConnectionString.parse(iotHubConnectionString);
        const expiry = Math.floor(Date.now() / 1000) + expiryInHours * 60 * 60;
        return SharedAccessSignature.create(connectionString.HostName, connectionString.SharedAccessKeyName, connectionString.SharedAccessKey, expiry).toString();
    }
github Azure / iothub-explorer / common.js View on Github external
function getSas(connectionString) {
  var sas;
  if (connectionString) {
    var cn;
    try {
      cn = ConnectionString.parse(connectionString);
    } catch (e) {
      if (e instanceof errors.ArgumentError) {
        inputError('Could not parse connection string: ' + connectionString);
      } else {
        throw e;
      }
    }
    var expiry = Math.floor(Date.now() / 1000) + 3600;
    sas = SharedAccessSignature.create(cn.HostName, cn.SharedAccessKeyName, cn.SharedAccessKey, expiry).toString();
  } else {
    sas = loadSasFromUserFile();
  }

  if (!sas) {
    inputError('You must either use the login command or the --login argument for iothub-explorer to authenticate with your IoT Hub instance');
  }

  return sas;
}
github Azure / iothub-explorer / lib / eventhubclient.js View on Github external
function EventHubClient(connString, path) {
  this.config = createConfig(connString, path);

  this.connectPromise = null;

  if (!this.config.eventHubName) {
    throw new Error('No event hub name specified');
  }

  var token = SharedAccessSignature.create(this.config.host, this.config.keyName, this.config.key, anHourFromNow()).toString();

  this.uri = 'amqps://' +
  encodeURIComponent(this.config.keyName + '@sas.root.' + this.config.namespace) + ':' +
  encodeURIComponent(token) + '@' +
  this.config.host;

  this.amqpClient = new amqp10.Client(amqp10.Policy.EventHub);
}