Skip to content

Commit

Permalink
Merge pull request #2 from otobongfp/main
Browse files Browse the repository at this point in the history
Update: Custom header to preserve signed url and extract same when verifying
  • Loading branch information
jasny authored Sep 26, 2024
2 parents 333179b + 0a949f1 commit 6a355c2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
.idea
.nyc_output
.yarn
.DS_Store
yarn.lock
package-lock.json
8 changes: 7 additions & 1 deletion src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ export function extractHeader({ headers }: RequestLike | ResponseLike, header: s
return val.toString().replace(/\s+/g, ' ');
}

function getUrl(message: RequestLike | ResponseLike, component: string): URL {
export function getUrl(message: RequestLike | ResponseLike, component: string): URL {
if ('url' in message && 'protocol' in message) {
const host = extractHeader(message, 'host');
const protocol = message.protocol || 'http';
const baseUrl = `${protocol}://${host}`;
return new URL(message.url, baseUrl);
}
if (!(message as RequestLike).url) throw new Error(`${component} is only valid for requests`);
return new URL((message as RequestLike).url);
}
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type HeaderValue = { toString(): string } | string | string[] | undefined
export type RequestLike = {
method: string;
url: string;
protocol?: string;
headers: Headers;
};

Expand Down
22 changes: 21 additions & 1 deletion test/build.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, Parameters, RequestLike } from '../src';
import { buildSignatureInputString, buildSignedData, extractComponent, extractHeader } from '../src/build';
import { buildSignatureInputString, buildSignedData, extractComponent, extractHeader, getUrl } from '../src/build';
import { expect } from 'chai';

describe('build', () => {
Expand Down Expand Up @@ -242,4 +242,24 @@ describe('build', () => {
);
});
});

describe('getUrl', () => {
it('should correctly construct a full URL from a RequestLike object with protocol and host', () => {
const message: RequestLike = {
method: 'GET',
url: '/path',
protocol: 'https',
headers: {
host: 'www.example.com',
},
};
const result = getUrl(message, '@target-uri');
expect(result.toString()).to.equal('https://www.example.com/path');
});

it('should throw an error if the message does not contain a URL', () => {
const message = {} as RequestLike;
expect(() => getUrl(message, '@target-uri')).to.throw('@target-uri is only valid for requests');
});
});
});

0 comments on commit 6a355c2

Please sign in to comment.