Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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);
}
var showDeprecationText = require('./common.js').showDeprecationText;
showDeprecationText('az login');
program
.description('Create a temporary session on your IoT hub')
.option('-d, --duration ', '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);
}
});
});
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();
}
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;
}
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);
}