more fix and update

This commit is contained in:
lendry
2026-06-24 20:15:19 +03:00
parent dcab6557d3
commit 9727cf3f35
53 changed files with 3479 additions and 494 deletions

View File

@@ -1,20 +1,26 @@
import { Body, Controller, Get, Param, Post } from '@nestjs/common';
import { Body, Controller, Get, Headers, Param, Post } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { ApiBearerAuth, ApiBody, ApiOperation, ApiParam, ApiResponse, ApiTags } from '@nestjs/swagger';
import { CoreGrpcService } from '../core-grpc.service';
import { OptionalPinDto, PinDto, VerifySecurityPinDto } from '../dto/security.dto';
import { OptionalPinDto, PinDto, TotpCodeDto, VerifySecurityPinDto } from '../dto/security.dto';
import { resolveAuthorizedPayload } from '../session-auth';
@ApiTags('Безопасность')
@ApiBearerAuth()
@Controller('security')
export class SecurityController {
constructor(private readonly core: CoreGrpcService) {}
constructor(
private readonly core: CoreGrpcService,
private readonly jwt: JwtService
) {}
@Get('users/:userId/devices')
@ApiOperation({ summary: 'Активные устройства', description: 'Показывает устройства пользователя и связанные активные сессии.' })
@ApiOperation({ summary: 'Активные устройства', description: 'Показывает устройства пользователя и связанные активные сессии. Текущее устройство не отображается.' })
@ApiParam({ name: 'userId', description: 'ID пользователя' })
@ApiResponse({ status: 200, description: 'Список устройств получен' })
listDevices(@Param('userId') userId: string) {
return this.core.security.ListActiveDevices({ userId });
async listDevices(@Param('userId') userId: string, @Headers('authorization') authorization?: string) {
const payload = await resolveAuthorizedPayload(this.jwt, this.core, authorization);
return this.core.security.ListActiveDevices({ userId, exceptSessionId: payload.sessionId });
}
@Get('users/:userId/sessions')
@@ -33,6 +39,36 @@ export class SecurityController {
return this.core.security.ListSignInHistory({ userId });
}
@Get('users/:userId/totp/status')
@ApiOperation({ summary: 'Статус TOTP', description: 'Показывает, включена ли двухфакторная аутентификация через приложение.' })
@ApiParam({ name: 'userId', description: 'ID пользователя' })
getTotpStatus(@Param('userId') userId: string) {
return this.core.security.GetTotpStatus({ userId });
}
@Post('users/:userId/totp/setup')
@ApiOperation({ summary: 'Настроить TOTP', description: 'Генерирует секрет и otpauth URL для Google Authenticator и аналогов.' })
@ApiParam({ name: 'userId', description: 'ID пользователя' })
setupTotp(@Param('userId') userId: string) {
return this.core.security.SetupTotp({ userId });
}
@Post('users/:userId/totp/enable')
@ApiOperation({ summary: 'Включить TOTP', description: 'Подтверждает код из приложения и включает двухфакторную аутентификацию.' })
@ApiParam({ name: 'userId', description: 'ID пользователя' })
@ApiBody({ type: TotpCodeDto })
enableTotp(@Param('userId') userId: string, @Body() dto: TotpCodeDto) {
return this.core.security.EnableTotp({ userId, code: dto.code });
}
@Post('users/:userId/totp/disable')
@ApiOperation({ summary: 'Отключить TOTP', description: 'Отключает двухфакторную аутентификацию после проверки кода.' })
@ApiParam({ name: 'userId', description: 'ID пользователя' })
@ApiBody({ type: TotpCodeDto })
disableTotp(@Param('userId') userId: string, @Body() dto: TotpCodeDto) {
return this.core.security.DisableTotp({ userId, code: dto.code });
}
@Post('users/:userId/pin/setup')
@ApiOperation({ summary: 'Настроить PIN-код', description: 'Создает или включает PIN-код пользователя. PIN хранится только в виде bcrypt hash.' })
@ApiParam({ name: 'userId', description: 'ID пользователя' })
@@ -102,10 +138,11 @@ export class SecurityController {
}
@Post('users/:userId/revoke-all-sessions')
@ApiOperation({ summary: 'Выйти везде', description: 'Отзывает все активные и PIN-заблокированные сессии пользователя.' })
@ApiOperation({ summary: 'Выйти везде', description: 'Отзывает все активные и PIN-заблокированные сессии пользователя, кроме текущей.' })
@ApiParam({ name: 'userId', description: 'ID пользователя' })
@ApiResponse({ status: 201, description: 'Все сессии отозваны' })
revokeAll(@Param('userId') userId: string) {
return this.core.security.RevokeAllSessions({ userId });
@ApiResponse({ status: 201, description: 'Остальные сессии отозваны' })
async revokeAll(@Param('userId') userId: string, @Headers('authorization') authorization?: string) {
const payload = await resolveAuthorizedPayload(this.jwt, this.core, authorization);
return this.core.security.RevokeAllSessions({ userId, exceptSessionId: payload.sessionId });
}
}