Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
this.handleRequest = function (done, body) {
var sig = SharedAccessSignature.parse(config.sharedAccessSignature);
if (config.host === 'bad') { // bad host
done(new Error('getaddrinfo ENOTFOUND bad'));
}
else if (sig.skn === 'bad') { // bad policy
done(createError());
}
else {
var cmpSig = SharedAccessSignature.create(sig.sr, sig.skn, 'bad', sig.se).toString();
if (config.sharedAccessSignature === cmpSig) { // bad key
done(createError());
}
else { // ok
done(null, body, new Response(204));
}
}
static fromSharedAccessSignature(sharedAccessSignature: string): SharedAccessSignatureAuthenticationProvider {
if (!sharedAccessSignature) {
/*Codes_SRS_NODE_SAS_AUTHENTICATION_PROVIDER_16_005: [The `fromSharedAccessSignature` method shall throw a `ReferenceError` if the `sharedAccessSignature` argument is falsy.]*/
throw new ReferenceError('sharedAccessSignature cannot be \'' + sharedAccessSignature + '\'');
}
const sas: SharedAccessSignature = SharedAccessSignature.parse(sharedAccessSignature);
const decodedUri = decodeURIComponent(sas.sr);
const uriSegments = decodedUri.split('/');
const credentials: TransportConfig = {
host: uriSegments[0],
deviceId: uriSegments[uriSegments.length - 1],
sharedAccessSignature: sharedAccessSignature
};
/*Codes_SRS_NODE_SAS_AUTHENTICATION_PROVIDER_16_006: [The `fromSharedAccessSignature` shall return a new `SharedAccessSignatureAuthenticationProvider` object initialized with the credentials parsed from the `sharedAccessSignature` argument.]*/
return new SharedAccessSignatureAuthenticationProvider(credentials);
}
}
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']);
}
};
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);
}
});
}
});
},
updateSharedAccessSignature: (sharedAccessSignature, updateSasCallback) => {
/*Codes_SRS_NODE_DEVICE_AMQP_06_010: [If the AMQP connection is established, the `updateSharedAccessSignature` method shall call the amqp transport `putToken` method with the first parameter `audience`, created from the `sr` of the shared access signature, the actual shared access signature, and a callback.]*/
this._amqp.putToken(SharedAccessSignature.parse(sharedAccessSignature, ['sr', 'sig', 'se']).sr, sharedAccessSignature, (err) => {
if (err) {
this._amqp.disconnect(() => {
updateSasCallback(getTranslatedError(err, 'AMQP Transport: Could not authorize with puttoken'));
});
} else {
/*Codes_SRS_NODE_DEVICE_AMQP_06_011: [The `updateSharedAccessSignature` method shall call the `done` callback with a null error object and a SharedAccessSignatureUpdated object as a result, indicating the client does NOT need to reestablish the transport connection.]*/
updateSasCallback(null, new results.SharedAccessSignatureUpdated(false));
}
});
},
getTwinReceiver: (callback) => {
export function parse(source: string): SharedAccessSignature {
/*Codes_SRS_NODE_DEVICE_SAS_05_001: [The parse method shall return the result of calling azure-iot-common.SharedAccessSignature.parse.]*/
/*Codes_SRS_NODE_DEVICE_SAS_05_002: [It shall throw ArgumentError if any of 'sr', 'sig', 'se' fields are not found in the source argument.]*/
return SharedAccessSignature.parse(source, ['sr', 'sig', 'se']);
}
export function parse(source: string): SharedAccessSignature {
/*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 SharedAccessSignature.parse(source, ['sr', 'sig', 'skn', 'se']);
}
this._amqp.initializeCBS((err) => {
if (err) {
/*Codes_SRS_NODE_DEVICE_AMQP_06_008: [If `initializeCBS` is not successful then the client will be disconnected.]*/
this._fsm.transition('disconnecting', connectCallback, getTranslatedError(err, 'AMQP Transport: Could not initialize CBS'));
} else {
/*Codes_SRS_NODE_DEVICE_AMQP_06_006: [If `initializeCBS` is successful, `putToken` shall be invoked If `initializeCBS` is successful, `putToken` shall be invoked with the first parameter `audience`, created from the `sr` of the shared access signature, the actual shared access signature, and a callback.]*/
const sasString = this._config.sharedAccessSignature.toString();
this._amqp.putToken(SharedAccessSignature.parse(sasString, ['sr', 'sig', 'se']).sr, sasString, (err) => {
if (err) {
/*Codes_SRS_NODE_DEVICE_AMQP_06_009: [If `putToken` is not successful then the client will be disconnected.]*/
this._fsm.transition('disconnecting', connectCallback, getTranslatedError(err, 'AMQP Transport: Could not authorize with puttoken'));
} else {
this._fsm.transition('authenticated', connectCallback, connectResult);
}
});
}
});
}
static fromSharedAccessSignature(value: string): Registry {
/*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`).]*/
const sas = SharedAccessSignature.parse(value);
const config: Registry.TransportConfig = {
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);
}