diff --git a/keys/README.md b/keys/README.md new file mode 100644 index 0000000..4481816 --- /dev/null +++ b/keys/README.md @@ -0,0 +1 @@ +## Keys - public/private key pairs for certificate QR signing and verification diff --git a/middleware/captcha.js b/middleware/captcha.js new file mode 100644 index 0000000..489d84c --- /dev/null +++ b/middleware/captcha.js @@ -0,0 +1,36 @@ +const fetch = require("cross-fetch"); + +const typedefs = require("../typedefs"); +const logger = require("../utils/logger")(module); + +/** + * Google ReCAPTCHA v2 verification + * + * @param {typedefs.Req} req + * @param {typedefs.Res} res + * @param {typedefs.Next} next + */ +const verifyCaptcha = async (req, res, next) => { + try { + const secretKey = process.env.CAPTCHA_SECRET; + + const verifyCaptchaURL = `https://google.com/recaptcha/api/siteverify?secret=${secretKey}&response=${req.body.captcha}`; + + const captchaResp = await fetch(verifyCaptchaURL); + const captchaData = await captchaResp.json(); + if (captchaData.success !== undefined && !captchaData.success) { + logger.error("Recaptcha", { captchaData }); + return res.status(403).send({ + message: "Failed captcha verification" + }); + } + next(); + } catch (error) { + logger.error("Error", { error }); + return res.status(500).send({ message: "Server Error. Try again." }); + } +} + +module.exports = { + verifyCaptcha +} \ No newline at end of file diff --git a/models/index.js b/models/index.js index 047dbf0..475acb0 100644 --- a/models/index.js +++ b/models/index.js @@ -2,7 +2,7 @@ const fs = require("fs"); const path = require("path"); const Sequelize = require("sequelize"); - +const logger = require("../utils/logger")(module); const basename = path.basename(__filename); const env = process.env.NODE_ENV || "development"; const config = require(__dirname + "/../config/sequelize.js")[env]; @@ -16,6 +16,16 @@ if (config.use_env_variable) { sequelize = new Sequelize(config.database, config.username, config.password, config); } +sequelize.authenticate() + .then( + () => { + logger.info('Sequelize auth success'); + }, + (err) => { + logger.error('Sequelize auth error', { err }); + } + ) + // Read model definitions from folder fs .readdirSync(__dirname)