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

Time machine server.js #1589

Merged
merged 6 commits into from
Aug 31, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.0.1",
"description": "",
"dependencies": {
"basic-auth": "^2.0.0",
"compression": "^1.7.2",
"currency-symbol-map": "^4.0.3",
"d3-array": "^1.2.1",
Expand Down
22 changes: 21 additions & 1 deletion web/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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
Copy link
Member

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

if (process.env['BASIC_AUTH_USERNAME;']){
Copy link
Member

Choose a reason for hiding this comment

The 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);
Copy link
Member

Choose a reason for hiding this comment

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

Linter will probably say it needs const instead.

let authorized = false;
if (user){
Copy link
Member

Choose a reason for hiding this comment

The 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);
Copy link
Member

Choose a reason for hiding this comment

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

More traditional to split using a comma

Copy link
Member

Choose a reason for hiding this comment

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

You can be more explicit by naming a variable hasMatchingUserName and directly do the !== -1 comparaison here.

if (ix !== -1 && user.pass === process.env['BASIC_AUTH_PASSWORDS'].split(';')[ix]){
Copy link
Member

Choose a reason for hiding this comment

The 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).
A better way would be to serialise auth using user1:pass1,user2:pass2...

authorized = true;
}
}
if (!authorized){
res.statusCode = 401;
res.setHeader('WWW-Authenticate', 'Basic realm="example"');
Copy link
Member

Choose a reason for hiding this comment

The 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) {
Expand Down