azure-iot-http-base
Advanced tools
Comparing version 1.11.8 to 1.11.9-alpha.210607.1
import { SharedAccessSignature, X509 } from 'azure-iot-common'; | ||
import { Http as HttpBase, HttpRequestOptions } from './http'; | ||
import { AccessToken, TokenCredential } from '@azure/core-http'; | ||
/** | ||
@@ -26,3 +27,8 @@ * @private | ||
export declare class RestApiClient { | ||
private _iotHubPublicScope; | ||
private _BearerTokenPrefix; | ||
private _MinutesBeforeProactiveRenewal; | ||
private _MillisecsBeforeProactiveRenewal; | ||
private _config; | ||
private _accessToken; | ||
private _http; | ||
@@ -62,2 +68,19 @@ private _userAgent; | ||
/** | ||
* @private | ||
* Calculates if the AccessToken's remaining time to live | ||
* is shorter than the proactive renewal time. | ||
* @param accessToken The AccessToken. | ||
* @returns {Boolean} True if the token's remaining time is shorter than the | ||
* proactive renewal time, false otherwise. | ||
*/ | ||
isAccessTokenCloseToExpiry(accessToken: AccessToken): Boolean; | ||
/** | ||
* @private | ||
* Returns the current AccessToken if it is still valid | ||
* or a new AccessToken if the current token is close to expire. | ||
* @returns {Promise<string>} The access token string. | ||
*/ | ||
getToken(): Promise<string>; | ||
private executeBody; | ||
/** | ||
* @method module:azure-iothub.RestApiClient.translateError | ||
@@ -79,4 +102,5 @@ * @description Translates an HTTP error into a transport-agnostic error. | ||
x509?: X509; | ||
tokenCredential?: TokenCredential; | ||
} | ||
type ResponseCallback = (err: Error, responseBody?: any, response?: any) => void; | ||
} |
// 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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -23,2 +59,6 @@ var azure_iot_common_1 = require("azure-iot-common"); | ||
function RestApiClient(config, userAgent, httpRequestBuilder) { | ||
this._iotHubPublicScope = ['https://iothubs.azure.net/.default']; | ||
this._BearerTokenPrefix = 'Bearer '; | ||
this._MinutesBeforeProactiveRenewal = 9; | ||
this._MillisecsBeforeProactiveRenewal = this._MinutesBeforeProactiveRenewal * 60000; | ||
/*Codes_SRS_NODE_IOTHUB_REST_API_CLIENT_16_001: [The `RestApiClient` constructor shall throw a `ReferenceError` if config is falsy.]*/ | ||
@@ -54,2 +94,3 @@ if (!config) | ||
RestApiClient.prototype.executeApiCall = function (method, path, headers, requestBody, timeout, requestOptions, done) { | ||
var _this = this; | ||
/*Codes_SRS_NODE_IOTHUB_REST_API_CLIENT_16_005: [The `executeApiCall` method shall throw a `ReferenceError` if the `method` argument is falsy.]*/ | ||
@@ -81,5 +122,86 @@ if (!method) | ||
var httpHeaders = headers || {}; | ||
if (this._config.sharedAccessSignature) { | ||
httpHeaders.Authorization = (typeof (this._config.sharedAccessSignature) === 'string') ? this._config.sharedAccessSignature : this._config.sharedAccessSignature.extend(azure_iot_common_1.anHourFromNow()); | ||
if (this._config.tokenCredential) { | ||
var accessToken = this.getToken(); | ||
Promise.resolve(accessToken).then(function (value) { | ||
if (value) { | ||
httpHeaders.Authorization = value; | ||
_this.executeBody(requestBody, httpHeaders, headers, method, path, timeout, requestOptions, done); | ||
} | ||
else { | ||
throw new Error('AccessToken creation failed'); | ||
} | ||
}); | ||
} | ||
else { | ||
if (this._config.sharedAccessSignature) { | ||
httpHeaders.Authorization = (typeof (this._config.sharedAccessSignature) === 'string') ? this._config.sharedAccessSignature : this._config.sharedAccessSignature.extend(azure_iot_common_1.anHourFromNow()); | ||
} | ||
this.executeBody(requestBody, httpHeaders, headers, method, path, timeout, requestOptions, done); | ||
} | ||
}; | ||
/** | ||
* @method module:azure-iothub.RestApiClient.updateSharedAccessSignature | ||
* @description Updates the shared access signature used to authentify API calls. | ||
* | ||
* @param {string} sharedAccessSignature The new shared access signature that should be used. | ||
* | ||
* @throws {ReferenceError} If the new sharedAccessSignature is falsy. | ||
*/ | ||
RestApiClient.prototype.updateSharedAccessSignature = function (sharedAccessSignature) { | ||
/*Codes_SRS_NODE_IOTHUB_REST_API_CLIENT_16_034: [The `updateSharedAccessSignature` method shall throw a `ReferenceError` if the `sharedAccessSignature` argument is falsy.]*/ | ||
if (!sharedAccessSignature) | ||
throw new ReferenceError('sharedAccessSignature cannot be \'' + sharedAccessSignature + '\''); | ||
/*Codes_SRS_NODE_IOTHUB_REST_API_CLIENT_16_028: [The `updateSharedAccessSignature` method shall update the `sharedAccessSignature` configuration parameter that is used in the `Authorization` header of all HTTP requests.]*/ | ||
this._config.sharedAccessSignature = sharedAccessSignature; | ||
}; | ||
/** | ||
* @private | ||
*/ | ||
RestApiClient.prototype.setOptions = function (options) { | ||
/*Codes_SRS_NODE_IOTHUB_REST_API_CLIENT_18_003: [ `setOptions` shall call `this._http.setOptions` passing the same parameters ]*/ | ||
this._http.setOptions(options); | ||
}; | ||
/** | ||
* @private | ||
* Calculates if the AccessToken's remaining time to live | ||
* is shorter than the proactive renewal time. | ||
* @param accessToken The AccessToken. | ||
* @returns {Boolean} True if the token's remaining time is shorter than the | ||
* proactive renewal time, false otherwise. | ||
*/ | ||
RestApiClient.prototype.isAccessTokenCloseToExpiry = function (accessToken) { | ||
var remainingTimeToLive = Date.now() - accessToken.expiresOnTimestamp; | ||
return remainingTimeToLive <= this._MillisecsBeforeProactiveRenewal; | ||
}; | ||
/** | ||
* @private | ||
* Returns the current AccessToken if it is still valid | ||
* or a new AccessToken if the current token is close to expire. | ||
* @returns {Promise<string>} The access token string. | ||
*/ | ||
RestApiClient.prototype.getToken = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _a; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
if (!((!this._accessToken) || this.isAccessTokenCloseToExpiry(this._accessToken))) return [3 /*break*/, 2]; | ||
_a = this; | ||
return [4 /*yield*/, this._config.tokenCredential.getToken(this._iotHubPublicScope)]; | ||
case 1: | ||
_a._accessToken = (_b.sent()); | ||
_b.label = 2; | ||
case 2: | ||
if (this._accessToken) { | ||
return [2 /*return*/, this._BearerTokenPrefix + this._accessToken.token]; | ||
} | ||
else { | ||
return [2 /*return*/, null]; | ||
} | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
RestApiClient.prototype.executeBody = function (requestBody, httpHeaders, headers, method, path, timeout, requestOptions, done) { | ||
httpHeaders['Request-Id'] = uuid.v4(); | ||
@@ -175,24 +297,2 @@ httpHeaders['User-Agent'] = this._userAgent; | ||
/** | ||
* @method module:azure-iothub.RestApiClient.updateSharedAccessSignature | ||
* @description Updates the shared access signature used to authentify API calls. | ||
* | ||
* @param {string} sharedAccessSignature The new shared access signature that should be used. | ||
* | ||
* @throws {ReferenceError} If the new sharedAccessSignature is falsy. | ||
*/ | ||
RestApiClient.prototype.updateSharedAccessSignature = function (sharedAccessSignature) { | ||
/*Codes_SRS_NODE_IOTHUB_REST_API_CLIENT_16_034: [The `updateSharedAccessSignature` method shall throw a `ReferenceError` if the `sharedAccessSignature` argument is falsy.]*/ | ||
if (!sharedAccessSignature) | ||
throw new ReferenceError('sharedAccessSignature cannot be \'' + sharedAccessSignature + '\''); | ||
/*Codes_SRS_NODE_IOTHUB_REST_API_CLIENT_16_028: [The `updateSharedAccessSignature` method shall update the `sharedAccessSignature` configuration parameter that is used in the `Authorization` header of all HTTP requests.]*/ | ||
this._config.sharedAccessSignature = sharedAccessSignature; | ||
}; | ||
/** | ||
* @private | ||
*/ | ||
RestApiClient.prototype.setOptions = function (options) { | ||
/*Codes_SRS_NODE_IOTHUB_REST_API_CLIENT_18_003: [ `setOptions` shall call `this._http.setOptions` passing the same parameters ]*/ | ||
this._http.setOptions(options); | ||
}; | ||
/** | ||
* @method module:azure-iothub.RestApiClient.translateError | ||
@@ -199,0 +299,0 @@ * @description Translates an HTTP error into a transport-agnostic error. |
{ | ||
"name": "azure-iot-http-base", | ||
"version": "1.11.8", | ||
"version": "1.11.9-alpha.210607.1", | ||
"description": "HTTP operations used by Azure IoT device and service SDKs", | ||
@@ -10,3 +10,3 @@ "author": "Microsoft Corporation", | ||
"dependencies": { | ||
"azure-iot-common": "1.12.8", | ||
"azure-iot-common": "1.12.9-alpha.210607.1", | ||
"debug": "^4.3.1", | ||
@@ -16,2 +16,3 @@ "uuid": "^3.3.2" | ||
"devDependencies": { | ||
"@azure/core-http": "1.2.3", | ||
"@types/node": "^9.6.61", | ||
@@ -56,6 +57,6 @@ "chai": "^4.3.3", | ||
"check-coverage": true, | ||
"lines": 93, | ||
"functions": 100, | ||
"branches": 89, | ||
"statements": 93 | ||
"lines": 88, | ||
"functions": 86, | ||
"branches": 84, | ||
"statements": 87 | ||
}, | ||
@@ -62,0 +63,0 @@ "mocha": { |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
51060
842
10
1
+ Addedazure-iot-common@1.12.9-alpha.210607.1(transitive)
- Removedazure-iot-common@1.12.8(transitive)