From 7c2663fa56ce54818c53be4cc42884eb6d27bbec Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Mon, 7 Mar 2022 13:26:54 +1300 Subject: [PATCH 01/14] Add passwordless login for local installs --- client/homebrew/homebrew.jsx | 1 + client/homebrew/navbar/account.navitem.jsx | 30 ++++++++++++++++++++++ server/app.js | 7 ++++- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/client/homebrew/homebrew.jsx b/client/homebrew/homebrew.jsx index 54dcf2206..84a69b3a7 100644 --- a/client/homebrew/homebrew.jsx +++ b/client/homebrew/homebrew.jsx @@ -36,6 +36,7 @@ const Homebrew = createClass({ global.account = this.props.account; global.version = this.props.version; global.enable_v3 = this.props.enable_v3; + global.config = this.props.config; }, render : function (){ return ( diff --git a/client/homebrew/navbar/account.navitem.jsx b/client/homebrew/navbar/account.navitem.jsx index 004685a13..e0fa555da 100644 --- a/client/homebrew/navbar/account.navitem.jsx +++ b/client/homebrew/navbar/account.navitem.jsx @@ -1,6 +1,7 @@ const React = require('react'); const createClass = require('create-react-class'); const Nav = require('naturalcrit/nav/nav.jsx'); +const jwt = require('jwt-simple'); const Account = createClass({ displayName : 'AccountNavItem', @@ -25,7 +26,26 @@ const Account = createClass({ }; }, + localLogin : function(){ + const username = prompt('Enter username:'); + if(!username) {return;}; + + const payload = { + username : username, + issued : new Date() + }; + const expiry = new Date; + expiry.setFullYear(expiry.getFullYear() + 1); + const token = jwt.encode(payload, global.config.secret); + + document.cookie = `nc_session=${token};expires=${expiry};path=/;samesite=lax;${window.domain ? `domain=${window.domain}` : ''}`; + window.location.reload(true); + }, + render : function(){ + const localEnvironments = ['local', 'docker']; + + // Logged in if(global.account){ return ; } + // Logged out + // LOCAL ONLY + if(localEnvironments.includes(global.config.environment)) { + return + login + ; + }; + + // Logged out + // Production site return login ; diff --git a/server/app.js b/server/app.js index de51c8882..fb1e4e64b 100644 --- a/server/app.js +++ b/server/app.js @@ -262,6 +262,10 @@ app.get('/print/:id', asyncHandler(async (req, res, next)=>{ //Render the page const templateFn = require('./../client/template.js'); app.use((req, res)=>{ + const configuration = { + environment : config.get('node_env'), + secret : config.get('secret') + }; const props = { version : require('./../package.json').version, url : req.originalUrl, @@ -269,7 +273,8 @@ app.use((req, res)=>{ brews : req.brews, googleBrews : req.googleBrews, account : req.account, - enable_v3 : config.get('enable_v3') + enable_v3 : config.get('enable_v3'), + config : configuration }; const title = req.brew ? req.brew.title : ''; templateFn('homebrew', title, props) From 535291a91ac814a9461cf01579d01405fb0c585f Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Mon, 7 Mar 2022 14:14:32 +1300 Subject: [PATCH 02/14] Add `local_environments` to configuration files Add `local` parameter to global `config` object to avoid repeating tests JWT key `secret` only added to global object `config` when local installation detected --- client/homebrew/navbar/account.navitem.jsx | 4 +--- config/default.json | 3 ++- server/app.js | 11 +++++++++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/client/homebrew/navbar/account.navitem.jsx b/client/homebrew/navbar/account.navitem.jsx index e0fa555da..9627107a5 100644 --- a/client/homebrew/navbar/account.navitem.jsx +++ b/client/homebrew/navbar/account.navitem.jsx @@ -43,8 +43,6 @@ const Account = createClass({ }, render : function(){ - const localEnvironments = ['local', 'docker']; - // Logged in if(global.account){ return @@ -75,7 +73,7 @@ const Account = createClass({ // Logged out // LOCAL ONLY - if(localEnvironments.includes(global.config.environment)) { + if(global.config.local) { return login ; diff --git a/config/default.json b/config/default.json index f74ce3b8e..f630b5b0f 100644 --- a/config/default.json +++ b/config/default.json @@ -3,5 +3,6 @@ "naturalcrit_url" : "local.naturalcrit.com:8010", "secret" : "secret", "web_port" : 8000, - "enable_v3" : true + "enable_v3" : true, + "local_environments" : ["docker", "local"] } diff --git a/server/app.js b/server/app.js index fb1e4e64b..87711433f 100644 --- a/server/app.js +++ b/server/app.js @@ -262,9 +262,16 @@ app.get('/print/:id', asyncHandler(async (req, res, next)=>{ //Render the page const templateFn = require('./../client/template.js'); app.use((req, res)=>{ + // Create configuration object const configuration = { - environment : config.get('node_env'), - secret : config.get('secret') + local : false, + environment : config.get('node_env') + }; + // Add local only items to configuration object + const localEnvironments = config.get('local_environments'); + if(localEnvironments.includes(configuration.environment)){ + configuration.local = true; + configuration.secret = config.get('secret'); }; const props = { version : require('./../package.json').version, From 0d8ad50e2a7915b4250103c6ac2fd69880792d55 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Tue, 22 Mar 2022 10:53:33 +1300 Subject: [PATCH 03/14] Increase home page test timeout (5s -> 30s) --- tests/routes/static-pages.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/routes/static-pages.test.js b/tests/routes/static-pages.test.js index e69706f56..bd62d64af 100644 --- a/tests/routes/static-pages.test.js +++ b/tests/routes/static-pages.test.js @@ -7,7 +7,7 @@ const app = supertest.agent(require('app.js').app) describe('Tests for static pages', ()=>{ it('Home page works', ()=>{ return app.get('/').expect(200); - }); + }, 30000); it('Home page v3 works', ()=>{ return app.get('/v3_preview').expect(200); From b6933406ed36d6473a49a6aeb26d0e94b25cfae2 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Sat, 26 Mar 2022 21:27:48 +1300 Subject: [PATCH 04/14] Remove test timeout increase --- tests/routes/static-pages.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/routes/static-pages.test.js b/tests/routes/static-pages.test.js index bd62d64af..e69706f56 100644 --- a/tests/routes/static-pages.test.js +++ b/tests/routes/static-pages.test.js @@ -7,7 +7,7 @@ const app = supertest.agent(require('app.js').app) describe('Tests for static pages', ()=>{ it('Home page works', ()=>{ return app.get('/').expect(200); - }, 30000); + }); it('Home page v3 works', ()=>{ return app.get('/v3_preview').expect(200); From c8f6dea1e11c80da428309fb5637b35165c61503 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Sun, 27 Mar 2022 17:12:15 +1300 Subject: [PATCH 05/14] Conflict fix for `homebrew.jsx` --- client/homebrew/homebrew.jsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/client/homebrew/homebrew.jsx b/client/homebrew/homebrew.jsx index 84a69b3a7..cb362aa0a 100644 --- a/client/homebrew/homebrew.jsx +++ b/client/homebrew/homebrew.jsx @@ -32,11 +32,12 @@ const Homebrew = createClass({ } }; }, - componentWillMount : function() { + getInitialState : function() { global.account = this.props.account; global.version = this.props.version; global.enable_v3 = this.props.enable_v3; global.config = this.props.config; + return {}; }, render : function (){ return ( From 0023e87d54d6b93c4d2c340e5a8f1b9e51541aa9 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Wed, 13 Apr 2022 12:37:50 +1200 Subject: [PATCH 06/14] Whitespace fix --- client/homebrew/homebrew.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/homebrew/homebrew.jsx b/client/homebrew/homebrew.jsx index fd55ee7c6..895dc2b3f 100644 --- a/client/homebrew/homebrew.jsx +++ b/client/homebrew/homebrew.jsx @@ -39,7 +39,7 @@ const Homebrew = createClass({ global.enable_v3 = this.props.enable_v3; global.config = this.props.config; - return {}; + return {}; }, render : function (){ From 1bdd08f878ddfdf83a8cb4944060b840058b4dcc Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Wed, 13 Apr 2022 12:40:16 +1200 Subject: [PATCH 07/14] Shift JWT generation to `app.js` --- client/homebrew/navbar/account.navitem.jsx | 19 ++++++++++++------- server/app.js | 19 +++++++++++++++++-- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/client/homebrew/navbar/account.navitem.jsx b/client/homebrew/navbar/account.navitem.jsx index 5d18ea53f..a1a739005 100644 --- a/client/homebrew/navbar/account.navitem.jsx +++ b/client/homebrew/navbar/account.navitem.jsx @@ -1,7 +1,7 @@ const React = require('react'); const createClass = require('create-react-class'); const Nav = require('naturalcrit/nav/nav.jsx'); -const jwt = require('jwt-simple'); +const request = require('superagent'); const Account = createClass({ displayName : 'AccountNavItem', @@ -29,17 +29,22 @@ const Account = createClass({ }; }, - localLogin : function(){ + localLogin : async function(){ const username = prompt('Enter username:'); if(!username) {return;}; - const payload = { - username : username, - issued : new Date() - }; const expiry = new Date; expiry.setFullYear(expiry.getFullYear() + 1); - const token = jwt.encode(payload, global.config.secret); + + const token = await request.post('/login') + .send({ username }) + .then((response)=>{ + return response.body; + }) + .catch((err)=>{ + return null; + }); + if(!token) return; document.cookie = `nc_session=${token};expires=${expiry};path=/;samesite=lax;${window.domain ? `domain=${window.domain}` : ''}`; window.location.reload(true); diff --git a/server/app.js b/server/app.js index 87711433f..9677ace56 100644 --- a/server/app.js +++ b/server/app.js @@ -259,6 +259,23 @@ app.get('/print/:id', asyncHandler(async (req, res, next)=>{ return next(); })); +const localEnvironments = config.get('local_environments'); +// Login +app.post('/login', (req, res)=>{ + // Local only + if(!localEnvironments.includes(config.get('node_env'))){ + return; + } + + const username = req.body.username; + if(!username) return; + + const payload = jwt.encode({ username: username, issued: new Date }, config.get('secret')); + return res.json(payload); +}); + + + //Render the page const templateFn = require('./../client/template.js'); app.use((req, res)=>{ @@ -268,10 +285,8 @@ app.use((req, res)=>{ environment : config.get('node_env') }; // Add local only items to configuration object - const localEnvironments = config.get('local_environments'); if(localEnvironments.includes(configuration.environment)){ configuration.local = true; - configuration.secret = config.get('secret'); }; const props = { version : require('./../package.json').version, From 811d1347eaadaab0fec13ca945371f30af0104a3 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Wed, 13 Apr 2022 12:57:04 +1200 Subject: [PATCH 08/14] Reduce calls to `config.get` --- server/app.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/server/app.js b/server/app.js index 9677ace56..746eed449 100644 --- a/server/app.js +++ b/server/app.js @@ -260,10 +260,11 @@ app.get('/print/:id', asyncHandler(async (req, res, next)=>{ })); const localEnvironments = config.get('local_environments'); +const nodeEnv = config.get('node_env'); // Login app.post('/login', (req, res)=>{ // Local only - if(!localEnvironments.includes(config.get('node_env'))){ + if(!localEnvironments.includes(nodeEnv)){ return; } @@ -282,10 +283,10 @@ app.use((req, res)=>{ // Create configuration object const configuration = { local : false, - environment : config.get('node_env') + environment : nodeEnv }; // Add local only items to configuration object - if(localEnvironments.includes(configuration.environment)){ + if(localEnvironments.includes(nodeEnv)){ configuration.local = true; }; const props = { From 5d3fe719b32e0fd45922ad6002c312628c31c322 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Wed, 13 Apr 2022 14:26:25 +1200 Subject: [PATCH 09/14] Remove errant semicolon Co-authored-by: Trevor Buckner --- client/homebrew/navbar/account.navitem.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/homebrew/navbar/account.navitem.jsx b/client/homebrew/navbar/account.navitem.jsx index a1a739005..3640a81a5 100644 --- a/client/homebrew/navbar/account.navitem.jsx +++ b/client/homebrew/navbar/account.navitem.jsx @@ -31,7 +31,7 @@ const Account = createClass({ localLogin : async function(){ const username = prompt('Enter username:'); - if(!username) {return;}; + if(!username) {return;} const expiry = new Date; expiry.setFullYear(expiry.getFullYear() + 1); From 0999125678973139c67db9275f0c35dc022e15e2 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Wed, 13 Apr 2022 14:30:49 +1200 Subject: [PATCH 10/14] Update client/homebrew/navbar/account.navitem.jsx Co-authored-by: Trevor Buckner --- client/homebrew/navbar/account.navitem.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/homebrew/navbar/account.navitem.jsx b/client/homebrew/navbar/account.navitem.jsx index 5ee33b3d0..67b5831ad 100644 --- a/client/homebrew/navbar/account.navitem.jsx +++ b/client/homebrew/navbar/account.navitem.jsx @@ -50,7 +50,7 @@ const Account = createClass({ return response.body; }) .catch((err)=>{ - return null; + console.warn(err); }); if(!token) return; From 6fdd415fcbd9fd75e0e87d6bbe7baa89da68a675 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Wed, 13 Apr 2022 14:37:06 +1200 Subject: [PATCH 11/14] Update server/app.js Co-authored-by: Trevor Buckner --- server/app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/app.js b/server/app.js index ae63cd029..4451e89b7 100644 --- a/server/app.js +++ b/server/app.js @@ -259,8 +259,8 @@ app.get('/print/:id', asyncHandler(async (req, res, next)=>{ return next(); })); -const localEnvironments = config.get('local_environments'); const nodeEnv = config.get('node_env'); +const isLocalEnvironment = config.get('local_environments').includes(nodeEnv); // Login app.post('/login', (req, res)=>{ // Local only From 84bc3d0be2509dea1fcf8da956138f53fefb2f11 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Wed, 13 Apr 2022 14:37:11 +1200 Subject: [PATCH 12/14] Update server/app.js Co-authored-by: Trevor Buckner --- server/app.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/server/app.js b/server/app.js index 4451e89b7..3a40368ac 100644 --- a/server/app.js +++ b/server/app.js @@ -264,7 +264,8 @@ const isLocalEnvironment = config.get('local_environments').includes(nodeEnv); // Login app.post('/login', (req, res)=>{ // Local only - if(!localEnvironments.includes(nodeEnv)){ + if(!isLocalEnvironment){ + return; } From 3787cdf11c516654eec88d78aaee96f5df6c44fe Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Wed, 13 Apr 2022 14:39:27 +1200 Subject: [PATCH 13/14] Update server/app.js Co-authored-by: Trevor Buckner --- server/app.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/server/app.js b/server/app.js index 3a40368ac..1e17424f2 100644 --- a/server/app.js +++ b/server/app.js @@ -283,13 +283,9 @@ const templateFn = require('./../client/template.js'); app.use((req, res)=>{ // Create configuration object const configuration = { - local : false, + local : isLocalEnvironment, environment : nodeEnv }; - // Add local only items to configuration object - if(localEnvironments.includes(nodeEnv)){ - configuration.local = true; - }; const props = { version : require('./../package.json').version, publicUrl : config.get('publicUrl') ?? '', From de977b3b94def2f6f7834d1540a1b5a452673dad Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Wed, 13 Apr 2022 14:49:16 +1200 Subject: [PATCH 14/14] Make local login route conditional and rename --- client/homebrew/navbar/account.navitem.jsx | 2 +- server/app.js | 23 ++++++++++------------ 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/client/homebrew/navbar/account.navitem.jsx b/client/homebrew/navbar/account.navitem.jsx index 67b5831ad..7ac687a41 100644 --- a/client/homebrew/navbar/account.navitem.jsx +++ b/client/homebrew/navbar/account.navitem.jsx @@ -44,7 +44,7 @@ const Account = createClass({ const expiry = new Date; expiry.setFullYear(expiry.getFullYear() + 1); - const token = await request.post('/login') + const token = await request.post('/local/login') .send({ username }) .then((response)=>{ return response.body; diff --git a/server/app.js b/server/app.js index 1e17424f2..381113a4a 100644 --- a/server/app.js +++ b/server/app.js @@ -261,20 +261,17 @@ app.get('/print/:id', asyncHandler(async (req, res, next)=>{ const nodeEnv = config.get('node_env'); const isLocalEnvironment = config.get('local_environments').includes(nodeEnv); -// Login -app.post('/login', (req, res)=>{ - // Local only - if(!isLocalEnvironment){ +// Local only +if(isLocalEnvironment){ + // Login + app.post('/local/login', (req, res)=>{ + const username = req.body.username; + if(!username) return; - return; - } - - const username = req.body.username; - if(!username) return; - - const payload = jwt.encode({ username: username, issued: new Date }, config.get('secret')); - return res.json(payload); -}); + const payload = jwt.encode({ username: username, issued: new Date }, config.get('secret')); + return res.json(payload); + }); +}