feat: Implement NestJS Auth, Users, and Licenses modules
Complete authentication system with JWT, refresh tokens, and TOTP 2FA. Auto-generates license keys on registration (CORR-XXXX-XXXX-XXXX format). JwtStrategy enriches payload with license_id and permissions from roles. Multi-tenant isolation enforced at license access layer. Auth Module: - 9 REST endpoints (login, register, refresh, 2FA setup/verify, profile, password reset) - Argon2 password hashing, TOTP with QR code generation - Public endpoints: login, register, forgot-password, reset-password, validate-key - Authenticated endpoints require JWT Bearer token Users Module: - Admin CRUD for user management (requires users.view permission) - Password fields excluded from all responses Licenses Module: - License lookup with owner authorization - Public key validation endpoint for plugin verification - License key generation via random hex parts All DTOs use class-validator, all controllers documented via Swagger. Custom decorators: @Public(), @CurrentUser(), @RequirePermission(). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
25
backend-nest/src/modules/users/users.controller.ts
Normal file
25
backend-nest/src/modules/users/users.controller.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { Controller, Get, Param } from '@nestjs/common';
|
||||
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
|
||||
import { UsersService } from './users.service';
|
||||
import { RequirePermission } from '../../common/decorators/require-permission.decorator';
|
||||
|
||||
@ApiTags('users')
|
||||
@ApiBearerAuth()
|
||||
@Controller('users')
|
||||
export class UsersController {
|
||||
constructor(private readonly usersService: UsersService) {}
|
||||
|
||||
@Get()
|
||||
@RequirePermission('users.view')
|
||||
@ApiOperation({ summary: 'Get all users (admin only)' })
|
||||
async findAll() {
|
||||
return this.usersService.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@RequirePermission('users.view')
|
||||
@ApiOperation({ summary: 'Get user by ID (admin only)' })
|
||||
async findById(@Param('id') id: string) {
|
||||
return this.usersService.findById(id);
|
||||
}
|
||||
}
|
||||
13
backend-nest/src/modules/users/users.module.ts
Normal file
13
backend-nest/src/modules/users/users.module.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { UsersController } from './users.controller';
|
||||
import { UsersService } from './users.service';
|
||||
import { User } from '../../entities/user.entity';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([User])],
|
||||
controllers: [UsersController],
|
||||
providers: [UsersService],
|
||||
exports: [UsersService],
|
||||
})
|
||||
export class UsersModule {}
|
||||
39
backend-nest/src/modules/users/users.service.ts
Normal file
39
backend-nest/src/modules/users/users.service.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { User } from '../../entities/user.entity';
|
||||
|
||||
@Injectable()
|
||||
export class UsersService {
|
||||
constructor(
|
||||
@InjectRepository(User)
|
||||
private userRepository: Repository<User>,
|
||||
) {}
|
||||
|
||||
async findById(id: string): Promise<User> {
|
||||
const user = await this.userRepository.findOne({
|
||||
where: { id },
|
||||
select: ['id', 'email', 'username', 'totp_enabled', 'is_super_admin', 'created_at', 'last_login_at'],
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new NotFoundException('User not found');
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
async findByEmail(email: string): Promise<User | null> {
|
||||
return this.userRepository.findOne({
|
||||
where: { email },
|
||||
select: ['id', 'email', 'username', 'totp_enabled', 'is_super_admin', 'created_at', 'last_login_at'],
|
||||
});
|
||||
}
|
||||
|
||||
async findAll(): Promise<User[]> {
|
||||
return this.userRepository.find({
|
||||
select: ['id', 'email', 'username', 'totp_enabled', 'is_super_admin', 'created_at', 'last_login_at'],
|
||||
order: { created_at: 'DESC' },
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user