embark-area-51/lib/modules/authenticator/index.js

31 lines
894 B
JavaScript
Raw Normal View History

2018-09-06 15:30:06 +00:00
const uuid = require('uuid/v1');
class Authenticator {
constructor(embark, _options) {
this.authToken = uuid();
2018-09-06 17:24:39 +00:00
embark.events.once('outputDone', () => {
2018-09-06 15:30:06 +00:00
embark.logger.info(__('Access the web backend with the following url: %s',
2018-09-06 17:24:39 +00:00
('http://localhost:8000/embark?token=' + this.authToken).underline));
2018-09-06 15:30:06 +00:00
});
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));
return res.status(403).send({error: __('Wrong authentication token')});
}
res.send();
}
);
}
}
module.exports = Authenticator;