Skip to content

Commit

Permalink
Merge pull request #22 from wanteddev/feature/avasconcelos114/auth_in…
Browse files Browse the repository at this point in the history
…jection

Release
  • Loading branch information
avasconcelos114 authored May 31, 2021
2 parents a262fe5 + e1ea0a9 commit 23765aa
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 20 deletions.
1 change: 1 addition & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ jobs:
run: |
docker tag ghcr.io/wanteddev/lighthouse:${{github.event.release.tag_name}} ghcr.io/wanteddev/lighthouse:latest
docker push ghcr.io/wanteddev/lighthouse:latest
if: "github.event.release.prerelease != true"
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
*.http
*.http
.env
23 changes: 14 additions & 9 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,35 +209,37 @@ router.post('/receive_submission', async function(req, res) {
user_id: res_data.user.id,
username: res_data.user.username,
channel,
auth_header: undefined,
cookie_name: undefined,
cookie_value: undefined,
};

console.log(JSON.stringify(values));
for (const key in values) {
if (values[key].audit_options && values[key].audit_options.selected_options && values[key].audit_options.selected_options.length > 0) {
values[key].audit_options.selected_options.forEach(option => {
submission[option.value] = true;
});
}

if (values[key].audit_url) {
submission.audit_url = values[key].audit_url.value;
}

if (values[key].schedule) {
submission.schedule = values[key].schedule.value;
} else {
for (const optionKey of Object.keys(values[key])) {
submission[optionKey] = values[key][optionKey].value;
}
}
}

try {
// Ad-hoc run
if (!is_schedule) {

const options = {
throttling: submission.throttling,
performance: submission.performance,
accessibility: submission.accessibility,
'best-practices': submission['best-practices'],
pwa: submission.pwa,
seo: submission.seo,
auth_header: submission.auth_header,
cookie_name: submission.cookie_name,
cookie_value: submission.cookie_value,
};
res.send();
await runAudit(submission.audit_url, submission.user_id, submission.channel, options);
Expand All @@ -255,6 +257,9 @@ router.post('/receive_submission', async function(req, res) {
'best-practices': schedule['best-practices'],
pwa: schedule.pwa,
seo: schedule.seo,
auth_header: schedule.auth_header,
cookie_name: schedule.cookie_name,
cookie_value: schedule.cookie_value,
};
await runAudit(schedule.audit_url, schedule.user_id, schedule.channel, options);
});
Expand Down
6 changes: 6 additions & 0 deletions src/store/schedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const schema = new mongoose.Schema({
seo: Boolean,
pwa: Boolean,
throttling: Boolean,
auth_header: String,
cookie_name: String,
cookie_value: String,
});

const ScheduleModel = mongoose.model('Schedule', schema);
Expand All @@ -34,6 +37,9 @@ async function createSchedule(payload) {
seo: payload.seo,
pwa: payload.pwa,
throttling: payload.throttling,
auth_header: payload.auth_header,
cookie_name: payload.cookie_name,
cookie_value: payload.cookie_value,
});

const data = await new_schedule.save();
Expand Down
23 changes: 13 additions & 10 deletions src/utils/lighthouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,22 @@ async function launchPuppeteer(url, options) {
'--disable-dev-shm-usage'
]
});
const page = await browser.newPage();

// Run authentication script (as injected javascript)
if (options.auth_script) {
const page = await browser.newPage();
await page.goto(url, {
waitUntil: 'networkidle0',
});
await page.waitForSelector(options.await_selector, {visible: true});
await page.evaluate(options.auth_script);
await page.waitForNavigation();
if (options.auth_header) {
await page.setExtraHTTPHeaders({
'Authorization': options.auth_header,
})
}

if (options.cookie_name && options.cookie_value) {
await page.setCookie({ name: options.cookie_name, value: options.cookie_value, url });
}
await page.goto(url, {
waitUntil: 'networkidle0',
});
await page.waitForSelector('body', {visible: true});
await page.close();
// Lighthouse will open URL. Puppeteer observes `targetchanged` and sets up network conditions.
// Possible race condition.
let opts = {
Expand Down Expand Up @@ -83,7 +87,6 @@ async function launchPuppeteer(url, options) {
const {lhr} = await lighthouse(url, opts);
// Return response back to main thread
parentPort.postMessage(lhr);

await browser.close();
return;
} catch(error) {
Expand Down
54 changes: 54 additions & 0 deletions src/utils/responseBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,60 @@ function generateAuditDialog(is_schedule) {
blocks.push(schedule);
}

const auth_header = {
type: 'input',
optional: true,
element: {
type: 'plain_text_input',
action_id: 'auth_header',
placeholder: {
type: 'plain_text',
text: 'JWT ofma3103dSFNsUJasn311ndSN'
}
},
label: {
type: 'plain_text',
text: 'Authorization Header'
}
};
blocks.push(auth_header);

const cookie_name = {
type: 'input',
optional: true,
element: {
type: 'plain_text_input',
action_id: 'cookie_name',
placeholder: {
type: 'plain_text',
text: 'jwt'
}
},
label: {
type: 'plain_text',
text: 'Cookie Name'
}
};
blocks.push(cookie_name);

const cookie_value = {
type: 'input',
optional: true,
element: {
type: 'plain_text_input',
action_id: 'cookie_value',
placeholder: {
type: 'plain_text',
text: 'ofma3103dSFNsUJasn311ndSN...'
}
},
label: {
type: 'plain_text',
text: 'Cookie Value'
}
};
blocks.push(cookie_value);

// Option dropdowns
const options = {
type: 'input',
Expand Down

0 comments on commit 23765aa

Please sign in to comment.