Compare commits
6 Commits
93b86a6b7a
...
9ab737b8c7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9ab737b8c7
|
||
|
|
b3035eb2ab
|
||
|
|
a6fdbdb06d
|
||
|
|
48b233eae4
|
||
|
|
89bd9d65e7
|
||
|
|
8cf1699717
|
36
.env.example
Normal file
36
.env.example
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
# Global
|
||||||
|
NODE_ENV=development
|
||||||
|
|
||||||
|
# Backend
|
||||||
|
BACKEND_PORT=3001
|
||||||
|
|
||||||
|
# Frontend
|
||||||
|
FRONTEND_PORT=3000
|
||||||
|
|
||||||
|
# Database (PostgreSQL)
|
||||||
|
POSTGRES_HOST=localhost
|
||||||
|
POSTGRES_PORT=5432
|
||||||
|
POSTGRES_DB=memegoat
|
||||||
|
POSTGRES_USER=app
|
||||||
|
POSTGRES_PASSWORD=app
|
||||||
|
|
||||||
|
# Storage (S3/MinIO) - À configurer lors de l'implémentation
|
||||||
|
# S3_ENDPOINT=localhost
|
||||||
|
# S3_PORT=9000
|
||||||
|
# S3_ACCESS_KEY=
|
||||||
|
# S3_SECRET_KEY=
|
||||||
|
# S3_BUCKET=memegoat
|
||||||
|
|
||||||
|
# Security (PGP & Auth) - À configurer lors de l'implémentation
|
||||||
|
# PGP_PASSPHRASE=
|
||||||
|
JWT_SECRET=super-secret-key-change-me-in-production
|
||||||
|
ENCRYPTION_KEY=another-super-secret-key-32-chars
|
||||||
|
|
||||||
|
# Mail
|
||||||
|
MAIL_HOST=localhost
|
||||||
|
MAIL_PORT=1025
|
||||||
|
MAIL_SECURE=false
|
||||||
|
MAIL_USER=user
|
||||||
|
MAIL_PASS=password
|
||||||
|
MAIL_FROM=noreply@memegoat.fr
|
||||||
|
DOMAIN_NAME=memegoat.fr
|
||||||
@@ -24,6 +24,7 @@
|
|||||||
"db:studio": "drizzle-kit studio"
|
"db:studio": "drizzle-kit studio"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@nestjs-modules/mailer": "^2.0.2",
|
||||||
"@nestjs/common": "^11.0.1",
|
"@nestjs/common": "^11.0.1",
|
||||||
"@nestjs/config": "^4.0.2",
|
"@nestjs/config": "^4.0.2",
|
||||||
"@nestjs/core": "^11.0.1",
|
"@nestjs/core": "^11.0.1",
|
||||||
@@ -34,6 +35,7 @@
|
|||||||
"drizzle-orm": "^0.45.1",
|
"drizzle-orm": "^0.45.1",
|
||||||
"jose": "^6.1.3",
|
"jose": "^6.1.3",
|
||||||
"minio": "^8.0.6",
|
"minio": "^8.0.6",
|
||||||
|
"nodemailer": "^7.0.12",
|
||||||
"pg": "^8.16.3",
|
"pg": "^8.16.3",
|
||||||
"reflect-metadata": "^0.2.2",
|
"reflect-metadata": "^0.2.2",
|
||||||
"rxjs": "^7.8.1"
|
"rxjs": "^7.8.1"
|
||||||
@@ -45,6 +47,7 @@
|
|||||||
"@types/express": "^5.0.0",
|
"@types/express": "^5.0.0",
|
||||||
"@types/jest": "^30.0.0",
|
"@types/jest": "^30.0.0",
|
||||||
"@types/node": "^22.10.7",
|
"@types/node": "^22.10.7",
|
||||||
|
"@types/nodemailer": "^7.0.4",
|
||||||
"@types/pg": "^8.16.0",
|
"@types/pg": "^8.16.0",
|
||||||
"@types/supertest": "^6.0.2",
|
"@types/supertest": "^6.0.2",
|
||||||
"drizzle-kit": "^0.31.8",
|
"drizzle-kit": "^0.31.8",
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { AppController } from "./app.controller";
|
|||||||
import { AppService } from "./app.service";
|
import { AppService } from "./app.service";
|
||||||
import { CryptoModule } from "./crypto/crypto.module";
|
import { CryptoModule } from "./crypto/crypto.module";
|
||||||
import { DatabaseModule } from "./database/database.module";
|
import { DatabaseModule } from "./database/database.module";
|
||||||
|
import { MailModule } from "./mail/mail.module";
|
||||||
import { S3Module } from "./s3/s3.module";
|
import { S3Module } from "./s3/s3.module";
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
@@ -11,6 +12,7 @@ import { S3Module } from "./s3/s3.module";
|
|||||||
DatabaseModule,
|
DatabaseModule,
|
||||||
CryptoModule,
|
CryptoModule,
|
||||||
S3Module,
|
S3Module,
|
||||||
|
MailModule,
|
||||||
ConfigModule.forRoot({
|
ConfigModule.forRoot({
|
||||||
isGlobal: true,
|
isGlobal: true,
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -7,11 +7,13 @@ jest.mock("@noble/post-quantum/ml-kem.js", () => ({
|
|||||||
publicKey: new Uint8Array(1184),
|
publicKey: new Uint8Array(1184),
|
||||||
secretKey: new Uint8Array(2400),
|
secretKey: new Uint8Array(2400),
|
||||||
})),
|
})),
|
||||||
encapsulate: jest.fn((pk: Uint8Array) => ({
|
encapsulate: jest.fn((_pk: Uint8Array) => ({
|
||||||
cipherText: new Uint8Array(1088),
|
cipherText: new Uint8Array(1088),
|
||||||
sharedSecret: new Uint8Array(32),
|
sharedSecret: new Uint8Array(32),
|
||||||
})),
|
})),
|
||||||
decapsulate: jest.fn((ct: Uint8Array, sk: Uint8Array) => new Uint8Array(32)),
|
decapsulate: jest.fn(
|
||||||
|
(_ct: Uint8Array, _sk: Uint8Array) => new Uint8Array(32),
|
||||||
|
),
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -51,7 +53,10 @@ jest.mock("jose", () => ({
|
|||||||
if (jws.includes("tampered") || jws.split(".").length !== 3) {
|
if (jws.includes("tampered") || jws.split(".").length !== 3) {
|
||||||
throw new Error("Tampered or invalid content");
|
throw new Error("Tampered or invalid content");
|
||||||
}
|
}
|
||||||
const payload = jws === "mocked.jws.token" ? "Important document content" : "Original content";
|
const payload =
|
||||||
|
jws === "mocked.jws.token"
|
||||||
|
? "Important document content"
|
||||||
|
: "Original content";
|
||||||
return Promise.resolve({
|
return Promise.resolve({
|
||||||
payload: new TextEncoder().encode(payload),
|
payload: new TextEncoder().encode(payload),
|
||||||
});
|
});
|
||||||
@@ -149,7 +154,7 @@ describe("CryptoService", () => {
|
|||||||
it("should fail to verify tampered content", async () => {
|
it("should fail to verify tampered content", async () => {
|
||||||
const content = "Original content";
|
const content = "Original content";
|
||||||
const jws = await service.signContent(content);
|
const jws = await service.signContent(content);
|
||||||
const parts = jws.split(".");
|
const _parts = jws.split(".");
|
||||||
// Tamper with the payload (middle part)
|
// Tamper with the payload (middle part)
|
||||||
const tamperedJws = "this.is.tampered";
|
const tamperedJws = "this.is.tampered";
|
||||||
|
|
||||||
|
|||||||
29
backend/src/mail/mail.module.ts
Normal file
29
backend/src/mail/mail.module.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import { Module } from "@nestjs/common";
|
||||||
|
import { ConfigService } from "@nestjs/config";
|
||||||
|
import { MailerModule } from "@nestjs-modules/mailer";
|
||||||
|
import { MailService } from "./mail.service";
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
imports: [
|
||||||
|
MailerModule.forRootAsync({
|
||||||
|
useFactory: async (config: ConfigService) => ({
|
||||||
|
transport: {
|
||||||
|
host: config.get("MAIL_HOST"),
|
||||||
|
port: Number(config.get("MAIL_PORT")),
|
||||||
|
secure: config.get("MAIL_SECURE") === "true",
|
||||||
|
auth: {
|
||||||
|
user: config.get("MAIL_USER"),
|
||||||
|
pass: config.get("MAIL_PASS"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
defaults: {
|
||||||
|
from: `"No Reply" <${config.get("MAIL_FROM")}>`,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
inject: [ConfigService],
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
providers: [MailService],
|
||||||
|
exports: [MailService],
|
||||||
|
})
|
||||||
|
export class MailModule {}
|
||||||
87
backend/src/mail/mail.service.spec.ts
Normal file
87
backend/src/mail/mail.service.spec.ts
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
import { Logger } from "@nestjs/common";
|
||||||
|
import { ConfigService } from "@nestjs/config";
|
||||||
|
import { Test, TestingModule } from "@nestjs/testing";
|
||||||
|
import { MailerService } from "@nestjs-modules/mailer";
|
||||||
|
import { MailService } from "./mail.service";
|
||||||
|
|
||||||
|
describe("MailService", () => {
|
||||||
|
let service: MailService;
|
||||||
|
let mailerService: MailerService;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
|
providers: [
|
||||||
|
MailService,
|
||||||
|
{
|
||||||
|
provide: MailerService,
|
||||||
|
useValue: {
|
||||||
|
sendMail: jest.fn().mockResolvedValue({}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
provide: ConfigService,
|
||||||
|
useValue: {
|
||||||
|
get: jest.fn().mockReturnValue("test.io"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}).compile();
|
||||||
|
|
||||||
|
service = module.get<MailService>(MailService);
|
||||||
|
mailerService = module.get<MailerService>(MailerService);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should be defined", () => {
|
||||||
|
expect(service).toBeDefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should send validation email with correct URL", async () => {
|
||||||
|
const email = "test@example.com";
|
||||||
|
const token = "token123";
|
||||||
|
await service.sendEmailValidation(email, token);
|
||||||
|
|
||||||
|
expect(mailerService.sendMail).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining({
|
||||||
|
to: email,
|
||||||
|
subject: "Validation de votre adresse email",
|
||||||
|
html: expect.stringContaining(
|
||||||
|
"https://test.io/api/auth/verify-email?token=token123",
|
||||||
|
),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should send password reset email with correct URL", async () => {
|
||||||
|
const email = "test@example.com";
|
||||||
|
const token = "token123";
|
||||||
|
await service.sendPasswordReset(email, token);
|
||||||
|
|
||||||
|
expect(mailerService.sendMail).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining({
|
||||||
|
to: email,
|
||||||
|
subject: "Réinitialisation de votre mot de passe",
|
||||||
|
html: expect.stringContaining(
|
||||||
|
"https://test.io/api/auth/reset-password?token=token123",
|
||||||
|
),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should throw and log error when sending fails", async () => {
|
||||||
|
const email = "test@example.com";
|
||||||
|
const token = "token123";
|
||||||
|
const error = new Error("SMTP Error");
|
||||||
|
(mailerService.sendMail as jest.Mock).mockRejectedValueOnce(error);
|
||||||
|
|
||||||
|
const loggerSpy = jest
|
||||||
|
.spyOn(Logger.prototype, "error")
|
||||||
|
.mockImplementation(() => {});
|
||||||
|
|
||||||
|
await expect(service.sendEmailValidation(email, token)).rejects.toThrow(
|
||||||
|
"SMTP Error",
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(loggerSpy).toHaveBeenCalled();
|
||||||
|
loggerSpy.mockRestore();
|
||||||
|
});
|
||||||
|
});
|
||||||
62
backend/src/mail/mail.service.ts
Normal file
62
backend/src/mail/mail.service.ts
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
import { Injectable, Logger } from "@nestjs/common";
|
||||||
|
import { ConfigService } from "@nestjs/config";
|
||||||
|
import { MailerService } from "@nestjs-modules/mailer";
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class MailService {
|
||||||
|
private readonly logger = new Logger(MailService.name);
|
||||||
|
private readonly domain: string;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private mailerService: MailerService,
|
||||||
|
private configService: ConfigService,
|
||||||
|
) {
|
||||||
|
this.domain = this.configService.get<string>("DOMAIN_NAME", "memegoat.io");
|
||||||
|
}
|
||||||
|
|
||||||
|
async sendEmailValidation(email: string, token: string) {
|
||||||
|
const url = `https://${this.domain}/api/auth/verify-email?token=${token}`;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await this.mailerService.sendMail({
|
||||||
|
to: email,
|
||||||
|
subject: "Validation de votre adresse email",
|
||||||
|
html: `
|
||||||
|
<p>Bonjour,</p>
|
||||||
|
<p>Veuillez cliquer sur le lien ci-dessous pour valider votre adresse email :</p>
|
||||||
|
<p><a href="${url}">${url}</a></p>
|
||||||
|
<p>Si vous n'avez pas demandé cette validation, vous pouvez ignorer cet email.</p>
|
||||||
|
`,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.error(
|
||||||
|
`Failed to send validation email to ${email}: ${error.message}`,
|
||||||
|
error.stack,
|
||||||
|
);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async sendPasswordReset(email: string, token: string) {
|
||||||
|
const url = `https://${this.domain}/api/auth/reset-password?token=${token}`;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await this.mailerService.sendMail({
|
||||||
|
to: email,
|
||||||
|
subject: "Réinitialisation de votre mot de passe",
|
||||||
|
html: `
|
||||||
|
<p>Bonjour,</p>
|
||||||
|
<p>Veuillez cliquer sur le lien ci-dessous pour réinitialiser votre mot de passe :</p>
|
||||||
|
<p><a href="${url}">${url}</a></p>
|
||||||
|
<p>Si vous n'avez pas demandé de réinitialisation, vous pouvez ignorer cet email.</p>
|
||||||
|
`,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.error(
|
||||||
|
`Failed to send password reset email to ${email}: ${error.message}`,
|
||||||
|
error.stack,
|
||||||
|
);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,6 +8,7 @@ jest.mock("minio");
|
|||||||
describe("S3Service", () => {
|
describe("S3Service", () => {
|
||||||
let service: S3Service;
|
let service: S3Service;
|
||||||
let _configService: ConfigService;
|
let _configService: ConfigService;
|
||||||
|
// biome-ignore lint/suspicious/noExplicitAny: Fine for testing purposes
|
||||||
let minioClient: any;
|
let minioClient: any;
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
|
|||||||
15828
pnpm-lock.yaml
generated
15828
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user