-
Notifications
You must be signed in to change notification settings - Fork 902
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
Time machine server.js #1589
Time machine server.js #1589
Changes from 5 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ const express = require('express'); | |
const fs = require('fs'); | ||
const http = require('http'); | ||
const i18n = require('i18n'); | ||
const auth = require('basic-auth'); | ||
|
||
// Custom module | ||
const translation = require(__dirname + '/src/helpers/translation'); | ||
|
@@ -158,9 +159,28 @@ app.get('/', (req, res) => { | |
} | ||
const { locale } = res; | ||
const fullUrl = 'https://www.electricitymap.org' + req.originalUrl; | ||
|
||
// basic auth and time machine cookie if we're in the back in time container | ||
if (process.env['BASIC_AUTH_USERNAME;']){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why does the env variable name have a semicolon at the end? |
||
let user = auth(req); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Linter will probably say it needs |
||
let authorized = false; | ||
if (user){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style: linter probably requires space between ) and { |
||
let ix = process.env['BASIC_AUTH_USERNAMES'].split(';').indexOf(user.name); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More traditional to split using a comma There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can be more explicit by naming a variable |
||
if (ix !== -1 && user.pass === process.env['BASIC_AUTH_PASSWORDS'].split(';')[ix]){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a tad dangerous because this forces us to have two lists (one for users, one for passwords), and is therefore prone to errors (if you offset one item for example). |
||
authorized = true; | ||
} | ||
} | ||
if (!authorized){ | ||
res.statusCode = 401; | ||
res.setHeader('WWW-Authenticate', 'Basic realm="example"'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. example? |
||
res.end('Access denied'); | ||
return; | ||
} | ||
res.cookie('electricitymap-token', process.env['ELECTRICITYMAP_TOKEN']); | ||
} | ||
res.render('pages/index', { | ||
alternateUrls: locales.map(function(l) { | ||
if (fullUrl.indexOf('lang') != -1) { | ||
if (fullUrl.indexOf('lang') !== -1) { | ||
return fullUrl.replace('lang=' + req.query.lang, 'lang=' + l) | ||
} else { | ||
if (Object.keys(req.query).length) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we change the comment to something even a contributor could understand
Something along the lines of:
// Basic auth for premium access