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);
}
if (!connStr) throw new ReferenceError('connStr is \'' + connStr + '\'');
/*Codes_SRS_NODE_IOTHUB_CLIENT_16_016: [The `fromConnectionString` 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_017: [The `fromConnectionString` method shall use the default Transport (Amqp) if the `Transport` optional argument is falsy.]*/
if(!Transport){
Transport = DefaultTransport;
}
/*Codes_SRS_NODE_IOTHUB_CLIENT_16_015: [The `fromConnectionString` method shall create a new transport instance and pass it a config object formed from the connection string given as argument.]*/
var cn = ConnectionString.parse(connStr);
var config = {
hubName: cn.HostName.split('.', 1)[0],
host: cn.HostName,
keyName: cn.SharedAccessKeyName,
sharedAccessSignature: SharedAccessSignature.create(cn.HostName, cn.SharedAccessKeyName, cn.SharedAccessKey, anHourFromNow())
};
/*Codes_SRS_NODE_IOTHUB_CLIENT_05_004: [The fromConnectionString method shall return a new instance of the Client object, as by a call to new Client(transport).]*/
return new Client(new Transport(config), new RestApiClient(config));
};
Client.fromConnectionString = function fromConnectionString(value) {
/*Codes_SRS_NODE_IOTHUB_CLIENT_05_002: [The fromConnectionString method shall throw ReferenceError if the value argument is falsy.]*/
if (!value) throw new ReferenceError('value is \'' + value + '\'');
/*Codes_SRS_NODE_IOTHUB_CLIENT_05_003: [Otherwise, it shall derive and transform the needed parts from the connection string in order to create a new instance of the default transport (azure-iothub.Transport).]*/
var cn = ConnectionString.parse(value);
var sas = SharedAccessSignature.create(cn.HostName, cn.SharedAccessKeyName, cn.SharedAccessKey, anHourFromNow());
var config = {
hubName: cn.HostName.split('.', 1)[0],
host: cn.HostName,
keyName: cn.SharedAccessKeyName,
sharedAccessSignature: sas.toString()
};
/*Codes_SRS_NODE_IOTHUB_CLIENT_05_004: [The fromConnectionString method shall return a new instance of the Client object, as by a call to new Client(transport).]*/
return new Client(new DefaultTransport(config));
};
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);
}
private _handleSASRenewal(): void {
const newSas = (this._config.sharedAccessSignature as SharedAccessSignature).extend(anHourFromNow());
this._fsm.handle('updateSharedAccessSignature', newSas, (err) => {
if (err) {
debug('error automatically renewing the sas token: ' + err.toString());
} else {
this._renewalTimeout = setTimeout(this._handleSASRenewal.bind(this), this._renewalNumberOfMilliseconds);
}
});
}
}
_this._amqp.initializeCBS(function (err) {
if (err) {
debug('error trying to initialize CBS: ' + err.toString());
/*Codes_SRS_NODE_IOTHUB_SERVICE_AMQP_06_002: [If `initializeCBS` is not successful then the client will remain disconnected and the callback, if provided, will be invoked with an error object.]*/
_this._fsm.transition('disconnecting', callback);
}
else {
debug('CBS initialized');
/*Codes_SRS_NODE_IOTHUB_SERVICE_AMQP_06_003: [If `initializeCBS` is successful, `putToken` shall be invoked with the first parameter audience, created from the sr of the sas signature, the next parameter of the actual sas, and a callback.]*/
var audience = azure_iot_common_1.SharedAccessSignature.parse(_this._config.sharedAccessSignature.toString(), ['sr', 'sig', 'se']).sr;
var applicationSuppliedSas_1 = typeof (_this._config.sharedAccessSignature) === 'string';
var sasToken = applicationSuppliedSas_1 ? _this._config.sharedAccessSignature : _this._config.sharedAccessSignature.extend(azure_iot_common_1.anHourFromNow());
_this._amqp.putToken(audience, sasToken, function (err) {
if (err) {
/*Codes_SRS_NODE_IOTHUB_SERVICE_AMQP_06_004: [** If `putToken` is not successful then the client will remain disconnected and the callback, if provided, will be invoked with an error object.]*/
_this._fsm.transition('disconnecting', err);
}
else {
_this._fsm.transition('authenticated', applicationSuppliedSas_1, callback);
}
});
}
});
},
this._amqp.initializeCBS((err) => {
if (err) {
debug('error trying to initialize CBS: ' + err.toString());
/*Codes_SRS_NODE_IOTHUB_SERVICE_AMQP_06_002: [If `initializeCBS` is not successful then the client will remain disconnected and the callback, if provided, will be invoked with an error object.]*/
this._fsm.transition('disconnecting', err, callback);
} else {
debug('CBS initialized');
/*Codes_SRS_NODE_IOTHUB_SERVICE_AMQP_06_003: [If `initializeCBS` is successful, `putToken` shall be invoked with the first parameter audience, created from the sr of the sas signature, the next parameter of the actual sas, and a callback.]*/
const audience = SharedAccessSignature.parse(this._config.sharedAccessSignature.toString(), ['sr', 'sig', 'se']).sr;
const applicationSuppliedSas = typeof(this._config.sharedAccessSignature) === 'string';
const sasToken = applicationSuppliedSas ? this._config.sharedAccessSignature as string : (this._config.sharedAccessSignature as SharedAccessSignature).extend(anHourFromNow());
this._amqp.putToken(audience, sasToken, (err) => {
if (err) {
/*Codes_SRS_NODE_IOTHUB_SERVICE_AMQP_06_004: [** If `putToken` is not successful then the client will remain disconnected and the callback, if provided, will be invoked with an error object.]*/
this._fsm.transition('disconnecting', err, callback);
} else {
this._fsm.transition('authenticated', applicationSuppliedSas, callback);
}
});
}
});
},
private _createRegistrationSas(registrationInfo: TpmRegistrationInfo, callback: (err: Error, sasToken?: string) => void): void {
/*Codes_SRS_NODE_DPS_TPM_REGISTRATION_16_005: [The `register` method shall create a signature for the initial SAS token by signing the following payload with the session key and the `TpmSecurityClient`:
```
/registrations/\n
```
with:
- `idScope` being the value of the `idScope` argument passed to the `TpmRegistration` constructor.
- `registrationId` being the previously computed registration id.
- `expiryTimeUtc` being the number of seconds since Epoch + a delay during which the initial sas token should be valid (1 hour by default).
]*/
const expiryTimeUtc = anHourFromNow();
const audience = encodeURIComponent(registrationInfo.request.idScope + '/registrations/' + registrationInfo.request.registrationId);
const payload = new Buffer(audience + '\n' + expiryTimeUtc.toString());
this._securityClient.signWithIdentity(payload, (err, signedBytes) => {
if (err) {
debug('failed to sign the initial authentication payload with sessionKey: ' + err.toString());
callback(err);
} else {
const signature = encodeURIComponent(signedBytes.toString('base64'));
/*Codes_SRS_NODE_DPS_TPM_REGISTRATION_16_006: [The `register` method shall create a SAS token to be used to get the actual registration result as follows:
```
SharedAccessSignature sr=&sig=&se=&skn=registration
```
With the following fields:
- `audience`: /registrations/
- `signature`: the base64 encoded version of the signature generated per `SRS_NODE_DPS_TPM_REGISTRATION_16_005`