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.
43 lines
944 B
TypeScript
43 lines
944 B
TypeScript
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);
|
|
}
|
|
}
|