|
| 1 | +import { |
| 2 | + Body, |
| 3 | + Controller, |
| 4 | + Get, |
| 5 | + HttpException, |
| 6 | + HttpStatus, |
| 7 | + Injectable, |
| 8 | + Post, |
| 9 | + UseGuards, |
| 10 | + UseInterceptors, |
| 11 | +} from '@nestjs/common'; |
| 12 | +import { ApiBearerAuth, ApiBody, ApiOperation, ApiParam, ApiResponse, ApiTags } from '@nestjs/swagger'; |
| 13 | +import { SlugUuid } from '../../decorators/index.js'; |
| 14 | +import { Timeout } from '../../decorators/timeout.decorator.js'; |
| 15 | +import { Messages } from '../../exceptions/text/messages.js'; |
| 16 | +import { ConnectionEditGuard } from '../../guards/connection-edit.guard.js'; |
| 17 | +import { ConnectionReadGuard } from '../../guards/connection-read.guard.js'; |
| 18 | +import { SentryInterceptor } from '../../interceptors/index.js'; |
| 19 | +import { IComplexPermission } from '../permission/permission.interface.js'; |
| 20 | +import { CedarAuthorizationService } from './cedar-authorization.service.js'; |
| 21 | +import { SaveCedarPolicyDto } from './dto/save-cedar-policy.dto.js'; |
| 22 | +import { ValidateCedarSchemaDto } from './dto/validate-cedar-schema.dto.js'; |
| 23 | + |
| 24 | +@UseInterceptors(SentryInterceptor) |
| 25 | +@Timeout() |
| 26 | +@Controller() |
| 27 | +@ApiBearerAuth() |
| 28 | +@ApiTags('Cedar Authorization') |
| 29 | +@Injectable() |
| 30 | +export class CedarAuthorizationController { |
| 31 | + constructor(private readonly cedarAuthService: CedarAuthorizationService) {} |
| 32 | + |
| 33 | + @ApiOperation({ summary: 'Get the current cedar schema used for authorization' }) |
| 34 | + @ApiResponse({ |
| 35 | + status: 200, |
| 36 | + description: 'Cedar schema returned.', |
| 37 | + }) |
| 38 | + @ApiParam({ name: 'connectionId', required: true }) |
| 39 | + @UseGuards(ConnectionReadGuard) |
| 40 | + @Get('/connection/cedar-schema/:connectionId') |
| 41 | + async getCedarSchema( |
| 42 | + @SlugUuid('connectionId') connectionId: string, |
| 43 | + ): Promise<{ cedarSchema: Record<string, unknown> }> { |
| 44 | + if (!connectionId) { |
| 45 | + throw new HttpException({ message: Messages.CONNECTION_ID_MISSING }, HttpStatus.BAD_REQUEST); |
| 46 | + } |
| 47 | + return { cedarSchema: this.cedarAuthService.getSchema() }; |
| 48 | + } |
| 49 | + |
| 50 | + @ApiOperation({ summary: 'Validate a cedar schema against the Cedar engine' }) |
| 51 | + @ApiResponse({ |
| 52 | + status: 200, |
| 53 | + description: 'Cedar schema is valid.', |
| 54 | + }) |
| 55 | + @ApiBody({ type: ValidateCedarSchemaDto }) |
| 56 | + @ApiParam({ name: 'connectionId', required: true }) |
| 57 | + @UseGuards(ConnectionReadGuard) |
| 58 | + @Post('/connection/cedar-schema/validate/:connectionId') |
| 59 | + async validateCedarSchema( |
| 60 | + @SlugUuid('connectionId') connectionId: string, |
| 61 | + @Body() dto: ValidateCedarSchemaDto, |
| 62 | + ): Promise<{ valid: boolean }> { |
| 63 | + if (!connectionId) { |
| 64 | + throw new HttpException({ message: Messages.CONNECTION_ID_MISSING }, HttpStatus.BAD_REQUEST); |
| 65 | + } |
| 66 | + this.cedarAuthService.validateCedarSchema(dto.cedarSchema); |
| 67 | + return { valid: true }; |
| 68 | + } |
| 69 | + |
| 70 | + @ApiOperation({ summary: 'Save a cedar policy for a group, generating classical permissions for backward compatibility' }) |
| 71 | + @ApiResponse({ |
| 72 | + status: 200, |
| 73 | + description: 'Cedar policy saved and classical permissions generated.', |
| 74 | + }) |
| 75 | + @ApiBody({ type: SaveCedarPolicyDto }) |
| 76 | + @ApiParam({ name: 'connectionId', required: true }) |
| 77 | + @UseGuards(ConnectionEditGuard) |
| 78 | + @Post('/connection/cedar-policy/:connectionId') |
| 79 | + async saveCedarPolicy( |
| 80 | + @SlugUuid('connectionId') connectionId: string, |
| 81 | + @Body() dto: SaveCedarPolicyDto, |
| 82 | + ): Promise<{ cedarPolicy: string; classicalPermissions: IComplexPermission }> { |
| 83 | + if (!connectionId) { |
| 84 | + throw new HttpException({ message: Messages.CONNECTION_ID_MISSING }, HttpStatus.BAD_REQUEST); |
| 85 | + } |
| 86 | + return this.cedarAuthService.saveCedarPolicy(connectionId, dto.groupId, dto.cedarPolicy); |
| 87 | + } |
| 88 | +} |
0 commit comments