From e68feb81afe26f2f8f5ac79d17f725fffef40105 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Thu, 6 Sep 2018 13:33:15 -0400 Subject: [PATCH] add console command to get token --- lib/modules/authenticator/index.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/modules/authenticator/index.js b/lib/modules/authenticator/index.js index abf28a5f..0ae53e70 100644 --- a/lib/modules/authenticator/index.js +++ b/lib/modules/authenticator/index.js @@ -4,26 +4,39 @@ class Authenticator { constructor(embark, _options) { this.authToken = uuid(); + this.embark = embark; + this.logger = embark.logger; - + this.registerCalls(); embark.events.once('outputDone', () => { embark.logger.info(__('Access the web backend with the following url: %s', ('http://localhost:8000/embark?token=' + this.authToken).underline)); }); + } - embark.registerAPICall( + registerCalls() { + this.embark.registerAPICall( 'post', '/embark-api/authenticate', (req, res) => { if (req.body.token !== this.authToken) { - embark.logger.warn(__('Someone tried and failed to authenticate to the backend')); - embark.logger.warn(__('- User-Agent: %s', req.headers['user-agent'])); - embark.logger.warn(__('- Referer: %s', req.headers.referer)); + this.logger.warn(__('Someone tried and failed to authenticate to the backend')); + this.logger.warn(__('- User-Agent: %s', req.headers['user-agent'])); + this.logger.warn(__('- Referer: %s', req.headers.referer)); return res.status(403).send({error: __('Wrong authentication token')}); } res.send(); } ); + + this.embark.registerConsoleCommand((cmd, _options) => { + return { + match: () => cmd === "token", + process: (callback) => { + callback(null, __('Your authorisation token: %s', this.authToken)); + } + }; + }); } }