import { computeHMACSHA256 , truncatedISO8061Date } from "../../common/utils/utils" ; import { BlobSASResourceType } from "./BlobSASResourceType" ; import { SASProtocol } from "./IAccountSASSignatureValues" ; import { IIPRange , ipRangeToString } from "./IIPRange" ; export class export interface IBlobSASSignatureValues { type { string } memberof version : string ; type { SASProtocol | string } memberof protocol ? : SASProtocol | string ; type { Date | string } memberof startTime ? : Date | string ; type { Date | string } memberof expiryTime ? : Date | string ; /**
     * Optional only when identifier is provided.
     * Please refer to either  ContainerSASPermissions or  BlobSASPermissions depending on the resource
     * being accessed for help constructing the permissions string.
     *
     *  type { string } memberof permissions ? : string ; type { IIPRange | string } memberof ipRange ? : IIPRange | string ; type { string } memberof containerName : string ; type { string } memberof blobName ? : string ; see https type { string } memberof identifier ? : string ; type { string } memberof cacheControl ? : string ; type { string } memberof contentDisposition ? : string ; type { string } memberof contentEncoding ? : string ; type { string } memberof contentLanguage ? : string ; type { string } memberof contentType ? : string ; type { string } memberof signedResource ? : string ; type { string } memberof snapshot ? : string ; } export param { IBlobSASSignatureValues } blobSASSignatureValues param { BlobSASResourceType } resource param { string } accountName param { Buffer } sharedKey returns { [ string , string ] } export function generateBlobSASSignature ( blobSASSignatureValues , resource , accountName , sharedKey ) { if ( blobSASSignatureValues . version >= "2018-11-09" ) { return generateBlobSASSignature20181109 ( blobSASSignatureValues , resource , accountName , sharedKey ) ; } else { return generateBlobSASSignature20150405 ( blobSASSignatureValues , resource , accountName , sharedKey ) ; } } function generateBlobSASSignature20181109 ( blobSASSignatureValues , resource , accountName , sharedKey ) { if ( ! blobSASSignatureValues . identifier && ( ! blobSASSignatureValues . permissions && ! blobSASSignatureValues . expiryTime ) ) { throw new RangeError ( "generateBlobSASSignature(): Must provide 'permissions' and 'expiryTime' for Blob SAS generation when 'identifier' is not provided." ) ; } const version = blobSASSignatureValues . version ; const verifiedPermissions = blobSASSignatureValues . permissions ; const stringToSign = [ verifiedPermissions ? verifiedPermissions : "" , blobSASSignatureValues . startTime === undefined ? "" : typeof blobSASSignatureValues . startTime === "string" ? blobSASSignatureValues . startTime : truncatedISO8061Date ( blobSASSignatureValues . startTime , false ) , blobSASSignatureValues . expiryTime === undefined ? "" : typeof blobSASSignatureValues . expiryTime === "string" ? blobSASSignatureValues . expiryTime : truncatedISO8061Date ( blobSASSignatureValues . expiryTime , false ) , getCanonicalName ( accountName , blobSASSignatureValues . containerName , resource === BlobSASResourceType . Blob || resource === BlobSASResourceType . BlobSnapshot ? blobSASSignatureValues . blobName : "" ) , blobSASSignatureValues . identifier , blobSASSignatureValues . ipRange ? typeof blobSASSignatureValues . ipRange === "string" ? blobSASSignatureValues . ipRange : ipRangeToString ( blobSASSignatureValues . ipRange ) : "" , blobSASSignatureValues . protocol ? blobSASSignatureValues . protocol : "" , version , blobSASSignatureValues . signedResource , blobSASSignatureValues . snapshot , blobSASSignatureValues . cacheControl ? blobSASSignatureValues . cacheControl : "" , blobSASSignatureValues . contentDisposition ? blobSASSignatureValues . contentDisposition : "" , blobSASSignatureValues . contentEncoding ? blobSASSignatureValues . contentEncoding : "" , blobSASSignatureValues . contentLanguage ? blobSASSignatureValues . contentLanguage : "" , blobSASSignatureValues . contentType ? blobSASSignatureValues . contentType : "" ] . join ( "\n" ) ; const signature = computeHMACSHA256 ( stringToSign , sharedKey ) ; return [ signature , stringToSign ] ; } function generateBlobSASSignature20150405 ( blobSASSignatureValues , resource , accountName , sharedKey ) { if ( ! blobSASSignatureValues . identifier && ( ! blobSASSignatureValues . permissions && ! blobSASSignatureValues . expiryTime ) ) { throw new RangeError ( "generateBlobSASSignature(): Must provide 'permissions' and 'expiryTime' for Blob SAS generation when 'identifier' is not provided." ) ; } const version = blobSASSignatureValues . version ; const verifiedPermissions = blobSASSignatureValues . permissions ; const stringToSign = [ verifiedPermissions ? verifiedPermissions : "" , blobSASSignatureValues . startTime === undefined ? "" : typeof blobSASSignatureValues . startTime === "string" ? blobSASSignatureValues . startTime : truncatedISO8061Date ( blobSASSignatureValues . startTime , false ) , blobSASSignatureValues . expiryTime === undefined ? "" : typeof blobSASSignatureValues . expiryTime === "string" ? blobSASSignatureValues . expiryTime : truncatedISO8061Date ( blobSASSignatureValues . expiryTime , false ) , getCanonicalName ( accountName , blobSASSignatureValues . containerName , resource === BlobSASResourceType . Blob ? blobSASSignatureValues . blobName : "" ) , blobSASSignatureValues . identifier , blobSASSignatureValues . ipRange ? typeof blobSASSignatureValues . ipRange === "string" ? blobSASSignatureValues . ipRange : ipRangeToString ( blobSASSignatureValues . ipRange ) : "" , blobSASSignatureValues . protocol ? blobSASSignatureValues . protocol : "" , version , blobSASSignatureValues . cacheControl ? blobSASSignatureValues . cacheControl : "" , blobSASSignatureValues . contentDisposition ? blobSASSignatureValues . contentDisposition : "" , blobSASSignatureValues . contentEncoding ? blobSASSignatureValues . contentEncoding : "" , blobSASSignatureValues . contentLanguage ? blobSASSignatureValues . contentLanguage : "" , blobSASSignatureValues . contentType ? blobSASSignatureValues . contentType : "" ] . join ( "\n" ) ; const signature = computeHMACSHA256 ( stringToSign , sharedKey ) ; return [ signature , stringToSign ] ; } function getCanonicalName ( accountName , containerName , blobName ? ) { const elements = [ `/blob/${ accountName }/${ containerName }` ] ; if ( blobName ) { elements . push ( `/${ blobName }` ) ; } return elements . join ( "" ) ; }