Sms factor

This commit is contained in:
Vins 2023-11-30 11:40:04 +01:00
parent aa85dab287
commit c0cfb8aec7
5 changed files with 78 additions and 50 deletions

View File

@ -46,6 +46,7 @@
"@pinata/sdk": "^2.1.0", "@pinata/sdk": "^2.1.0",
"@prisma/client": "^4.11.0", "@prisma/client": "^4.11.0",
"adm-zip": "^0.5.10", "adm-zip": "^0.5.10",
"axios": "^1.6.2",
"bcrypt": "^5.1.1", "bcrypt": "^5.1.1",
"class-transformer": "^0.5.1", "class-transformer": "^0.5.1",
"class-validator": "^0.14.0", "class-validator": "^0.14.0",

View File

@ -121,6 +121,9 @@ export class BackendVariables {
@IsNotEmpty() @IsNotEmpty()
public readonly OVH_CONSUMER_KEY!: string; public readonly OVH_CONSUMER_KEY!: string;
@IsNotEmpty()
public readonly SMS_FACTOR_TOKEN!: string;
public constructor() { public constructor() {
dotenv.config(); dotenv.config();
this.DATABASE_PORT = process.env["DATABASE_PORT"]!; this.DATABASE_PORT = process.env["DATABASE_PORT"]!;
@ -162,6 +165,7 @@ export class BackendVariables {
this.OVH_APP_KEY = process.env["OVH_APP_KEY"]!; this.OVH_APP_KEY = process.env["OVH_APP_KEY"]!;
this.OVH_APP_SECRET = process.env["OVH_APP_SECRET"]!; this.OVH_APP_SECRET = process.env["OVH_APP_SECRET"]!;
this.OVH_CONSUMER_KEY = process.env["OVH_CONSUMER_KEY"]!; this.OVH_CONSUMER_KEY = process.env["OVH_CONSUMER_KEY"]!;
this.SMS_FACTOR_TOKEN = process.env["SMS_FACTOR_TOKEN"]!;
} }

View File

@ -4,40 +4,40 @@ import { Service } from "typedi";
@Service() @Service()
export default class OvhService extends BaseService { export default class OvhService extends BaseService {
constructor(private variables: BackendVariables) {
constructor(private variables: BackendVariables) {
super(); super();
} }
/** public async sendSms(phoneNumber: string, message: string): Promise<boolean> {
* @description : Get all Customers const ovh = require("ovh")({
* @throws {Error} If Customers cannot be get appKey: this.variables.OVH_APP_KEY,
*/ appSecret: this.variables.OVH_APP_SECRET,
public async sendSms(phoneNumber: string, message: string): Promise<void> { consumerKey: this.variables.OVH_CONSUMER_KEY,
});
const ovh = require('ovh')({ ovh.request("GET", "/sms", function (err: any, serviceName: string) {
appKey: this.variables.OVH_APP_KEY, if (err) {
appSecret: this.variables.OVH_APP_SECRET, console.log(err, serviceName);
consumerKey: this.variables.OVH_CONSUMER_KEY, return false;
}); } else {
console.log("My account SMS is " + serviceName);
// Get the serviceName (name of your SMS account) // Send a simple SMS with a short number using your serviceName
ovh.request('GET', '/sms', function (err: any, serviceName: string) { ovh.request(
if(err) { "POST",
console.log(err, serviceName); "/sms/" + serviceName + "/jobs",
} {
else { message: message,
console.log("My account SMS is " + serviceName); senderForResponse: true,
receivers: [phoneNumber],
// Send a simple SMS with a short number using your serviceName },
ovh.request('POST', '/sms/' + serviceName + '/jobs', { function (errsend: any, result: any) {
message: message, console.log(errsend, result);
senderForResponse: true, },
receivers: [phoneNumber] );
}, function (errsend: any, result: any) { return true;
console.log(errsend, result); }
}); });
} return false;
}); }
}
} }

View File

@ -0,0 +1,29 @@
import { BackendVariables } from "@Common/config/variables/Variables";
import BaseService from "@Services/BaseService";
import { Service } from "typedi";
import axios from "axios";
@Service()
export default class SmsFactorService extends BaseService {
constructor(private variables: BackendVariables) {
super();
}
public async sendSms(phoneNumber: string, message: string): Promise<boolean> {
axios.post('https://api.smsfactor.com/send/', {
token: this.variables.SMS_FACTOR_TOKEN,
sender: "LeCoffre",
to: phoneNumber,
text: message,
}).then(response => {
console.log('SMS sent successfully:', response.data);
return true;
})
.catch(error => {
console.error('Error sending SMS:', error.response.data);
return false;
});
return false;
}
}

View File

@ -8,6 +8,7 @@ import TotpCodes, { TotpCodesReasons } from "le-coffre-resources/dist/Customer/T
import { Customer } from "le-coffre-resources/dist/Notary"; import { Customer } from "le-coffre-resources/dist/Notary";
import { Service } from "typedi"; import { Service } from "typedi";
import OvhService from "@Services/common/OvhService/OvhService"; import OvhService from "@Services/common/OvhService/OvhService";
import SmsFactorService from "@Services/common/SmsFactorService/SmsFactorService";
export class SmsNotExpiredError extends Error { export class SmsNotExpiredError extends Error {
constructor() { constructor() {
@ -58,6 +59,7 @@ export default class CustomersService extends BaseService {
private totpCodesRepository: TotpCodesRepository, private totpCodesRepository: TotpCodesRepository,
private variables: BackendVariables, private variables: BackendVariables,
private ovhService: OvhService, private ovhService: OvhService,
private smsFactorService: SmsFactorService,
) { ) {
super(); super();
} }
@ -345,25 +347,17 @@ export default class CustomersService extends BaseService {
} }
private async sendSmsCodeToCustomer(totpPin: number, customer: Customer) { private async sendSmsCodeToCustomer(totpPin: number, customer: Customer) {
try { // Sélectionnez le fournisseur de SMS en fonction de la variable d'environnement
// Sélectionnez le fournisseur de SMS en fonction de la variable d'environnement const selectedProvider = this.variables.SMS_PROVIDER === "OVH" ? this.ovhService : this.smsFactorService;
const selectedProvider = this.variables.SMS_PROVIDER === 'OVH' ? this.ovhService : this.ovhService;
// Envoi du SMS // Envoi du SMS
if(!customer.contact?.phone_number) return false; if (!customer.contact?.phone_number) return;
let success = await selectedProvider.sendSms(customer.contact?.phone_number, totpPin.toString()); let success = await selectedProvider.sendSms(customer.contact?.phone_number, totpPin.toString());
// Si l'envoi échoue, basculez automatiquement sur le second fournisseur // Si l'envoi échoue, basculez automatiquement sur le second fournisseur
// if (!success) { if (!success) {
// const alternateProvider = this.variables.SMS_PROVIDER === 'OVH' ? this.smsService2 : this.ovhService; const alternateProvider = this.variables.SMS_PROVIDER === "OVH" ? this.smsFactorService : this.ovhService;
// success = await alternateProvider.sendSms(customer.contact?.phone_number, totpPin); success = await alternateProvider.sendSms(customer.contact?.phone_number, totpPin.toString());
// }
return success;
} catch (error) {
console.error(`Erreur lors de l'envoi du SMS : ${error}`);
return false;
} }
} }