import { computeHMACSHA256 , truncatedISO8061Date } from "../../common/utils/utils" ; import { SASProtocol } from "./IAccountSASSignatureValues" ; import { IIPRange , ipRangeToString } from "./IIPRange" ; export class export interface ITableSASSignatureValues { 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  TableSASPermissions 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 tableName : string ; see https type { string } memberof identifier ? : string ; startingPartitionKey ? : string ; startingRowKey ? : string ; endingPartitionKey ? : string ; endingRowKey ? : string ; } export param { ITableSASSignatureValues } tableSASSignatureValues param { TableSASResourceType } resource param { string } accountName param { Buffer } sharedKey returns { [ string , string ] } export function generateTableSASSignature ( tableSASSignatureValues , accountName , sharedKey ) { if ( tableSASSignatureValues . version >= "2018-11-09" ) { return generateTableSASSignature20181109 ( tableSASSignatureValues , accountName , sharedKey ) ; } else { return generateTableSASSignature20150405 ( tableSASSignatureValues , accountName , sharedKey ) ; } } function generateTableSASSignature20181109 ( tableSASSignatureValues , accountName , sharedKey ) { if ( ! tableSASSignatureValues . identifier && ( ! tableSASSignatureValues . permissions && ! tableSASSignatureValues . expiryTime ) ) { throw new RangeError ( "generateTableSASSignature(): Must provide 'permissions' and 'expiryTime' for Table SAS generation when 'identifier' is not provided." ) ; } const version = tableSASSignatureValues . version ; const verifiedPermissions = tableSASSignatureValues . permissions ; const stringToSign = [ verifiedPermissions ? verifiedPermissions : "" , tableSASSignatureValues . startTime === undefined ? "" : typeof tableSASSignatureValues . startTime === "string" ? tableSASSignatureValues . startTime : truncatedISO8061Date ( tableSASSignatureValues . startTime , false ) , tableSASSignatureValues . expiryTime === undefined ? "" : typeof tableSASSignatureValues . expiryTime === "string" ? tableSASSignatureValues . expiryTime : truncatedISO8061Date ( tableSASSignatureValues . expiryTime , false ) , getCanonicalName ( accountName , tableSASSignatureValues . tableName ) , tableSASSignatureValues . identifier , tableSASSignatureValues . ipRange ? typeof tableSASSignatureValues . ipRange === "string" ? tableSASSignatureValues . ipRange : ipRangeToString ( tableSASSignatureValues . ipRange ) : "" , tableSASSignatureValues . protocol ? tableSASSignatureValues . protocol : "" , version , tableSASSignatureValues . startingPartitionKey ? tableSASSignatureValues . startingPartitionKey : "" , tableSASSignatureValues . startingRowKey ? tableSASSignatureValues . startingRowKey : "" , tableSASSignatureValues . endingPartitionKey ? tableSASSignatureValues . endingPartitionKey : "" , tableSASSignatureValues . endingRowKey ? tableSASSignatureValues . endingRowKey : "" ] . join ( "\n" ) ; const signature = computeHMACSHA256 ( stringToSign , sharedKey ) ; return [ signature , stringToSign ] ; } function generateTableSASSignature20150405 ( tableSASSignatureValues , accountName , sharedKey ) { if ( ! tableSASSignatureValues . identifier && ( ! tableSASSignatureValues . permissions && ! tableSASSignatureValues . expiryTime ) ) { throw new RangeError ( "generateTableSASSignature(): Must provide 'permissions' and 'expiryTime' for Table SAS generation when 'identifier' is not provided." ) ; } const version = tableSASSignatureValues . version ; const verifiedPermissions = tableSASSignatureValues . permissions ; const stringToSign = [ verifiedPermissions ? verifiedPermissions : "" , tableSASSignatureValues . startTime === undefined ? "" : typeof tableSASSignatureValues . startTime === "string" ? tableSASSignatureValues . startTime : truncatedISO8061Date ( tableSASSignatureValues . startTime , false ) , tableSASSignatureValues . expiryTime === undefined ? "" : typeof tableSASSignatureValues . expiryTime === "string" ? tableSASSignatureValues . expiryTime : truncatedISO8061Date ( tableSASSignatureValues . expiryTime , false ) , getCanonicalName ( accountName , tableSASSignatureValues . tableName ) , tableSASSignatureValues . identifier , tableSASSignatureValues . ipRange ? typeof tableSASSignatureValues . ipRange === "string" ? tableSASSignatureValues . ipRange : ipRangeToString ( tableSASSignatureValues . ipRange ) : "" , tableSASSignatureValues . protocol ? tableSASSignatureValues . protocol : "" , version , tableSASSignatureValues . startingPartitionKey ? tableSASSignatureValues . startingPartitionKey : "" , tableSASSignatureValues . startingRowKey ? tableSASSignatureValues . startingRowKey : "" , tableSASSignatureValues . endingPartitionKey ? tableSASSignatureValues . endingPartitionKey : "" , tableSASSignatureValues . endingRowKey ? tableSASSignatureValues . endingRowKey : "" ] . join ( "\n" ) ; const signature = computeHMACSHA256 ( stringToSign , sharedKey ) ; return [ signature , stringToSign ] ; } function getCanonicalName ( accountName , tableName ) { return `/table/${ accountName }/${ tableName }` ; }