captcha, db check

This commit is contained in:
Kaushik 2023-01-23 22:52:04 +05:30
parent 1f0b049cb7
commit af932c038d
3 changed files with 48 additions and 1 deletions

1
keys/README.md Normal file
View File

@ -0,0 +1 @@
## Keys - public/private key pairs for certificate QR signing and verification

36
middleware/captcha.js Normal file
View File

@ -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
}

View File

@ -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)