Skip to content

Commit

Permalink
add new config option requestHeaders
Browse files Browse the repository at this point in the history
  • Loading branch information
parthlambdatest committed Sep 19, 2024
1 parent 17f8186 commit 2df270d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
10 changes: 8 additions & 2 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 @@ -12,6 +12,7 @@ export default (options: Record<string, string>): Context => {
let webConfig: WebConfig;
let mobileConfig: MobileConfig;
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 @@ -74,7 +79,8 @@ export default (options: Record<string, string>): Context => {
cliEnableJavaScript: config.cliEnableJavaScript || true,
scrollTime: config.scrollTime || constants.DEFAULT_SCROLL_TIME,
allowedHostnames: config.allowedHostnames || [],
basicAuthorization: basicAuthObj
basicAuthorization: basicAuthObj,
requestHeaders: requestHeaderObj
},
uploadFilePath: '',
webStaticConfig: [],
Expand Down
10 changes: 8 additions & 2 deletions src/lib/processSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,15 @@ 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) {

if (ctx.config.basicAuthorization || ctx.config.requestHeaders) {
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');
let token;
if(ctx.config.basicAuthorization){
token = Buffer.from(`${ctx.config.basicAuthorization.username}:${ctx.config.basicAuthorization.password}`).toString('base64');
} else {
token = ctx.config.requestHeaders.auth
}
requestOptions.headers = {
...request.headers(),
Authorization: `Basic ${token}`
Expand Down
9 changes: 9 additions & 0 deletions src/lib/schemaValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ const ConfigSchema = {
errorMessage: "Invalid config; password is mandatory"
},
}
},
requestHeaders: {
type: "object",
properties: {
auth: {
type: "string",
errorMessage: "Invalid config; auth is mandatory"
},
}
}
},
anyOf: [
Expand Down
8 changes: 7 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export interface Context {
cliEnableJavaScript: boolean;
scrollTime: number;
allowedHostnames: Array<string>;
basicAuthorization: basicAuth | undefined
basicAuthorization: basicAuth | undefined;
requestHeaders: authToken | undefined

};
uploadFilePath: string;
webStaticConfig: WebStaticConfig;
Expand Down Expand Up @@ -147,3 +149,7 @@ export interface basicAuth {
username: string;
password: string;
}

export interface authToken {
auth: string;
}

0 comments on commit 2df270d

Please sign in to comment.