Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public getTrustBundle(callback: (err?: Error, ca?: string) => void): void {
// Codes_SRS_NODE_IOTEDGED_AUTHENTICATION_PROVIDER_13_020: [ The getTrustBundle method shall throw a ReferenceError if the callback parameter is falsy or is not a function. ]
if (!callback || typeof callback !== 'function') {
throw new ReferenceError('callback cannot be \'' + callback + '\'');
}
// Codes_SRS_NODE_IOTEDGED_AUTHENTICATION_PROVIDER_13_022: [ The getTrustBundle method shall build the HTTP request path in the format /trust-bundle?api-version=2018-06-28. ]
const path = `/trust-bundle?api-version=${encodeUriComponentStrict(WORKLOAD_API_VERSION)}`;
// Codes_SRS_NODE_IOTEDGED_AUTHENTICATION_PROVIDER_13_021: [ The getTrustBundle method shall invoke this._restApiClient.executeApiCall to make the REST call on iotedged using the GET method. ]
// Codes_SRS_NODE_IOTEDGED_AUTHENTICATION_PROVIDER_13_023: [** The `getTrustBundle` method shall set the HTTP request option's `request` property to use the `http.request` object.
// Codes_SRS_NODE_IOTEDGED_AUTHENTICATION_PROVIDER_13_024: [** The `getTrustBundle` method shall set the HTTP request option's `port` property to use the workload URI's port if available.
this._restApiClient.executeApiCall('GET', path, null, null, this._getRequestOptions(), (err, ca) => {
if (err) {
callback(err);
} else {
callback(null, ca.certificate);
}
});
}
throw new ReferenceError('callback cannot be \'' + callback + '\'');
}
// Codes_SRS_NODE_IOTEDGED_AUTHENTICATION_PROVIDER_13_010: [ The _sign method invoke callback with a ReferenceError if the resourceUri parameter is falsy. ]
if (!resourceUri) {
callback(new ReferenceError('resourceUri cannot be \'' + resourceUri + '\''), null);
return;
}
// Codes_SRS_NODE_IOTEDGED_AUTHENTICATION_PROVIDER_13_011: [ The _sign method shall build the HTTP request path in the format /modules//genid//sign?api-version=2018-06-28. ]
// the request path needs to look like this:
// /modules//genid//sign?api-version=2018-06-28
const path = `/modules/${encodeUriComponentStrict(this._authConfig.moduleId)}/genid/${encodeUriComponentStrict(
this._authConfig.generationId
)}/sign?api-version=${encodeUriComponentStrict(WORKLOAD_API_VERSION)}`;
// Codes_SRS_NODE_IOTEDGED_AUTHENTICATION_PROVIDER_13_027: [** The `_sign` method shall use the `SharedAccessSignature.createWithSigningFunction` function to build the data buffer which is to be signed by iotedged.
SharedAccessSignature.createWithSigningFunction(this._credentials, expiry, (buffer, signCallback) => {
// Codes_SRS_NODE_IOTEDGED_AUTHENTICATION_PROVIDER_13_014: [ The _sign method shall build an object with the following schema as the HTTP request body as the sign request:
// interface SignRequest {
// keyId: string;
// algo: string;
// data: string;
// }
// ]
// Codes_SRS_NODE_IOTEDGED_AUTHENTICATION_PROVIDER_13_013: [ The _sign method shall build the sign request using the following values:
// const signRequest = {
// keyId: "primary"
// algo: "HMACSHA256"
// data: `${data}\n${expiry}`
// Codes_SRS_NODE_IOTEDGED_AUTHENTICATION_PROVIDER_13_009: [ The _sign method shall throw a ReferenceError if the callback parameter is falsy or is not a function. ]
if (!callback || typeof callback !== 'function') {
throw new ReferenceError('callback cannot be \'' + callback + '\'');
}
// Codes_SRS_NODE_IOTEDGED_AUTHENTICATION_PROVIDER_13_010: [ The _sign method invoke callback with a ReferenceError if the resourceUri parameter is falsy. ]
if (!resourceUri) {
callback(new ReferenceError('resourceUri cannot be \'' + resourceUri + '\''), null);
return;
}
// Codes_SRS_NODE_IOTEDGED_AUTHENTICATION_PROVIDER_13_011: [ The _sign method shall build the HTTP request path in the format /modules//genid//sign?api-version=2018-06-28. ]
// the request path needs to look like this:
// /modules//genid//sign?api-version=2018-06-28
const path = `/modules/${encodeUriComponentStrict(this._authConfig.moduleId)}/genid/${encodeUriComponentStrict(
this._authConfig.generationId
)}/sign?api-version=${encodeUriComponentStrict(WORKLOAD_API_VERSION)}`;
// Codes_SRS_NODE_IOTEDGED_AUTHENTICATION_PROVIDER_13_027: [** The `_sign` method shall use the `SharedAccessSignature.createWithSigningFunction` function to build the data buffer which is to be signed by iotedged.
SharedAccessSignature.createWithSigningFunction(this._credentials, expiry, (buffer, signCallback) => {
// Codes_SRS_NODE_IOTEDGED_AUTHENTICATION_PROVIDER_13_014: [ The _sign method shall build an object with the following schema as the HTTP request body as the sign request:
// interface SignRequest {
// keyId: string;
// algo: string;
// data: string;
// }
// ]
// Codes_SRS_NODE_IOTEDGED_AUTHENTICATION_PROVIDER_13_013: [ The _sign method shall build the sign request using the following values:
// const signRequest = {
// keyId: "primary"
this._init((err) => {
if (err) {
/*Codes_SRS_NODE_DEVICE_METHOD_CLIENT_16_008: [The `invokeMethod` method shall call its callback with an `Error` if it fails to get the latest credentials from the `AuthenticationProvider` object.]*/
callback(err);
} else {
/*Codes_SRS_NODE_DEVICE_METHOD_CLIENT_16_009: [The `invokeMethod` method shall call the `setOptions` method on the `RestApiClient` with its options as argument to make sure the CA certificate is populated.]*/
this._restApiClient.setOptions(this._options);
/*Codes_SRS_NODE_DEVICE_METHOD_CLIENT_16_010: [The `invokeMethod` method shall construct the HTTP request path as `/twins/encodeUriComponentStrict()/methods` if the target is a device.]*/
let path = `/twins/${encodeUriComponentStrict(deviceId)}`;
/*Codes_SRS_NODE_DEVICE_METHOD_CLIENT_16_011: [The `invokeMethod` method shall construct the HTTP request path as `/twins/encodeUriComponentStrict()/modules/encodeUriComponentStrict()/methods` if the target is a module.]*/
if (moduleId) {
path += `/modules/${encodeUriComponentStrict(moduleId)}`;
}
path += '/methods';
/*Codes_SRS_NODE_DEVICE_METHOD_CLIENT_16_012: [The `invokeMethod` method shall call `RestApiClient.executeApiCall` with:
- `POST` for the HTTP method argument.
- `path` as defined in `SRS_NODE_DEVICE_METHOD_CLIENT_16_010` and `SRS_NODE_DEVICE_METHOD_CLIENT_16_011`
- 2 custom headers:
- `Content-Type` shall be set to `application/json`
- `x-ms-edge-moduleId` shall be set to `/` with `deviceId` and `moduleId` being the identifiers for the current module (as opposed to the target module)
- the stringified version of the `MethodParams` object as the body of the request
- a timeout value in milliseconds that is the sum of the `connectTimeoutInSeconds` and `responseTimeoutInSeconds` parameters of the `MethodParams` object.]*/
const body = JSON.stringify(methodParams);
const methodTimeout = 1000 * (methodParams.connectTimeoutInSeconds + methodParams.responseTimeoutInSeconds);
/*Codes_SRS_NODE_SAK_AUTH_PROVIDER_16_009: [Every token shall be created with a validity period of `tokenValidTimeInSeconds` if specified when the constructor was called, or 1 hour by default.]*/
const newExpiry = Math.floor(Date.now() / 1000) + this._tokenValidTimeInSeconds;
/*Codes_SRS_NODE_SAK_AUTH_PROVIDER_16_010: [Every token shall be created using the `azure-iot-common.SharedAccessSignature.create` method and then serialized as a string, with the arguments to the create methods being:
```
resourceUri: /devices/
keyName: the `SharedAccessKeyName` parameter of the connection string or `null`
key: the `SharedAccessKey` parameter of the connection string
expiry: the expiration time of the token, which is now + the token validity time, formatted as the number of seconds since Epoch (Jan 1st, 1970, 00:00 UTC).
```]*/
let resourceString = this._credentials.host + '/devices/' + this._credentials.deviceId;
if (this._credentials.moduleId) {
resourceString += '/modules/' + this._credentials.moduleId;
}
const resourceUri = encodeUriComponentStrict(resourceString);
this._sign(resourceUri, newExpiry, (err, signature) => {
if (err) {
callback(err);
} else {
this._currentTokenExpiryTimeInSeconds = newExpiry;
this._credentials.sharedAccessSignature = signature;
callback(null, this._credentials);
}
});
}
function calPasswd(connectionString) {
var se = Math.round(new Date().getTime() / 1000) + 24 * 3600;
var uri = encodeUriComponentStrict(connectionString['HostName'] + '/devices/' + connectionString['DeviceId']);
return SharedAccessSignature.create(uri, null, connectionString['SharedAccessKey'], se).toString();
}
export function create(host: string, deviceId: string, key: string, expiry: string | number): SharedAccessSignature {
/*Codes_SRS_NODE_DEVICE_SAS_05_004: [ shall be the URL-encoded value of deviceId.]*/
const uri = encodeUriComponentStrict(host + '/devices/' + deviceId);
/*Codes_SRS_NODE_DEVICE_SAS_05_003: [The create method shall return the result of calling azure-iot-common.SharedAccessSignature.create with following arguments:
resourceUri - host + '%2Fdevices%2F' +
keyName - null
key - key
expiry - expiry]*/
return SharedAccessSignature.create(uri, null, key, expiry);
}
this._authenticationProvider.getDeviceCredentials((err, deviceCredentials) => {
/*Codes_SRS_NODE_FILE_UPLOAD_ENDPOINT_16_013: [`notifyUploadComplete` shall create a `POST` HTTP request to a path formatted as the following:`/devices/URI_ENCODED()/files/?api-version=`]*/
const path = endpoint.devicePath(encodeUriComponentStrict(deviceCredentials.deviceId)) + '/files/notifications/' + encodeURIComponent(correlationId) + endpoint.versionQueryString();
const body = JSON.stringify(uploadResult);
/*Codes_SRS_NODE_FILE_UPLOAD_ENDPOINT_16_014: [The `POST` HTTP request shall have the following headers:
```
Host: ,
Authorization: ,
'User-Agent': ,
'Content-Type': 'application/json; charset=utf-8',
'Content-Length': <content length="">,
'iothub-name':
```]*/
let headers: any = {
Host: deviceCredentials.host,
'User-Agent': packageJson.name + '/' + packageJson.version,
'Content-Type': 'application/json; charset=utf-8',
'Content-Length': body.length,</content>