commit
9fab5882f9
11
.env.exemple
Normal file
11
.env.exemple
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
# Configuration OVH
|
||||||
|
OVH_APP_KEY=
|
||||||
|
OVH_APP_SECRET=
|
||||||
|
OVH_CONSUMER_KEY=
|
||||||
|
OVH_SMS_SERVICE_NAME=
|
||||||
|
|
||||||
|
# Configuration SMS Factor
|
||||||
|
SMS_FACTOR_TOKEN=
|
||||||
|
|
||||||
|
# Configuration serveur
|
||||||
|
PORT=
|
@ -9,8 +9,10 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
|
"dotenv": "^17.2.0",
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
"node-fetch": "^2.6.7"
|
"node-fetch": "^2.6.7",
|
||||||
|
"ovh": "^2.0.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"nodemon": "^3.0.1"
|
"nodemon": "^3.0.1"
|
||||||
|
216
src/server.js
216
src/server.js
@ -1,6 +1,8 @@
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const cors = require('cors');
|
const cors = require('cors');
|
||||||
const fetch = require('node-fetch');
|
const fetch = require('node-fetch');
|
||||||
|
const ovh = require('ovh');
|
||||||
|
require('dotenv').config();
|
||||||
|
|
||||||
// Initialisation de l'application Express
|
// Initialisation de l'application Express
|
||||||
const app = express();
|
const app = express();
|
||||||
@ -174,6 +176,220 @@ app.post('/api/v1/idnot/user/:code', async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//------------------------------------ SMS Section ------------------------------------
|
||||||
|
|
||||||
|
const config = {
|
||||||
|
// OVH config
|
||||||
|
OVH_APP_KEY: process.env.OVH_APP_KEY,
|
||||||
|
OVH_APP_SECRET: process.env.OVH_APP_SECRET,
|
||||||
|
OVH_CONSUMER_KEY: process.env.OVH_CONSUMER_KEY,
|
||||||
|
OVH_SMS_SERVICE_NAME: process.env.OVH_SMS_SERVICE_NAME,
|
||||||
|
|
||||||
|
// SMS Factor config
|
||||||
|
SMS_FACTOR_TOKEN: process.env.SMS_FACTOR_TOKEN,
|
||||||
|
|
||||||
|
PORT: process.env.PORT || 8080
|
||||||
|
};
|
||||||
|
|
||||||
|
// Codes storage
|
||||||
|
const verificationCodes = new Map();
|
||||||
|
|
||||||
|
// Service SMS
|
||||||
|
class SmsService {
|
||||||
|
static generateCode() {
|
||||||
|
return Math.floor(100000 + Math.random() * 900000);
|
||||||
|
}
|
||||||
|
|
||||||
|
// OVH Service
|
||||||
|
static sendSmsWithOvh(phoneNumber, message) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const ovhClient = ovh({
|
||||||
|
appKey: config.OVH_APP_KEY,
|
||||||
|
appSecret: config.OVH_APP_SECRET,
|
||||||
|
consumerKey: config.OVH_CONSUMER_KEY
|
||||||
|
});
|
||||||
|
|
||||||
|
ovhClient.request('POST', `/sms/${config.OVH_SMS_SERVICE_NAME}/jobs`, {
|
||||||
|
message: message,
|
||||||
|
receivers: [phoneNumber],
|
||||||
|
senderForResponse: false,
|
||||||
|
sender: "not.IT Fact",
|
||||||
|
noStopClause: true
|
||||||
|
}, (error, result) => {
|
||||||
|
if (error) {
|
||||||
|
console.error('Erreur OVH SMS:', error);
|
||||||
|
resolve({ success: false, error: 'Échec de l\'envoi du SMS via OVH' });
|
||||||
|
} else {
|
||||||
|
resolve({ success: true });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// SMS Factor Service
|
||||||
|
static async sendSmsWithSmsFactor(phoneNumber, message) {
|
||||||
|
try {
|
||||||
|
const url = new URL('https://api.smsfactor.com/send/simulate');
|
||||||
|
url.searchParams.append('to', phoneNumber);
|
||||||
|
url.searchParams.append('text', message);
|
||||||
|
url.searchParams.append('sender', 'LeCoffre');
|
||||||
|
url.searchParams.append('token', config.SMS_FACTOR_TOKEN);
|
||||||
|
|
||||||
|
const response = await fetch(url.toString());
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`HTTP error! status: ${response.status}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return { success: true };
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Erreur SMS Factor:', error);
|
||||||
|
return { success: false, error: 'Échec de l\'envoi du SMS via SMS Factor' };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Main method
|
||||||
|
static async sendSms(phoneNumber, message) {
|
||||||
|
// Try first with OVH
|
||||||
|
const ovhResult = await this.sendSmsWithOvh(phoneNumber, message);
|
||||||
|
|
||||||
|
if (ovhResult.success) {
|
||||||
|
return ovhResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If OVH fails, try with SMS Factor
|
||||||
|
console.log('OVH SMS failed, trying SMS Factor...');
|
||||||
|
return await this.sendSmsWithSmsFactor(phoneNumber, message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Phone number validation middleware
|
||||||
|
const validatePhoneNumber = (req, res, next) => {
|
||||||
|
const { phoneNumber } = req.body;
|
||||||
|
|
||||||
|
if (!phoneNumber) {
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
message: 'Le numéro de téléphone est requis'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validation basique du format
|
||||||
|
const phoneRegex = /^\+?[1-9]\d{1,14}$/;
|
||||||
|
if (!phoneRegex.test(phoneNumber)) {
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
message: 'Format de numéro de téléphone invalide'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
};
|
||||||
|
|
||||||
|
// Routes
|
||||||
|
app.post('/api/send-code', validatePhoneNumber, async (req, res) => {
|
||||||
|
const { phoneNumber } = req.body;
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Check if a code already exists and is not expired
|
||||||
|
const existingVerification = verificationCodes.get(phoneNumber);
|
||||||
|
if (existingVerification) {
|
||||||
|
const timeSinceLastSend = Date.now() - existingVerification.timestamp;
|
||||||
|
if (timeSinceLastSend < 30000) { // 30 secondes
|
||||||
|
return res.status(429).json({
|
||||||
|
success: false,
|
||||||
|
message: 'Veuillez attendre 30 secondes avant de demander un nouveau code'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate a new code
|
||||||
|
const code = SmsService.generateCode();
|
||||||
|
|
||||||
|
// Store the code
|
||||||
|
verificationCodes.set(phoneNumber, {
|
||||||
|
code,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
attempts: 0
|
||||||
|
});
|
||||||
|
|
||||||
|
// Send the SMS
|
||||||
|
const message = `Votre code de vérification LeCoffre est : ${code}`;
|
||||||
|
const result = await SmsService.sendSms(phoneNumber, message);
|
||||||
|
|
||||||
|
if (result.success) {
|
||||||
|
res.json({
|
||||||
|
success: true,
|
||||||
|
message: 'Code envoyé avec succès',
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
message: 'Échec de l\'envoi du SMS via les deux fournisseurs'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Erreur:', error);
|
||||||
|
res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
message: 'Erreur serveur lors de l\'envoi du code'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.post('/api/verify-code', validatePhoneNumber, (req, res) => {
|
||||||
|
const { phoneNumber, code } = req.body;
|
||||||
|
|
||||||
|
if (!code) {
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
message: 'Le code est requis'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const verification = verificationCodes.get(phoneNumber);
|
||||||
|
|
||||||
|
if (!verification) {
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
message: 'Aucun code n\'a été envoyé à ce numéro'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the code has not expired (5 minutes)
|
||||||
|
if (Date.now() - verification.timestamp > 5 * 60 * 1000) {
|
||||||
|
verificationCodes.delete(phoneNumber);
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
message: 'Le code a expiré'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the code is correct
|
||||||
|
if (verification.code.toString() === code.toString()) {
|
||||||
|
verificationCodes.delete(phoneNumber);
|
||||||
|
res.json({
|
||||||
|
success: true,
|
||||||
|
message: 'Code vérifié avec succès'
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
verification.attempts += 1;
|
||||||
|
|
||||||
|
if (verification.attempts >= 3) {
|
||||||
|
verificationCodes.delete(phoneNumber);
|
||||||
|
res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
message: 'Trop de tentatives. Veuillez demander un nouveau code'
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
message: 'Code incorrect'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
app.listen(PORT, () => {
|
app.listen(PORT, () => {
|
||||||
console.log(`Server is running on port ${PORT}`);
|
console.log(`Server is running on port ${PORT}`);
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user