-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* up api * hotifx * add app logic * Update hmac.js * Update index.js
- Loading branch information
Showing
14 changed files
with
470 additions
and
1,203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,10 +26,12 @@ | |
"bcryptjs": "^2.4.3", | ||
"cors": "^2.8.5", | ||
"cross-env": "^7.0.3", | ||
"crypto-js": "^4.2.0", | ||
"date-fns": "^2.30.0", | ||
"date-fns-tz": "^1.3.7", | ||
"dotenv": "^16.3.1", | ||
"express": "^4.18.2", | ||
"express-rate-limit": "^7.5.0", | ||
"helmet": "^4.0.0", | ||
"morgan": "^1.10.0", | ||
"node-cron": "^3.0.2", | ||
|
@@ -46,4 +48,4 @@ | |
"node": ">= 14" | ||
}, | ||
"packageManager": "[email protected]" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,16 +4,20 @@ const { TIPIMAIL_API_USER, TIPIMAIL_API_KEY, ENVIRONMENT } = require("../config" | |
const { catchErrors } = require("../middlewares/errors"); | ||
const router = express.Router(); | ||
const { capture } = require("../third-parties/sentry"); | ||
const { validateHMAC } = require("../middlewares/hmac"); | ||
const { mailLimiter } = require("../middlewares/rateLimit"); | ||
|
||
router.post( | ||
"/", | ||
validateHMAC, | ||
mailLimiter, | ||
catchErrors(async (req, res) => { | ||
let { to, replyTo, replyToName, subject, text, html } = req.body || {}; | ||
|
||
if (!subject || (!text && !html)) return res.status(400).json({ ok: false, error: "wrong parameters" }); | ||
|
||
if (!to) { | ||
to = ENVIRONMENT === "development" ? "[email protected]" : "[email protected]"; | ||
to = ENVIRONMENT === "development" ? process.env.MAIL_TO_DEV : "[email protected]"; | ||
} | ||
|
||
if (!replyTo) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const crypto = require("crypto"); | ||
const { HMAC_SECRET } = require("../config"); | ||
|
||
const validateHMAC = (req, res, next) => { | ||
const secret = HMAC_SECRET; | ||
if (!secret) { | ||
return next(); | ||
} | ||
const { "x-signature": signature, "x-timestamp": timestamp } = req.headers; | ||
|
||
if (!signature || !timestamp) { | ||
return res.status(400).json({ error: "Missing signature or timestamp" }); | ||
} | ||
|
||
const now = Date.now(); | ||
if (Math.abs(now - timestamp) > 5 * 60 * 1000) { | ||
// Vérifie un délai de 5 minutes | ||
return res.status(400).json({ error: "Timestamp expired" }); | ||
} | ||
|
||
const payload = JSON.stringify(req.body); | ||
const dataToSign = `${timestamp}:${payload}`; | ||
const expectedSignature = crypto.createHmac("sha256", secret).update(dataToSign).digest("hex"); | ||
|
||
if (signature !== expectedSignature) { | ||
return res.status(401).json({ error: "Invalid signature" }); | ||
} | ||
|
||
next(); | ||
}; | ||
|
||
module.exports = { validateHMAC }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const rateLimit = require("express-rate-limit"); | ||
|
||
// Middleware de rate limiting pour la route /reminder | ||
const reminderLimiter = rateLimit({ | ||
windowMs: 15 * 60 * 1000, // Fenêtre de 15 minutes | ||
max: 10, // Maximum de 10 requêtes dans cette période | ||
keyGenerator: (req) => req.body.pushNotifToken || req.ip, // Limite basée sur pushNotifToken ou IP | ||
message: { | ||
ok: false, | ||
error: "Too many requests. Please try again later.", | ||
}, | ||
}); | ||
|
||
// Middleware de rate limiting pour la route /mail | ||
const mailLimiter = rateLimit({ | ||
windowMs: 1 * 60 * 1000, // Fenêtre de 1 minute | ||
max: 5, // Maximum de 5 mails par minute | ||
keyGenerator: (req) => req.ip, // Limite basée sur l'IP uniquement | ||
message: { | ||
ok: false, | ||
error: "Too many emails sent. Please wait a moment and try again.", | ||
}, | ||
}); | ||
|
||
module.exports = { reminderLimiter, mailLimiter }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,12 @@ | ||
// import envConfig from "react-native-config"; | ||
import {version, buildNumber} from '../package.json'; | ||
|
||
// const SCHEME = envConfig.SCHEME; | ||
// const HOST = envConfig.HOST; | ||
// const APP_ENV = envConfig.APP_ENV; | ||
const SCHEME = process.env.EXPO_PUBLIC_SCHEME; | ||
const HOST = process.env.EXPO_PUBLIC_HOST; | ||
const APP_ENV = process.env.EXPO_PUBLIC_APP_ENV; | ||
const VERSION = version; | ||
const BUILD_NUMBER = buildNumber; | ||
// const TIPIMAIL_API_KEY = envConfig.TIPIMAIL_API_KEY; | ||
// const TIPIMAIL_API_USER = envConfig.TIPIMAIL_API_USER; | ||
|
||
export {SCHEME, HOST, APP_ENV, VERSION, BUILD_NUMBER}; | ||
const HMAC_SECRET = process.env.EXPO_PUBLIC_HMAC_SECRET; | ||
|
||
export {SCHEME, HOST, APP_ENV, VERSION, BUILD_NUMBER, HMAC_SECRET}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import crypto from "crypto-js"; | ||
import { HMAC_SECRET } from "../config"; | ||
|
||
export const generateHMAC = (payload) => { | ||
const timestamp = Date.now().toString(); | ||
const dataToSign = `${timestamp}:${JSON.stringify(payload)}`; | ||
const signature = crypto.HmacSHA256(dataToSign, HMAC_SECRET).toString(); | ||
|
||
return { signature, timestamp }; | ||
}; |
Oops, something went wrong.