@azure/data-tables
Version:
An isomorphic client library for the Azure Tables service.
76 lines • 3.81 kB
JavaScript
"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateTableSasQueryParameters = generateTableSasQueryParameters;
const sasIPRange_js_1 = require("./sasIPRange.js");
const sasQueryParameters_js_1 = require("./sasQueryParameters.js");
const tableSasPermisions_js_1 = require("./tableSasPermisions.js");
const constants_js_1 = require("../utils/constants.js");
const computeHMACSHA256_js_1 = require("../utils/computeHMACSHA256.js");
const truncateISO8061Date_js_1 = require("../utils/truncateISO8061Date.js");
/**
* ONLY AVAILABLE IN NODE.JS RUNTIME.
*
* Creates an instance of SASQueryParameters.
*
* **Note**: When identifier is not provided, permissions has a default value of "read" and expiresOn of one hour from the time the token is generated.
*/
function generateTableSasQueryParameters(tableName, credential, tableSasSignatureValues) {
const version = tableSasSignatureValues.version ?? constants_js_1.SERVICE_VERSION;
if (credential === undefined) {
throw TypeError("Invalid NamedKeyCredential");
}
if (!tableName) {
throw new Error("Must provide a 'tableName'");
}
const signedPermissions = (0, tableSasPermisions_js_1.tableSasPermissionsToString)(tableSasSignatureValues.permissions);
const signedStart = tableSasSignatureValues.startsOn
? (0, truncateISO8061Date_js_1.truncatedISO8061Date)(tableSasSignatureValues.startsOn, false /** withMilliseconds */)
: "";
const signedExpiry = tableSasSignatureValues.expiresOn
? (0, truncateISO8061Date_js_1.truncatedISO8061Date)(tableSasSignatureValues.expiresOn, false /** withMilliseconds */)
: "";
const canonicalizedResource = getCanonicalName(credential.name, tableName);
const signedIdentifier = tableSasSignatureValues.identifier ?? "";
const signedIP = (0, sasIPRange_js_1.ipRangeToString)(tableSasSignatureValues.ipRange);
const signedProtocol = tableSasSignatureValues.protocol || "";
const startingPartitionKey = tableSasSignatureValues.startPartitionKey ?? "";
const startingRowKey = tableSasSignatureValues.startRowKey ?? "";
const endingPartitionKey = tableSasSignatureValues.endPartitionKey ?? "";
const endingRowKey = tableSasSignatureValues.endRowKey ?? "";
const stringToSign = [
signedPermissions,
signedStart,
signedExpiry,
canonicalizedResource,
signedIdentifier,
signedIP,
signedProtocol,
version,
startingPartitionKey,
startingRowKey,
endingPartitionKey,
endingRowKey,
].join("\n");
const signature = (0, computeHMACSHA256_js_1.computeHMACSHA256)(stringToSign, credential.key);
return new sasQueryParameters_js_1.SasQueryParameters(version, signature, {
permissions: signedPermissions,
protocol: tableSasSignatureValues.protocol,
startsOn: tableSasSignatureValues.startsOn,
expiresOn: tableSasSignatureValues.expiresOn,
ipRange: tableSasSignatureValues.ipRange,
identifier: tableSasSignatureValues.identifier,
tableName,
startPartitionKey: tableSasSignatureValues.startPartitionKey,
startRowKey: tableSasSignatureValues.startRowKey,
endPartitionKey: tableSasSignatureValues.endPartitionKey,
endRowKey: tableSasSignatureValues.endRowKey,
});
}
function getCanonicalName(accountName, tableName) {
// Sample CanonicalName for URL = https://myaccount.table.core.windows.net/Employees(PartitionKey='Jeff',RowKey='Price'):
// canonicalizedResource = "/table/myaccount/employees"
return `/table/${accountName}/${tableName.toLowerCase()}`;
}
//# sourceMappingURL=tableSasSignatureValues.js.map