Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// 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 ConnectionString = require('azure-iot-common').ConnectionString;
var runTests = require('azure-iot-device-amqp/lib/_client_test_integration.js');
var AmqpWs = require('./amqp_ws.js');
var host = ConnectionString.parse(process.env.IOTHUB_CONNECTION_STRING).HostName;
var deviceId = process.env.IOTHUB_DEVICE_ID;
var key = process.env.IOTHUB_DEVICE_KEY;
function makeConnectionString(host, device, key) {
return 'HostName='+host+';DeviceId='+device+';SharedAccessKey='+key;
}
var connectionString = makeConnectionString(host, deviceId, key);
var badConnStrings = [
makeConnectionString('bad', 'device', 'key'),
makeConnectionString('host', 'bad', 'key'),
makeConnectionString('host', 'device', 'bad')
];
describe('Over AMQP/WS', function () {
// 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 assert = require('chai').assert;
var Client = require('azure-iot-device').Client;
var ConnectionString = require('azure-iot-common').ConnectionString;
var Message = require('azure-iot-common').Message;
var Amqp = require('./amqp.js');
var host = ConnectionString.parse(process.env.IOTHUB_CONNECTION_STRING).HostName;
var deviceId = process.env.IOTHUB_DEVICE_ID;
var key = process.env.IOTHUB_DEVICE_KEY;
function makeConnectionString(host, device, key) {
return 'HostName='+host+';DeviceId='+device+';SharedAccessKey='+key;
}
var connectionString = makeConnectionString(host, deviceId, key);
var badConnStrings = [
makeConnectionString('bad', deviceId, key),
makeConnectionString(host, 'bad', key),
makeConnectionString(host, deviceId, 'bad')
];
function badConfigTests(opName, badConnStrings, Transport, requestFn) {
function createConfig(connectionString, entityPath) {
var cn = ConnectionString.parse(connectionString);
var host = cn.HostName || (cn.Endpoint || '').slice('sb://'.length);
return {
host: host,
namespace: host.split('.')[0],
keyName: cn.SharedAccessKeyName,
key: cn.SharedAccessKey,
eventHubName: entityPath
};
}
function createConfig(connectionString, entityPath) {
var cn = ConnectionString.parse(connectionString);
var host = cn.HostName || (cn.Endpoint || '').slice('sb://'.length);
return {
host: host,
namespace: host.split('.')[0],
keyName: cn.SharedAccessKeyName,
key: cn.SharedAccessKey,
eventHubName: entityPath
};
}
parse: function parse(source) {
/*Codes_SRS_NODE_IOTHUB_CONNSTR_05_001: [The parse method shall return the result of calling azure-iot-common.ConnectionString.parse.]*/
/*Codes_SRS_NODE_IOTHUB_CONNSTR_05_002: [It shall throw ArgumentError if any of 'HostName', 'SharedAccessKeyName', or 'SharedAccessKey' fields are not found in the source argument.]*/
return Base.parse(source, ['HostName', 'SharedAccessKeyName', 'SharedAccessKey']);
}
};
constructor(connectionString) {
this._connectionString = ConnectionString.parse(connectionString, ['HostName', 'SharedAccessKeyName', 'SharedAccessKey', 'RepositoryId']);
}
static fromConnectionString(connStr: string, transportCtor: any): ModuleClient {
/*Codes_SRS_NODE_MODULE_CLIENT_05_003: [The fromConnectionString method shall throw ReferenceError if the connStr argument is falsy.]*/
if (!connStr) throw new ReferenceError('connStr is \'' + connStr + '\'');
const cn = ConnectionString.parse(connStr);
/*Codes_SRS_NODE_MODULE_CLIENT_16_087: [The `fromConnectionString` method shall create a new `SharedAccessKeyAuthorizationProvider` object with the connection string passed as argument if it contains a SharedAccessKey parameter and pass this object to the transport constructor.]*/
let authenticationProvider: AuthenticationProvider;
if (cn.SharedAccessKey) {
authenticationProvider = SharedAccessKeyAuthenticationProvider.fromConnectionString(connStr);
} else {
/*Codes_SRS_NODE_MODULE_CLIENT_16_001: [The `fromConnectionString` method shall throw a `NotImplementedError` if the connection string does not contain a `SharedAccessKey` field because x509 authentication is not supported yet for modules.]*/
throw new errors.NotImplementedError('ModuleClient only supports SAS Token authentication');
}
/*Codes_SRS_NODE_MODULE_CLIENT_05_006: [The fromConnectionString method shall return a new instance of the Client object, as by a call to new Client(new transportCtor(...)).]*/
return new ModuleClient(new transportCtor(authenticationProvider), null);
}
static fromConnectionString(value: string): ProvisioningServiceClient {
/*Codes_SRS_NODE_PROVISIONING_SERVICE_CLIENT_06_005: [The `fromConnectionString` method shall throw `ReferenceError` if the `value` argument is falsy.]*/
if (!value) throw new ReferenceError('value is \'' + value + '\'');
const cn = ConnectionString.parse(value);
const config: RestApiClient.TransportConfig = {
host: cn.HostName,
sharedAccessSignature: SharedAccessSignature.create(cn.HostName, cn.SharedAccessKeyName, cn.SharedAccessKey, Date.now())
};
/*Codes_SRS_NODE_PROVISIONING_SERVICE_CLIENT_06_006: [`fromConnectionString` method shall derive and transform the needed parts from the connection string in order to create a `config` object for the constructor (see `SRS_NODE_PROVISIONING_SERVICE_CLIENT_06_002`).] */
/*Codes_SRS_NODE_PROVISIONING_SERVICE_CLIENT_06_007: [The `fromConnectionString` method shall return a new instance of the `ProvisioningServiceClient` object.] */
return new ProvisioningServiceClient(config);
}
parse: function parse(source) {
/*Codes_SRS_NODE_DEVICE_CONNSTR_05_001: [The parse method shall return the result of calling azure-iot-common.ConnectionString.parse.]*/
/*Codes_SRS_NODE_DEVICE_CONNSTR_05_002: [It shall throw ArgumentError if any of 'HostName' or 'DeviceId' fields are not found in the source argument.]*/
var connectionString = Base.parse(source, ['HostName', 'DeviceId']);
/*Codes_SRS_NODE_DEVICE_CONNSTR_16_001: [It shall throw `ArgumentError` if `SharedAccessKey` and `x509` are present at the same time or if none of them are present.]*/
if((connectionString.SharedAccessKey && connectionString.x509) || (!connectionString.SharedAccessKey && !connectionString.x509)) {
throw new errors.ArgumentError('The connection string must contain either a SharedAccessKey or x509=true');
}
return connectionString;
}
};
static fromConnectionString(connectionString: string, tokenValidTimeInSeconds?: number, tokenRenewalMarginInSeconds?: number): SharedAccessKeyAuthenticationProvider {
if (!connectionString) {
/*Codes_SRS_NODE_SAK_AUTH_PROVIDER_16_006: [The `fromConnectionString` method shall throw a `ReferenceError` if the `connectionString` parameter is falsy.]*/
throw new ReferenceError('connectionString cannot be \'' + connectionString + '\'');
}
/*Codes_SRS_NODE_SAK_AUTH_PROVIDER_16_007: [The `fromConnectionString` method shall throw an `errors.ArgumentError` if the `connectionString` does not have a SharedAccessKey parameter.]*/
const cs: ConnectionString = ConnectionString.parse(connectionString, ['DeviceId', 'HostName', 'SharedAccessKey']);
/*Codes_SRS_NODE_SAK_AUTH_PROVIDER_16_008: [The `fromConnectionString` method shall extract the credentials from the `connectionString` argument and create a new `SharedAccessKeyAuthenticationProvider` that uses these credentials to generate security tokens.]*/
const credentials: TransportConfig = {
host: cs.HostName,
gatewayHostName: cs.GatewayHostName,
deviceId: cs.DeviceId,
moduleId: cs.ModuleId,
sharedAccessKeyName: cs.SharedAccessKeyName,
sharedAccessKey: cs.SharedAccessKey
};
return new SharedAccessKeyAuthenticationProvider(credentials, tokenValidTimeInSeconds, tokenRenewalMarginInSeconds);
}
}