feat: implement ApiKeysModule with services, controller, and CRUD operations
Added a dedicated ApiKeysModule to manage API keys. Includes functionality to create, list, revoke, and validate keys, leveraging cryptographic hashing and database support. Integrated with authentication guards for security.
This commit is contained in:
42
backend/src/api-keys/api-keys.controller.ts
Normal file
42
backend/src/api-keys/api-keys.controller.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
Param,
|
||||
Post,
|
||||
Req,
|
||||
UseGuards,
|
||||
} from "@nestjs/common";
|
||||
import { AuthGuard } from "../auth/guards/auth.guard";
|
||||
import type { AuthenticatedRequest } from "../common/interfaces/request.interface";
|
||||
import { ApiKeysService } from "./api-keys.service";
|
||||
|
||||
@Controller("api-keys")
|
||||
@UseGuards(AuthGuard)
|
||||
export class ApiKeysController {
|
||||
constructor(private readonly apiKeysService: ApiKeysService) {}
|
||||
|
||||
@Post()
|
||||
create(
|
||||
@Req() req: AuthenticatedRequest,
|
||||
@Body("name") name: string,
|
||||
@Body("expiresAt") expiresAt?: string,
|
||||
) {
|
||||
return this.apiKeysService.create(
|
||||
req.user.sub,
|
||||
name,
|
||||
expiresAt ? new Date(expiresAt) : undefined,
|
||||
);
|
||||
}
|
||||
|
||||
@Get()
|
||||
findAll(@Req() req: AuthenticatedRequest) {
|
||||
return this.apiKeysService.findAll(req.user.sub);
|
||||
}
|
||||
|
||||
@Delete(":id")
|
||||
revoke(@Req() req: AuthenticatedRequest, @Param("id") id: string) {
|
||||
return this.apiKeysService.revoke(req.user.sub, id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user