Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions modules/sdk-hmac/src/hmac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,12 @@ export function calculateHMACSubject<T extends string | Buffer = string>({
? [method.toUpperCase(), timestamp, '3.0', queryPath].join('|')
: [timestamp, queryPath].join('|');
}
prefixedText += '|';

const isBuffer = Buffer.isBuffer(text);
if (isBuffer) {
return Buffer.concat([Buffer.from(prefixedText, 'utf-8'), text]) as T;
return Buffer.concat([Buffer.from(prefixedText + '|', 'utf-8'), text]) as T;
}
return (prefixedText + text) as T;
return [prefixedText, text].join('|') as T;
}

/**
Expand Down
30 changes: 30 additions & 0 deletions modules/sdk-hmac/test/hmac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ describe('HMAC Utility Functions', () => {
).to.equal(expectedSubject);
});

it('should not include undefined for a response when text is undefined', () => {
const expectedSubject = 'GET|1672531200000|/api/test|200|';
expect(
calculateHMACSubject({
urlPath: '/api/test',
text: undefined as unknown as string,
timestamp: MOCK_TIMESTAMP,
statusCode: 200,
method: 'get',
authVersion: 3,
})
).to.equal(expectedSubject);
});

it('should handle Buffer text input and return a Buffer for requests', () => {
const buffer = Buffer.from('binary-data-content');
const result = calculateHMACSubject({
Expand All @@ -96,6 +110,22 @@ describe('HMAC Utility Functions', () => {
expect(result).to.deep.equal(expectedBuffer);
});

it('should handle Buffer undefined text input and return a Buffer for requests', () => {
const buffer = undefined as unknown as Buffer;
const result = calculateHMACSubject({
urlPath: '/api/test',
text: buffer,
timestamp: MOCK_TIMESTAMP,
method: 'get',
authVersion: 3,
});

expect(Buffer.isBuffer(result)).to.be.false;

const expectedSubject = 'GET|1672531200000|3.0|/api/test|';
expect(result).to.deep.equal(expectedSubject);
});

it('should handle Buffer text input and return a Buffer for responses', () => {
const buffer = Buffer.from('binary-response-data');
const result = calculateHMACSubject({
Expand Down