feat: complete scheduled publishing, security remediation and DEV_TICKETS implementation
This commit is contained in:
1 parent
d6d4158cbe
commit
efb3c473c4
35 files changed
+685
-185
No files matched your search
@@ -150,17 +150,41 @@ export class AuthService {
|
||||
async verifyOtp(verifyOtpDto: VerifyOtpDto) {
|
||||
const phoneNumber = normalizeMobile(verifyOtpDto.phoneNumber);
|
||||
const code = String(verifyOtpDto.code).trim();
|
||||
const attemptsKey = `otp_attempts:${phoneNumber}`;
|
||||
|
||||
// SEC-007: Check failed attempts to prevent brute-forcing OTP
|
||||
const currentAttempts = await this.redisService.get(attemptsKey);
|
||||
if (currentAttempts && Number(currentAttempts) >= 5) {
|
||||
await this.redisService.del(`otp:${phoneNumber}`);
|
||||
throw new BadRequestException({
|
||||
message:
|
||||
'تعداد تلاشهای ناموفق بیش از حد مجاز است. لطفاً پس از چند دقیقه دوباره کد جدید دریافت کنید.',
|
||||
error: 'TOO_MANY_OTP_ATTEMPTS',
|
||||
});
|
||||
}
|
||||
|
||||
const savedCode = await this.redisService.get(`otp:${phoneNumber}`);
|
||||
|
||||
if (!savedCode || savedCode !== code) {
|
||||
const attempts = await this.redisService.incr(attemptsKey, 300);
|
||||
const remaining = Math.max(0, 5 - attempts);
|
||||
if (remaining === 0) {
|
||||
await this.redisService.del(`otp:${phoneNumber}`);
|
||||
throw new BadRequestException({
|
||||
message:
|
||||
'کد تایید به دلیل ۵ بار تلاش ناموفق باطل شد. لطفاً کد جدید دریافت کنید.',
|
||||
error: 'TOO_MANY_OTP_ATTEMPTS',
|
||||
});
|
||||
}
|
||||
throw new BadRequestException({
|
||||
message: 'کد وارد شده اشتباه یا منقضی شده است',
|
||||
message: `کد وارد شده اشتباه یا منقضی شده است. (${remaining} تلاش باقیمانده)`,
|
||||
error: 'INVALID_OTP',
|
||||
});
|
||||
}
|
||||
|
||||
// Success - clear OTP and attempts
|
||||
await this.redisService.del(`otp:${phoneNumber}`);
|
||||
await this.redisService.del(attemptsKey);
|
||||
|
||||
let user = await this.prisma.user.findUnique({
|
||||
where: { mobile: phoneNumber },
|
||||
@@ -346,11 +370,10 @@ export class AuthService {
|
||||
Buffer.from(crypto.createHash('sha256').update(adminEmail).digest()),
|
||||
);
|
||||
|
||||
const passwordMatches =
|
||||
crypto.timingSafeEqual(
|
||||
Buffer.from(crypto.createHash('sha256').update(body.password).digest()),
|
||||
Buffer.from(crypto.createHash('sha256').update(adminPassword).digest()),
|
||||
);
|
||||
const passwordMatches = crypto.timingSafeEqual(
|
||||
Buffer.from(crypto.createHash('sha256').update(body.password).digest()),
|
||||
Buffer.from(crypto.createHash('sha256').update(adminPassword).digest()),
|
||||
);
|
||||
|
||||
if (emailMatches && passwordMatches) {
|
||||
const payload = {
|
||||
@@ -391,7 +414,9 @@ export class AuthService {
|
||||
secret: getJwtRefreshSecret(),
|
||||
});
|
||||
} catch {
|
||||
throw new UnauthorizedException('توکن تازهسازی نامعتبر یا منقضی شده است');
|
||||
throw new UnauthorizedException(
|
||||
'توکن تازهسازی نامعتبر یا منقضی شده است',
|
||||
);
|
||||
}
|
||||
|
||||
const userId = decoded.sub;
|
||||
@@ -405,7 +430,9 @@ export class AuthService {
|
||||
const tokenKey = `refresh_token:${userId}:${tokenId}`;
|
||||
const tokenValid = await this.redisService.get(tokenKey);
|
||||
if (!tokenValid) {
|
||||
throw new UnauthorizedException('توکن تازهسازی باطل شده است. لطفاً مجدداً وارد شوید');
|
||||
throw new UnauthorizedException(
|
||||
'توکن تازهسازی باطل شده است. لطفاً مجدداً وارد شوید',
|
||||
);
|
||||
}
|
||||
|
||||
// Invalidate old token (Rotation)
|
||||
@@ -492,7 +519,9 @@ export class AuthService {
|
||||
|
||||
// If token payload has jti and sub, also invalidate the refresh token in Redis
|
||||
if (decoded && decoded.sub && decoded.jti) {
|
||||
await this.redisService.del(`refresh_token:${decoded.sub}:${decoded.jti}`);
|
||||
await this.redisService.del(
|
||||
`refresh_token:${decoded.sub}:${decoded.jti}`,
|
||||
);
|
||||
}
|
||||
} catch {
|
||||
await this.redisService.set(`bl_token:${token}`, '1', 15 * 60);
|
||||
|
||||
Reference in new issue
Block a user