@azure/storage-blob 12.13.0-beta.1 → 12.20.0-alpha.20230303.1
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/dist/index.js +13661 -14216
- package/dist/index.js.map +1 -1
- package/dist-esm/storage-blob/src/BatchResponse.js.map +1 -1
- package/dist-esm/storage-blob/src/BatchResponseParser.js +3 -2
- package/dist-esm/storage-blob/src/BatchResponseParser.js.map +1 -1
- package/dist-esm/storage-blob/src/BlobBatch.js +74 -87
- package/dist-esm/storage-blob/src/BlobBatch.js.map +1 -1
- package/dist-esm/storage-blob/src/BlobBatchClient.js +10 -23
- package/dist-esm/storage-blob/src/BlobBatchClient.js.map +1 -1
- package/dist-esm/storage-blob/src/BlobDownloadResponse.js +1 -1
- package/dist-esm/storage-blob/src/BlobDownloadResponse.js.map +1 -1
- package/dist-esm/storage-blob/src/BlobLeaseClient.js +54 -83
- package/dist-esm/storage-blob/src/BlobLeaseClient.js.map +1 -1
- package/dist-esm/storage-blob/src/BlobQueryResponse.browser.js.map +1 -1
- package/dist-esm/storage-blob/src/BlobQueryResponse.js +1 -1
- package/dist-esm/storage-blob/src/BlobQueryResponse.js.map +1 -1
- package/dist-esm/storage-blob/src/BlobServiceClient.js +67 -165
- package/dist-esm/storage-blob/src/BlobServiceClient.js.map +1 -1
- package/dist-esm/storage-blob/src/Clients.js +651 -889
- package/dist-esm/storage-blob/src/Clients.js.map +1 -1
- package/dist-esm/storage-blob/src/ContainerClient.js +115 -238
- package/dist-esm/storage-blob/src/ContainerClient.js.map +1 -1
- package/dist-esm/storage-blob/src/PageBlobRangeResponse.js.map +1 -1
- package/dist-esm/storage-blob/src/Pipeline.js +194 -40
- package/dist-esm/storage-blob/src/Pipeline.js.map +1 -1
- package/dist-esm/storage-blob/src/StorageBrowserPolicyFactory.js.map +1 -1
- package/dist-esm/storage-blob/src/StorageClient.js +4 -17
- package/dist-esm/storage-blob/src/StorageClient.js.map +1 -1
- package/dist-esm/storage-blob/src/StorageRetryPolicyFactory.js.map +1 -1
- package/dist-esm/storage-blob/src/credentials/AnonymousCredential.js.map +1 -1
- package/dist-esm/storage-blob/src/credentials/Credential.js.map +1 -1
- package/dist-esm/storage-blob/src/credentials/StorageSharedKeyCredential.js.map +1 -1
- package/dist-esm/storage-blob/src/generated/src/index.js +1 -1
- package/dist-esm/storage-blob/src/generated/src/index.js.map +1 -1
- package/dist-esm/storage-blob/src/generated/src/models/index.js +248 -1
- package/dist-esm/storage-blob/src/generated/src/models/index.js.map +1 -1
- package/dist-esm/storage-blob/src/generated/src/models/mappers.js +13 -11
- package/dist-esm/storage-blob/src/generated/src/models/mappers.js.map +1 -1
- package/dist-esm/storage-blob/src/generated/src/models/parameters.js +5 -5
- package/dist-esm/storage-blob/src/generated/src/models/parameters.js.map +1 -1
- package/dist-esm/storage-blob/src/generated/src/operations/appendBlob.js +11 -27
- package/dist-esm/storage-blob/src/generated/src/operations/appendBlob.js.map +1 -1
- package/dist-esm/storage-blob/src/generated/src/operations/blob.js +28 -110
- package/dist-esm/storage-blob/src/generated/src/operations/blob.js.map +1 -1
- package/dist-esm/storage-blob/src/generated/src/operations/blockBlob.js +16 -43
- package/dist-esm/storage-blob/src/generated/src/operations/blockBlob.js.map +1 -1
- package/dist-esm/storage-blob/src/generated/src/operations/container.js +22 -86
- package/dist-esm/storage-blob/src/generated/src/operations/container.js.map +1 -1
- package/dist-esm/storage-blob/src/generated/src/operations/pageBlob.js +16 -54
- package/dist-esm/storage-blob/src/generated/src/operations/pageBlob.js.map +1 -1
- package/dist-esm/storage-blob/src/generated/src/operations/service.js +12 -42
- package/dist-esm/storage-blob/src/generated/src/operations/service.js.map +1 -1
- package/dist-esm/storage-blob/src/generated/src/operationsInterfaces/appendBlob.js +9 -0
- package/dist-esm/storage-blob/src/generated/src/operationsInterfaces/appendBlob.js.map +1 -0
- package/dist-esm/storage-blob/src/generated/src/operationsInterfaces/blob.js +9 -0
- package/dist-esm/storage-blob/src/generated/src/operationsInterfaces/blob.js.map +1 -0
- package/dist-esm/storage-blob/src/generated/src/operationsInterfaces/blockBlob.js +9 -0
- package/dist-esm/storage-blob/src/generated/src/operationsInterfaces/blockBlob.js.map +1 -0
- package/dist-esm/storage-blob/src/generated/src/operationsInterfaces/container.js +9 -0
- package/dist-esm/storage-blob/src/generated/src/operationsInterfaces/container.js.map +1 -0
- package/dist-esm/storage-blob/src/generated/src/operationsInterfaces/index.js +14 -0
- package/dist-esm/storage-blob/src/generated/src/operationsInterfaces/index.js.map +1 -0
- package/dist-esm/storage-blob/src/generated/src/operationsInterfaces/pageBlob.js +9 -0
- package/dist-esm/storage-blob/src/generated/src/operationsInterfaces/pageBlob.js.map +1 -0
- package/dist-esm/storage-blob/src/generated/src/operationsInterfaces/service.js +9 -0
- package/dist-esm/storage-blob/src/generated/src/operationsInterfaces/service.js.map +1 -0
- package/dist-esm/storage-blob/src/generated/src/storageClient.js +32 -10
- package/dist-esm/storage-blob/src/generated/src/storageClient.js.map +1 -1
- package/dist-esm/storage-blob/src/generatedModels.js.map +1 -1
- package/dist-esm/storage-blob/src/index.browser.js +3 -2
- package/dist-esm/storage-blob/src/index.browser.js.map +1 -1
- package/dist-esm/storage-blob/src/index.js +3 -2
- package/dist-esm/storage-blob/src/index.js.map +1 -1
- package/dist-esm/storage-blob/src/policies/AnonymousCredentialPolicy.js.map +1 -1
- package/dist-esm/storage-blob/src/policies/CredentialPolicy.js +1 -1
- package/dist-esm/storage-blob/src/policies/CredentialPolicy.js.map +1 -1
- package/dist-esm/storage-blob/src/policies/PathParameterWorkaroundPolicy.js +25 -0
- package/dist-esm/storage-blob/src/policies/PathParameterWorkaroundPolicy.js.map +1 -0
- package/dist-esm/storage-blob/src/policies/RequestPolicy.js +40 -0
- package/dist-esm/storage-blob/src/policies/RequestPolicy.js.map +1 -0
- package/dist-esm/storage-blob/src/policies/StorageBrowserPolicy.js +2 -1
- package/dist-esm/storage-blob/src/policies/StorageBrowserPolicy.js.map +1 -1
- package/dist-esm/storage-blob/src/policies/StorageBrowserPolicyV2.js +31 -0
- package/dist-esm/storage-blob/src/policies/StorageBrowserPolicyV2.js.map +1 -0
- package/dist-esm/storage-blob/src/policies/StorageRetryPolicy.js +1 -1
- package/dist-esm/storage-blob/src/policies/StorageRetryPolicy.js.map +1 -1
- package/dist-esm/storage-blob/src/policies/StorageRetryPolicyV2.js +165 -0
- package/dist-esm/storage-blob/src/policies/StorageRetryPolicyV2.js.map +1 -0
- package/dist-esm/storage-blob/src/policies/StorageSharedKeyCredentialPolicy.js.map +1 -1
- package/dist-esm/storage-blob/src/policies/StorageSharedKeyCredentialPolicyV2.browser.js +18 -0
- package/dist-esm/storage-blob/src/policies/StorageSharedKeyCredentialPolicyV2.browser.js.map +1 -0
- package/dist-esm/storage-blob/src/policies/StorageSharedKeyCredentialPolicyV2.js +131 -0
- package/dist-esm/storage-blob/src/policies/StorageSharedKeyCredentialPolicyV2.js.map +1 -0
- package/dist-esm/storage-blob/src/pollers/BlobStartCopyFromUrlPoller.js +1 -1
- package/dist-esm/storage-blob/src/pollers/BlobStartCopyFromUrlPoller.js.map +1 -1
- package/dist-esm/storage-blob/src/utils/BlobQuickQueryStream.js.map +1 -1
- package/dist-esm/storage-blob/src/utils/RetriableReadableStream.js +10 -2
- package/dist-esm/storage-blob/src/utils/RetriableReadableStream.js.map +1 -1
- package/dist-esm/storage-blob/src/utils/cache.js +5 -2
- package/dist-esm/storage-blob/src/utils/cache.js.map +1 -1
- package/dist-esm/storage-blob/src/utils/constants.js +2 -2
- package/dist-esm/storage-blob/src/utils/constants.js.map +1 -1
- package/dist-esm/storage-blob/src/utils/tracing.js +5 -18
- package/dist-esm/storage-blob/src/utils/tracing.js.map +1 -1
- package/dist-esm/storage-blob/src/utils/utils.common.js +83 -42
- package/dist-esm/storage-blob/src/utils/utils.common.js.map +1 -1
- package/package.json +22 -15
- package/types/3.1/storage-blob.d.ts +2311 -604
- package/types/latest/storage-blob.d.ts +2612 -725
- package/dist-esm/storage-blob/src/TelemetryPolicyFactory.js +0 -50
- package/dist-esm/storage-blob/src/TelemetryPolicyFactory.js.map +0 -1
- package/dist-esm/storage-blob/src/generated/src/storageClientContext.js +0 -39
- package/dist-esm/storage-blob/src/generated/src/storageClientContext.js.map +0 -1
- package/dist-esm/storage-blob/src/policies/StorageBearerTokenChallengeAuthenticationPolicy.js +0 -245
- package/dist-esm/storage-blob/src/policies/StorageBearerTokenChallengeAuthenticationPolicy.js.map +0 -1
- package/dist-esm/storage-blob/src/policies/TelemetryPolicy.js +0 -36
- package/dist-esm/storage-blob/src/policies/TelemetryPolicy.js.map +0 -1
@@ -1,50 +0,0 @@
|
|
1
|
-
// Copyright (c) Microsoft Corporation.
|
2
|
-
// Licensed under the MIT license.
|
3
|
-
import { isNode, } from "@azure/core-http";
|
4
|
-
import * as os from "os";
|
5
|
-
import { TelemetryPolicy } from "./policies/TelemetryPolicy";
|
6
|
-
import { SDK_VERSION } from "./utils/constants";
|
7
|
-
/**
|
8
|
-
* TelemetryPolicyFactory is a factory class helping generating {@link TelemetryPolicy} objects.
|
9
|
-
*/
|
10
|
-
export class TelemetryPolicyFactory {
|
11
|
-
/**
|
12
|
-
* Creates an instance of TelemetryPolicyFactory.
|
13
|
-
* @param telemetry -
|
14
|
-
*/
|
15
|
-
constructor(telemetry) {
|
16
|
-
const userAgentInfo = [];
|
17
|
-
if (isNode) {
|
18
|
-
if (telemetry) {
|
19
|
-
const telemetryString = telemetry.userAgentPrefix || "";
|
20
|
-
if (telemetryString.length > 0 && userAgentInfo.indexOf(telemetryString) === -1) {
|
21
|
-
userAgentInfo.push(telemetryString);
|
22
|
-
}
|
23
|
-
}
|
24
|
-
// e.g. azsdk-js-storageblob/10.0.0
|
25
|
-
const libInfo = `azsdk-js-storageblob/${SDK_VERSION}`;
|
26
|
-
if (userAgentInfo.indexOf(libInfo) === -1) {
|
27
|
-
userAgentInfo.push(libInfo);
|
28
|
-
}
|
29
|
-
// e.g. (NODE-VERSION 4.9.1; Windows_NT 10.0.16299)
|
30
|
-
let runtimeInfo = `(NODE-VERSION ${process.version})`;
|
31
|
-
if (os) {
|
32
|
-
runtimeInfo = `(NODE-VERSION ${process.version}; ${os.type()} ${os.release()})`;
|
33
|
-
}
|
34
|
-
if (userAgentInfo.indexOf(runtimeInfo) === -1) {
|
35
|
-
userAgentInfo.push(runtimeInfo);
|
36
|
-
}
|
37
|
-
}
|
38
|
-
this.telemetryString = userAgentInfo.join(" ");
|
39
|
-
}
|
40
|
-
/**
|
41
|
-
* Creates a TelemetryPolicy object.
|
42
|
-
*
|
43
|
-
* @param nextPolicy -
|
44
|
-
* @param options -
|
45
|
-
*/
|
46
|
-
create(nextPolicy, options) {
|
47
|
-
return new TelemetryPolicy(nextPolicy, options, this.telemetryString);
|
48
|
-
}
|
49
|
-
}
|
50
|
-
//# sourceMappingURL=TelemetryPolicyFactory.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"TelemetryPolicyFactory.js","sourceRoot":"","sources":["../../../src/TelemetryPolicyFactory.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EACL,MAAM,GAKP,MAAM,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AAEzB,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAC7D,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD;;GAEG;AACH,MAAM,OAAO,sBAAsB;IAMjC;;;OAGG;IACH,YAAY,SAA4B;QACtC,MAAM,aAAa,GAAa,EAAE,CAAC;QAEnC,IAAI,MAAM,EAAE;YACV,IAAI,SAAS,EAAE;gBACb,MAAM,eAAe,GAAG,SAAS,CAAC,eAAe,IAAI,EAAE,CAAC;gBACxD,IAAI,eAAe,CAAC,MAAM,GAAG,CAAC,IAAI,aAAa,CAAC,OAAO,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC,EAAE;oBAC/E,aAAa,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;iBACrC;aACF;YAED,mCAAmC;YACnC,MAAM,OAAO,GAAG,wBAAwB,WAAW,EAAE,CAAC;YACtD,IAAI,aAAa,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE;gBACzC,aAAa,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;aAC7B;YAED,mDAAmD;YACnD,IAAI,WAAW,GAAG,iBAAiB,OAAO,CAAC,OAAO,GAAG,CAAC;YACtD,IAAI,EAAE,EAAE;gBACN,WAAW,GAAG,iBAAiB,OAAO,CAAC,OAAO,KAAK,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,OAAO,EAAE,GAAG,CAAC;aACjF;YACD,IAAI,aAAa,CAAC,OAAO,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC,EAAE;gBAC7C,aAAa,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;aACjC;SACF;QAED,IAAI,CAAC,eAAe,GAAG,aAAa,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACjD,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,UAAyB,EAAE,OAA6B;QACpE,OAAO,IAAI,eAAe,CAAC,UAAU,EAAE,OAAO,EAAE,IAAI,CAAC,eAAe,CAAC,CAAC;IACxE,CAAC;CACF","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nimport {\n isNode,\n RequestPolicy,\n RequestPolicyFactory,\n RequestPolicyOptions,\n UserAgentOptions,\n} from \"@azure/core-http\";\nimport * as os from \"os\";\n\nimport { TelemetryPolicy } from \"./policies/TelemetryPolicy\";\nimport { SDK_VERSION } from \"./utils/constants\";\n\n/**\n * TelemetryPolicyFactory is a factory class helping generating {@link TelemetryPolicy} objects.\n */\nexport class TelemetryPolicyFactory implements RequestPolicyFactory {\n /**\n * @internal\n */\n public readonly telemetryString: string;\n\n /**\n * Creates an instance of TelemetryPolicyFactory.\n * @param telemetry -\n */\n constructor(telemetry?: UserAgentOptions) {\n const userAgentInfo: string[] = [];\n\n if (isNode) {\n if (telemetry) {\n const telemetryString = telemetry.userAgentPrefix || \"\";\n if (telemetryString.length > 0 && userAgentInfo.indexOf(telemetryString) === -1) {\n userAgentInfo.push(telemetryString);\n }\n }\n\n // e.g. azsdk-js-storageblob/10.0.0\n const libInfo = `azsdk-js-storageblob/${SDK_VERSION}`;\n if (userAgentInfo.indexOf(libInfo) === -1) {\n userAgentInfo.push(libInfo);\n }\n\n // e.g. (NODE-VERSION 4.9.1; Windows_NT 10.0.16299)\n let runtimeInfo = `(NODE-VERSION ${process.version})`;\n if (os) {\n runtimeInfo = `(NODE-VERSION ${process.version}; ${os.type()} ${os.release()})`;\n }\n if (userAgentInfo.indexOf(runtimeInfo) === -1) {\n userAgentInfo.push(runtimeInfo);\n }\n }\n\n this.telemetryString = userAgentInfo.join(\" \");\n }\n\n /**\n * Creates a TelemetryPolicy object.\n *\n * @param nextPolicy -\n * @param options -\n */\n public create(nextPolicy: RequestPolicy, options: RequestPolicyOptions): TelemetryPolicy {\n return new TelemetryPolicy(nextPolicy, options, this.telemetryString);\n }\n}\n"]}
|
@@ -1,39 +0,0 @@
|
|
1
|
-
/*
|
2
|
-
* Copyright (c) Microsoft Corporation.
|
3
|
-
* Licensed under the MIT License.
|
4
|
-
*
|
5
|
-
* Code generated by Microsoft (R) AutoRest Code Generator.
|
6
|
-
* Changes may cause incorrect behavior and will be lost if the code is regenerated.
|
7
|
-
*/
|
8
|
-
import * as coreHttp from "@azure/core-http";
|
9
|
-
const packageName = "azure-storage-blob";
|
10
|
-
const packageVersion = "12.13.0-beta.1";
|
11
|
-
export class StorageClientContext extends coreHttp.ServiceClient {
|
12
|
-
/**
|
13
|
-
* Initializes a new instance of the StorageClientContext class.
|
14
|
-
* @param url The URL of the service account, container, or blob that is the target of the desired
|
15
|
-
* operation.
|
16
|
-
* @param options The parameter options
|
17
|
-
*/
|
18
|
-
constructor(url, options) {
|
19
|
-
if (url === undefined) {
|
20
|
-
throw new Error("'url' cannot be null");
|
21
|
-
}
|
22
|
-
// Initializing default values for options
|
23
|
-
if (!options) {
|
24
|
-
options = {};
|
25
|
-
}
|
26
|
-
if (!options.userAgent) {
|
27
|
-
const defaultUserAgent = coreHttp.getDefaultUserAgentValue();
|
28
|
-
options.userAgent = `${packageName}/${packageVersion} ${defaultUserAgent}`;
|
29
|
-
}
|
30
|
-
super(undefined, options);
|
31
|
-
this.requestContentType = "application/json; charset=utf-8";
|
32
|
-
this.baseUri = options.endpoint || "{url}";
|
33
|
-
// Parameter assignments
|
34
|
-
this.url = url;
|
35
|
-
// Assigning values to Constant parameters
|
36
|
-
this.version = options.version || "2021-12-02";
|
37
|
-
}
|
38
|
-
}
|
39
|
-
//# sourceMappingURL=storageClientContext.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"storageClientContext.js","sourceRoot":"","sources":["../../../../../src/generated/src/storageClientContext.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,QAAQ,MAAM,kBAAkB,CAAC;AAG7C,MAAM,WAAW,GAAG,oBAAoB,CAAC;AACzC,MAAM,cAAc,GAAG,gBAAgB,CAAC;AAExC,MAAM,OAAO,oBAAqB,SAAQ,QAAQ,CAAC,aAAa;IAI9D;;;;;OAKG;IACH,YAAY,GAAW,EAAE,OAAqC;QAC5D,IAAI,GAAG,KAAK,SAAS,EAAE;YACrB,MAAM,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC;SACzC;QAED,0CAA0C;QAC1C,IAAI,CAAC,OAAO,EAAE;YACZ,OAAO,GAAG,EAAE,CAAC;SACd;QAED,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE;YACtB,MAAM,gBAAgB,GAAG,QAAQ,CAAC,wBAAwB,EAAE,CAAC;YAC7D,OAAO,CAAC,SAAS,GAAG,GAAG,WAAW,IAAI,cAAc,IAAI,gBAAgB,EAAE,CAAC;SAC5E;QAED,KAAK,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;QAE1B,IAAI,CAAC,kBAAkB,GAAG,iCAAiC,CAAC;QAE5D,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,QAAQ,IAAI,OAAO,CAAC;QAE3C,wBAAwB;QACxB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QAEf,0CAA0C;QAC1C,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,YAAY,CAAC;IACjD,CAAC;CACF","sourcesContent":["/*\n * Copyright (c) Microsoft Corporation.\n * Licensed under the MIT License.\n *\n * Code generated by Microsoft (R) AutoRest Code Generator.\n * Changes may cause incorrect behavior and will be lost if the code is regenerated.\n */\n\nimport * as coreHttp from \"@azure/core-http\";\nimport { StorageClientOptionalParams } from \"./models\";\n\nconst packageName = \"azure-storage-blob\";\nconst packageVersion = \"12.13.0-beta.1\";\n\nexport class StorageClientContext extends coreHttp.ServiceClient {\n url: string;\n version: string;\n\n /**\n * Initializes a new instance of the StorageClientContext class.\n * @param url The URL of the service account, container, or blob that is the target of the desired\n * operation.\n * @param options The parameter options\n */\n constructor(url: string, options?: StorageClientOptionalParams) {\n if (url === undefined) {\n throw new Error(\"'url' cannot be null\");\n }\n\n // Initializing default values for options\n if (!options) {\n options = {};\n }\n\n if (!options.userAgent) {\n const defaultUserAgent = coreHttp.getDefaultUserAgentValue();\n options.userAgent = `${packageName}/${packageVersion} ${defaultUserAgent}`;\n }\n\n super(undefined, options);\n\n this.requestContentType = \"application/json; charset=utf-8\";\n\n this.baseUri = options.endpoint || \"{url}\";\n\n // Parameter assignments\n this.url = url;\n\n // Assigning values to Constant parameters\n this.version = options.version || \"2021-12-02\";\n }\n}\n"]}
|
package/dist-esm/storage-blob/src/policies/StorageBearerTokenChallengeAuthenticationPolicy.js
DELETED
@@ -1,245 +0,0 @@
|
|
1
|
-
// Copyright (c) Microsoft Corporation.
|
2
|
-
// Licensed under the MIT license.
|
3
|
-
import { URLBuilder } from "@azure/core-http";
|
4
|
-
import { BaseRequestPolicy, } from "@azure/core-http";
|
5
|
-
import { delay } from "@azure/core-http";
|
6
|
-
/**
|
7
|
-
* A set of constants used internally when processing requests.
|
8
|
-
*/
|
9
|
-
const Constants = {
|
10
|
-
DefaultScope: "/.default",
|
11
|
-
/**
|
12
|
-
* Defines constants for use with HTTP headers.
|
13
|
-
*/
|
14
|
-
HeaderConstants: {
|
15
|
-
/**
|
16
|
-
* The Authorization header.
|
17
|
-
*/
|
18
|
-
AUTHORIZATION: "authorization",
|
19
|
-
},
|
20
|
-
};
|
21
|
-
// Default options for the cycler if none are provided
|
22
|
-
const DEFAULT_CYCLER_OPTIONS = {
|
23
|
-
forcedRefreshWindowInMs: 1000,
|
24
|
-
retryIntervalInMs: 3000,
|
25
|
-
refreshWindowInMs: 1000 * 60 * 2, // Start refreshing 2m before expiry
|
26
|
-
};
|
27
|
-
/**
|
28
|
-
* Converts an an unreliable access token getter (which may resolve with null)
|
29
|
-
* into an AccessTokenGetter by retrying the unreliable getter in a regular
|
30
|
-
* interval.
|
31
|
-
*
|
32
|
-
* @param getAccessToken - a function that produces a promise of an access
|
33
|
-
* token that may fail by returning null
|
34
|
-
* @param retryIntervalInMs - the time (in milliseconds) to wait between retry
|
35
|
-
* attempts
|
36
|
-
* @param timeoutInMs - the timestamp after which the refresh attempt will fail,
|
37
|
-
* throwing an exception
|
38
|
-
* @returns - a promise that, if it resolves, will resolve with an access token
|
39
|
-
*/
|
40
|
-
async function beginRefresh(getAccessToken, retryIntervalInMs, timeoutInMs) {
|
41
|
-
// This wrapper handles exceptions gracefully as long as we haven't exceeded
|
42
|
-
// the timeout.
|
43
|
-
async function tryGetAccessToken() {
|
44
|
-
if (Date.now() < timeoutInMs) {
|
45
|
-
try {
|
46
|
-
return await getAccessToken();
|
47
|
-
}
|
48
|
-
catch (_a) {
|
49
|
-
return null;
|
50
|
-
}
|
51
|
-
}
|
52
|
-
else {
|
53
|
-
const finalToken = await getAccessToken();
|
54
|
-
// Timeout is up, so throw if it's still null
|
55
|
-
if (finalToken === null) {
|
56
|
-
throw new Error("Failed to refresh access token.");
|
57
|
-
}
|
58
|
-
return finalToken;
|
59
|
-
}
|
60
|
-
}
|
61
|
-
let token = await tryGetAccessToken();
|
62
|
-
while (token === null) {
|
63
|
-
await delay(retryIntervalInMs);
|
64
|
-
token = await tryGetAccessToken();
|
65
|
-
}
|
66
|
-
return token;
|
67
|
-
}
|
68
|
-
/**
|
69
|
-
* Creates a token cycler from a credential, scopes, and optional settings.
|
70
|
-
*
|
71
|
-
* A token cycler represents a way to reliably retrieve a valid access token
|
72
|
-
* from a TokenCredential. It will handle initializing the token, refreshing it
|
73
|
-
* when it nears expiration, and synchronizes refresh attempts to avoid
|
74
|
-
* concurrency hazards.
|
75
|
-
*
|
76
|
-
* @param credential - the underlying TokenCredential that provides the access
|
77
|
-
* token
|
78
|
-
* @param scopes - the scopes to request authorization for
|
79
|
-
* @param tokenCyclerOptions - optionally override default settings for the cycler
|
80
|
-
*
|
81
|
-
* @returns - a function that reliably produces a valid access token
|
82
|
-
*/
|
83
|
-
function createTokenCycler(credential, scopes, tokenCyclerOptions) {
|
84
|
-
let refreshWorker = null;
|
85
|
-
let token = null;
|
86
|
-
const options = Object.assign(Object.assign({}, DEFAULT_CYCLER_OPTIONS), tokenCyclerOptions);
|
87
|
-
/**
|
88
|
-
* This little holder defines several predicates that we use to construct
|
89
|
-
* the rules of refreshing the token.
|
90
|
-
*/
|
91
|
-
const cycler = {
|
92
|
-
/**
|
93
|
-
* Produces true if a refresh job is currently in progress.
|
94
|
-
*/
|
95
|
-
get isRefreshing() {
|
96
|
-
return refreshWorker !== null;
|
97
|
-
},
|
98
|
-
/**
|
99
|
-
* Produces true if the cycler SHOULD refresh (we are within the refresh
|
100
|
-
* window and not already refreshing)
|
101
|
-
*/
|
102
|
-
get shouldRefresh() {
|
103
|
-
var _a;
|
104
|
-
return (!cycler.isRefreshing &&
|
105
|
-
((_a = token === null || token === void 0 ? void 0 : token.expiresOnTimestamp) !== null && _a !== void 0 ? _a : 0) - options.refreshWindowInMs < Date.now());
|
106
|
-
},
|
107
|
-
/**
|
108
|
-
* Produces true if the cycler MUST refresh (null or nearly-expired
|
109
|
-
* token).
|
110
|
-
*/
|
111
|
-
get mustRefresh() {
|
112
|
-
return (token === null || token.expiresOnTimestamp - options.forcedRefreshWindowInMs < Date.now());
|
113
|
-
},
|
114
|
-
};
|
115
|
-
/**
|
116
|
-
* Starts a refresh job or returns the existing job if one is already
|
117
|
-
* running.
|
118
|
-
*/
|
119
|
-
function refresh(getTokenOptions) {
|
120
|
-
var _a;
|
121
|
-
if (!cycler.isRefreshing) {
|
122
|
-
// We bind `scopes` here to avoid passing it around a lot
|
123
|
-
const tryGetAccessToken = () => credential.getToken(scopes, getTokenOptions);
|
124
|
-
// Take advantage of promise chaining to insert an assignment to `token`
|
125
|
-
// before the refresh can be considered done.
|
126
|
-
refreshWorker = beginRefresh(tryGetAccessToken, options.retryIntervalInMs,
|
127
|
-
// If we don't have a token, then we should timeout immediately
|
128
|
-
(_a = token === null || token === void 0 ? void 0 : token.expiresOnTimestamp) !== null && _a !== void 0 ? _a : Date.now())
|
129
|
-
.then((_token) => {
|
130
|
-
refreshWorker = null;
|
131
|
-
token = _token;
|
132
|
-
return token;
|
133
|
-
})
|
134
|
-
.catch((reason) => {
|
135
|
-
// We also should reset the refresher if we enter a failed state. All
|
136
|
-
// existing awaiters will throw, but subsequent requests will start a
|
137
|
-
// new retry chain.
|
138
|
-
refreshWorker = null;
|
139
|
-
token = null;
|
140
|
-
throw reason;
|
141
|
-
});
|
142
|
-
}
|
143
|
-
return refreshWorker;
|
144
|
-
}
|
145
|
-
return async (tokenOptions) => {
|
146
|
-
//
|
147
|
-
// Simple rules:
|
148
|
-
// - If we MUST refresh, then return the refresh task, blocking
|
149
|
-
// the pipeline until a token is available.
|
150
|
-
// - If we SHOULD refresh, then run refresh but don't return it
|
151
|
-
// (we can still use the cached token).
|
152
|
-
// - Return the token, since it's fine if we didn't return in
|
153
|
-
// step 1.
|
154
|
-
//
|
155
|
-
if (cycler.mustRefresh)
|
156
|
-
return refresh(tokenOptions);
|
157
|
-
if (cycler.shouldRefresh) {
|
158
|
-
refresh(tokenOptions);
|
159
|
-
}
|
160
|
-
return token;
|
161
|
-
};
|
162
|
-
}
|
163
|
-
/**
|
164
|
-
* We will retrieve the challenge only if the response status code was 401,
|
165
|
-
* and if the response contained the header "WWW-Authenticate" with a non-empty value.
|
166
|
-
*/
|
167
|
-
function getChallenge(response) {
|
168
|
-
const challenge = response.headers.get("WWW-Authenticate");
|
169
|
-
if (response.status === 401 && challenge) {
|
170
|
-
return challenge;
|
171
|
-
}
|
172
|
-
return;
|
173
|
-
}
|
174
|
-
/**
|
175
|
-
* Converts: `Bearer a="b" c="d"`.
|
176
|
-
* Into: `[ { a: 'b', c: 'd' }]`.
|
177
|
-
*
|
178
|
-
* @internal
|
179
|
-
*/
|
180
|
-
function parseChallenge(challenge) {
|
181
|
-
const bearerChallenge = challenge.slice("Bearer ".length);
|
182
|
-
const challengeParts = `${bearerChallenge.trim()} `.split(" ").filter((x) => x);
|
183
|
-
const keyValuePairs = challengeParts.map((keyValue) => (([key, value]) => ({ [key]: value }))(keyValue.trim().split("=")));
|
184
|
-
// Key-value pairs to plain object:
|
185
|
-
return keyValuePairs.reduce((a, b) => (Object.assign(Object.assign({}, a), b)), {});
|
186
|
-
}
|
187
|
-
// #endregion
|
188
|
-
/**
|
189
|
-
* Creates a new factory for a RequestPolicy that applies a bearer token to
|
190
|
-
* the requests' `Authorization` headers.
|
191
|
-
*
|
192
|
-
* @param credential - The TokenCredential implementation that can supply the bearer token.
|
193
|
-
* @param scopes - The scopes for which the bearer token applies.
|
194
|
-
*/
|
195
|
-
export function storageBearerTokenChallengeAuthenticationPolicy(credential, scopes) {
|
196
|
-
// This simple function encapsulates the entire process of reliably retrieving the token
|
197
|
-
let getToken = createTokenCycler(credential, scopes);
|
198
|
-
class StorageBearerTokenChallengeAuthenticationPolicy extends BaseRequestPolicy {
|
199
|
-
constructor(nextPolicy, options) {
|
200
|
-
super(nextPolicy, options);
|
201
|
-
}
|
202
|
-
async sendRequest(webResource) {
|
203
|
-
if (!webResource.url.toLowerCase().startsWith("https://")) {
|
204
|
-
throw new Error("Bearer token authentication is not permitted for non-TLS protected (non-https) URLs.");
|
205
|
-
}
|
206
|
-
const getTokenInternal = getToken;
|
207
|
-
const token = (await getTokenInternal({
|
208
|
-
abortSignal: webResource.abortSignal,
|
209
|
-
tracingOptions: {
|
210
|
-
tracingContext: webResource.tracingContext,
|
211
|
-
},
|
212
|
-
})).token;
|
213
|
-
webResource.headers.set(Constants.HeaderConstants.AUTHORIZATION, `Bearer ${token}`);
|
214
|
-
const response = await this._nextPolicy.sendRequest(webResource);
|
215
|
-
if ((response === null || response === void 0 ? void 0 : response.status) === 401) {
|
216
|
-
const challenge = getChallenge(response);
|
217
|
-
if (challenge) {
|
218
|
-
const challengeInfo = parseChallenge(challenge);
|
219
|
-
const challengeScopes = challengeInfo.resource_id + Constants.DefaultScope;
|
220
|
-
const parsedAuthUri = URLBuilder.parse(challengeInfo.authorization_uri);
|
221
|
-
const pathSegments = parsedAuthUri.getPath().split("/");
|
222
|
-
const tenantId = pathSegments[1];
|
223
|
-
const getTokenForChallenge = createTokenCycler(credential, challengeScopes);
|
224
|
-
const tokenForChallenge = (await getTokenForChallenge({
|
225
|
-
abortSignal: webResource.abortSignal,
|
226
|
-
tracingOptions: {
|
227
|
-
tracingContext: webResource.tracingContext,
|
228
|
-
},
|
229
|
-
tenantId: tenantId,
|
230
|
-
})).token;
|
231
|
-
getToken = getTokenForChallenge;
|
232
|
-
webResource.headers.set(Constants.HeaderConstants.AUTHORIZATION, `Bearer ${tokenForChallenge}`);
|
233
|
-
return this._nextPolicy.sendRequest(webResource);
|
234
|
-
}
|
235
|
-
}
|
236
|
-
return response;
|
237
|
-
}
|
238
|
-
}
|
239
|
-
return {
|
240
|
-
create: (nextPolicy, options) => {
|
241
|
-
return new StorageBearerTokenChallengeAuthenticationPolicy(nextPolicy, options);
|
242
|
-
},
|
243
|
-
};
|
244
|
-
}
|
245
|
-
//# sourceMappingURL=StorageBearerTokenChallengeAuthenticationPolicy.js.map
|
package/dist-esm/storage-blob/src/policies/StorageBearerTokenChallengeAuthenticationPolicy.js.map
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"StorageBearerTokenChallengeAuthenticationPolicy.js","sourceRoot":"","sources":["../../../../src/policies/StorageBearerTokenChallengeAuthenticationPolicy.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EAAiD,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC7F,OAAO,EACL,iBAAiB,GAIlB,MAAM,kBAAkB,CAAC;AAG1B,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAEzC;;GAEG;AACH,MAAM,SAAS,GAAG;IAChB,YAAY,EAAE,WAAW;IACzB;;OAEG;IACH,eAAe,EAAE;QACf;;WAEG;QACH,aAAa,EAAE,eAAe;KAC/B;CACF,CAAC;AAiCF,sDAAsD;AACtD,MAAM,sBAAsB,GAAuB;IACjD,uBAAuB,EAAE,IAAI;IAC7B,iBAAiB,EAAE,IAAI;IACvB,iBAAiB,EAAE,IAAI,GAAG,EAAE,GAAG,CAAC,EAAE,oCAAoC;CACvE,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,KAAK,UAAU,YAAY,CACzB,cAAiD,EACjD,iBAAyB,EACzB,WAAmB;IAEnB,4EAA4E;IAC5E,eAAe;IACf,KAAK,UAAU,iBAAiB;QAC9B,IAAI,IAAI,CAAC,GAAG,EAAE,GAAG,WAAW,EAAE;YAC5B,IAAI;gBACF,OAAO,MAAM,cAAc,EAAE,CAAC;aAC/B;YAAC,WAAM;gBACN,OAAO,IAAI,CAAC;aACb;SACF;aAAM;YACL,MAAM,UAAU,GAAG,MAAM,cAAc,EAAE,CAAC;YAE1C,6CAA6C;YAC7C,IAAI,UAA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,KAAK,CAAC;oBAER,QAAQ,GAAG,oBAAoB,CAAC;oBAChC,WAAW,CAAC,OAAO,CAAC,GAAG,CACrB,SAAS,CAAC,eAAe,CAAC,aAAa,EACvC,UAAU,iBAAiB,EAAE,CAC9B,CAAC;oBACF,OAAO,IAAI,CAAC,WAAW,CAAC,WAAW,CAAC,WAAW,CAAC,CAAC;iBAClD;aACF;YAED,OAAO,QAAQ,CAAC;QAClB,CAAC;KACF;IAED,OAAO;QACL,MAAM,EAAE,CAAC,UAAyB,EAAE,OAA6B,EAAE,EAAE;YACnE,OAAO,IAAI,+CAA+C,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;QAClF,CAAC;KACF,CAAC;AACJ,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nimport { AccessToken, GetTokenOptions, TokenCredential, URLBuilder } from \"@azure/core-http\";\nimport {\n BaseRequestPolicy,\n RequestPolicy,\n RequestPolicyFactory,\n RequestPolicyOptions,\n} from \"@azure/core-http\";\nimport { HttpOperationResponse } from \"@azure/core-http\";\nimport { WebResourceLike } from \"@azure/core-http\";\nimport { delay } from \"@azure/core-http\";\n\n/**\n * A set of constants used internally when processing requests.\n */\nconst Constants = {\n DefaultScope: \"/.default\",\n /**\n * Defines constants for use with HTTP headers.\n */\n HeaderConstants: {\n /**\n * The Authorization header.\n */\n AUTHORIZATION: \"authorization\",\n },\n};\n\n// #region Access Token Cycler\n\n/**\n * A function that gets a promise of an access token and allows providing\n * options.\n *\n * @param options - the options to pass to the underlying token provider\n */\ntype AccessTokenGetter = (options: GetTokenOptions) => Promise<AccessToken>;\n\ninterface TokenCyclerOptions {\n /**\n * The window of time before token expiration during which the token will be\n * considered unusable due to risk of the token expiring before sending the\n * request.\n *\n * This will only become meaningful if the refresh fails for over\n * (refreshWindow - forcedRefreshWindow) milliseconds.\n */\n forcedRefreshWindowInMs: number;\n /**\n * Interval in milliseconds to retry failed token refreshes.\n */\n retryIntervalInMs: number;\n /**\n * The window of time before token expiration during which\n * we will attempt to refresh the token.\n */\n refreshWindowInMs: number;\n}\n\n// Default options for the cycler if none are provided\nconst DEFAULT_CYCLER_OPTIONS: TokenCyclerOptions = {\n forcedRefreshWindowInMs: 1000, // Force waiting for a refresh 1s before the token expires\n retryIntervalInMs: 3000, // Allow refresh attempts every 3s\n refreshWindowInMs: 1000 * 60 * 2, // Start refreshing 2m before expiry\n};\n\n/**\n * Converts an an unreliable access token getter (which may resolve with null)\n * into an AccessTokenGetter by retrying the unreliable getter in a regular\n * interval.\n *\n * @param getAccessToken - a function that produces a promise of an access\n * token that may fail by returning null\n * @param retryIntervalInMs - the time (in milliseconds) to wait between retry\n * attempts\n * @param timeoutInMs - the timestamp after which the refresh attempt will fail,\n * throwing an exception\n * @returns - a promise that, if it resolves, will resolve with an access token\n */\nasync function beginRefresh(\n getAccessToken: () => Promise<AccessToken | null>,\n retryIntervalInMs: number,\n timeoutInMs: number\n): Promise<AccessToken> {\n // This wrapper handles exceptions gracefully as long as we haven't exceeded\n // the timeout.\n async function tryGetAccessToken(): Promise<AccessToken | null> {\n if (Date.now() < timeoutInMs) {\n try {\n return await getAccessToken();\n } catch {\n return null;\n }\n } else {\n const finalToken = await getAccessToken();\n\n // Timeout is up, so throw if it's still null\n if (finalToken === null) {\n throw new Error(\"Failed to refresh access token.\");\n }\n\n return finalToken;\n }\n }\n\n let token: AccessToken | null = await tryGetAccessToken();\n\n while (token === null) {\n await delay(retryIntervalInMs);\n\n token = await tryGetAccessToken();\n }\n\n return token;\n}\n\n/**\n * Creates a token cycler from a credential, scopes, and optional settings.\n *\n * A token cycler represents a way to reliably retrieve a valid access token\n * from a TokenCredential. It will handle initializing the token, refreshing it\n * when it nears expiration, and synchronizes refresh attempts to avoid\n * concurrency hazards.\n *\n * @param credential - the underlying TokenCredential that provides the access\n * token\n * @param scopes - the scopes to request authorization for\n * @param tokenCyclerOptions - optionally override default settings for the cycler\n *\n * @returns - a function that reliably produces a valid access token\n */\nfunction createTokenCycler(\n credential: TokenCredential,\n scopes: string | string[],\n tokenCyclerOptions?: Partial<TokenCyclerOptions>\n): AccessTokenGetter {\n let refreshWorker: Promise<AccessToken> | null = null;\n let token: AccessToken | null = null;\n\n const options = {\n ...DEFAULT_CYCLER_OPTIONS,\n ...tokenCyclerOptions,\n };\n\n /**\n * This little holder defines several predicates that we use to construct\n * the rules of refreshing the token.\n */\n const cycler = {\n /**\n * Produces true if a refresh job is currently in progress.\n */\n get isRefreshing(): boolean {\n return refreshWorker !== null;\n },\n /**\n * Produces true if the cycler SHOULD refresh (we are within the refresh\n * window and not already refreshing)\n */\n get shouldRefresh(): boolean {\n return (\n !cycler.isRefreshing &&\n (token?.expiresOnTimestamp ?? 0) - options.refreshWindowInMs < Date.now()\n );\n },\n /**\n * Produces true if the cycler MUST refresh (null or nearly-expired\n * token).\n */\n get mustRefresh(): boolean {\n return (\n token === null || token.expiresOnTimestamp - options.forcedRefreshWindowInMs < Date.now()\n );\n },\n };\n\n /**\n * Starts a refresh job or returns the existing job if one is already\n * running.\n */\n function refresh(getTokenOptions: GetTokenOptions): Promise<AccessToken> {\n if (!cycler.isRefreshing) {\n // We bind `scopes` here to avoid passing it around a lot\n const tryGetAccessToken = (): Promise<AccessToken | null> =>\n credential.getToken(scopes, getTokenOptions);\n\n // Take advantage of promise chaining to insert an assignment to `token`\n // before the refresh can be considered done.\n refreshWorker = beginRefresh(\n tryGetAccessToken,\n options.retryIntervalInMs,\n // If we don't have a token, then we should timeout immediately\n token?.expiresOnTimestamp ?? Date.now()\n )\n .then((_token) => {\n refreshWorker = null;\n token = _token;\n return token;\n })\n .catch((reason) => {\n // We also should reset the refresher if we enter a failed state. All\n // existing awaiters will throw, but subsequent requests will start a\n // new retry chain.\n refreshWorker = null;\n token = null;\n throw reason;\n });\n }\n\n return refreshWorker as Promise<AccessToken>;\n }\n\n return async (tokenOptions: GetTokenOptions): Promise<AccessToken> => {\n //\n // Simple rules:\n // - If we MUST refresh, then return the refresh task, blocking\n // the pipeline until a token is available.\n // - If we SHOULD refresh, then run refresh but don't return it\n // (we can still use the cached token).\n // - Return the token, since it's fine if we didn't return in\n // step 1.\n //\n\n if (cycler.mustRefresh) return refresh(tokenOptions);\n\n if (cycler.shouldRefresh) {\n refresh(tokenOptions);\n }\n\n return token as AccessToken;\n };\n}\n/**\n * We will retrieve the challenge only if the response status code was 401,\n * and if the response contained the header \"WWW-Authenticate\" with a non-empty value.\n */\nfunction getChallenge(response: HttpOperationResponse): string | undefined {\n const challenge = response.headers.get(\"WWW-Authenticate\");\n if (response.status === 401 && challenge) {\n return challenge;\n }\n return;\n}\n\n/**\n * Challenge structure\n */\ninterface Challenge {\n authorization_uri: string;\n resource_id: string;\n}\n\n/**\n * Converts: `Bearer a=\"b\" c=\"d\"`.\n * Into: `[ { a: 'b', c: 'd' }]`.\n *\n * @internal\n */\nfunction parseChallenge(challenge: string): any {\n const bearerChallenge = challenge.slice(\"Bearer \".length);\n const challengeParts = `${bearerChallenge.trim()} `.split(\" \").filter((x) => x);\n const keyValuePairs = challengeParts.map((keyValue) =>\n (([key, value]) => ({ [key]: value }))(keyValue.trim().split(\"=\"))\n );\n // Key-value pairs to plain object:\n return keyValuePairs.reduce((a, b) => ({ ...a, ...b }), {});\n}\n\n// #endregion\n\n/**\n * Creates a new factory for a RequestPolicy that applies a bearer token to\n * the requests' `Authorization` headers.\n *\n * @param credential - The TokenCredential implementation that can supply the bearer token.\n * @param scopes - The scopes for which the bearer token applies.\n */\n\nexport function storageBearerTokenChallengeAuthenticationPolicy(\n credential: TokenCredential,\n scopes: string | string[]\n): RequestPolicyFactory {\n // This simple function encapsulates the entire process of reliably retrieving the token\n let getToken = createTokenCycler(credential, scopes);\n\n class StorageBearerTokenChallengeAuthenticationPolicy extends BaseRequestPolicy {\n public constructor(nextPolicy: RequestPolicy, options: RequestPolicyOptions) {\n super(nextPolicy, options);\n }\n\n public async sendRequest(webResource: WebResourceLike): Promise<HttpOperationResponse> {\n if (!webResource.url.toLowerCase().startsWith(\"https://\")) {\n throw new Error(\n \"Bearer token authentication is not permitted for non-TLS protected (non-https) URLs.\"\n );\n }\n\n const getTokenInternal = getToken;\n const token = (\n await getTokenInternal({\n abortSignal: webResource.abortSignal,\n tracingOptions: {\n tracingContext: webResource.tracingContext,\n },\n })\n ).token;\n webResource.headers.set(Constants.HeaderConstants.AUTHORIZATION, `Bearer ${token}`);\n\n const response = await this._nextPolicy.sendRequest(webResource);\n\n if (response?.status === 401) {\n const challenge = getChallenge(response);\n if (challenge) {\n const challengeInfo: Challenge = parseChallenge(challenge);\n const challengeScopes = challengeInfo.resource_id + Constants.DefaultScope;\n const parsedAuthUri = URLBuilder.parse(challengeInfo.authorization_uri);\n const pathSegments = parsedAuthUri.getPath()!.split(\"/\");\n const tenantId = pathSegments[1];\n const getTokenForChallenge = createTokenCycler(credential, challengeScopes);\n\n const tokenForChallenge = (\n await getTokenForChallenge({\n abortSignal: webResource.abortSignal,\n tracingOptions: {\n tracingContext: webResource.tracingContext,\n },\n tenantId: tenantId,\n })\n ).token;\n\n getToken = getTokenForChallenge;\n webResource.headers.set(\n Constants.HeaderConstants.AUTHORIZATION,\n `Bearer ${tokenForChallenge}`\n );\n return this._nextPolicy.sendRequest(webResource);\n }\n }\n\n return response;\n }\n }\n\n return {\n create: (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => {\n return new StorageBearerTokenChallengeAuthenticationPolicy(nextPolicy, options);\n },\n };\n}\n"]}
|
@@ -1,36 +0,0 @@
|
|
1
|
-
// Copyright (c) Microsoft Corporation.
|
2
|
-
// Licensed under the MIT license.
|
3
|
-
import { BaseRequestPolicy, HttpHeaders, isNode, } from "@azure/core-http";
|
4
|
-
import { HeaderConstants } from "../utils/constants";
|
5
|
-
/**
|
6
|
-
* TelemetryPolicy is a policy used to tag user-agent header for every requests.
|
7
|
-
*/
|
8
|
-
export class TelemetryPolicy extends BaseRequestPolicy {
|
9
|
-
/**
|
10
|
-
* Creates an instance of TelemetryPolicy.
|
11
|
-
* @param nextPolicy -
|
12
|
-
* @param options -
|
13
|
-
* @param telemetry -
|
14
|
-
*/
|
15
|
-
constructor(nextPolicy, options, telemetry) {
|
16
|
-
super(nextPolicy, options);
|
17
|
-
this.telemetry = telemetry;
|
18
|
-
}
|
19
|
-
/**
|
20
|
-
* Sends out request.
|
21
|
-
*
|
22
|
-
* @param request -
|
23
|
-
*/
|
24
|
-
async sendRequest(request) {
|
25
|
-
if (isNode) {
|
26
|
-
if (!request.headers) {
|
27
|
-
request.headers = new HttpHeaders();
|
28
|
-
}
|
29
|
-
if (!request.headers.get(HeaderConstants.USER_AGENT)) {
|
30
|
-
request.headers.set(HeaderConstants.USER_AGENT, this.telemetry);
|
31
|
-
}
|
32
|
-
}
|
33
|
-
return this._nextPolicy.sendRequest(request);
|
34
|
-
}
|
35
|
-
}
|
36
|
-
//# sourceMappingURL=TelemetryPolicy.js.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"TelemetryPolicy.js","sourceRoot":"","sources":["../../../../src/policies/TelemetryPolicy.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EACL,iBAAiB,EACjB,WAAW,EAEX,MAAM,GAIP,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAErD;;GAEG;AACH,MAAM,OAAO,eAAgB,SAAQ,iBAAiB;IAMpD;;;;;OAKG;IACH,YAAY,UAAyB,EAAE,OAA6B,EAAE,SAAiB;QACrF,KAAK,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;QAC3B,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC7B,CAAC;IAED;;;;OAIG;IACI,KAAK,CAAC,WAAW,CAAC,OAAoB;QAC3C,IAAI,MAAM,EAAE;YACV,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE;gBACpB,OAAO,CAAC,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;aACrC;YACD,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,UAAU,CAAC,EAAE;gBACpD,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,UAAU,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC;aACjE;SACF;QAED,OAAO,IAAI,CAAC,WAAW,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;IAC/C,CAAC;CACF","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nimport {\n BaseRequestPolicy,\n HttpHeaders,\n HttpOperationResponse,\n isNode,\n RequestPolicy,\n RequestPolicyOptions,\n WebResource,\n} from \"@azure/core-http\";\n\nimport { HeaderConstants } from \"../utils/constants\";\n\n/**\n * TelemetryPolicy is a policy used to tag user-agent header for every requests.\n */\nexport class TelemetryPolicy extends BaseRequestPolicy {\n /**\n * Telemetry string.\n */\n public readonly telemetry: string;\n\n /**\n * Creates an instance of TelemetryPolicy.\n * @param nextPolicy -\n * @param options -\n * @param telemetry -\n */\n constructor(nextPolicy: RequestPolicy, options: RequestPolicyOptions, telemetry: string) {\n super(nextPolicy, options);\n this.telemetry = telemetry;\n }\n\n /**\n * Sends out request.\n *\n * @param request -\n */\n public async sendRequest(request: WebResource): Promise<HttpOperationResponse> {\n if (isNode) {\n if (!request.headers) {\n request.headers = new HttpHeaders();\n }\n if (!request.headers.get(HeaderConstants.USER_AGENT)) {\n request.headers.set(HeaderConstants.USER_AGENT, this.telemetry);\n }\n }\n\n return this._nextPolicy.sendRequest(request);\n }\n}\n"]}
|