Skip to content

Commit d5777cd

Browse files
queekusmethibmeu
authored andcommitted
Added abilioty to customise the components provided to the signer
1 parent ce167b8 commit d5777cd

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

packages/web-bot-auth/src/index.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export interface SignatureParams {
3030
expires: Date;
3131
nonce?: string;
3232
key?: string;
33+
components?: httpsig.Component[];
3334
}
3435

3536
export interface VerificationParams {
@@ -74,10 +75,19 @@ export function signatureHeaders<
7475
}
7576
}
7677
const signatureAgent = httpsig.extractHeader(message, SIGNATURE_AGENT_HEADER);
77-
let components: string[] = REQUEST_COMPONENTS;
78-
// not the ideal check, but extractHeader returns "" instead of throwing or null when the header does not exist
79-
if (!signatureAgent) {
80-
components = REQUEST_COMPONENTS_WITHOUT_SIGNATURE_AGENT;
78+
let components: string[] | undefined = params.components;
79+
if(!components) {
80+
// not the ideal check, but extractHeader returns "" instead of throwing or null when the header does not exist
81+
if (!signatureAgent) {
82+
components = REQUEST_COMPONENTS_WITHOUT_SIGNATURE_AGENT;
83+
} else {
84+
components = REQUEST_COMPONENTS
85+
}
86+
} else {
87+
if(signatureAgent && components.indexOf("SIGNATURE_AGENT_HEADER") === -1)
88+
{
89+
throw new Error(`${SIGNATURE_AGENT_HEADER} is required in params.component when included as a header param`);
90+
}
8191
}
8292
return httpsig.signatureHeaders(message, {
8393
signer,
@@ -110,10 +120,19 @@ export function signatureHeadersSync<
110120
}
111121
}
112122
const signatureAgent = httpsig.extractHeader(message, SIGNATURE_AGENT_HEADER);
113-
let components: string[] = REQUEST_COMPONENTS;
114-
// not the ideal check, but extractHeader returns "" instead of throwing or null when the header does not exist
115-
if (!signatureAgent) {
116-
components = REQUEST_COMPONENTS_WITHOUT_SIGNATURE_AGENT;
123+
let components: string[] | undefined = params.components;
124+
if(!components) {
125+
// not the ideal check, but extractHeader returns "" instead of throwing or null when the header does not exist
126+
if (!signatureAgent) {
127+
components = REQUEST_COMPONENTS_WITHOUT_SIGNATURE_AGENT;
128+
} else {
129+
components = REQUEST_COMPONENTS
130+
}
131+
} else {
132+
if(signatureAgent && components.indexOf("SIGNATURE_AGENT_HEADER") === -1)
133+
{
134+
throw new Error(`${SIGNATURE_AGENT_HEADER} is required in params.component when included as a header param`);
135+
}
117136
}
118137
return httpsig.signatureHeadersSync(message, {
119138
signer,

0 commit comments

Comments
 (0)