UNPKG

@azure/storage-file-share

Version:
247 lines 9.19 kB
// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. import { ipRangeToString } from "./SasIPRange.js"; import { truncatedISO8061Date } from "./utils/utils.common.js"; /** * Protocols for generated SAS. */ export var SASProtocol; (function (SASProtocol) { /** * Protocol that allows HTTPS only */ SASProtocol["Https"] = "https"; /** * Protocol that allows both HTTPS and HTTP */ SASProtocol["HttpsAndHttp"] = "https,http"; })(SASProtocol || (SASProtocol = {})); /** * Represents the components that make up an Azure Storage SAS' query parameters. This type is not constructed directly * by the user; it is only generated by the {@link AccountSASSignatureValues} and {@link FileSASSignatureValues} * types. Once generated, it can be encoded into a {@link String} and appended to a URL directly (though caution should * be taken here in case there are existing query parameters, which might affect the appropriate means of appending * these query parameters). * * NOTE: Instances of this class are immutable. */ export class SASQueryParameters { /** * The storage API version. */ version; /** * Optional. The allowed HTTP protocol(s). */ protocol; /** * Optional. The start time for this SAS token. */ startsOn; /** * Optional only when identifier is provided. The expiry time for this SAS token. */ expiresOn; /** * Optional only when identifier is provided. * Please refer to {@link AccountSASPermissions}, {@link FileSASPermissions}, or {@link ShareSASPermissions} for * more details. */ permissions; /** * Optional. The storage services being accessed (only for Account SAS). Please refer to {@link AccountSASServices} * for more details. */ services; /** * Optional. The storage resource types being accessed (only for Account SAS). Please refer to * {@link AccountSASResourceTypes} for more details. */ resourceTypes; /** * Optional. The signed identifier (only for {@link FileSASSignatureValues}). * * @see https://learn.microsoft.com/rest/api/storageservices/establishing-a-stored-access-policy */ identifier; /** * Optional. The storage share or file path (only for {@link FileSASSignatureValues}). */ resource; /** * The signature for the SAS token. */ signature; /** * Value for cache-control header in Blob/File Service SAS. */ cacheControl; /** * Value for content-disposition header in Blob/File Service SAS. */ contentDisposition; /** * Value for content-encoding header in Blob/File Service SAS. */ contentEncoding; /** * Value for content-length header in Blob/File Service SAS. */ contentLanguage; /** * Value for content-type header in Blob/File Service SAS. */ contentType; /** * Inner value of getter ipRange. */ ipRangeInner; /** * Optional. IP range allowed for this SAS. * * @readonly */ get ipRange() { if (this.ipRangeInner) { return { end: this.ipRangeInner.end, start: this.ipRangeInner.start, }; } return undefined; } /** * Creates an instance of SASQueryParameters. * * @param version - Representing the storage version * @param signature - Representing the signature for the SAS token * @param permissions - Representing the storage permissions * @param services - Representing the storage services being accessed (only for Account SAS) * @param resourceTypes - Representing the storage resource types being accessed (only for Account SAS) * @param protocol - Representing the allowed HTTP protocol(s) * @param startsOn - Representing the start time for this SAS token * @param expiresOn - Representing the expiry time for this SAS token * @param ipRange - Representing the range of valid IP addresses for this SAS token * @param identifier - Representing the signed identifier (only for Service SAS) * @param resource - Representing the storage container or blob (only for Service SAS) * @param cacheControl - Representing the cache-control header (only for Blob/File Service SAS) * @param contentDisposition - Representing the content-disposition header (only for Blob/File Service SAS) * @param contentEncoding - Representing the content-encoding header (only for Blob/File Service SAS) * @param contentLanguage - Representing the content-language header (only for Blob/File Service SAS) * @param contentType - Representing the content-type header (only for Blob/File Service SAS) */ constructor(version, signature, permissions, services, resourceTypes, protocol, startsOn, expiresOn, ipRange, identifier, resource, cacheControl, contentDisposition, contentEncoding, contentLanguage, contentType) { this.version = version; this.services = services; this.resourceTypes = resourceTypes; this.expiresOn = expiresOn; this.permissions = permissions; this.protocol = protocol; this.startsOn = startsOn; this.ipRangeInner = ipRange; this.identifier = identifier; this.resource = resource; this.signature = signature; this.cacheControl = cacheControl; this.contentDisposition = contentDisposition; this.contentEncoding = contentEncoding; this.contentLanguage = contentLanguage; this.contentType = contentType; } /** * Encodes all SAS query parameters into a string that can be appended to a URL. * */ toString() { const params = [ "sv", "ss", "srt", "spr", "st", "se", "sip", "si", "sr", "sp", "sig", "rscc", "rscd", "rsce", "rscl", "rsct", ]; const queries = []; for (const param of params) { switch (param) { case "sv": this.tryAppendQueryParameter(queries, param, this.version); break; case "ss": this.tryAppendQueryParameter(queries, param, this.services); break; case "srt": this.tryAppendQueryParameter(queries, param, this.resourceTypes); break; case "spr": this.tryAppendQueryParameter(queries, param, this.protocol); break; case "st": this.tryAppendQueryParameter(queries, param, this.startsOn ? truncatedISO8061Date(this.startsOn, false) : undefined); break; case "se": this.tryAppendQueryParameter(queries, param, this.expiresOn ? truncatedISO8061Date(this.expiresOn, false) : undefined); break; case "sip": this.tryAppendQueryParameter(queries, param, this.ipRange ? ipRangeToString(this.ipRange) : undefined); break; case "si": this.tryAppendQueryParameter(queries, param, this.identifier); break; case "sr": this.tryAppendQueryParameter(queries, param, this.resource); break; case "sp": this.tryAppendQueryParameter(queries, param, this.permissions); break; case "sig": this.tryAppendQueryParameter(queries, param, this.signature); break; case "rscc": this.tryAppendQueryParameter(queries, param, this.cacheControl); break; case "rscd": this.tryAppendQueryParameter(queries, param, this.contentDisposition); break; case "rsce": this.tryAppendQueryParameter(queries, param, this.contentEncoding); break; case "rscl": this.tryAppendQueryParameter(queries, param, this.contentLanguage); break; case "rsct": this.tryAppendQueryParameter(queries, param, this.contentType); break; } } return queries.join("&"); } /** * A private helper method used to filter and append query key/value pairs into an array. * * @param queries - * @param key - * @param value - */ tryAppendQueryParameter(queries, key, value) { if (!value) { return; } key = encodeURIComponent(key); value = encodeURIComponent(value); if (key.length > 0 && value.length > 0) { queries.push(`${key}=${value}`); } } } //# sourceMappingURL=SASQueryParameters.js.map