Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add new config option requestHeaders #130

Open
wants to merge 3 commits into
base: stage
Choose a base branch
from
Open
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
12 changes: 9 additions & 3 deletions src/lib/ctx.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context, Env, WebConfig, MobileConfig, basicAuth } from '../types.js'
import { Context, Env, WebConfig, MobileConfig, basicAuth, authToken } from '../types.js'
import constants from './constants.js'
import { version } from '../../package.json'
import { validateConfig } from './schemaValidation.js'
Expand All @@ -11,7 +11,8 @@ export default (options: Record<string, string>): Context => {
let env: Env = getEnv();
let webConfig: WebConfig;
let mobileConfig: MobileConfig;
let basicAuthObj: basicAuth
let basicAuthObj: basicAuth;
let requestHeaderObj: authToken;
let config = constants.DEFAULT_CONFIG;
let port: number;
let resolutionOff: boolean;
Expand Down Expand Up @@ -60,6 +61,10 @@ export default (options: Record<string, string>): Context => {
if (config.basicAuthorization) {
basicAuthObj = config.basicAuthorization
}
if (config.requestHeaders){
requestHeaderObj = config.requestHeaders
}


return {
env: env,
Expand All @@ -75,7 +80,8 @@ export default (options: Record<string, string>): Context => {
scrollTime: config.scrollTime || constants.DEFAULT_SCROLL_TIME,
allowedHostnames: config.allowedHostnames || [],
basicAuthorization: basicAuthObj,
smartIgnore: config.smartIgnore ?? false
smartIgnore: config.smartIgnore ?? false,
requestHeaders: requestHeaderObj
},
uploadFilePath: '',
webStaticConfig: [],
Expand Down
19 changes: 15 additions & 4 deletions src/lib/processSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,23 @@ async function processSnapshot(snapshot: Snapshot, ctx: Context): Promise<Record
// handle discovery config
ctx.config.allowedHostnames.push(new URL(snapshot.url).hostname);
if (ctx.config.enableJavaScript) ALLOWED_RESOURCES.push('script');
if (ctx.config.basicAuthorization) {
ctx.log.debug(`Adding basic authorization to the headers for root url`);
let token = Buffer.from(`${ctx.config.basicAuthorization.username}:${ctx.config.basicAuthorization.password}`).toString('base64');

if (ctx.config.basicAuthorization || ctx.config.requestHeaders) {
ctx.log.debug(`Adding basic authorization to the headers for url ${requestUrl}`);
let basicToken;
if (ctx.config.basicAuthorization){
if(ctx.config.basicAuthorization.username && ctx.config.basicAuthorization.password){
let token = Buffer.from(`${ctx.config.basicAuthorization.username}:${ctx.config.basicAuthorization.password}`).toString('base64');
basicToken = `Basic ${token}`
}
} else if (ctx.config.requestHeaders) {
if(ctx.config.requestHeaders.auth){
basicToken = ctx.config.requestHeaders.auth
}
}
requestOptions.headers = {
...request.headers(),
Authorization: `Basic ${token}`
Authorization: basicToken
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is incorrect. there may be more headers not only authorization

};
}

Expand Down
9 changes: 9 additions & 0 deletions src/lib/schemaValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ const ConfigSchema = {
errorMessage: "Invalid config; password is mandatory"
},
}
},
requestHeaders: {
type: "object",
properties: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incorrect. can have custom headers as well

auth: {
type: "string",
errorMessage: "Invalid config; auth is mandatory"
},
}
}
},
anyOf: [
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface Context {
allowedHostnames: Array<string>;
basicAuthorization: basicAuth | undefined;
smartIgnore: boolean;
requestHeaders: authToken | undefined
};
uploadFilePath: string;
webStaticConfig: WebStaticConfig;
Expand Down Expand Up @@ -148,3 +149,7 @@ export interface basicAuth {
username: string;
password: string;
}

export interface authToken {
auth: string;
}