ibm-cloud-sdk-core 2.3.1 → 2.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +28 -0
- package/auth/authenticators/authenticator.js +1 -0
- package/auth/authenticators/basic-authenticator.js +1 -0
- package/auth/authenticators/bearer-token-authenticator.js +1 -0
- package/auth/authenticators/cloud-pak-for-data-authenticator.js +1 -0
- package/auth/authenticators/iam-authenticator.js +1 -0
- package/auth/authenticators/index.js +7 -7
- package/auth/authenticators/no-auth-authenticator.js +1 -0
- package/auth/authenticators/token-request-based-authenticator.js +1 -0
- package/auth/index.js +13 -6
- package/auth/token-managers/cp4d-token-manager.d.ts +2 -2
- package/auth/token-managers/cp4d-token-manager.js +1 -0
- package/auth/token-managers/iam-token-manager.d.ts +2 -2
- package/auth/token-managers/iam-token-manager.js +1 -0
- package/auth/token-managers/index.d.ts +2 -1
- package/auth/token-managers/index.js +5 -3
- package/auth/token-managers/jwt-token-manager.d.ts +5 -75
- package/auth/token-managers/jwt-token-manager.js +26 -140
- package/auth/token-managers/token-manager.d.ts +127 -0
- package/auth/token-managers/token-manager.js +203 -0
- package/auth/utils/get-authenticator-from-environment.js +1 -0
- package/auth/utils/helpers.d.ts +6 -0
- package/auth/utils/helpers.js +10 -0
- package/auth/utils/index.js +14 -7
- package/auth/utils/read-credentials-file.browser.js +1 -0
- package/auth/utils/read-credentials-file.js +1 -0
- package/auth/utils/read-external-sources.js +1 -0
- package/index.d.ts +2 -1
- package/index.js +19 -10
- package/lib/base-service.js +5 -2
- package/lib/helper.js +12 -1
- package/lib/request-wrapper.js +2 -1
- package/{test/utils/unit-test-helpers.d.ts → lib/sdk-test-helpers.d.ts} +1 -4
- package/{test/utils/unit-test-helpers.js → lib/sdk-test-helpers.js} +3 -5
- package/lib/stream-to-promise.js +1 -0
- package/package.json +5 -5
- package/test/utils/index.d.ts +0 -17
- package/test/utils/index.js +0 -25
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,31 @@
|
|
|
1
|
+
## [2.4.2](https://github.com/IBM/node-sdk-core/compare/v2.4.1...v2.4.2) (2020-07-22)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* move test utilities to lib/ and rely directly on `expect` ([#101](https://github.com/IBM/node-sdk-core/issues/101)) ([57ca4c2](https://github.com/IBM/node-sdk-core/commit/57ca4c2edba31a39bae479a041b974ffd94f31a3))
|
|
7
|
+
|
|
8
|
+
## [2.4.1](https://github.com/IBM/node-sdk-core/compare/v2.4.0...v2.4.1) (2020-05-14)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* Remove trailing slash from url for extra cases ([#95](https://github.com/IBM/node-sdk-core/issues/95)) ([bcaa168](https://github.com/IBM/node-sdk-core/commit/bcaa168b4717b5921d33f72287f8bfbe7ce812f7))
|
|
14
|
+
|
|
15
|
+
# [2.4.0](https://github.com/IBM/node-sdk-core/compare/v2.3.2...v2.4.0) (2020-05-05)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Features
|
|
19
|
+
|
|
20
|
+
* **token-manager:** Introduce a token-manager class for token handling ([#89](https://github.com/IBM/node-sdk-core/issues/89)) ([23c5f3f](https://github.com/IBM/node-sdk-core/commit/23c5f3ff26ec2c9ae58c47163c93a9a1532998b0))
|
|
21
|
+
|
|
22
|
+
## [2.3.2](https://github.com/IBM/node-sdk-core/compare/v2.3.1...v2.3.2) (2020-05-04)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### Bug Fixes
|
|
26
|
+
|
|
27
|
+
* flag required params as missing when explicitly undefined, null, or an empty string ([#90](https://github.com/IBM/node-sdk-core/issues/90)) ([414b674](https://github.com/IBM/node-sdk-core/commit/414b674d607e6dec73a163b0445c1854245f69b2))
|
|
28
|
+
|
|
1
29
|
## [2.3.1](https://github.com/IBM/node-sdk-core/compare/v2.3.0...v2.3.1) (2020-05-01)
|
|
2
30
|
|
|
3
31
|
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
*/
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.Authenticator = void 0;
|
|
18
19
|
/**
|
|
19
20
|
* Base Authenticator class for other Authenticators to extend. Not intended
|
|
20
21
|
* to be used as a stand-alone authenticator.
|
|
@@ -28,6 +28,7 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
28
28
|
};
|
|
29
29
|
})();
|
|
30
30
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
|
+
exports.BasicAuthenticator = void 0;
|
|
31
32
|
var extend = require("extend");
|
|
32
33
|
var utils_1 = require("../utils");
|
|
33
34
|
var authenticator_1 = require("./authenticator");
|
|
@@ -28,6 +28,7 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
28
28
|
};
|
|
29
29
|
})();
|
|
30
30
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
|
+
exports.BearerTokenAuthenticator = void 0;
|
|
31
32
|
var extend = require("extend");
|
|
32
33
|
var utils_1 = require("../utils");
|
|
33
34
|
var authenticator_1 = require("./authenticator");
|
|
@@ -28,6 +28,7 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
28
28
|
};
|
|
29
29
|
})();
|
|
30
30
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
|
+
exports.CloudPakForDataAuthenticator = void 0;
|
|
31
32
|
var token_managers_1 = require("../token-managers");
|
|
32
33
|
var utils_1 = require("../utils");
|
|
33
34
|
var token_request_based_authenticator_1 = require("./token-request-based-authenticator");
|
|
@@ -28,6 +28,7 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
28
28
|
};
|
|
29
29
|
})();
|
|
30
30
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
|
+
exports.IamAuthenticator = void 0;
|
|
31
32
|
var token_managers_1 = require("../token-managers");
|
|
32
33
|
var utils_1 = require("../utils");
|
|
33
34
|
var token_request_based_authenticator_1 = require("./token-request-based-authenticator");
|
|
@@ -16,16 +16,16 @@
|
|
|
16
16
|
*/
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
18
|
var authenticator_1 = require("./authenticator");
|
|
19
|
-
exports
|
|
19
|
+
Object.defineProperty(exports, "Authenticator", { enumerable: true, get: function () { return authenticator_1.Authenticator; } });
|
|
20
20
|
var basic_authenticator_1 = require("./basic-authenticator");
|
|
21
|
-
exports
|
|
21
|
+
Object.defineProperty(exports, "BasicAuthenticator", { enumerable: true, get: function () { return basic_authenticator_1.BasicAuthenticator; } });
|
|
22
22
|
var bearer_token_authenticator_1 = require("./bearer-token-authenticator");
|
|
23
|
-
exports
|
|
23
|
+
Object.defineProperty(exports, "BearerTokenAuthenticator", { enumerable: true, get: function () { return bearer_token_authenticator_1.BearerTokenAuthenticator; } });
|
|
24
24
|
var cloud_pak_for_data_authenticator_1 = require("./cloud-pak-for-data-authenticator");
|
|
25
|
-
exports
|
|
25
|
+
Object.defineProperty(exports, "CloudPakForDataAuthenticator", { enumerable: true, get: function () { return cloud_pak_for_data_authenticator_1.CloudPakForDataAuthenticator; } });
|
|
26
26
|
var iam_authenticator_1 = require("./iam-authenticator");
|
|
27
|
-
exports
|
|
27
|
+
Object.defineProperty(exports, "IamAuthenticator", { enumerable: true, get: function () { return iam_authenticator_1.IamAuthenticator; } });
|
|
28
28
|
var no_auth_authenticator_1 = require("./no-auth-authenticator");
|
|
29
|
-
exports
|
|
29
|
+
Object.defineProperty(exports, "NoAuthAuthenticator", { enumerable: true, get: function () { return no_auth_authenticator_1.NoAuthAuthenticator; } });
|
|
30
30
|
var token_request_based_authenticator_1 = require("./token-request-based-authenticator");
|
|
31
|
-
exports
|
|
31
|
+
Object.defineProperty(exports, "TokenRequestBasedAuthenticator", { enumerable: true, get: function () { return token_request_based_authenticator_1.TokenRequestBasedAuthenticator; } });
|
|
@@ -28,6 +28,7 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
28
28
|
};
|
|
29
29
|
})();
|
|
30
30
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
|
+
exports.NoAuthAuthenticator = void 0;
|
|
31
32
|
var authenticator_1 = require("./authenticator");
|
|
32
33
|
/**
|
|
33
34
|
* NoAuthAuthenticator is a placeholder authenticator implementation which
|
|
@@ -28,6 +28,7 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
28
28
|
};
|
|
29
29
|
})();
|
|
30
30
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
|
+
exports.TokenRequestBasedAuthenticator = void 0;
|
|
31
32
|
var extend = require("extend");
|
|
32
33
|
var token_managers_1 = require("../token-managers");
|
|
33
34
|
var authenticator_1 = require("./authenticator");
|
package/auth/index.js
CHANGED
|
@@ -14,10 +14,17 @@
|
|
|
14
14
|
* See the License for the specific language governing permissions and
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
*/
|
|
17
|
-
function
|
|
18
|
-
|
|
19
|
-
}
|
|
17
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
18
|
+
if (k2 === undefined) k2 = k;
|
|
19
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
20
|
+
}) : (function(o, m, k, k2) {
|
|
21
|
+
if (k2 === undefined) k2 = k;
|
|
22
|
+
o[k2] = m[k];
|
|
23
|
+
}));
|
|
24
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
25
|
+
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
|
|
26
|
+
};
|
|
20
27
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
28
|
+
__exportStar(require("./authenticators"), exports);
|
|
29
|
+
__exportStar(require("./token-managers"), exports);
|
|
30
|
+
__exportStar(require("./utils"), exports);
|
|
@@ -13,9 +13,9 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
-
import { JwtTokenManager,
|
|
16
|
+
import { JwtTokenManager, JwtTokenManagerOptions } from './jwt-token-manager';
|
|
17
17
|
/** Configuration options for CP4D token retrieval. */
|
|
18
|
-
interface Options extends
|
|
18
|
+
interface Options extends JwtTokenManagerOptions {
|
|
19
19
|
/** The endpoint for CP4D token requests. */
|
|
20
20
|
url: string;
|
|
21
21
|
/** The username portion of basic authentication. */
|
|
@@ -28,6 +28,7 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
28
28
|
};
|
|
29
29
|
})();
|
|
30
30
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
|
+
exports.Cp4dTokenManager = void 0;
|
|
31
32
|
var extend = require("extend");
|
|
32
33
|
var utils_1 = require("../utils");
|
|
33
34
|
var jwt_token_manager_1 = require("./jwt-token-manager");
|
|
@@ -13,9 +13,9 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
-
import { JwtTokenManager,
|
|
16
|
+
import { JwtTokenManager, JwtTokenManagerOptions } from './jwt-token-manager';
|
|
17
17
|
/** Configuration options for IAM token retrieval. */
|
|
18
|
-
interface Options extends
|
|
18
|
+
interface Options extends JwtTokenManagerOptions {
|
|
19
19
|
apikey: string;
|
|
20
20
|
clientId?: string;
|
|
21
21
|
clientSecret?: string;
|
|
@@ -28,6 +28,7 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
28
28
|
};
|
|
29
29
|
})();
|
|
30
30
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
|
+
exports.IamTokenManager = void 0;
|
|
31
32
|
var extend = require("extend");
|
|
32
33
|
var logger_1 = require("../../lib/logger");
|
|
33
34
|
var utils_1 = require("../utils");
|
|
@@ -30,4 +30,5 @@
|
|
|
30
30
|
*/
|
|
31
31
|
export { IamTokenManager } from './iam-token-manager';
|
|
32
32
|
export { Cp4dTokenManager } from './cp4d-token-manager';
|
|
33
|
-
export { JwtTokenManager } from './jwt-token-manager';
|
|
33
|
+
export { JwtTokenManager, JwtTokenManagerOptions } from './jwt-token-manager';
|
|
34
|
+
export { TokenManager, TokenManagerOptions } from './token-manager';
|
|
@@ -31,8 +31,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
31
31
|
* JwtTokenManager: A class for shared functionality for parsing, storing, and requesting JWT tokens.
|
|
32
32
|
*/
|
|
33
33
|
var iam_token_manager_1 = require("./iam-token-manager");
|
|
34
|
-
exports
|
|
34
|
+
Object.defineProperty(exports, "IamTokenManager", { enumerable: true, get: function () { return iam_token_manager_1.IamTokenManager; } });
|
|
35
35
|
var cp4d_token_manager_1 = require("./cp4d-token-manager");
|
|
36
|
-
exports
|
|
36
|
+
Object.defineProperty(exports, "Cp4dTokenManager", { enumerable: true, get: function () { return cp4d_token_manager_1.Cp4dTokenManager; } });
|
|
37
37
|
var jwt_token_manager_1 = require("./jwt-token-manager");
|
|
38
|
-
exports
|
|
38
|
+
Object.defineProperty(exports, "JwtTokenManager", { enumerable: true, get: function () { return jwt_token_manager_1.JwtTokenManager; } });
|
|
39
|
+
var token_manager_1 = require("./token-manager");
|
|
40
|
+
Object.defineProperty(exports, "TokenManager", { enumerable: true, get: function () { return token_manager_1.TokenManager; } });
|
|
@@ -13,40 +13,18 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
-
|
|
17
|
-
import { OutgoingHttpHeaders } from 'http';
|
|
18
|
-
import { RequestWrapper } from '../../lib/request-wrapper';
|
|
16
|
+
import { TokenManager, TokenManagerOptions } from "./token-manager";
|
|
19
17
|
/** Configuration options for JWT token retrieval. */
|
|
20
|
-
export declare type
|
|
21
|
-
/** The endpoint for token requests. */
|
|
22
|
-
url?: string;
|
|
23
|
-
/** Headers to be sent with every service token request. */
|
|
24
|
-
headers?: OutgoingHttpHeaders;
|
|
25
|
-
/**
|
|
26
|
-
* A flag that indicates whether verification of
|
|
27
|
-
* the server's SSL certificate should be disabled or not.
|
|
28
|
-
*/
|
|
29
|
-
disableSslVerification?: boolean;
|
|
30
|
-
/** Allow additional request config parameters */
|
|
31
|
-
[propName: string]: any;
|
|
32
|
-
};
|
|
18
|
+
export declare type JwtTokenManagerOptions = TokenManagerOptions;
|
|
33
19
|
/**
|
|
34
20
|
* A class for shared functionality for parsing, storing, and requesting
|
|
35
21
|
* JWT tokens. Intended to be used as a parent to be extended for token
|
|
36
22
|
* request management. Child classes should implement `requestToken()`
|
|
37
23
|
* to retrieve the bearer token from intended sources.
|
|
38
24
|
*/
|
|
39
|
-
export declare class JwtTokenManager {
|
|
40
|
-
protected url: string;
|
|
25
|
+
export declare class JwtTokenManager extends TokenManager {
|
|
41
26
|
protected tokenName: string;
|
|
42
|
-
protected
|
|
43
|
-
protected headers: OutgoingHttpHeaders;
|
|
44
|
-
protected requestWrapperInstance: RequestWrapper;
|
|
45
|
-
private tokenInfo;
|
|
46
|
-
private expireTime;
|
|
47
|
-
private refreshTime;
|
|
48
|
-
private requestTime;
|
|
49
|
-
private pendingRequests;
|
|
27
|
+
protected tokenInfo: any;
|
|
50
28
|
/**
|
|
51
29
|
* Create a new [[JwtTokenManager]] instance.
|
|
52
30
|
* @constructor
|
|
@@ -58,40 +36,7 @@ export declare class JwtTokenManager {
|
|
|
58
36
|
* @param {object<string, string>} [options.headers] Headers to be sent with every
|
|
59
37
|
* outbound HTTP requests to token services.
|
|
60
38
|
*/
|
|
61
|
-
constructor(options:
|
|
62
|
-
/**
|
|
63
|
-
* Retrieve a new token using `requestToken()` in the case there is not a
|
|
64
|
-
* currently stored token from a previous call, or the previous token
|
|
65
|
-
* has expired.
|
|
66
|
-
*/
|
|
67
|
-
getToken(): Promise<any>;
|
|
68
|
-
/**
|
|
69
|
-
* Setter for the disableSslVerification property.
|
|
70
|
-
*
|
|
71
|
-
* @param {boolean} value - the new value for the disableSslVerification
|
|
72
|
-
* property
|
|
73
|
-
* @returns {void}
|
|
74
|
-
*/
|
|
75
|
-
setDisableSslVerification(value: boolean): void;
|
|
76
|
-
/**
|
|
77
|
-
* Set a completely new set of headers.
|
|
78
|
-
*
|
|
79
|
-
* @param {OutgoingHttpHeaders} headers - the new set of headers as an object
|
|
80
|
-
* @returns {void}
|
|
81
|
-
*/
|
|
82
|
-
setHeaders(headers: OutgoingHttpHeaders): void;
|
|
83
|
-
/**
|
|
84
|
-
* Paces requests to request_token.
|
|
85
|
-
*
|
|
86
|
-
* This method pseudo-serializes requests for an access_token
|
|
87
|
-
* when the current token is undefined or expired.
|
|
88
|
-
* The first caller to this method records its `requestTime` and
|
|
89
|
-
* then issues the token request. Subsequent callers will check the
|
|
90
|
-
* `requestTime` to see if a request is active (has been issued within
|
|
91
|
-
* the past 60 seconds), and if so will queue their promise for the
|
|
92
|
-
* active requestor to resolve when that request completes.
|
|
93
|
-
*/
|
|
94
|
-
protected pacedRequestToken(): Promise<any>;
|
|
39
|
+
constructor(options: JwtTokenManagerOptions);
|
|
95
40
|
/**
|
|
96
41
|
* Request a JWT using an API key.
|
|
97
42
|
*
|
|
@@ -106,19 +51,4 @@ export declare class JwtTokenManager {
|
|
|
106
51
|
* @returns {void}
|
|
107
52
|
*/
|
|
108
53
|
protected saveTokenInfo(tokenResponse: any): void;
|
|
109
|
-
/**
|
|
110
|
-
* Check if currently stored token is expired
|
|
111
|
-
*
|
|
112
|
-
* @private
|
|
113
|
-
* @returns {boolean}
|
|
114
|
-
*/
|
|
115
|
-
private isTokenExpired;
|
|
116
|
-
/**
|
|
117
|
-
* Check if currently stored token should be refreshed
|
|
118
|
-
* i.e. past the window to request a new token
|
|
119
|
-
*
|
|
120
|
-
* @private
|
|
121
|
-
* @returns {boolean}
|
|
122
|
-
*/
|
|
123
|
-
private tokenNeedsRefresh;
|
|
124
54
|
}
|
|
@@ -14,21 +14,33 @@
|
|
|
14
14
|
* See the License for the specific language governing permissions and
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
*/
|
|
17
|
+
var __extends = (this && this.__extends) || (function () {
|
|
18
|
+
var extendStatics = function (d, b) {
|
|
19
|
+
extendStatics = Object.setPrototypeOf ||
|
|
20
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
21
|
+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
|
22
|
+
return extendStatics(d, b);
|
|
23
|
+
};
|
|
24
|
+
return function (d, b) {
|
|
25
|
+
extendStatics(d, b);
|
|
26
|
+
function __() { this.constructor = d; }
|
|
27
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
28
|
+
};
|
|
29
|
+
})();
|
|
17
30
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
|
+
exports.JwtTokenManager = void 0;
|
|
18
32
|
var extend = require("extend");
|
|
19
33
|
var jwt = require("jsonwebtoken");
|
|
20
34
|
var logger_1 = require("../../lib/logger");
|
|
21
|
-
var
|
|
22
|
-
function getCurrentTime() {
|
|
23
|
-
return Math.floor(Date.now() / 1000);
|
|
24
|
-
}
|
|
35
|
+
var token_manager_1 = require("./token-manager");
|
|
25
36
|
/**
|
|
26
37
|
* A class for shared functionality for parsing, storing, and requesting
|
|
27
38
|
* JWT tokens. Intended to be used as a parent to be extended for token
|
|
28
39
|
* request management. Child classes should implement `requestToken()`
|
|
29
40
|
* to retrieve the bearer token from intended sources.
|
|
30
41
|
*/
|
|
31
|
-
var JwtTokenManager = /** @class */ (function () {
|
|
42
|
+
var JwtTokenManager = /** @class */ (function (_super) {
|
|
43
|
+
__extends(JwtTokenManager, _super);
|
|
32
44
|
/**
|
|
33
45
|
* Create a new [[JwtTokenManager]] instance.
|
|
34
46
|
* @constructor
|
|
@@ -41,109 +53,14 @@ var JwtTokenManager = /** @class */ (function () {
|
|
|
41
53
|
* outbound HTTP requests to token services.
|
|
42
54
|
*/
|
|
43
55
|
function JwtTokenManager(options) {
|
|
56
|
+
var _this = this;
|
|
44
57
|
// all parameters are optional
|
|
45
58
|
options = options || {};
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
// request options
|
|
52
|
-
this.disableSslVerification = Boolean(options.disableSslVerification);
|
|
53
|
-
this.headers = options.headers || {};
|
|
54
|
-
// any config options for the internal request library, like `proxy`, will be passed here
|
|
55
|
-
this.requestWrapperInstance = new request_wrapper_1.RequestWrapper(options);
|
|
56
|
-
// Array of requests pending completion of an active token request -- initially empty
|
|
57
|
-
this.pendingRequests = [];
|
|
59
|
+
_this = _super.call(this, options) || this;
|
|
60
|
+
_this.tokenName = 'access_token';
|
|
61
|
+
_this.tokenInfo = {};
|
|
62
|
+
return _this;
|
|
58
63
|
}
|
|
59
|
-
/**
|
|
60
|
-
* Retrieve a new token using `requestToken()` in the case there is not a
|
|
61
|
-
* currently stored token from a previous call, or the previous token
|
|
62
|
-
* has expired.
|
|
63
|
-
*/
|
|
64
|
-
JwtTokenManager.prototype.getToken = function () {
|
|
65
|
-
var _this = this;
|
|
66
|
-
if (!this.tokenInfo[this.tokenName] || this.isTokenExpired()) {
|
|
67
|
-
// 1. request a new token
|
|
68
|
-
return this.pacedRequestToken().then(function () {
|
|
69
|
-
return _this.tokenInfo[_this.tokenName];
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
else {
|
|
73
|
-
// If refresh needed, kick one off
|
|
74
|
-
if (this.tokenNeedsRefresh()) {
|
|
75
|
-
this.requestToken().then(function (tokenResponse) {
|
|
76
|
-
_this.saveTokenInfo(tokenResponse);
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
// 2. use valid, managed token
|
|
80
|
-
return Promise.resolve(this.tokenInfo[this.tokenName]);
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
/**
|
|
84
|
-
* Setter for the disableSslVerification property.
|
|
85
|
-
*
|
|
86
|
-
* @param {boolean} value - the new value for the disableSslVerification
|
|
87
|
-
* property
|
|
88
|
-
* @returns {void}
|
|
89
|
-
*/
|
|
90
|
-
JwtTokenManager.prototype.setDisableSslVerification = function (value) {
|
|
91
|
-
// if they try to pass in a non-boolean value,
|
|
92
|
-
// use the "truthy-ness" of the value
|
|
93
|
-
this.disableSslVerification = Boolean(value);
|
|
94
|
-
};
|
|
95
|
-
/**
|
|
96
|
-
* Set a completely new set of headers.
|
|
97
|
-
*
|
|
98
|
-
* @param {OutgoingHttpHeaders} headers - the new set of headers as an object
|
|
99
|
-
* @returns {void}
|
|
100
|
-
*/
|
|
101
|
-
JwtTokenManager.prototype.setHeaders = function (headers) {
|
|
102
|
-
if (typeof headers !== 'object') {
|
|
103
|
-
// do nothing, for now
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
this.headers = headers;
|
|
107
|
-
};
|
|
108
|
-
/**
|
|
109
|
-
* Paces requests to request_token.
|
|
110
|
-
*
|
|
111
|
-
* This method pseudo-serializes requests for an access_token
|
|
112
|
-
* when the current token is undefined or expired.
|
|
113
|
-
* The first caller to this method records its `requestTime` and
|
|
114
|
-
* then issues the token request. Subsequent callers will check the
|
|
115
|
-
* `requestTime` to see if a request is active (has been issued within
|
|
116
|
-
* the past 60 seconds), and if so will queue their promise for the
|
|
117
|
-
* active requestor to resolve when that request completes.
|
|
118
|
-
*/
|
|
119
|
-
JwtTokenManager.prototype.pacedRequestToken = function () {
|
|
120
|
-
var _this = this;
|
|
121
|
-
var currentTime = getCurrentTime();
|
|
122
|
-
if (this.requestTime > (currentTime - 60)) {
|
|
123
|
-
// token request is active -- queue the promise for this request
|
|
124
|
-
return new Promise(function (resolve, reject) {
|
|
125
|
-
_this.pendingRequests.push({ resolve: resolve, reject: reject });
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
else {
|
|
129
|
-
this.requestTime = currentTime;
|
|
130
|
-
return this.requestToken().then(function (tokenResponse) {
|
|
131
|
-
_this.saveTokenInfo(tokenResponse);
|
|
132
|
-
_this.pendingRequests.forEach(function (_a) {
|
|
133
|
-
var resolve = _a.resolve;
|
|
134
|
-
resolve();
|
|
135
|
-
});
|
|
136
|
-
_this.pendingRequests = [];
|
|
137
|
-
_this.requestTime = 0;
|
|
138
|
-
}).catch(function (err) {
|
|
139
|
-
_this.pendingRequests.forEach(function (_a) {
|
|
140
|
-
var reject = _a.reject;
|
|
141
|
-
reject(err);
|
|
142
|
-
});
|
|
143
|
-
throw (err);
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
};
|
|
147
64
|
/**
|
|
148
65
|
* Request a JWT using an API key.
|
|
149
66
|
*
|
|
@@ -164,15 +81,15 @@ var JwtTokenManager = /** @class */ (function () {
|
|
|
164
81
|
*/
|
|
165
82
|
JwtTokenManager.prototype.saveTokenInfo = function (tokenResponse) {
|
|
166
83
|
var responseBody = tokenResponse.result || {};
|
|
167
|
-
|
|
168
|
-
if (!accessToken) {
|
|
84
|
+
this.accessToken = responseBody[this.tokenName];
|
|
85
|
+
if (!this.accessToken) {
|
|
169
86
|
var err = 'Access token not present in response';
|
|
170
87
|
logger_1.default.error(err);
|
|
171
88
|
throw new Error(err);
|
|
172
89
|
}
|
|
173
90
|
// the time of expiration is found by decoding the JWT access token
|
|
174
91
|
// exp is the time of expire and iat is the time of token retrieval
|
|
175
|
-
var decodedResponse = jwt.decode(accessToken);
|
|
92
|
+
var decodedResponse = jwt.decode(this.accessToken);
|
|
176
93
|
if (!decodedResponse) {
|
|
177
94
|
var err = 'Access token recieved is not a valid JWT';
|
|
178
95
|
logger_1.default.error(err);
|
|
@@ -192,37 +109,6 @@ var JwtTokenManager = /** @class */ (function () {
|
|
|
192
109
|
}
|
|
193
110
|
this.tokenInfo = extend({}, responseBody);
|
|
194
111
|
};
|
|
195
|
-
/**
|
|
196
|
-
* Check if currently stored token is expired
|
|
197
|
-
*
|
|
198
|
-
* @private
|
|
199
|
-
* @returns {boolean}
|
|
200
|
-
*/
|
|
201
|
-
JwtTokenManager.prototype.isTokenExpired = function () {
|
|
202
|
-
var expireTime = this.expireTime;
|
|
203
|
-
if (!expireTime) {
|
|
204
|
-
return true;
|
|
205
|
-
}
|
|
206
|
-
var currentTime = getCurrentTime();
|
|
207
|
-
return expireTime <= currentTime;
|
|
208
|
-
};
|
|
209
|
-
/**
|
|
210
|
-
* Check if currently stored token should be refreshed
|
|
211
|
-
* i.e. past the window to request a new token
|
|
212
|
-
*
|
|
213
|
-
* @private
|
|
214
|
-
* @returns {boolean}
|
|
215
|
-
*/
|
|
216
|
-
JwtTokenManager.prototype.tokenNeedsRefresh = function () {
|
|
217
|
-
var refreshTime = this.refreshTime;
|
|
218
|
-
var currentTime = getCurrentTime();
|
|
219
|
-
if (refreshTime && refreshTime > currentTime) {
|
|
220
|
-
return false;
|
|
221
|
-
}
|
|
222
|
-
// Update refreshTime to 60 seconds from now to avoid redundant refreshes
|
|
223
|
-
this.refreshTime = currentTime + 60;
|
|
224
|
-
return true;
|
|
225
|
-
};
|
|
226
112
|
return JwtTokenManager;
|
|
227
|
-
}());
|
|
113
|
+
}(token_manager_1.TokenManager));
|
|
228
114
|
exports.JwtTokenManager = JwtTokenManager;
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright 2020 IBM Corp. All Rights Reserved.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
/// <reference types="node" />
|
|
17
|
+
import { OutgoingHttpHeaders } from 'http';
|
|
18
|
+
import { RequestWrapper } from '../../lib/request-wrapper';
|
|
19
|
+
/** Configuration options for token retrieval. */
|
|
20
|
+
export declare type TokenManagerOptions = {
|
|
21
|
+
/** The endpoint for token requests. */
|
|
22
|
+
url?: string;
|
|
23
|
+
/** Headers to be sent with every service token request. */
|
|
24
|
+
headers?: OutgoingHttpHeaders;
|
|
25
|
+
/**
|
|
26
|
+
* A flag that indicates whether verification of
|
|
27
|
+
* the server's SSL certificate should be disabled or not.
|
|
28
|
+
*/
|
|
29
|
+
disableSslVerification?: boolean;
|
|
30
|
+
/** Allow additional request config parameters */
|
|
31
|
+
[propName: string]: any;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* A class for shared functionality for storing, and requesting tokens.
|
|
35
|
+
* Intended to be used as a parent to be extended for token request management.
|
|
36
|
+
* Child classes should implement `requestToken()` to retrieve the token
|
|
37
|
+
* from intended sources and `saveTokenInfo(tokenResponse)` to parse and save
|
|
38
|
+
* token information from the response.
|
|
39
|
+
*/
|
|
40
|
+
export declare class TokenManager {
|
|
41
|
+
protected url: string;
|
|
42
|
+
protected disableSslVerification: boolean;
|
|
43
|
+
protected headers: OutgoingHttpHeaders;
|
|
44
|
+
protected requestWrapperInstance: RequestWrapper;
|
|
45
|
+
protected accessToken: string;
|
|
46
|
+
protected expireTime: number;
|
|
47
|
+
protected refreshTime: number;
|
|
48
|
+
private requestTime;
|
|
49
|
+
private pendingRequests;
|
|
50
|
+
/**
|
|
51
|
+
* Create a new [[TokenManager]] instance.
|
|
52
|
+
* @constructor
|
|
53
|
+
* @param {object} options Configuration options.
|
|
54
|
+
* @param {string} options.url for HTTP token requests.
|
|
55
|
+
* @param {boolean} [options.disableSslVerification] A flag that indicates
|
|
56
|
+
* whether verification of the token server's SSL certificate should be
|
|
57
|
+
* disabled or not.
|
|
58
|
+
* @param {object<string, string>} [options.headers] Headers to be sent with every
|
|
59
|
+
* outbound HTTP requests to token services.
|
|
60
|
+
*/
|
|
61
|
+
constructor(options: TokenManagerOptions);
|
|
62
|
+
/**
|
|
63
|
+
* Retrieve a new token using `requestToken()` in the case there is not a
|
|
64
|
+
* currently stored token from a previous call, or the previous token
|
|
65
|
+
* has expired.
|
|
66
|
+
*/
|
|
67
|
+
getToken(): Promise<any>;
|
|
68
|
+
/**
|
|
69
|
+
* Setter for the disableSslVerification property.
|
|
70
|
+
*
|
|
71
|
+
* @param {boolean} value - the new value for the disableSslVerification
|
|
72
|
+
* property
|
|
73
|
+
* @returns {void}
|
|
74
|
+
*/
|
|
75
|
+
setDisableSslVerification(value: boolean): void;
|
|
76
|
+
/**
|
|
77
|
+
* Set a completely new set of headers.
|
|
78
|
+
*
|
|
79
|
+
* @param {OutgoingHttpHeaders} headers - the new set of headers as an object
|
|
80
|
+
* @returns {void}
|
|
81
|
+
*/
|
|
82
|
+
setHeaders(headers: OutgoingHttpHeaders): void;
|
|
83
|
+
/**
|
|
84
|
+
* Paces requests to request_token.
|
|
85
|
+
*
|
|
86
|
+
* This method pseudo-serializes requests for an access_token
|
|
87
|
+
* when the current token is undefined or expired.
|
|
88
|
+
* The first caller to this method records its `requestTime` and
|
|
89
|
+
* then issues the token request. Subsequent callers will check the
|
|
90
|
+
* `requestTime` to see if a request is active (has been issued within
|
|
91
|
+
* the past 60 seconds), and if so will queue their promise for the
|
|
92
|
+
* active requestor to resolve when that request completes.
|
|
93
|
+
*/
|
|
94
|
+
protected pacedRequestToken(): Promise<any>;
|
|
95
|
+
/**
|
|
96
|
+
* Request a token using an API endpoint.
|
|
97
|
+
*
|
|
98
|
+
* @returns {Promise}
|
|
99
|
+
*/
|
|
100
|
+
protected requestToken(): Promise<any>;
|
|
101
|
+
/**
|
|
102
|
+
* Parse and save token information from the response.
|
|
103
|
+
* Save the requested token into field `accessToken`.
|
|
104
|
+
* Calculate expiration and refresh time from the received info
|
|
105
|
+
* and store them in fields `expireTime` and `refreshTime`.
|
|
106
|
+
*
|
|
107
|
+
* @param tokenResponse - Response object from a token service request
|
|
108
|
+
* @protected
|
|
109
|
+
* @returns {void}
|
|
110
|
+
*/
|
|
111
|
+
protected saveTokenInfo(tokenResponse: any): void;
|
|
112
|
+
/**
|
|
113
|
+
* Check if currently stored token is expired
|
|
114
|
+
*
|
|
115
|
+
* @private
|
|
116
|
+
* @returns {boolean}
|
|
117
|
+
*/
|
|
118
|
+
private isTokenExpired;
|
|
119
|
+
/**
|
|
120
|
+
* Check if currently stored token should be refreshed
|
|
121
|
+
* i.e. past the window to request a new token
|
|
122
|
+
*
|
|
123
|
+
* @private
|
|
124
|
+
* @returns {boolean}
|
|
125
|
+
*/
|
|
126
|
+
private tokenNeedsRefresh;
|
|
127
|
+
}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright 2020 IBM Corp. All Rights Reserved.
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.TokenManager = void 0;
|
|
19
|
+
var helper_1 = require("../../lib/helper");
|
|
20
|
+
var logger_1 = require("../../lib/logger");
|
|
21
|
+
var request_wrapper_1 = require("../../lib/request-wrapper");
|
|
22
|
+
var utils_1 = require("../utils");
|
|
23
|
+
/**
|
|
24
|
+
* A class for shared functionality for storing, and requesting tokens.
|
|
25
|
+
* Intended to be used as a parent to be extended for token request management.
|
|
26
|
+
* Child classes should implement `requestToken()` to retrieve the token
|
|
27
|
+
* from intended sources and `saveTokenInfo(tokenResponse)` to parse and save
|
|
28
|
+
* token information from the response.
|
|
29
|
+
*/
|
|
30
|
+
var TokenManager = /** @class */ (function () {
|
|
31
|
+
/**
|
|
32
|
+
* Create a new [[TokenManager]] instance.
|
|
33
|
+
* @constructor
|
|
34
|
+
* @param {object} options Configuration options.
|
|
35
|
+
* @param {string} options.url for HTTP token requests.
|
|
36
|
+
* @param {boolean} [options.disableSslVerification] A flag that indicates
|
|
37
|
+
* whether verification of the token server's SSL certificate should be
|
|
38
|
+
* disabled or not.
|
|
39
|
+
* @param {object<string, string>} [options.headers] Headers to be sent with every
|
|
40
|
+
* outbound HTTP requests to token services.
|
|
41
|
+
*/
|
|
42
|
+
function TokenManager(options) {
|
|
43
|
+
// all parameters are optional
|
|
44
|
+
options = options || {};
|
|
45
|
+
if (options.url) {
|
|
46
|
+
this.url = helper_1.stripTrailingSlash(options.url);
|
|
47
|
+
}
|
|
48
|
+
// request options
|
|
49
|
+
this.disableSslVerification = Boolean(options.disableSslVerification);
|
|
50
|
+
this.headers = options.headers || {};
|
|
51
|
+
// any config options for the internal request library, like `proxy`, will be passed here
|
|
52
|
+
this.requestWrapperInstance = new request_wrapper_1.RequestWrapper(options);
|
|
53
|
+
// Array of requests pending completion of an active token request -- initially empty
|
|
54
|
+
this.pendingRequests = [];
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Retrieve a new token using `requestToken()` in the case there is not a
|
|
58
|
+
* currently stored token from a previous call, or the previous token
|
|
59
|
+
* has expired.
|
|
60
|
+
*/
|
|
61
|
+
TokenManager.prototype.getToken = function () {
|
|
62
|
+
var _this = this;
|
|
63
|
+
if (!this.accessToken || this.isTokenExpired()) {
|
|
64
|
+
// 1. request a new token
|
|
65
|
+
return this.pacedRequestToken().then(function () {
|
|
66
|
+
return _this.accessToken;
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
// If refresh needed, kick one off
|
|
71
|
+
if (this.tokenNeedsRefresh()) {
|
|
72
|
+
this.requestToken().then(function (tokenResponse) {
|
|
73
|
+
_this.saveTokenInfo(tokenResponse);
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
// 2. use valid, managed token
|
|
77
|
+
return Promise.resolve(this.accessToken);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* Setter for the disableSslVerification property.
|
|
82
|
+
*
|
|
83
|
+
* @param {boolean} value - the new value for the disableSslVerification
|
|
84
|
+
* property
|
|
85
|
+
* @returns {void}
|
|
86
|
+
*/
|
|
87
|
+
TokenManager.prototype.setDisableSslVerification = function (value) {
|
|
88
|
+
// if they try to pass in a non-boolean value,
|
|
89
|
+
// use the "truthy-ness" of the value
|
|
90
|
+
this.disableSslVerification = Boolean(value);
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* Set a completely new set of headers.
|
|
94
|
+
*
|
|
95
|
+
* @param {OutgoingHttpHeaders} headers - the new set of headers as an object
|
|
96
|
+
* @returns {void}
|
|
97
|
+
*/
|
|
98
|
+
TokenManager.prototype.setHeaders = function (headers) {
|
|
99
|
+
if (typeof headers !== 'object') {
|
|
100
|
+
// do nothing, for now
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
this.headers = headers;
|
|
104
|
+
};
|
|
105
|
+
/**
|
|
106
|
+
* Paces requests to request_token.
|
|
107
|
+
*
|
|
108
|
+
* This method pseudo-serializes requests for an access_token
|
|
109
|
+
* when the current token is undefined or expired.
|
|
110
|
+
* The first caller to this method records its `requestTime` and
|
|
111
|
+
* then issues the token request. Subsequent callers will check the
|
|
112
|
+
* `requestTime` to see if a request is active (has been issued within
|
|
113
|
+
* the past 60 seconds), and if so will queue their promise for the
|
|
114
|
+
* active requestor to resolve when that request completes.
|
|
115
|
+
*/
|
|
116
|
+
TokenManager.prototype.pacedRequestToken = function () {
|
|
117
|
+
var _this = this;
|
|
118
|
+
var currentTime = utils_1.getCurrentTime();
|
|
119
|
+
if (this.requestTime > (currentTime - 60)) {
|
|
120
|
+
// token request is active -- queue the promise for this request
|
|
121
|
+
return new Promise(function (resolve, reject) {
|
|
122
|
+
_this.pendingRequests.push({ resolve: resolve, reject: reject });
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
this.requestTime = currentTime;
|
|
127
|
+
return this.requestToken().then(function (tokenResponse) {
|
|
128
|
+
_this.saveTokenInfo(tokenResponse);
|
|
129
|
+
_this.pendingRequests.forEach(function (_a) {
|
|
130
|
+
var resolve = _a.resolve;
|
|
131
|
+
resolve();
|
|
132
|
+
});
|
|
133
|
+
_this.pendingRequests = [];
|
|
134
|
+
_this.requestTime = 0;
|
|
135
|
+
}).catch(function (err) {
|
|
136
|
+
_this.pendingRequests.forEach(function (_a) {
|
|
137
|
+
var reject = _a.reject;
|
|
138
|
+
reject(err);
|
|
139
|
+
});
|
|
140
|
+
throw (err);
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
/**
|
|
145
|
+
* Request a token using an API endpoint.
|
|
146
|
+
*
|
|
147
|
+
* @returns {Promise}
|
|
148
|
+
*/
|
|
149
|
+
TokenManager.prototype.requestToken = function () {
|
|
150
|
+
var errMsg = '`requestToken` MUST be overridden by a subclass of TokenManagerV1.';
|
|
151
|
+
var err = new Error(errMsg);
|
|
152
|
+
logger_1.default.error(errMsg);
|
|
153
|
+
return Promise.reject(err);
|
|
154
|
+
};
|
|
155
|
+
/**
|
|
156
|
+
* Parse and save token information from the response.
|
|
157
|
+
* Save the requested token into field `accessToken`.
|
|
158
|
+
* Calculate expiration and refresh time from the received info
|
|
159
|
+
* and store them in fields `expireTime` and `refreshTime`.
|
|
160
|
+
*
|
|
161
|
+
* @param tokenResponse - Response object from a token service request
|
|
162
|
+
* @protected
|
|
163
|
+
* @returns {void}
|
|
164
|
+
*/
|
|
165
|
+
TokenManager.prototype.saveTokenInfo = function (tokenResponse) {
|
|
166
|
+
var errMsg = '`saveTokenInfo` MUST be overridden by a subclass of TokenManager.';
|
|
167
|
+
var err = new Error(errMsg);
|
|
168
|
+
logger_1.default.error(errMsg);
|
|
169
|
+
};
|
|
170
|
+
/**
|
|
171
|
+
* Check if currently stored token is expired
|
|
172
|
+
*
|
|
173
|
+
* @private
|
|
174
|
+
* @returns {boolean}
|
|
175
|
+
*/
|
|
176
|
+
TokenManager.prototype.isTokenExpired = function () {
|
|
177
|
+
var expireTime = this.expireTime;
|
|
178
|
+
if (!expireTime) {
|
|
179
|
+
return true;
|
|
180
|
+
}
|
|
181
|
+
var currentTime = utils_1.getCurrentTime();
|
|
182
|
+
return expireTime <= currentTime;
|
|
183
|
+
};
|
|
184
|
+
/**
|
|
185
|
+
* Check if currently stored token should be refreshed
|
|
186
|
+
* i.e. past the window to request a new token
|
|
187
|
+
*
|
|
188
|
+
* @private
|
|
189
|
+
* @returns {boolean}
|
|
190
|
+
*/
|
|
191
|
+
TokenManager.prototype.tokenNeedsRefresh = function () {
|
|
192
|
+
var refreshTime = this.refreshTime;
|
|
193
|
+
var currentTime = utils_1.getCurrentTime();
|
|
194
|
+
if (refreshTime && refreshTime > currentTime) {
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
// Update refreshTime to 60 seconds from now to avoid redundant refreshes
|
|
198
|
+
this.refreshTime = currentTime + 60;
|
|
199
|
+
return true;
|
|
200
|
+
};
|
|
201
|
+
return TokenManager;
|
|
202
|
+
}());
|
|
203
|
+
exports.TokenManager = TokenManager;
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
*/
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.getAuthenticatorFromEnvironment = void 0;
|
|
18
19
|
var authenticators_1 = require("../authenticators");
|
|
19
20
|
var read_external_sources_1 = require("./read-external-sources");
|
|
20
21
|
/**
|
package/auth/utils/helpers.d.ts
CHANGED
|
@@ -34,3 +34,9 @@ export declare function checkCredentials(obj: any, credsToCheck: string[]): Erro
|
|
|
34
34
|
* @param {string[]} requiredOptions - The list of properties that must be specified.
|
|
35
35
|
*/
|
|
36
36
|
export declare function validateInput(options: any, requiredOptions: string[]): void;
|
|
37
|
+
/**
|
|
38
|
+
* Get the current time
|
|
39
|
+
*
|
|
40
|
+
* @returns {number} - Returns the current time in seconds.
|
|
41
|
+
*/
|
|
42
|
+
export declare function getCurrentTime(): number;
|
package/auth/utils/helpers.js
CHANGED
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
*/
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.getCurrentTime = exports.validateInput = exports.checkCredentials = exports.computeBasicAuthHeader = void 0;
|
|
18
19
|
var helper_1 = require("../../lib/helper");
|
|
19
20
|
/**
|
|
20
21
|
* Compute and return a Basic Authorization header from a username and password.
|
|
@@ -75,3 +76,12 @@ function validateInput(options, requiredOptions) {
|
|
|
75
76
|
}
|
|
76
77
|
}
|
|
77
78
|
exports.validateInput = validateInput;
|
|
79
|
+
/**
|
|
80
|
+
* Get the current time
|
|
81
|
+
*
|
|
82
|
+
* @returns {number} - Returns the current time in seconds.
|
|
83
|
+
*/
|
|
84
|
+
function getCurrentTime() {
|
|
85
|
+
return Math.floor(Date.now() / 1000);
|
|
86
|
+
}
|
|
87
|
+
exports.getCurrentTime = getCurrentTime;
|
package/auth/utils/index.js
CHANGED
|
@@ -14,9 +14,16 @@
|
|
|
14
14
|
* See the License for the specific language governing permissions and
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
*/
|
|
17
|
-
function
|
|
18
|
-
|
|
19
|
-
}
|
|
17
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
18
|
+
if (k2 === undefined) k2 = k;
|
|
19
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
20
|
+
}) : (function(o, m, k, k2) {
|
|
21
|
+
if (k2 === undefined) k2 = k;
|
|
22
|
+
o[k2] = m[k];
|
|
23
|
+
}));
|
|
24
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
25
|
+
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
|
|
26
|
+
};
|
|
20
27
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
28
|
/**
|
|
22
29
|
* @module utils
|
|
@@ -26,9 +33,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
26
33
|
* getAuthenticatorFromEnvironment: Get authenticator from external sources.
|
|
27
34
|
* readExternalSources: Get config object from external sources.
|
|
28
35
|
*/
|
|
29
|
-
|
|
30
|
-
|
|
36
|
+
__exportStar(require("./helpers"), exports);
|
|
37
|
+
__exportStar(require("./read-credentials-file"), exports);
|
|
31
38
|
var get_authenticator_from_environment_1 = require("./get-authenticator-from-environment");
|
|
32
|
-
exports
|
|
39
|
+
Object.defineProperty(exports, "getAuthenticatorFromEnvironment", { enumerable: true, get: function () { return get_authenticator_from_environment_1.getAuthenticatorFromEnvironment; } });
|
|
33
40
|
var read_external_sources_1 = require("./read-external-sources");
|
|
34
|
-
exports
|
|
41
|
+
Object.defineProperty(exports, "readExternalSources", { enumerable: true, get: function () { return read_external_sources_1.readExternalSources; } });
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
*/
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.readExternalSources = void 0;
|
|
18
19
|
var camelcase = require("camelcase");
|
|
19
20
|
var isEmpty = require("lodash.isempty");
|
|
20
21
|
var logger_1 = require("../../lib/logger");
|
package/index.d.ts
CHANGED
|
@@ -22,4 +22,5 @@ export * from './lib/helper';
|
|
|
22
22
|
export { default as qs } from './lib/querystring';
|
|
23
23
|
export { default as contentType } from './lib/content-type';
|
|
24
24
|
export * from './lib/stream-to-promise';
|
|
25
|
-
|
|
25
|
+
import * as unitTestUtils from './lib/sdk-test-helpers';
|
|
26
|
+
export { unitTestUtils };
|
package/index.js
CHANGED
|
@@ -14,20 +14,29 @@
|
|
|
14
14
|
* See the License for the specific language governing permissions and
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
*/
|
|
17
|
-
function
|
|
18
|
-
|
|
19
|
-
}
|
|
17
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
18
|
+
if (k2 === undefined) k2 = k;
|
|
19
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
20
|
+
}) : (function(o, m, k, k2) {
|
|
21
|
+
if (k2 === undefined) k2 = k;
|
|
22
|
+
o[k2] = m[k];
|
|
23
|
+
}));
|
|
24
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
25
|
+
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
|
|
26
|
+
};
|
|
20
27
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
28
|
+
exports.unitTestUtils = void 0;
|
|
21
29
|
/**
|
|
22
30
|
* @module ibm-cloud-sdk-core
|
|
23
31
|
*/
|
|
24
32
|
var base_service_1 = require("./lib/base-service");
|
|
25
|
-
exports
|
|
26
|
-
|
|
27
|
-
|
|
33
|
+
Object.defineProperty(exports, "BaseService", { enumerable: true, get: function () { return base_service_1.BaseService; } });
|
|
34
|
+
__exportStar(require("./auth"), exports);
|
|
35
|
+
__exportStar(require("./lib/helper"), exports);
|
|
28
36
|
var querystring_1 = require("./lib/querystring");
|
|
29
|
-
exports
|
|
37
|
+
Object.defineProperty(exports, "qs", { enumerable: true, get: function () { return querystring_1.default; } });
|
|
30
38
|
var content_type_1 = require("./lib/content-type");
|
|
31
|
-
exports
|
|
32
|
-
|
|
33
|
-
|
|
39
|
+
Object.defineProperty(exports, "contentType", { enumerable: true, get: function () { return content_type_1.default; } });
|
|
40
|
+
__exportStar(require("./lib/stream-to-promise"), exports);
|
|
41
|
+
var unitTestUtils = require("./lib/sdk-test-helpers");
|
|
42
|
+
exports.unitTestUtils = unitTestUtils;
|
package/lib/base-service.js
CHANGED
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
*/
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.BaseService = void 0;
|
|
18
19
|
var extend = require("extend");
|
|
19
20
|
var auth_1 = require("../auth");
|
|
20
21
|
var helper_1 = require("./helper");
|
|
@@ -89,7 +90,9 @@ var BaseService = /** @class */ (function () {
|
|
|
89
90
|
* @param {string} url The base URL for the service.
|
|
90
91
|
*/
|
|
91
92
|
BaseService.prototype.setServiceUrl = function (url) {
|
|
92
|
-
|
|
93
|
+
if (url) {
|
|
94
|
+
this.baseOptions.serviceUrl = helper_1.stripTrailingSlash(url);
|
|
95
|
+
}
|
|
93
96
|
};
|
|
94
97
|
/**
|
|
95
98
|
* Configure the service using external configuration
|
|
@@ -147,7 +150,7 @@ var BaseService = /** @class */ (function () {
|
|
|
147
150
|
// the user can define two client-level variables in the credentials file: url and disableSsl
|
|
148
151
|
var url = properties.url, disableSsl = properties.disableSsl;
|
|
149
152
|
if (url) {
|
|
150
|
-
results.serviceUrl = url;
|
|
153
|
+
results.serviceUrl = helper_1.stripTrailingSlash(url);
|
|
151
154
|
}
|
|
152
155
|
if (disableSsl === true) {
|
|
153
156
|
results.disableSslVerification = disableSsl;
|
package/lib/helper.js
CHANGED
|
@@ -22,6 +22,7 @@ var __spreadArrays = (this && this.__spreadArrays) || function () {
|
|
|
22
22
|
return r;
|
|
23
23
|
};
|
|
24
24
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
+
exports.toLowerKeys = exports.buildRequestFileObject = exports.getFormat = exports.isHTML = exports.getMissingParams = exports.stripTrailingSlash = exports.getContentType = exports.isEmptyObject = exports.isFileData = exports.isFileWithMetadata = void 0;
|
|
25
26
|
var extend = require("extend");
|
|
26
27
|
var fileType = require("file-type");
|
|
27
28
|
var isstream_1 = require("isstream");
|
|
@@ -93,7 +94,7 @@ function getMissingParams(params, requires) {
|
|
|
93
94
|
else {
|
|
94
95
|
missing = [];
|
|
95
96
|
requires.forEach(function (require) {
|
|
96
|
-
if (
|
|
97
|
+
if (isMissing(params[require])) {
|
|
97
98
|
missing.push(require);
|
|
98
99
|
}
|
|
99
100
|
});
|
|
@@ -103,6 +104,16 @@ function getMissingParams(params, requires) {
|
|
|
103
104
|
: null;
|
|
104
105
|
}
|
|
105
106
|
exports.getMissingParams = getMissingParams;
|
|
107
|
+
/**
|
|
108
|
+
* Returns true if value is determined to be "missing". Currently defining "missing"
|
|
109
|
+
* as `undefined`, `null`, or the empty string.
|
|
110
|
+
*
|
|
111
|
+
* @param value - the parameter value
|
|
112
|
+
* @returns boolean
|
|
113
|
+
*/
|
|
114
|
+
function isMissing(value) {
|
|
115
|
+
return value === undefined || value === null || value === '';
|
|
116
|
+
}
|
|
106
117
|
/**
|
|
107
118
|
* Return true if 'text' is html
|
|
108
119
|
* @param {string} text - The 'text' to analyze
|
package/lib/request-wrapper.js
CHANGED
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
*/
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.RequestWrapper = void 0;
|
|
18
19
|
var axios_1 = require("axios");
|
|
19
20
|
var axios_cookiejar_support_1 = require("axios-cookiejar-support");
|
|
20
21
|
var extend = require("extend");
|
|
@@ -156,7 +157,7 @@ var RequestWrapper = /** @class */ (function () {
|
|
|
156
157
|
}
|
|
157
158
|
// Add service default endpoint if options.url start with /
|
|
158
159
|
if (url && url.charAt(0) === '/') {
|
|
159
|
-
url = serviceUrl + url;
|
|
160
|
+
url = helper_1.stripTrailingSlash(serviceUrl) + url;
|
|
160
161
|
}
|
|
161
162
|
url = helper_1.stripTrailingSlash(url);
|
|
162
163
|
var data = body;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Copyright 2019 IBM Corp. All Rights Reserved.
|
|
2
|
+
* Copyright 2019, 2020 IBM Corp. All Rights Reserved.
|
|
3
3
|
*
|
|
4
4
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
5
|
* you may not use this file except in compliance with the License.
|
|
@@ -17,9 +17,6 @@
|
|
|
17
17
|
* This module provides a set of helper methods used to reduce code duplication in the generated unit tests
|
|
18
18
|
* for the SDKs that depend on this core package. Note that these methods are not used by the tests for this
|
|
19
19
|
* package - they are meant to be exported and made available to dependent libraries.
|
|
20
|
-
*
|
|
21
|
-
* They are included in the `test` directory since they rely on `jest` globals (like `expect`) and this
|
|
22
|
-
* requires special linting rules that should not be configured in the rest of the source code.
|
|
23
20
|
*/
|
|
24
21
|
/**
|
|
25
22
|
* Takes the request options constructed by the SDK and checks that the `url` and `method` properties
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/**
|
|
3
|
-
* Copyright 2019 IBM Corp. All Rights Reserved.
|
|
3
|
+
* Copyright 2019, 2020 IBM Corp. All Rights Reserved.
|
|
4
4
|
*
|
|
5
5
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
6
|
* you may not use this file except in compliance with the License.
|
|
@@ -15,14 +15,12 @@
|
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
*/
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
-
|
|
18
|
+
exports.expectToBePromise = exports.getOptions = exports.checkForSuccessfulExecution = exports.checkUserHeader = exports.checkMediaHeaders = exports.checkUrlAndMethod = void 0;
|
|
19
|
+
var expect = require("expect");
|
|
19
20
|
/**
|
|
20
21
|
* This module provides a set of helper methods used to reduce code duplication in the generated unit tests
|
|
21
22
|
* for the SDKs that depend on this core package. Note that these methods are not used by the tests for this
|
|
22
23
|
* package - they are meant to be exported and made available to dependent libraries.
|
|
23
|
-
*
|
|
24
|
-
* They are included in the `test` directory since they rely on `jest` globals (like `expect`) and this
|
|
25
|
-
* requires special linting rules that should not be configured in the rest of the source code.
|
|
26
24
|
*/
|
|
27
25
|
/**
|
|
28
26
|
* Takes the request options constructed by the SDK and checks that the `url` and `method` properties
|
package/lib/stream-to-promise.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.streamToPromise = void 0;
|
|
3
4
|
/**
|
|
4
5
|
* Helper method that can be bound to a stream - it sets the output to utf-8, captures all of the results, and returns a promise that resolves to the final text
|
|
5
6
|
* Essentially a smaller version of concat-stream wrapped in a promise
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ibm-cloud-sdk-core",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.2",
|
|
4
4
|
"description": "Core functionality to support SDKs generated with IBM's OpenAPI 3 SDK Generator.",
|
|
5
5
|
"main": "index",
|
|
6
6
|
"repository": {
|
|
@@ -32,13 +32,13 @@
|
|
|
32
32
|
"@types/extend": "~3.0.0",
|
|
33
33
|
"@types/file-type": "~5.2.1",
|
|
34
34
|
"@types/isstream": "^0.1.0",
|
|
35
|
-
"@types/jest": "^24.0.23",
|
|
36
35
|
"@types/node": "~10.14.19",
|
|
37
|
-
"axios": "^0.18.
|
|
38
|
-
"axios-cookiejar-support": "^0.
|
|
36
|
+
"axios": "^0.18.1",
|
|
37
|
+
"axios-cookiejar-support": "^1.0.0",
|
|
39
38
|
"camelcase": "^5.3.1",
|
|
40
39
|
"debug": "^4.1.1",
|
|
41
40
|
"dotenv": "^6.2.0",
|
|
41
|
+
"expect": "^26.1.0",
|
|
42
42
|
"extend": "~3.0.2",
|
|
43
43
|
"file-type": "^7.7.1",
|
|
44
44
|
"form-data": "^2.3.3",
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
"object.omit": "~3.0.0",
|
|
50
50
|
"object.pick": "~1.3.0",
|
|
51
51
|
"semver": "^6.2.0",
|
|
52
|
-
"
|
|
52
|
+
"tough-cookie": "^4.0.0"
|
|
53
53
|
},
|
|
54
54
|
"browser": {
|
|
55
55
|
"./auth/utils/read-credentials-file": "./auth/utils/read-credentials-file.browser"
|
package/test/utils/index.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright 2019 IBM Corp. All Rights Reserved.
|
|
3
|
-
*
|
|
4
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
* you may not use this file except in compliance with the License.
|
|
6
|
-
* You may obtain a copy of the License at
|
|
7
|
-
*
|
|
8
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
*
|
|
10
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
* See the License for the specific language governing permissions and
|
|
14
|
-
* limitations under the License.
|
|
15
|
-
*/
|
|
16
|
-
import * as unitTestUtils from './unit-test-helpers';
|
|
17
|
-
export { unitTestUtils };
|
package/test/utils/index.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Copyright 2019 IBM Corp. All Rights Reserved.
|
|
4
|
-
*
|
|
5
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
-
* you may not use this file except in compliance with the License.
|
|
7
|
-
* You may obtain a copy of the License at
|
|
8
|
-
*
|
|
9
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
-
*
|
|
11
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
-
* See the License for the specific language governing permissions and
|
|
15
|
-
* limitations under the License.
|
|
16
|
-
*/
|
|
17
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
-
/*
|
|
19
|
-
* The utility methods in this directory are not to assist in the testing
|
|
20
|
-
* of the core, but to assist with the testing of the SDKs that depend on
|
|
21
|
-
* this core library. Specifically, the unit tests generated by the
|
|
22
|
-
* IBM OpenAPI SDK Gen project rely on these methods.
|
|
23
|
-
*/
|
|
24
|
-
var unitTestUtils = require("./unit-test-helpers");
|
|
25
|
-
exports.unitTestUtils = unitTestUtils;
|