Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function ConnectionConfig(connectionString, path) {
var cn = aziot.ConnectionString.parse(connectionString);
this.isIotHub = !!cn.HostName; // HostName is present in IoTHub connection strings, Endpoint in the case of Event Hubs
this.keyName = cn.SharedAccessKeyName;
this.key = cn.SharedAccessKey;
if(this.isIotHub) {
this.host = cn.HostName;
var hubName = this.host.split('.')[0];
this.sharedAccessSignature = aziot.SharedAccessSignature.create(this.host, this.keyName, this.key, aziot.anHourFromNow());
this.path = path || 'messages/events/';
this.saslPlainUri = 'amqps://' +
encodeURIComponent(this.keyName) +
'%40sas.root.' +
hubName +
':' +
encodeURIComponent(this.sharedAccessSignature) +
'@' +
this.host;
} else {
var endpoint = cn.Endpoint || '';
this.host = (endpoint.match('sb://([^/]*)') || [])[1];
this.path = cn.EntityPath || path;
this.saslPlainUri = 'amqps://' +
encodeURIComponent(this.keyName) + ':' +
encodeURIComponent(this.key) + '@' +
function ConnectionConfig(connectionString, path) {
var cn = aziot.ConnectionString.parse(connectionString);
this.isIotHub = !!cn.HostName; // HostName is present in IoTHub connection strings, Endpoint in the case of Event Hubs
this.keyName = cn.SharedAccessKeyName;
this.key = cn.SharedAccessKey;
if(this.isIotHub) {
this.host = cn.HostName;
var hubName = this.host.split('.')[0];
this.sharedAccessSignature = aziot.SharedAccessSignature.create(this.host, this.keyName, this.key, aziot.anHourFromNow());
this.path = path || 'messages/events/';
this.saslPlainUri = 'amqps://' +
encodeURIComponent(this.keyName) +
'%40sas.root.' +
hubName +
':' +
encodeURIComponent(this.sharedAccessSignature) +
'@' +
this.host;
} else {
var endpoint = cn.Endpoint || '';
this.host = (endpoint.match('sb://([^/]*)') || [])[1];
this.path = cn.EntityPath || path;
this.saslPlainUri = 'amqps://' +
encodeURIComponent(this.keyName) + ':' +
encodeURIComponent(this.key) + '@' +
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
'use strict';
var SharedAccessSignature = require('azure-iot-common').SharedAccessSignature;
var goodJobId = "g00dj0b";
function Response(statusCode) {
this.statusCode = statusCode;
}
function createError() {
var err = new Error();
err.response = new Response(401);
return err;
}
function SimulatedHttp(config) {
this.fakeFailure = false;
this.handleRequest = function (done, body) {
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
'use strict';
var Base = require('azure-iot-common').SharedAccessSignature;
module.exports = {
create: function create(host, policy, key, expiry) {
/*Codes_SRS_NODE_IOTHUB_SAS_05_003: [The create method shall return the result of calling azure-iot-common.SharedAccessSignature.create with following arguments:
resourceUri - host
keyName - policy
key - key
expiry - expiry]*/
return Base.create(host, policy, key, expiry);
},
parse: function parse(source) {
/*Codes_SRS_NODE_IOTHUB_SAS_05_001: [The parse method shall return the result of calling azure-iot-common.SharedAccessSignature.parse.]*/
/*Codes_SRS_NODE_IOTHUB_SAS_05_002: [It shall throw ArgumentError if any of 'sr', 'sig', 'skn' or 'se' fields are not found in the source argument.]*/
return Base.parse(source, ['sr', 'sig', 'skn', 'se']);
}
};
function create(host, policy, key, expiry) {
/*Codes_SRS_NODE_IOTHUB_SAS_05_003: [The create method shall return the result of calling azure-iot-common.SharedAccessSignature.create with following arguments:
resourceUri - host
keyName - policy
key - key
expiry - expiry]*/
return azure_iot_common_1.SharedAccessSignature.create(host, policy, key, expiry);
}
exports.create = create;
Client.fromSharedAccessSignature = function (sharedAccessSignature, transportCtor) {
/*Codes_SRS_NODE_IOTHUB_CLIENT_05_005: [The fromSharedAccessSignature method shall throw ReferenceError if the sharedAccessSignature argument is falsy.]*/
if (!sharedAccessSignature)
throw new ReferenceError('sharedAccessSignature is \'' + sharedAccessSignature + '\'');
/*Codes_SRS_NODE_IOTHUB_CLIENT_16_019: [The `fromSharedAccessSignature` method shall use the `Transport` constructor passed as argument to instantiate a transport object if it's not falsy.]*/
/*Codes_SRS_NODE_IOTHUB_CLIENT_16_020: [The `fromSharedAccessSignature` method shall use the default Transport (Amqp) if the `Transport` optional argument is falsy.]*/
if (!transportCtor) {
transportCtor = amqp_1.Amqp;
}
var sas = azure_iot_common_1.SharedAccessSignature.parse(sharedAccessSignature);
var decodedUri = decodeURIComponent(sas.sr);
/*Codes_SRS_NODE_IOTHUB_CLIENT_16_018: [The `fromSharedAccessSignature` method shall create a new transport instance and pass it a config object formed from the connection string given as argument.]*/
var config = {
host: decodedUri,
keyName: sas.skn,
sharedAccessSignature: sas.toString()
};
/*Codes_SRS_NODE_IOTHUB_CLIENT_05_007: [The fromSharedAccessSignature method shall return a new instance of the Client object, as by a call to new Client(transport).]*/
return new Client(new transportCtor(config), new azure_iot_http_base_1.RestApiClient(config, packageJson.name + '/' + packageJson.version));
};
return Client;
updateSharedAccessSignature: function (updatedSAS, callback) {
/*Codes_SRS_NODE_IOTHUB_SERVICE_AMQP_16_031: [The `updateSharedAccessSignature` shall trigger a `putToken` call on the base transport if it is connected.]*/
var audience = azure_iot_common_1.SharedAccessSignature.parse(_this._config.sharedAccessSignature.toString(), ['sr', 'sig', 'se']).sr;
_this._amqp.putToken(audience, updatedSAS, callback);
},
amqpError: function (err) {
Registry.fromSharedAccessSignature = function (value) {
/*Codes_SRS_NODE_IOTHUB_REGISTRY_05_011: [The `fromSharedAccessSignature` method shall throw ReferenceError if the value argument is falsy.]*/
if (!value)
throw new ReferenceError('value is \'' + value + '\'');
/*Codes_SRS_NODE_IOTHUB_REGISTRY_05_012: [The `fromSharedAccessSignature` method shall derive and transform the needed parts from the shared access signature in order to create a `config` object for the constructor (see `SRS_NODE_IOTHUB_REGISTRY_05_001`).]*/
var sas = azure_iot_common_1.SharedAccessSignature.parse(value);
var config = {
host: sas.sr,
sharedAccessSignature: sas.toString()
};
/*Codes_SRS_NODE_IOTHUB_REGISTRY_05_013: [The fromSharedAccessSignature method shall return a new instance of the `Registry` object.]*/
return new Registry(config);
};
return Registry;