@@ -69,15 +69,11 @@ export function calculateHMACSubject<T extends string | Buffer = string>(
6969/**
7070 * Calculate the HMAC for an HTTP request
7171 */
72- export function calculateRequestHMAC < T extends string | Buffer = string > ( {
73- url : urlPath ,
74- text,
75- timestamp,
76- token,
77- method,
78- authVersion,
79- } : CalculateRequestHmacOptions < T > ) : string {
80- const signatureSubject = calculateHMACSubject ( { urlPath, text, timestamp, method, authVersion } ) ;
72+ export function calculateRequestHMAC < T extends string | Buffer = string > (
73+ { url : urlPath , text, timestamp, token, method, authVersion } : CalculateRequestHmacOptions < T > ,
74+ useOriginalPath = false
75+ ) : string {
76+ const signatureSubject = calculateHMACSubject ( { urlPath, text, timestamp, method, authVersion } , useOriginalPath ) ;
8177
8278 // calculate the HMAC
8379 return calculateHMAC ( token , signatureSubject ) ;
@@ -86,15 +82,12 @@ export function calculateRequestHMAC<T extends string | Buffer = string>({
8682/**
8783 * Calculate request headers with HMAC
8884 */
89- export function calculateRequestHeaders < T extends string | Buffer = string > ( {
90- url,
91- text,
92- token,
93- method,
94- authVersion,
95- } : CalculateRequestHeadersOptions < T > ) : RequestHeaders {
85+ export function calculateRequestHeaders < T extends string | Buffer = string > (
86+ { url, text, token, method, authVersion } : CalculateRequestHeadersOptions < T > ,
87+ useOriginalPath = false
88+ ) : RequestHeaders {
9689 const timestamp = Date . now ( ) ;
97- const hmac = calculateRequestHMAC ( { url, text, timestamp, token, method, authVersion } ) ;
90+ const hmac = calculateRequestHMAC ( { url, text, timestamp, token, method, authVersion } , useOriginalPath ) ;
9891
9992 // calculate the SHA256 hash of the token
10093 const hashDigest = sjcl . hash . sha256 . hash ( token ) ;
@@ -109,31 +102,31 @@ export function calculateRequestHeaders<T extends string | Buffer = string>({
109102/**
110103 * Verify the HMAC for an HTTP response
111104 */
112- export function verifyResponse < T extends string | Buffer = string > ( {
113- url : urlPath ,
114- statusCode,
115- text,
116- timestamp,
117- token,
118- hmac,
119- method,
120- authVersion,
121- } : VerifyResponseOptions < T > ) : VerifyResponseInfo < T > {
122- const signatureSubject = calculateHMACSubject ( {
123- urlPath,
124- text,
125- timestamp,
126- statusCode,
127- method,
128- authVersion,
129- } ) ;
105+ export function verifyResponse < T extends string | Buffer = string > (
106+ { url : urlPath , statusCode, text, timestamp, token, hmac, method, authVersion } : VerifyResponseOptions < T > ,
107+ useOriginalPath = false
108+ ) : VerifyResponseInfo < T > {
109+ const signatureSubject = calculateHMACSubject (
110+ {
111+ urlPath,
112+ text,
113+ timestamp,
114+ statusCode,
115+ method,
116+ authVersion,
117+ } ,
118+ useOriginalPath
119+ ) ;
130120
131121 // calculate the HMAC
132122 const expectedHmac = calculateHMAC ( token , signatureSubject ) ;
133123
134- // determine if the response is still within the validity window (5 minute window)
124+ // determine if the response is still within the validity window (5-minute backwards window, 1- minute forward window)
135125 const now = Date . now ( ) ;
136- const isInResponseValidityWindow = timestamp >= now - 1000 * 60 * 5 && timestamp <= now ;
126+ const backwardValidityWindow = 1000 * 60 * 5 ;
127+ const forwardValidityWindow = 1000 * 60 ;
128+ const isInResponseValidityWindow =
129+ timestamp >= now - backwardValidityWindow && timestamp <= now + forwardValidityWindow ;
137130
138131 // verify the HMAC and timestamp
139132 return {
0 commit comments