Skip to content

Commit

Permalink
Merge pull request #42 from datasektionen/env-rewrite
Browse files Browse the repository at this point in the history
Rewrite env solution, update compose, and add dependency
  • Loading branch information
Herkarl authored Apr 22, 2024
2 parents d8c4b73 + 5a175a9 commit 3fd9745
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 68 deletions.
9 changes: 8 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var kthpeople = require('./routes/kthpeople');
var login = require('./routes/login');
var groups = require('./routes/groups');
var helpers = require('./routes/helpers');
var env = require("./util/env");

var pug = require("pug");
var babel = require("jade-babel");
Expand All @@ -37,8 +38,14 @@ app.use('/', express.static(path.join(__dirname, 'public')));
app.use(session({
saveUninitialized: true,
resave: true,
secret: process.env.SESSION_SECRET,
secret: env.session_secret,
}));

app.use((req, res, next) => {
res.locals.login_url = env.login_frontend_url;
next();
})

require('./config/passport')(passport);
app.use(passport.initialize());
app.use(passport.session());
Expand Down
3 changes: 2 additions & 1 deletion bin/www
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
var debug = require('debug')('express-example');
var app = require('../app');
var models = require("../models");
var env = require("../util/env");

app.set('port', process.env.PORT || 5000);
app.set('port', env.port);

models.sequelize.sync().then(function () {
var server = app.listen(app.get('port'), function() {
Expand Down
17 changes: 14 additions & 3 deletions docker-compose.yml → compose.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
version: "2"
services:
db:
image: postgres:9.5
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: pass123
POSTGRES_DB: dfunkt

volumes:
- ./database/:/docker-entrypoint-initdb.d/
healthcheck:
test: ["CMD", "pg_isready", "-U", "postgres"]
interval: 3s
timeout: 3s
retries: 5

node:
tty: true
build: .
environment:
- SESSION_SECRET="asdf"
- DATABASE_URL=postgresql://postgres:pass123@db:5432/dfunkt
SESSION_SECRET: "asdf"
DATABASE_URL: postgresql://postgres:pass123@db:5432/dfunkt
LOGIN_API_URL: "http://login:7002"
LOGIN_FRONTEND_URL: http://localhost:7002
LOGIN_KEY: 1234567890abcdef
PLS_URL: http://login:7001
env_file: .env
volumes:
- ./:/app
Expand All @@ -27,3 +32,9 @@ services:
depends_on:
db:
condition: service_healthy

login:
image: ghcr.io/datasektionen/nyckeln-under-dorrmattan
ports:
- 7002:7002
- 7001:7001
41 changes: 6 additions & 35 deletions config/strategies/dauth.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,13 @@
var passport = require('passport');
var CustomStrategy = require('passport-custom').Strategy;
var https = require('https');

var fetch = require('node-fetch');
var env = require("../../util/env");

function verify(token, callback) {
var options = {
host: "login.datasektionen.se",
path: "/verify/" + token + ".json?api_key=" + process.env.LOGIN_KEY,
method: "GET"
};

var requestCallback = function(res) {
var collectedData = "";
res.setEncoding("utf-8");

res.on("data", function(data) {
collectedData += data;
});

res.on("end", function() {
if (collectedData) {
try {
var user = JSON.parse(collectedData);
callback(user);
} catch(e) {
callback(undefined);
}
} else {
callback(undefined);
}
});

res.on("error", function(err) {
callback(undefined);
});
};
var request = https.request(options, requestCallback);
request.end();
fetch(`${env.login_api_url}/verify/${token}?api_key=${env.login_key}`)
.then((res) => res.json())
.then((data) => callback(data))
.catch(() => callback(undefined))
}


Expand Down
8 changes: 4 additions & 4 deletions models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
var fs = require("fs");
var path = require("path");
var Sequelize = require("sequelize");
var env = process.env.NODE_ENV || "development";
var config = require(path.join(__dirname, '..', 'config', 'config.json'))[env];
var sequelize;
var env = require("../util/env")
var config = require(path.join(__dirname, '..', 'config', 'config.json'))[app_env];

sequelize = new Sequelize(process.env.DATABASE_URL);
var app_env = env.node_env;
var sequelize = new Sequelize(env.database_url);

var db = {};

Expand Down
60 changes: 60 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"ipaddr": "0.0.9",
"jade-babel": "^5.0.0",
"moment": "^2.29.1",
"node-fetch": "^2.7.0",
"passport": "^0.3.2",
"passport-custom": "^1.0.5",
"pg": "^6.1.0",
Expand Down
33 changes: 11 additions & 22 deletions routes/helpers.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,29 @@
var models = require('../models');
var moment = require('moment');
var request = require('request');
var env = require('../util/env');
var fetch = require('node-fetch');

var denied = function(res) {
res.status(403);
res.send('denied');
};

var isadmin = function(user) {
var plsurl = "https://pls.datasektionen.se/api/user/" + user + "/dfunkt/admin";
return new Promise(function (resolve) {
request({uri: plsurl, method: 'GET'}, function (error, response, body) {
if(error) console.error(error);
if (body === "true") {
resolve(true);
} else {
resolve(false);
}
});
});
return fetch(`${env.pls_url}/api/user/${user}/dfunkt/admin`)
.then((res) => res.json())
.then((data) => (resolve) => resolve(data))
.catch((err) => () => console.error(err))

};

exports.isadmin = isadmin;

var issearch = function(user) {
var plsurl = "https://pls.datasektionen.se/api/user/" + user + "/dfunkt/search";
return new Promise(function (resolve) {
request({uri: plsurl, method: 'GET'}, function (error, response, body) {
if(error) console.error(error);
if (body === "true") {
resolve(true);
} else {
resolve(false);
}
});
});
return fetch(`${env.pls_url}/api/user/${user}/dfunkt/search`)
.then((res) => res.json())
.then((data) => (resolve) => resolve(data))
.catch((err) => () => console.error(err))
};

exports.issearch = issearch;
Expand Down
2 changes: 1 addition & 1 deletion routes/mandates.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function zfingerCreateUser(ugkthid) {
}

function findOrCreateUser(user) {
debug("GOind to find or create dis: " + user);
debug("Goind to find or create dis: " + user);
return models.User.findOrCreate({
where: {
first_name: user.first_name,
Expand Down
12 changes: 12 additions & 0 deletions util/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const env = {
session_secret: process.env.SESSION_SECRET,
node_env: process.env.NODE_ENV || "development",
port: process.env.PORT || 5000,
database_url: process.env.DATABASE_URL,
login_key: process.env.LOGIN_KEY,
login_api_url: process.env.LOGIN_API_URL || "https://login.datasektionen.se",
login_frontend_url: process.env.LOGIN_FRONTEND_URL || "https://login.datasektionen.se",
pls_url: process.env.PLS_URL || "https://pls.datasektionen.se"
};

module.exports = env;
2 changes: 1 addition & 1 deletion views/layout.pug
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ html
callback += ":" + location.port
}
callback += "/login/dauth/";
return "https://login.datasektionen.se/login?callback=" + callback
return '#{ login_url }' + "/login?callback=" + callback
}

var loginname="#{user}";
Expand Down

0 comments on commit 3fd9745

Please sign in to comment.