Skip to content

Commit 386e545

Browse files
committed
feat: support original url for hmac calculation
Ticket: ANT-1086
1 parent 5ccb89d commit 386e545

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

modules/sdk-hmac/src/hmac.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,23 @@ export function calculateHMAC(key: string | BinaryLike | KeyObject, message: str
2828
* @param statusCode Only set for HTTP responses, leave blank for requests
2929
* @param method request method
3030
* @param authVersion authentication version (2 or 3)
31+
* @param useOriginalPath whether to use the original urlPath without parsing (default false)
3132
* @returns {string | Buffer}
3233
*/
33-
export function calculateHMACSubject<T extends string | Buffer = string>({
34-
urlPath,
35-
text,
36-
timestamp,
37-
statusCode,
38-
method,
39-
authVersion,
40-
}: CalculateHmacSubjectOptions<T>): T {
34+
export function calculateHMACSubject<T extends string | Buffer = string>(
35+
{ urlPath, text, timestamp, statusCode, method, authVersion }: CalculateHmacSubjectOptions<T>,
36+
useOriginalPath = false
37+
): T {
4138
/* Normalize legacy 'del' to 'delete' for backward compatibility */
4239
if (method === 'del') {
4340
method = 'delete';
4441
}
45-
const urlDetails = urlLib.parse(urlPath);
46-
const queryPath = urlDetails.query && urlDetails.query.length > 0 ? urlDetails.path : urlDetails.pathname;
42+
43+
let queryPath: string | null = urlPath;
44+
if (!useOriginalPath) {
45+
const urlDetails = urlLib.parse(urlPath);
46+
queryPath = urlDetails.query && urlDetails.query.length > 0 ? urlDetails.path : urlDetails.pathname;
47+
}
4748

4849
let prefixedText: string;
4950
if (statusCode !== undefined && isFinite(statusCode) && Number.isInteger(statusCode)) {

modules/sdk-hmac/test/hmac.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,35 @@ describe('HMAC Utility Functions', () => {
6161
).to.equal(expectedSubject);
6262
});
6363

64+
it('should calculate the correct subject for a request with a trailing ? when useOriginalPath is true', () => {
65+
const expectedSubject = 'GET|1672531200000|3.0|/api/test?|body-content';
66+
expect(
67+
calculateHMACSubject(
68+
{
69+
urlPath: '/api/test?',
70+
text: 'body-content',
71+
timestamp: MOCK_TIMESTAMP,
72+
method: 'get',
73+
authVersion: 3,
74+
},
75+
true
76+
)
77+
).to.equal(expectedSubject);
78+
});
79+
80+
it('should calculate the correct subject for a request with a trailing ? when useOriginalPath is false', () => {
81+
const expectedSubject = 'GET|1672531200000|3.0|/api/test|body-content';
82+
expect(
83+
calculateHMACSubject({
84+
urlPath: '/api/test?',
85+
text: 'body-content',
86+
timestamp: MOCK_TIMESTAMP,
87+
method: 'get',
88+
authVersion: 3,
89+
})
90+
).to.equal(expectedSubject);
91+
});
92+
6493
it('should include statusCode for a response', () => {
6594
const expectedSubject = 'GET|1672531200000|/api/test|200|response-body';
6695
expect(

0 commit comments

Comments
 (0)