add console command to get token

This commit is contained in:
Jonathan Rainville 2018-09-06 13:33:15 -04:00 committed by Pascal Precht
parent faf09b7d39
commit e68feb81af
No known key found for this signature in database
GPG Key ID: 0EE28D8D6FD85D7D
1 changed files with 18 additions and 5 deletions

View File

@ -4,26 +4,39 @@ class Authenticator {
constructor(embark, _options) { constructor(embark, _options) {
this.authToken = uuid(); this.authToken = uuid();
this.embark = embark;
this.logger = embark.logger;
this.registerCalls();
embark.events.once('outputDone', () => { embark.events.once('outputDone', () => {
embark.logger.info(__('Access the web backend with the following url: %s', embark.logger.info(__('Access the web backend with the following url: %s',
('http://localhost:8000/embark?token=' + this.authToken).underline)); ('http://localhost:8000/embark?token=' + this.authToken).underline));
}); });
}
embark.registerAPICall( registerCalls() {
this.embark.registerAPICall(
'post', 'post',
'/embark-api/authenticate', '/embark-api/authenticate',
(req, res) => { (req, res) => {
if (req.body.token !== this.authToken) { if (req.body.token !== this.authToken) {
embark.logger.warn(__('Someone tried and failed to authenticate to the backend')); this.logger.warn(__('Someone tried and failed to authenticate to the backend'));
embark.logger.warn(__('- User-Agent: %s', req.headers['user-agent'])); this.logger.warn(__('- User-Agent: %s', req.headers['user-agent']));
embark.logger.warn(__('- Referer: %s', req.headers.referer)); this.logger.warn(__('- Referer: %s', req.headers.referer));
return res.status(403).send({error: __('Wrong authentication token')}); return res.status(403).send({error: __('Wrong authentication token')});
} }
res.send(); res.send();
} }
); );
this.embark.registerConsoleCommand((cmd, _options) => {
return {
match: () => cmd === "token",
process: (callback) => {
callback(null, __('Your authorisation token: %s', this.authToken));
}
};
});
} }
} }