Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a90aba2748
|
||
|
|
3f0b1e5119
|
||
|
|
aff8acebf8
|
||
|
|
a721b4041c
|
||
|
|
f4a1a2f4df
|
||
|
|
0548c418c7
|
||
|
|
dd0a9e620b
|
||
|
|
7e7b19fe9f
|
||
|
|
57bc51290b
|
@@ -83,7 +83,7 @@ jobs:
|
||||
|
||||
- name: Déployer avec Docker Compose
|
||||
run: |
|
||||
docker compose -f docker-compose.prod.yml up -d --build
|
||||
docker compose -f docker-compose.prod.yml up -d --build --remove-orphans
|
||||
env:
|
||||
BACKEND_PORT: ${{ secrets.BACKEND_PORT }}
|
||||
FRONTEND_PORT: ${{ secrets.FRONTEND_PORT }}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@memegoat/backend",
|
||||
"version": "1.1.0",
|
||||
"version": "1.2.0",
|
||||
"description": "",
|
||||
"author": "",
|
||||
"private": true,
|
||||
@@ -107,7 +107,7 @@
|
||||
"coverageDirectory": "../coverage",
|
||||
"testEnvironment": "node",
|
||||
"transformIgnorePatterns": [
|
||||
"node_modules/(?!(.pnpm/)?(jose|@noble|uuid)/)"
|
||||
"node_modules/(?!(.pnpm/)?(jose|@noble|uuid))"
|
||||
],
|
||||
"transform": {
|
||||
"^.+\\.(t|j)sx?$": "ts-jest"
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { Body, Controller, Headers, Post, Req, Res } from "@nestjs/common";
|
||||
import { Body, Controller, Get, Headers, Post, Query, Req, Res } from "@nestjs/common";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import { Throttle } from "@nestjs/throttler";
|
||||
import type { Request, Response } from "express";
|
||||
import { getIronSession } from "iron-session";
|
||||
import { AuthService } from "./auth.service";
|
||||
import { BootstrapService } from "./bootstrap.service";
|
||||
import { LoginDto } from "./dto/login.dto";
|
||||
import { RegisterDto } from "./dto/register.dto";
|
||||
import { Verify2faDto } from "./dto/verify-2fa.dto";
|
||||
@@ -13,6 +14,7 @@ import { getSessionOptions, SessionData } from "./session.config";
|
||||
export class AuthController {
|
||||
constructor(
|
||||
private readonly authService: AuthService,
|
||||
private readonly bootstrapService: BootstrapService,
|
||||
private readonly configService: ConfigService,
|
||||
) {}
|
||||
|
||||
@@ -120,4 +122,12 @@ export class AuthController {
|
||||
session.destroy();
|
||||
return res.json({ message: "User logged out" });
|
||||
}
|
||||
|
||||
@Get("bootstrap-admin")
|
||||
async bootstrapAdmin(
|
||||
@Query("token") token: string,
|
||||
@Query("username") username: string,
|
||||
) {
|
||||
return this.bootstrapService.consumeToken(token, username);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import { SessionsModule } from "../sessions/sessions.module";
|
||||
import { UsersModule } from "../users/users.module";
|
||||
import { AuthController } from "./auth.controller";
|
||||
import { AuthService } from "./auth.service";
|
||||
import { BootstrapService } from "./bootstrap.service";
|
||||
import { AuthGuard } from "./guards/auth.guard";
|
||||
import { OptionalAuthGuard } from "./guards/optional-auth.guard";
|
||||
import { RolesGuard } from "./guards/roles.guard";
|
||||
@@ -15,6 +16,7 @@ import { RbacRepository } from "./repositories/rbac.repository";
|
||||
providers: [
|
||||
AuthService,
|
||||
RbacService,
|
||||
BootstrapService,
|
||||
RbacRepository,
|
||||
AuthGuard,
|
||||
OptionalAuthGuard,
|
||||
|
||||
105
backend/src/auth/bootstrap.service.spec.ts
Normal file
105
backend/src/auth/bootstrap.service.spec.ts
Normal file
@@ -0,0 +1,105 @@
|
||||
import { Test, TestingModule } from "@nestjs/testing";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import { UnauthorizedException } from "@nestjs/common";
|
||||
import { BootstrapService } from "./bootstrap.service";
|
||||
import { RbacService } from "./rbac.service";
|
||||
import { UsersService } from "../users/users.service";
|
||||
|
||||
describe("BootstrapService", () => {
|
||||
let service: BootstrapService;
|
||||
let rbacService: RbacService;
|
||||
let usersService: UsersService;
|
||||
|
||||
const mockRbacService = {
|
||||
countAdmins: jest.fn(),
|
||||
assignRoleToUser: jest.fn(),
|
||||
};
|
||||
|
||||
const mockUsersService = {
|
||||
findPublicProfile: jest.fn(),
|
||||
};
|
||||
|
||||
const mockConfigService = {
|
||||
get: jest.fn(),
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
jest.clearAllMocks();
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
BootstrapService,
|
||||
{ provide: RbacService, useValue: mockRbacService },
|
||||
{ provide: UsersService, useValue: mockUsersService },
|
||||
{ provide: ConfigService, useValue: mockConfigService },
|
||||
],
|
||||
}).compile();
|
||||
|
||||
service = module.get<BootstrapService>(BootstrapService);
|
||||
rbacService = module.get<RbacService>(RbacService);
|
||||
usersService = module.get<UsersService>(UsersService);
|
||||
});
|
||||
|
||||
it("should be defined", () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
describe("onApplicationBootstrap", () => {
|
||||
it("should generate a token if no admin exists", async () => {
|
||||
mockRbacService.countAdmins.mockResolvedValue(0);
|
||||
const generateTokenSpy = jest.spyOn(service as any, "generateBootstrapToken");
|
||||
|
||||
await service.onApplicationBootstrap();
|
||||
|
||||
expect(rbacService.countAdmins).toHaveBeenCalled();
|
||||
expect(generateTokenSpy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should not generate a token if admin exists", async () => {
|
||||
mockRbacService.countAdmins.mockResolvedValue(1);
|
||||
const generateTokenSpy = jest.spyOn(service as any, "generateBootstrapToken");
|
||||
|
||||
await service.onApplicationBootstrap();
|
||||
|
||||
expect(rbacService.countAdmins).toHaveBeenCalled();
|
||||
expect(generateTokenSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("consumeToken", () => {
|
||||
it("should throw UnauthorizedException if token is invalid", async () => {
|
||||
mockRbacService.countAdmins.mockResolvedValue(0);
|
||||
await service.onApplicationBootstrap();
|
||||
|
||||
await expect(
|
||||
service.consumeToken("wrong-token", "user1"),
|
||||
).rejects.toThrow(UnauthorizedException);
|
||||
});
|
||||
|
||||
it("should throw UnauthorizedException if user not found", async () => {
|
||||
mockRbacService.countAdmins.mockResolvedValue(0);
|
||||
await service.onApplicationBootstrap();
|
||||
const token = (service as any).bootstrapToken;
|
||||
|
||||
mockUsersService.findPublicProfile.mockResolvedValue(null);
|
||||
|
||||
await expect(service.consumeToken(token, "user1")).rejects.toThrow(
|
||||
UnauthorizedException,
|
||||
);
|
||||
});
|
||||
|
||||
it("should assign admin role and invalidate token on success", async () => {
|
||||
mockRbacService.countAdmins.mockResolvedValue(0);
|
||||
await service.onApplicationBootstrap();
|
||||
const token = (service as any).bootstrapToken;
|
||||
|
||||
const mockUser = { uuid: "user-uuid", username: "user1" };
|
||||
mockUsersService.findPublicProfile.mockResolvedValue(mockUser);
|
||||
|
||||
const result = await service.consumeToken(token, "user1");
|
||||
|
||||
expect(rbacService.assignRoleToUser).toHaveBeenCalledWith("user-uuid", "admin");
|
||||
expect((service as any).bootstrapToken).toBeNull();
|
||||
expect(result.message).toContain("user1 is now an administrator");
|
||||
});
|
||||
});
|
||||
});
|
||||
59
backend/src/auth/bootstrap.service.ts
Normal file
59
backend/src/auth/bootstrap.service.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import {
|
||||
Injectable,
|
||||
Logger,
|
||||
OnApplicationBootstrap,
|
||||
UnauthorizedException,
|
||||
} from "@nestjs/common";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import * as crypto from "node:crypto";
|
||||
import { UsersService } from "../users/users.service";
|
||||
import { RbacService } from "./rbac.service";
|
||||
|
||||
@Injectable()
|
||||
export class BootstrapService implements OnApplicationBootstrap {
|
||||
private readonly logger = new Logger(BootstrapService.name);
|
||||
private bootstrapToken: string | null = null;
|
||||
|
||||
constructor(
|
||||
private readonly rbacService: RbacService,
|
||||
private readonly usersService: UsersService,
|
||||
private readonly configService: ConfigService,
|
||||
) {}
|
||||
|
||||
async onApplicationBootstrap() {
|
||||
const adminCount = await this.rbacService.countAdmins();
|
||||
if (adminCount === 0) {
|
||||
this.generateBootstrapToken();
|
||||
}
|
||||
}
|
||||
|
||||
private generateBootstrapToken() {
|
||||
this.bootstrapToken = crypto.randomBytes(32).toString("hex");
|
||||
const domain = this.configService.get("DOMAIN_NAME") || "localhost";
|
||||
const protocol = domain.includes("localhost") ? "http" : "https";
|
||||
const url = `${protocol}://${domain}/auth/bootstrap-admin`;
|
||||
|
||||
this.logger.warn("SECURITY ALERT: No administrator found in database.");
|
||||
this.logger.warn("To create the first administrator, use the following endpoint:");
|
||||
this.logger.warn(`Endpoint: GET ${url}?token=${this.bootstrapToken}&username=votre_nom_utilisateur`);
|
||||
this.logger.warn("Exemple: curl -X GET \"http://localhost/auth/bootstrap-admin?token=...&username=...\"");
|
||||
this.logger.warn("This token is one-time use only.");
|
||||
}
|
||||
|
||||
async consumeToken(token: string, username: string) {
|
||||
if (!this.bootstrapToken || token !== this.bootstrapToken) {
|
||||
throw new UnauthorizedException("Invalid or expired bootstrap token");
|
||||
}
|
||||
|
||||
const user = await this.usersService.findPublicProfile(username);
|
||||
if (!user) {
|
||||
throw new UnauthorizedException(`User ${username} not found`);
|
||||
}
|
||||
|
||||
await this.rbacService.assignRoleToUser(user.uuid, "admin");
|
||||
this.bootstrapToken = null; // One-time use
|
||||
|
||||
this.logger.log(`User ${username} has been promoted to administrator via bootstrap token.`);
|
||||
return { message: `User ${username} is now an administrator` };
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,8 @@ describe("RbacService", () => {
|
||||
const mockRbacRepository = {
|
||||
findRolesByUserId: jest.fn(),
|
||||
findPermissionsByUserId: jest.fn(),
|
||||
countRoles: jest.fn(),
|
||||
createRole: jest.fn(),
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
@@ -58,4 +60,35 @@ describe("RbacService", () => {
|
||||
expect(repository.findPermissionsByUserId).toHaveBeenCalledWith(userId);
|
||||
});
|
||||
});
|
||||
|
||||
describe("seedRoles", () => {
|
||||
it("should be called on application bootstrap", async () => {
|
||||
const seedRolesSpy = jest.spyOn(service, "seedRoles");
|
||||
await service.onApplicationBootstrap();
|
||||
expect(seedRolesSpy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should seed roles if none exist", async () => {
|
||||
mockRbacRepository.countRoles.mockResolvedValue(0);
|
||||
|
||||
await service.seedRoles();
|
||||
|
||||
expect(repository.countRoles).toHaveBeenCalled();
|
||||
expect(repository.createRole).toHaveBeenCalledTimes(3);
|
||||
expect(repository.createRole).toHaveBeenCalledWith(
|
||||
"Administrator",
|
||||
"admin",
|
||||
"Full system access",
|
||||
);
|
||||
});
|
||||
|
||||
it("should not seed roles if some already exist", async () => {
|
||||
mockRbacRepository.countRoles.mockResolvedValue(3);
|
||||
|
||||
await service.seedRoles();
|
||||
|
||||
expect(repository.countRoles).toHaveBeenCalled();
|
||||
expect(repository.createRole).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,10 +1,49 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { Injectable, Logger, OnApplicationBootstrap } from "@nestjs/common";
|
||||
import { RbacRepository } from "./repositories/rbac.repository";
|
||||
|
||||
@Injectable()
|
||||
export class RbacService {
|
||||
export class RbacService implements OnApplicationBootstrap {
|
||||
private readonly logger = new Logger(RbacService.name);
|
||||
|
||||
constructor(private readonly rbacRepository: RbacRepository) {}
|
||||
|
||||
async onApplicationBootstrap() {
|
||||
this.logger.log("RbacService initialized, checking roles...");
|
||||
await this.seedRoles();
|
||||
}
|
||||
|
||||
async seedRoles() {
|
||||
try {
|
||||
const count = await this.rbacRepository.countRoles();
|
||||
if (count === 0) {
|
||||
this.logger.log("No roles found, seeding default roles...");
|
||||
const defaultRoles = [
|
||||
{ name: "Administrator", slug: "admin", description: "Full system access" },
|
||||
{
|
||||
name: "Moderator",
|
||||
slug: "moderator",
|
||||
description: "Access to moderation tools",
|
||||
},
|
||||
{ name: "User", slug: "user", description: "Standard user access" },
|
||||
];
|
||||
|
||||
for (const role of defaultRoles) {
|
||||
await this.rbacRepository.createRole(
|
||||
role.name,
|
||||
role.slug,
|
||||
role.description,
|
||||
);
|
||||
this.logger.log(`Created role: ${role.slug}`);
|
||||
}
|
||||
this.logger.log("Default roles seeded successfully.");
|
||||
} else {
|
||||
this.logger.log(`${count} roles already exist, skipping seeding.`);
|
||||
}
|
||||
} catch (error) {
|
||||
this.logger.error("Error during roles seeding:", error);
|
||||
}
|
||||
}
|
||||
|
||||
async getUserRoles(userId: string) {
|
||||
return this.rbacRepository.findRolesByUserId(userId);
|
||||
}
|
||||
@@ -12,4 +51,12 @@ export class RbacService {
|
||||
async getUserPermissions(userId: string) {
|
||||
return this.rbacRepository.findPermissionsByUserId(userId);
|
||||
}
|
||||
|
||||
async countAdmins() {
|
||||
return this.rbacRepository.countAdmins();
|
||||
}
|
||||
|
||||
async assignRoleToUser(userId: string, roleSlug: string) {
|
||||
return this.rbacRepository.assignRole(userId, roleSlug);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,4 +39,52 @@ export class RbacRepository {
|
||||
|
||||
return Array.from(new Set(result.map((p) => p.slug)));
|
||||
}
|
||||
|
||||
async countRoles(): Promise<number> {
|
||||
const result = await this.databaseService.db
|
||||
.select({ count: roles.id })
|
||||
.from(roles);
|
||||
return result.length;
|
||||
}
|
||||
|
||||
async countAdmins(): Promise<number> {
|
||||
const result = await this.databaseService.db
|
||||
.select({ count: usersToRoles.userId })
|
||||
.from(usersToRoles)
|
||||
.innerJoin(roles, eq(usersToRoles.roleId, roles.id))
|
||||
.where(eq(roles.slug, "admin"));
|
||||
return result.length;
|
||||
}
|
||||
|
||||
async createRole(name: string, slug: string, description?: string) {
|
||||
return this.databaseService.db
|
||||
.insert(roles)
|
||||
.values({
|
||||
name,
|
||||
slug,
|
||||
description,
|
||||
})
|
||||
.returning();
|
||||
}
|
||||
|
||||
async assignRole(userId: string, roleSlug: string) {
|
||||
const role = await this.databaseService.db
|
||||
.select()
|
||||
.from(roles)
|
||||
.where(eq(roles.slug, roleSlug))
|
||||
.limit(1);
|
||||
|
||||
if (!role[0]) {
|
||||
throw new Error(`Role with slug ${roleSlug} not found`);
|
||||
}
|
||||
|
||||
return this.databaseService.db
|
||||
.insert(usersToRoles)
|
||||
.values({
|
||||
userId,
|
||||
roleId: role[0].id,
|
||||
})
|
||||
.onConflictDoNothing()
|
||||
.returning();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,15 +18,21 @@ Cette page documente tous les points de terminaison disponibles sur l'API Memego
|
||||
Inscrit un nouvel utilisateur.
|
||||
|
||||
**Corps de la requête (JSON) :**
|
||||
- `username` (string) : Nom d'utilisateur unique.
|
||||
- `username` (string, max: 32) : Nom d'utilisateur unique.
|
||||
- `email` (string) : Adresse email valide.
|
||||
- `password` (string) : Mot de passe (min. 8 caractères).
|
||||
- `password` (string, min: 8) : Mot de passe.
|
||||
- `displayName` (string, optional, max: 32) : Nom d'affichage.
|
||||
|
||||
**Réponses :**
|
||||
- `201 Created` : Utilisateur créé.
|
||||
- `400 Bad Request` : Validation échouée ou utilisateur déjà existant.
|
||||
|
||||
```json
|
||||
{
|
||||
"username": "goat_user",
|
||||
"email": "user@memegoat.fr",
|
||||
"password": "strong-password"
|
||||
"password": "strong-password",
|
||||
"displayName": "Le Bouc"
|
||||
}
|
||||
```
|
||||
</Accordion>
|
||||
@@ -38,23 +44,25 @@ Cette page documente tous les points de terminaison disponibles sur l'API Memego
|
||||
- `email` (string)
|
||||
- `password` (string)
|
||||
|
||||
**Réponse (Succès) :**
|
||||
```json
|
||||
{
|
||||
"message": "User logged in successfully",
|
||||
"userId": "uuid-v4"
|
||||
}
|
||||
```
|
||||
*Note: L'access_token et le refresh_token sont stockés dans un cookie HttpOnly chiffré.*
|
||||
**Réponses :**
|
||||
- `200 OK` : Connexion réussie.
|
||||
```json
|
||||
{
|
||||
"message": "User logged in successfully",
|
||||
"userId": "uuid-v4"
|
||||
}
|
||||
```
|
||||
- `200 OK` (2FA requise) :
|
||||
```json
|
||||
{
|
||||
"message": "2FA required",
|
||||
"requires2FA": true,
|
||||
"userId": "uuid-v4"
|
||||
}
|
||||
```
|
||||
- `401 Unauthorized` : Identifiants invalides.
|
||||
|
||||
**Réponse (2FA requise) :**
|
||||
```json
|
||||
{
|
||||
"message": "2FA required",
|
||||
"requires2FA": true,
|
||||
"userId": "uuid-v4"
|
||||
}
|
||||
```
|
||||
*Note: L'access_token et le refresh_token sont stockés dans un cookie HttpOnly chiffré.*
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="POST /auth/verify-2fa">
|
||||
@@ -63,15 +71,41 @@ Cette page documente tous les points de terminaison disponibles sur l'API Memego
|
||||
**Corps de la requête :**
|
||||
- `userId` (uuid) : ID de l'utilisateur.
|
||||
- `token` (string) : Code TOTP à 6 chiffres.
|
||||
|
||||
**Réponses :**
|
||||
- `200 OK` : Vérification réussie, session établie.
|
||||
- `401 Unauthorized` : Token invalide ou utilisateur non autorisé.
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="POST /auth/refresh">
|
||||
Obtient un nouvel `access_token` à partir du `refresh_token` stocké dans la session.
|
||||
Met à jour automatiquement le cookie de session.
|
||||
|
||||
**Réponses :**
|
||||
- `200 OK` : Token rafraîchi.
|
||||
- `401 Unauthorized` : Refresh token absent ou invalide.
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="POST /auth/logout">
|
||||
Invalide la session actuelle.
|
||||
Invalide la session actuelle en détruisant le cookie de session.
|
||||
|
||||
**Réponses :**
|
||||
- `200 OK` : Déconnexion réussie.
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="GET /auth/bootstrap-admin">
|
||||
Élève les privilèges d'un utilisateur au rang d'administrateur.
|
||||
<Callout type="warn">
|
||||
Cette route n'est active que si aucun administrateur n'existe en base de données. Le token est affiché dans les logs de la console au démarrage.
|
||||
</Callout>
|
||||
|
||||
**Query Params :**
|
||||
- `token` (string) : Token à usage unique généré par le système.
|
||||
- `username` (string) : Nom de l'utilisateur à promouvoir.
|
||||
|
||||
**Réponses :**
|
||||
- `200 OK` : Utilisateur promu.
|
||||
- `401 Unauthorized` : Token invalide ou utilisateur non trouvé.
|
||||
</Accordion>
|
||||
</Accordions>
|
||||
|
||||
@@ -80,36 +114,62 @@ Cette page documente tous les points de terminaison disponibles sur l'API Memego
|
||||
<Accordions>
|
||||
<Accordion title="GET /users/me">
|
||||
Récupère les informations détaillées de l'utilisateur connecté. Requiert l'authentification.
|
||||
|
||||
**Réponses :**
|
||||
- `200 OK` : Retourne l'objet utilisateur complet (incluant données privées).
|
||||
- `401 Unauthorized` : Session invalide.
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="GET /users/public/:username">
|
||||
Récupère le profil public d'un utilisateur par son nom d'utilisateur.
|
||||
**Réponse :** `id`, `username`, `displayName`, `avatarUrl`, `createdAt`.
|
||||
Récupère le profil public d'un utilisateur par son nom d'utilisateur. Mise en cache pendant 1 minute.
|
||||
|
||||
**Réponses :**
|
||||
- `200 OK` : Profil public (id, username, displayName, bio, avatarUrl, createdAt).
|
||||
- `404 Not Found` : Utilisateur non trouvé.
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="GET /users/me/export">
|
||||
Extrait l'intégralité des données de l'utilisateur au format JSON (Conformité RGPD).
|
||||
Contient le profil, les contenus et les favoris.
|
||||
Contient le profil, les contenus créés et les favoris.
|
||||
|
||||
**Réponses :**
|
||||
- `200 OK` : Archive JSON des données.
|
||||
- `401 Unauthorized` : Non authentifié.
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="PATCH /users/me">
|
||||
Met à jour les informations du profil.
|
||||
**Corps :**
|
||||
- `displayName` (string)
|
||||
- `bio` (string)
|
||||
|
||||
**Corps de la requête :**
|
||||
- `displayName` (string, optional, max: 32)
|
||||
- `bio` (string, optional, max: 255)
|
||||
- `avatarUrl` (string, optional) : URL directe de l'avatar.
|
||||
|
||||
**Réponses :**
|
||||
- `200 OK` : Profil mis à jour.
|
||||
- `400 Bad Request` : Validation échouée.
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="POST /users/me/avatar">
|
||||
Met à jour l'avatar de l'utilisateur.
|
||||
Met à jour l'avatar de l'utilisateur via upload de fichier.
|
||||
|
||||
**Type :** `multipart/form-data`
|
||||
**Champ :** `file` (Image)
|
||||
**Champ :** `file` (Image: png, jpeg, webp)
|
||||
|
||||
**Réponses :**
|
||||
- `201 Created` : Avatar téléchargé et mis à jour.
|
||||
- `400 Bad Request` : Fichier invalide ou trop volumineux.
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="PATCH /users/me/consent">
|
||||
Met à jour les consentements légaux de l'utilisateur.
|
||||
**Corps :**
|
||||
- `termsVersion` (string)
|
||||
- `privacyVersion` (string)
|
||||
Met à jour les consentements légaux de l'utilisateur (CGU/RGPD).
|
||||
|
||||
**Corps de la requête :**
|
||||
- `termsVersion` (string, max: 16)
|
||||
- `privacyVersion` (string, max: 16)
|
||||
|
||||
**Réponses :**
|
||||
- `200 OK` : Consentements enregistrés.
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="DELETE /users/me">
|
||||
@@ -117,132 +177,388 @@ Cette page documente tous les points de terminaison disponibles sur l'API Memego
|
||||
<Callout type="warn">
|
||||
Les données sont définitivement purgées après un délai légal de 30 jours.
|
||||
</Callout>
|
||||
|
||||
**Réponses :**
|
||||
- `200 OK` : Suppression planifiée.
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="Gestion 2FA">
|
||||
- `POST /users/me/2fa/setup` : Génère un secret et QR Code.
|
||||
- `POST /users/me/2fa/enable` : Active après vérification du jeton.
|
||||
- `POST /users/me/2fa/disable` : Désactive avec jeton.
|
||||
<Accordion title="POST /users/me/2fa/setup">
|
||||
Génère un secret et un QR Code pour la configuration de la 2FA.
|
||||
|
||||
**Réponses :**
|
||||
- `201 Created` :
|
||||
```json
|
||||
{
|
||||
"secret": "JBSWY3DPEHPK3PXP",
|
||||
"qrCodeDataUrl": "data:image/png;base64,..."
|
||||
}
|
||||
```
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="Administration (Admin uniquement)">
|
||||
- `GET /users/admin` : Liste tous les utilisateurs (avec pagination `limit`, `offset`).
|
||||
- `DELETE /users/:uuid` : Supprime définitivement un utilisateur par son UUID.
|
||||
<Accordion title="POST /users/me/2fa/enable">
|
||||
Active la 2FA après vérification du jeton TOTP.
|
||||
|
||||
**Corps de la requête :**
|
||||
- `token` (string) : Code TOTP généré par l'app.
|
||||
|
||||
**Réponses :**
|
||||
- `200 OK` : 2FA activée.
|
||||
- `400 Bad Request` : Token invalide ou 2FA non initiée.
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="POST /users/me/2fa/disable">
|
||||
Désactive la 2FA en utilisant un jeton TOTP valide.
|
||||
|
||||
**Corps de la requête :**
|
||||
- `token` (string) : Code TOTP.
|
||||
|
||||
**Réponses :**
|
||||
- `200 OK` : 2FA désactivée.
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="GET /users/admin">
|
||||
Liste tous les utilisateurs. **Réservé aux administrateurs.**
|
||||
|
||||
**Query Params :**
|
||||
- `limit` (number) : Défaut 10.
|
||||
- `offset` (number) : Défaut 0.
|
||||
|
||||
**Réponses :**
|
||||
- `200 OK` : Liste paginée des utilisateurs.
|
||||
- `403 Forbidden` : Droits insuffisants.
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="DELETE /users/:uuid">
|
||||
Supprime définitivement un utilisateur par son UUID. **Réservé aux administrateurs.**
|
||||
|
||||
**Réponses :**
|
||||
- `200 OK` : Utilisateur supprimé.
|
||||
</Accordion>
|
||||
</Accordions>
|
||||
|
||||
### 🖼️ Contenus (`/contents`)
|
||||
|
||||
<Accordions>
|
||||
<Accordion title="GET /contents/explore | /trends | /recent">
|
||||
Recherche et filtre les contenus. Ces endpoints sont mis en cache (Redis + Navigateur).
|
||||
<Accordion title="GET /contents/explore">
|
||||
Recherche et filtre les contenus. Cet endpoint est mis en cache pendant 1 minute.
|
||||
|
||||
**Query Params :**
|
||||
- `limit` (number) : Défaut 10.
|
||||
- `offset` (number) : Défaut 0.
|
||||
- `sort` : `trend` | `recent` (uniquement sur `/explore`)
|
||||
- `tag` (string) : Filtrer par tag.
|
||||
- `category` (slug ou id) : Filtrer par catégorie.
|
||||
- `sort` : `trend` | `recent`
|
||||
- `tag` (string) : Filtrer par tag (nom).
|
||||
- `category` (slug ou uuid) : Filtrer par catégorie.
|
||||
- `author` (username) : Filtrer par auteur.
|
||||
- `query` (titre) : Recherche textuelle.
|
||||
- `favoritesOnly` (bool) : Ne montrer que les favoris de l'utilisateur connecté.
|
||||
- `userId` (uuid) : Filtrer les contenus d'un utilisateur spécifique.
|
||||
- `query` (string) : Recherche textuelle dans le titre.
|
||||
- `favoritesOnly` (boolean) : Ne montrer que les favoris de l'utilisateur (nécessite auth).
|
||||
- `userId` (uuid) : Filtrer par ID utilisateur.
|
||||
|
||||
**Réponses :**
|
||||
- `200 OK` : Liste paginée des contenus.
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="GET /contents/trends">
|
||||
Récupère les contenus les plus populaires du moment. Cache de 5 minutes.
|
||||
|
||||
**Query Params :** `limit`, `offset`.
|
||||
|
||||
**Réponses :**
|
||||
- `200 OK` : Liste des tendances.
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="GET /contents/recent">
|
||||
Récupère les contenus les plus récents. Cache de 1 minute.
|
||||
|
||||
**Query Params :** `limit`, `offset`.
|
||||
|
||||
**Réponses :**
|
||||
- `200 OK` : Liste des contenus récents.
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="GET /contents/:idOrSlug">
|
||||
Récupère un contenu par son ID ou son Slug.
|
||||
Récupère un contenu par son ID ou son Slug. Cache de 1 heure.
|
||||
|
||||
**Détection de Bots (SEO) :**
|
||||
Si l'User-Agent correspond à un robot d'indexation (Googlebot, Twitterbot, etc.), l'API retourne un rendu HTML minimal contenant les méta-tags **OpenGraph** et **Twitter Cards** pour un partage optimal. Pour les autres clients, les données sont retournées en JSON.
|
||||
Si l'User-Agent correspond à un robot d'indexation (Googlebot, Twitterbot, etc.), l'API retourne un rendu HTML minimal contenant les méta-tags **OpenGraph** et **Twitter Cards**.
|
||||
|
||||
**Réponses :**
|
||||
- `200 OK` : Objet Contenu ou Rendu HTML (Bots).
|
||||
- `404 Not Found` : Contenu inexistant.
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="POST /contents">
|
||||
Crée une entrée de contenu (sans upload de fichier direct). Utile pour référencer des URLs externes.
|
||||
**Corps :** `title`, `description`, `url`, `type`, `categoryId`, `tags`.
|
||||
Crée une entrée de contenu à partir d'une ressource déjà uploadée ou externe.
|
||||
|
||||
**Corps de la requête :**
|
||||
- `type` : `meme` | `gif`
|
||||
- `title` (string, max: 255)
|
||||
- `storageKey` (string, max: 512) : Clé du fichier sur S3.
|
||||
- `mimeType` (string, max: 128)
|
||||
- `fileSize` (number)
|
||||
- `categoryId` (uuid, optional)
|
||||
- `tags` (string[], optional)
|
||||
|
||||
**Réponses :**
|
||||
- `201 Created` : Contenu référencé.
|
||||
- `401 Unauthorized` : Non authentifié.
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="POST /contents/upload">
|
||||
Upload un fichier avec traitement automatique par le serveur.
|
||||
**Type :** `multipart/form-data`
|
||||
Upload un fichier et crée le contenu associé en une seule étape.
|
||||
|
||||
**Type :** `multipart/form-data`
|
||||
**Champs :**
|
||||
- `file` (binary) : png, jpeg, webp, webm, gif.
|
||||
- `type` : `meme` | `gif`
|
||||
- `title` : string
|
||||
- `categoryId`? : uuid
|
||||
- `tags`? : string[]
|
||||
- `title` (string)
|
||||
- `categoryId` (uuid, optional)
|
||||
- `tags` (string[], optional)
|
||||
|
||||
**Réponses :**
|
||||
- `201 Created` : Upload réussi et contenu créé.
|
||||
- `400 Bad Request` : Fichier non supporté ou données invalides.
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="POST /contents/upload-url">
|
||||
Génère une URL présignée pour un upload direct vers S3.
|
||||
**Query Param :** `fileName` (string).
|
||||
|
||||
**Query Param :**
|
||||
- `fileName` (string) : Nom du fichier avec extension.
|
||||
|
||||
**Réponses :**
|
||||
- `201 Created` : Retourne l'URL présignée et les champs requis.
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="POST /contents/:id/view | /use">
|
||||
Incrémente les statistiques de vue ou d'utilisation.
|
||||
<Accordion title="POST /contents/:id/view">
|
||||
Incrémente le compteur de vues d'un contenu.
|
||||
|
||||
**Réponses :**
|
||||
- `201 Created` : Compteur incrémenté.
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="POST /contents/:id/use">
|
||||
Incrémente le compteur d'utilisation (clic sur "Utiliser").
|
||||
|
||||
**Réponses :**
|
||||
- `201 Created` : Compteur incrémenté.
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="DELETE /contents/:id">
|
||||
Supprime un contenu (Soft Delete). Doit être l'auteur.
|
||||
|
||||
**Réponses :**
|
||||
- `200 OK` : Contenu supprimé.
|
||||
- `403 Forbidden` : Tentative de supprimer le contenu d'autrui.
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="DELETE /contents/:id/admin">
|
||||
Supprime définitivement un contenu. **Réservé aux administrateurs.**
|
||||
|
||||
**Réponses :**
|
||||
- `200 OK` : Contenu supprimé définitivement.
|
||||
</Accordion>
|
||||
</Accordions>
|
||||
|
||||
### 📂 Catégories, ⭐ Favoris, 🚩 Signalements
|
||||
### 📂 Catégories (`/categories`)
|
||||
|
||||
<Accordions>
|
||||
<Accordion title="Catégories (/categories)">
|
||||
- `GET /categories` : Liste toutes les catégories.
|
||||
- `GET /categories/:id` : Détails d'une catégorie.
|
||||
- `POST /categories` : Création (Admin uniquement).
|
||||
- `PATCH /categories/:id` : Mise à jour (Admin uniquement).
|
||||
- `DELETE /categories/:id` : Suppression (Admin uniquement).
|
||||
<Accordion title="GET /categories">
|
||||
Liste toutes les catégories de mèmes disponibles. Cache de 1 heure.
|
||||
|
||||
**Réponses :**
|
||||
- `200 OK` : Liste d'objets catégorie.
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="Favoris (/favorites)">
|
||||
Requiert l'authentification.
|
||||
- `GET /favorites` : Liste les favoris de l'utilisateur (avec pagination `limit`, `offset`).
|
||||
- `POST /favorites/:contentId` : Ajoute un favori.
|
||||
- `DELETE /favorites/:contentId` : Retire un favori.
|
||||
<Accordion title="GET /categories/:id">
|
||||
Récupère les détails d'une catégorie spécifique.
|
||||
|
||||
**Réponses :**
|
||||
- `200 OK` : Objet catégorie.
|
||||
- `404 Not Found` : Catégorie non trouvée.
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="Signalements (/reports)">
|
||||
- `POST /reports` : Signale un contenu ou un tag.
|
||||
- `GET /reports` : Liste des signalements (Pagination `limit`, `offset`). **Admin/Modérateurs**.
|
||||
- `PATCH /reports/:id/status` : Change le statut (`pending`, `resolved`, `dismissed`). **Admin/Modérateurs**.
|
||||
<Accordion title="POST /categories">
|
||||
Crée une nouvelle catégorie. **Admin uniquement.**
|
||||
|
||||
**Corps de la requête :**
|
||||
- `name` (string, max: 64)
|
||||
- `description` (string, optional, max: 255)
|
||||
- `iconUrl` (string, optional, max: 512)
|
||||
|
||||
**Réponses :**
|
||||
- `201 Created` : Catégorie créée.
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="PATCH /categories/:id">
|
||||
Met à jour une catégorie existante. **Admin uniquement.**
|
||||
|
||||
**Corps de la requête :** (Tous optionnels) `name`, `description`, `iconUrl`.
|
||||
|
||||
**Réponses :**
|
||||
- `200 OK` : Catégorie mise à jour.
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="DELETE /categories/:id">
|
||||
Supprime une catégorie. **Admin uniquement.**
|
||||
|
||||
**Réponses :**
|
||||
- `200 OK` : Catégorie supprimée.
|
||||
</Accordion>
|
||||
</Accordions>
|
||||
|
||||
### 🔑 Clés API & 🏷️ Tags
|
||||
### ⭐ Favoris (`/favorites`)
|
||||
|
||||
<Accordions>
|
||||
<Accordion title="Clés API (/api-keys)">
|
||||
- `POST /api-keys` : Génère une clé `{ name, expiresAt? }`.
|
||||
- `GET /api-keys` : Liste les clés actives.
|
||||
- `DELETE /api-keys/:id` : Révoque une clé.
|
||||
<Accordion title="GET /favorites">
|
||||
Liste les favoris de l'utilisateur connecté.
|
||||
|
||||
**Query Params :**
|
||||
- `limit` (number) : Défaut 10.
|
||||
- `offset` (number) : Défaut 0.
|
||||
|
||||
**Réponses :**
|
||||
- `200 OK` : Liste paginée des favoris.
|
||||
- `401 Unauthorized` : Non authentifié.
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="Tags (/tags)">
|
||||
- `GET /tags` : Recherche de tags.
|
||||
- **Params :** `query` (recherche), `sort` (`popular` | `recent`), `limit`, `offset`.
|
||||
<Accordion title="POST /favorites/:contentId">
|
||||
Ajoute un contenu aux favoris de l'utilisateur.
|
||||
|
||||
**Réponses :**
|
||||
- `201 Created` : Favori ajouté.
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="DELETE /favorites/:contentId">
|
||||
Retire un contenu des favoris de l'utilisateur.
|
||||
|
||||
**Réponses :**
|
||||
- `200 OK` : Favori supprimé.
|
||||
</Accordion>
|
||||
</Accordions>
|
||||
|
||||
### 🛠️ Système & Médias
|
||||
### 🚩 Signalements (`/reports`)
|
||||
|
||||
<Accordions>
|
||||
<Accordion title="Santé (/health)">
|
||||
- `GET /health` : Vérifie l'état de l'API et de la connexion à la base de données.
|
||||
<Accordion title="POST /reports">
|
||||
Signale un contenu ou un tag pour modération.
|
||||
|
||||
**Corps de la requête :**
|
||||
- `contentId` (uuid, optional) : ID du contenu à signaler.
|
||||
- `tagId` (uuid, optional) : ID du tag à signaler.
|
||||
- `reason` : `inappropriate` | `spam` | `copyright` | `other`
|
||||
- `description` (string, optional, max: 1000)
|
||||
|
||||
**Réponses :**
|
||||
- `201 Created` : Signalement enregistré.
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="Médias (/media)">
|
||||
- `GET /media?path=key` : Accès direct aux fichiers stockés sur S3 via le paramètre `path`. Supporte la mise en cache agressive.
|
||||
<Accordion title="GET /reports">
|
||||
Liste les signalements. **Réservé aux administrateurs et modérateurs.**
|
||||
|
||||
**Query Params :**
|
||||
- `limit` (number) : Défaut 10.
|
||||
- `offset` (number) : Défaut 0.
|
||||
|
||||
**Réponses :**
|
||||
- `200 OK` : Liste des signalements.
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="Administration (/admin)">
|
||||
- `GET /admin/stats` : Récupère les statistiques globales de la plateforme. **Admin uniquement**.
|
||||
<Accordion title="PATCH /reports/:id/status">
|
||||
Met à jour le statut d'un signalement. **Réservé aux administrateurs et modérateurs.**
|
||||
|
||||
**Corps de la requête :**
|
||||
- `status` : `pending` | `reviewed` | `resolved` | `dismissed`
|
||||
|
||||
**Réponses :**
|
||||
- `200 OK` : Statut mis à jour.
|
||||
</Accordion>
|
||||
</Accordions>
|
||||
|
||||
### 🔑 Clés API (`/api-keys`)
|
||||
|
||||
<Accordions>
|
||||
<Accordion title="POST /api-keys">
|
||||
Génère une nouvelle clé API pour l'utilisateur.
|
||||
|
||||
**Corps de la requête :**
|
||||
- `name` (string, max: 128) : Nom descriptif de la clé.
|
||||
- `expiresAt` (date-string, optional) : Date d'expiration.
|
||||
|
||||
**Réponses :**
|
||||
- `201 Created` : Clé générée. Retourne le token (à conserver précieusement, ne sera plus affiché).
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="GET /api-keys">
|
||||
Liste toutes les clés API actives de l'utilisateur.
|
||||
|
||||
**Réponses :**
|
||||
- `200 OK` : Liste des métadonnées des clés (nom, date de création, expiration).
|
||||
</Accordion>
|
||||
|
||||
<Accordion title="DELETE /api-keys/:id">
|
||||
Révoque une clé API spécifique.
|
||||
|
||||
**Réponses :**
|
||||
- `200 OK` : Clé révoquée.
|
||||
</Accordion>
|
||||
</Accordions>
|
||||
|
||||
### 🏷️ Tags (`/tags`)
|
||||
|
||||
<Accordions>
|
||||
<Accordion title="GET /tags">
|
||||
Liste les tags populaires ou recherchés. Cache de 5 minutes.
|
||||
|
||||
**Query Params :**
|
||||
- `limit` (number) : Défaut 10.
|
||||
- `offset` (number) : Défaut 0.
|
||||
- `query` (string, optional) : Recherche par nom.
|
||||
- `sort` : `popular` | `recent`
|
||||
|
||||
**Réponses :**
|
||||
- `200 OK` : Liste paginée des tags.
|
||||
</Accordion>
|
||||
</Accordions>
|
||||
|
||||
### 🛠️ Système (`/health`)
|
||||
|
||||
<Accordions>
|
||||
<Accordion title="GET /health">
|
||||
Vérifie l'état de santé de l'API et de ses dépendances (DB, Redis).
|
||||
|
||||
**Réponses :**
|
||||
- `200 OK` : Système opérationnel.
|
||||
```json
|
||||
{
|
||||
"status": "ok",
|
||||
"timestamp": "2024-01-21T10:00:00.000Z",
|
||||
"database": "connected",
|
||||
"redis": "connected"
|
||||
}
|
||||
```
|
||||
- `503 Service Unavailable` : Problème sur l'un des composants.
|
||||
</Accordion>
|
||||
</Accordions>
|
||||
|
||||
### 📁 Médias (`/media`)
|
||||
|
||||
<Accordions>
|
||||
<Accordion title="GET /media">
|
||||
Sert un fichier média stocké sur S3 avec une gestion optimisée du cache.
|
||||
|
||||
**Query Params :**
|
||||
- `path` (string) : Chemin relatif du fichier sur le bucket.
|
||||
|
||||
**Réponses :**
|
||||
- `200 OK` : Flux binaire du fichier. Headers `Content-Type` et `Cache-Control` inclus.
|
||||
- `404 Not Found` : Fichier introuvable.
|
||||
</Accordion>
|
||||
</Accordions>
|
||||
|
||||
### 📊 Administration (`/admin`)
|
||||
|
||||
<Accordions>
|
||||
<Accordion title="GET /admin/stats">
|
||||
Récupère les statistiques globales d'utilisation de la plateforme (**Admin uniquement**).
|
||||
</Accordion>
|
||||
</Accordions>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@memegoat/frontend",
|
||||
"version": "1.1.0",
|
||||
"version": "1.2.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "next dev",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@memegoat/source",
|
||||
"version": "1.1.0",
|
||||
"version": "1.2.0",
|
||||
"description": "",
|
||||
"scripts": {
|
||||
"version:get": "cmake -P version.cmake GET",
|
||||
|
||||
Reference in New Issue
Block a user