diff --git a/app.js b/app.js index 6ae2129..0cf0d5f 100644 --- a/app.js +++ b/app.js @@ -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"); @@ -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()); diff --git a/bin/www b/bin/www index dc0234b..c51b415 100644 --- a/bin/www +++ b/bin/www @@ -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() { diff --git a/docker-compose.yml b/compose.yml similarity index 58% rename from docker-compose.yml rename to compose.yml index 193baac..ca9229a 100644 --- a/docker-compose.yml +++ b/compose.yml @@ -1,4 +1,3 @@ -version: "2" services: db: image: postgres:9.5 @@ -6,6 +5,7 @@ services: POSTGRES_USER: postgres POSTGRES_PASSWORD: pass123 POSTGRES_DB: dfunkt + volumes: - ./database/:/docker-entrypoint-initdb.d/ healthcheck: @@ -13,12 +13,17 @@ services: 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 @@ -27,3 +32,9 @@ services: depends_on: db: condition: service_healthy + + login: + image: ghcr.io/datasektionen/nyckeln-under-dorrmattan + ports: + - 7002:7002 + - 7001:7001 \ No newline at end of file diff --git a/config/strategies/dauth.js b/config/strategies/dauth.js index 739a0c0..0c1c4c8 100644 --- a/config/strategies/dauth.js +++ b/config/strategies/dauth.js @@ -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)) } diff --git a/models/index.js b/models/index.js index 458ec95..57a2413 100644 --- a/models/index.js +++ b/models/index.js @@ -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 = {}; diff --git a/package-lock.json b/package-lock.json index bbf1193..ea89fc2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -25,6 +25,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", @@ -1698,6 +1699,15 @@ "node": ">=0.10.0" } }, + "node_modules/bindings": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz", + "integrity": "sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==", + "optional": true, + "dependencies": { + "file-uri-to-path": "1.0.0" + } + }, "node_modules/bl": { "version": "1.2.3", "resolved": "https://registry.npmjs.org/bl/-/bl-1.2.3.tgz", @@ -5074,6 +5084,12 @@ "resolved": "https://registry.npmjs.org/fast-safe-stringify/-/fast-safe-stringify-2.0.7.tgz", "integrity": "sha512-Utm6CdzT+6xsDk2m8S6uL8VHxNwI6Jub+e9NYTcAms28T84pTa25GJQV9j0CY0N1rM8hK4x6grpF2BQf+2qwVA==" }, + "node_modules/file-uri-to-path": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz", + "integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==", + "optional": true + }, "node_modules/fill-range": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-4.0.0.tgz", @@ -7438,6 +7454,12 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" }, + "node_modules/nan": { + "version": "2.19.0", + "resolved": "https://registry.npmjs.org/nan/-/nan-2.19.0.tgz", + "integrity": "sha512-nO1xXxfh/RWNxfd/XPfbIfFk5vgLsAxUR9y5O0cHMJu/AW9U95JLXqthYHjEp+8gQ5p96K9jUp8nbVOxCdRbtw==", + "optional": true + }, "node_modules/nanomatch": { "version": "1.2.13", "resolved": "https://registry.npmjs.org/nanomatch/-/nanomatch-1.2.13.tgz", @@ -7501,6 +7523,25 @@ "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-2.0.0.tgz", "integrity": "sha512-ASCL5U13as7HhOExbT6OlWJJUV/lLzL2voOSP1UVehpRD8FbSrSDjfScK/KwAvVTI5AS6r4VwbOMlIqtvRidnA==" }, + "node_modules/node-fetch": { + "version": "2.7.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz", + "integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==", + "dependencies": { + "whatwg-url": "^5.0.0" + }, + "engines": { + "node": "4.x || >=6.0.0" + }, + "peerDependencies": { + "encoding": "^0.1.0" + }, + "peerDependenciesMeta": { + "encoding": { + "optional": true + } + } + }, "node_modules/node-gyp": { "version": "3.8.0", "resolved": "https://registry.npmjs.org/node-gyp/-/node-gyp-3.8.0.tgz", @@ -12491,6 +12532,11 @@ "node": ">=0.8" } }, + "node_modules/tr46": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", + "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" + }, "node_modules/trim-right": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/trim-right/-/trim-right-1.0.1.tgz", @@ -12783,6 +12829,20 @@ "watchify": "bin/cmd.js" } }, + "node_modules/webidl-conversions": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", + "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==" + }, + "node_modules/whatwg-url": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", + "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", + "dependencies": { + "tr46": "~0.0.3", + "webidl-conversions": "^3.0.0" + } + }, "node_modules/which": { "version": "1.3.1", "resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz", diff --git a/package.json b/package.json index ace389a..b1ea9a4 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/routes/helpers.js b/routes/helpers.js index 2066d83..58880ec 100644 --- a/routes/helpers.js +++ b/routes/helpers.js @@ -1,6 +1,8 @@ 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); @@ -8,33 +10,20 @@ var denied = function(res) { }; 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; diff --git a/routes/mandates.js b/routes/mandates.js index a622eae..bcb7de8 100644 --- a/routes/mandates.js +++ b/routes/mandates.js @@ -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, diff --git a/util/env.js b/util/env.js new file mode 100644 index 0000000..c8049cc --- /dev/null +++ b/util/env.js @@ -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; \ No newline at end of file diff --git a/views/layout.pug b/views/layout.pug index 0a83326..bae7a01 100644 --- a/views/layout.pug +++ b/views/layout.pug @@ -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}";