Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('can send "imploded" telemetry and it is received on the Event Hubs endpoint', function (done) {
this.timeout(60000);
const testTelemetryBody = {
firstTelemetryProperty: 1,
thirdTelemetryProperty: 'end'
};
const startAfterTime = Date.now() - 5000;
let ehClient;
// test device client
const deviceSasExpiry = Math.floor(new Date() / 1000) + 3600;
const deviceSas = DeviceSas.create(hubHostName, createdDevice.deviceId, createdDevice.authentication.symmetricKey.primaryKey, deviceSasExpiry);
const deviceClient = DeviceClient.fromSharedAccessSignature(deviceSas, Mqtt);
const digitalTwinClient = new DigitalTwinDeviceClient(capabilityModelDocument['@id'], deviceClient);
const testInterfaceInstance = new TestInterfaceInstance('testInterfaceInstance', function () {}, function () {});
digitalTwinClient.addInterfaceInstance(testInterfaceInstance);
const onEventHubMessage = function (eventData) {
if (eventData.annotations['iothub-connection-device-id'] === createdDevice.deviceId) {
debug('received a message from the test device: ');
debug(JSON.stringify(eventData.body));
if ((eventData.body.firstTelemetryProperty && eventData.body.firstTelemetryProperty === testTelemetryBody.firstTelemetryProperty) &&
(eventData.body.thirdTelemetryProperty && eventData.body.thirdTelemetryProperty === testTelemetryBody.thirdTelemetryProperty) &&
(Object.keys(eventData.body).length === 2)) {
debug('found telemetry message from test device. test successful.');
closeClients(deviceClient, ehClient, done);
}
} else {
it('service client gets the digital twin, updates a property using a full patch, the device accepts the property update and the service is updated again.', function (done) {
this.timeout(60000);
// test device client
const deviceSasExpiry = Math.floor(new Date() / 1000) + 3600;
const deviceSas = DeviceSas.create(hubHostName, createdDevice.deviceId, createdDevice.authentication.symmetricKey.primaryKey, deviceSasExpiry);
const deviceClient = DeviceClient.fromSharedAccessSignature(deviceSas, Mqtt);
const digitalTwinClient = new DigitalTwinDeviceClient(capabilityModelDocument['@id'], deviceClient);
const desiredPropertyValue = uuid.v4();
const closeClients = function (deviceClient, err) {
debug('closing device and event hubs clients');
deviceClient.close()
.then(() => {
debug('device client closed');
done(err);
}).catch((closeErr)=> {
debug('error closing clients: ' + closeErr.toString());
done(err || closeErr);
});
};
it('service invokes an async command and it is acknowledged then updated by the device client', function (done) {
this.timeout(60000); // eslint-disable-line no-invalid-this
// test device client
const deviceSasExpiry = Math.floor(new Date() / 1000) + 3600;
const deviceSas = DeviceSas.create(hubHostName, createdDevice.deviceId, createdDevice.authentication.symmetricKey.primaryKey, deviceSasExpiry);
const deviceClient = DeviceClient.fromSharedAccessSignature(deviceSas, Mqtt);
const digitalTwinClient = new DigitalTwinDeviceClient(capabilityModelDocument['@id'], deviceClient);
const digitalTwinServiceClient = new DigitalTwinServiceClient(credentials);
const startAfterTime = Date.now() - 5000;
let ehClient;
let requestId;
const onEventHubMessage = function (eventData) {
if (eventData.annotations['iothub-connection-device-id'] === createdDevice.deviceId) {
debug('received a message from the test device: ');
if (eventData.body.modelInformation) {
debug('device registered its interfaces');
}
if (eventData.applicationProperties && eventData.applicationProperties['iothub-command-name'] === asyncCommandName) {
assert.strictEqual(eventData.body, invokeCommandResponse);
this.handleRequest = function (done) {
var sig = SharedAccessSignature.parse(config.sharedAccessSignature);
if (config.host === 'bad') { // bad host
done(new Error('Invalid host address'));
}
else if (config.deviceId === 'bad') { // bad policy
done(makeError('Connection Refused'));
}
else if (config.gatewayHostName === 'bad') { // bad gateway url
done(makeError('Invalid gateway address'));
}
else {
var cmpSig = (SharedAccessSignature.create(config.host, config.deviceId, 'bad', sig.se)).toString();
if (config.sharedAccessSignature === cmpSig) { // bad key
done(makeError('Connection Refused'));
}
else {
done(null, 'OK');
}
}
};
}
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());
printSuccess('is valid until: ' + new Date(expiry * 1000).toString());
}
}
});
public static generateSasTokenForDevice(deviceConnectionString: string, expiryInHours = 1): string {
const connectionString = DeviceConnectionString.parse(deviceConnectionString);
const expiry = Math.floor(Date.now() / 1000) + expiryInHours * 60 * 60;
return DeviceSharedAccessSignature.create(connectionString.HostName, connectionString.DeviceId, connectionString.SharedAccessKey, expiry).toString();
}