test: comprehensive test suites and quality gates pass across backend, storefront and admin
- Backend (NestJS): 82 suites, 641 tests passing; 0 ESLint errors; 0 tsc errors. Hardened SSL cert/key matching, SSRF IP pinning, and eBank validation. - Admin Panel (Vite/React): 48 suites, 300 tests passing; 0 ESLint errors; 0 tsc errors. Full coverage across all pages and settings. - Storefront (Next.js): 62 suites, 368 tests passing; 0 ESLint errors; 0 tsc errors. - Fixed unmount lifecycle guards, promise error handling, and component edge cases.
This commit is contained in:
1 parent
1e73715169
commit
ddbf1ca026
439 files changed
+32911
-6107
No files matched your search
@@ -2,22 +2,22 @@ import {
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { CreateApiKeyDto } from './dto/api-key.dto';
|
||||
import * as crypto from 'crypto';
|
||||
} from "@nestjs/common";
|
||||
import { PrismaService } from "../prisma/prisma.service";
|
||||
import { CreateApiKeyDto } from "./dto/api-key.dto";
|
||||
import * as crypto from "crypto";
|
||||
|
||||
@Injectable()
|
||||
export class ApiKeysService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
private hashKey(key: string): string {
|
||||
return crypto.createHash('sha256').update(key).digest('hex');
|
||||
return crypto.createHash("sha256").update(key).digest("hex");
|
||||
}
|
||||
|
||||
async findAll() {
|
||||
const keys = await this.prisma.apiKey.findMany({
|
||||
orderBy: { createdAt: 'desc' },
|
||||
orderBy: { createdAt: "desc" },
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
@@ -34,20 +34,20 @@ export class ApiKeysService {
|
||||
|
||||
return keys.map((k) => ({
|
||||
...k,
|
||||
scopes: k.scopes ? k.scopes.split(',').map((s) => s.trim()) : ['*'],
|
||||
scopes: k.scopes ? k.scopes.split(",").map((s) => s.trim()) : ["*"],
|
||||
isExpired: k.expiresAt ? new Date(k.expiresAt) < new Date() : false,
|
||||
}));
|
||||
}
|
||||
|
||||
async create(dto: CreateApiKeyDto) {
|
||||
// Generate secure random key: cn_live_<32 hex chars>
|
||||
const randomBytes = crypto.randomBytes(24).toString('hex');
|
||||
const randomBytes = crypto.randomBytes(24).toString("hex");
|
||||
const rawKey = `cn_live_${randomBytes}`;
|
||||
const keyPrefix = rawKey.substring(0, 12);
|
||||
const keyHash = this.hashKey(rawKey);
|
||||
|
||||
const scopesStr =
|
||||
dto.scopes && dto.scopes.length > 0 ? dto.scopes.join(',') : '*';
|
||||
dto.scopes && dto.scopes.length > 0 ? dto.scopes.join(",") : "*";
|
||||
|
||||
const apiKey = await this.prisma.apiKey.create({
|
||||
data: {
|
||||
@@ -57,7 +57,7 @@ export class ApiKeysService {
|
||||
scopes: scopesStr,
|
||||
description: dto.description || null,
|
||||
expiresAt: dto.expiresAt ? new Date(dto.expiresAt) : null,
|
||||
status: 'ACTIVE',
|
||||
status: "ACTIVE",
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
@@ -74,7 +74,7 @@ export class ApiKeysService {
|
||||
return {
|
||||
apiKey: {
|
||||
...apiKey,
|
||||
scopes: apiKey.scopes.split(','),
|
||||
scopes: apiKey.scopes.split(","),
|
||||
},
|
||||
rawKey, // Return raw secret only once upon creation
|
||||
};
|
||||
@@ -83,10 +83,10 @@ export class ApiKeysService {
|
||||
async toggleStatus(id: string) {
|
||||
const key = await this.prisma.apiKey.findUnique({ where: { id } });
|
||||
if (!key) {
|
||||
throw new NotFoundException('کلید یافت نشد');
|
||||
throw new NotFoundException("کلید یافت نشد");
|
||||
}
|
||||
|
||||
const nextStatus = key.status === 'ACTIVE' ? 'REVOKED' : 'ACTIVE';
|
||||
const nextStatus = key.status === "ACTIVE" ? "REVOKED" : "ACTIVE";
|
||||
const updated = await this.prisma.apiKey.update({
|
||||
where: { id },
|
||||
data: { status: nextStatus },
|
||||
@@ -102,14 +102,14 @@ export class ApiKeysService {
|
||||
|
||||
return {
|
||||
...updated,
|
||||
scopes: updated.scopes.split(','),
|
||||
scopes: updated.scopes.split(","),
|
||||
};
|
||||
}
|
||||
|
||||
async delete(id: string) {
|
||||
const key = await this.prisma.apiKey.findUnique({ where: { id } });
|
||||
if (!key) {
|
||||
throw new NotFoundException('کلید یافت نشد');
|
||||
throw new NotFoundException("کلید یافت نشد");
|
||||
}
|
||||
|
||||
await this.prisma.apiKey.delete({ where: { id } });
|
||||
@@ -119,10 +119,10 @@ export class ApiKeysService {
|
||||
async validateKey(rawKey: string) {
|
||||
if (
|
||||
!rawKey ||
|
||||
typeof rawKey !== 'string' ||
|
||||
!rawKey.startsWith('cn_live_')
|
||||
typeof rawKey !== "string" ||
|
||||
!rawKey.startsWith("cn_live_")
|
||||
) {
|
||||
throw new UnauthorizedException('API Key نامعتبر است');
|
||||
throw new UnauthorizedException("API Key نامعتبر است");
|
||||
}
|
||||
|
||||
const keyHash = this.hashKey(rawKey);
|
||||
@@ -131,15 +131,15 @@ export class ApiKeysService {
|
||||
});
|
||||
|
||||
if (!key) {
|
||||
throw new UnauthorizedException('API Key یافت نشد یا معتبر نیست');
|
||||
throw new UnauthorizedException("API Key یافت نشد یا معتبر نیست");
|
||||
}
|
||||
|
||||
if (key.status !== 'ACTIVE') {
|
||||
throw new UnauthorizedException('این API Key باطل یا غیرفعال شده است');
|
||||
if (key.status !== "ACTIVE") {
|
||||
throw new UnauthorizedException("این API Key باطل یا غیرفعال شده است");
|
||||
}
|
||||
|
||||
if (key.expiresAt && new Date(key.expiresAt) < new Date()) {
|
||||
throw new UnauthorizedException('این API Key منقضی شده است');
|
||||
throw new UnauthorizedException("این API Key منقضی شده است");
|
||||
}
|
||||
|
||||
// Update lastUsedAt asynchronously in background
|
||||
@@ -150,15 +150,15 @@ export class ApiKeysService {
|
||||
})
|
||||
.catch(() => {});
|
||||
|
||||
const scopes = key.scopes.split(',').map((s) => s.trim().toLowerCase());
|
||||
const scopes = key.scopes.split(",").map((s) => s.trim().toLowerCase());
|
||||
// SEC-016: Disallow wildcard '*' from granting admin role; require explicit 'admin' scope
|
||||
const hasAdminScope = scopes.includes('admin');
|
||||
const hasAdminScope = scopes.includes("admin");
|
||||
|
||||
return {
|
||||
id: key.id,
|
||||
name: key.name,
|
||||
role: hasAdminScope ? 'Admin' : 'USER',
|
||||
email: 'apikey-system@canina.ir',
|
||||
role: hasAdminScope ? "Admin" : "USER",
|
||||
email: "apikey-system@canina.ir",
|
||||
scopes,
|
||||
isApiKey: true,
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user