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 all 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
24 changes: 23 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,30 @@ app.get('/', (req, res) => {
}
const { locale } = res;
const fullUrl = 'https://www.electricitymap.org' + req.originalUrl;

// basic auth for premium access
if (process.env.BASIC_AUTH_CREDENTIALS) {
const user = auth(req);
let authorized = false;
if (user) {
process.env.BASIC_AUTH_CREDENTIALS.split(',').forEach((cred) => {
const [name, pass] = cred.split(':');
if (name === user.name && pass === user.pass) {
authorized = true;
}
});
}
if (!authorized) {
res.statusCode = 401;
res.setHeader('WWW-Authenticate', 'Basic realm="Premium access to electricitymap.org"');
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