chore(api): validate deviceId string

This commit is contained in:
isra el
2023-04-17 11:07:15 +03:00
parent a00982bc13
commit 398ec30daa
2 changed files with 10 additions and 0 deletions

View File

@@ -69,6 +69,7 @@ export class AuthController {
return { data }
}
// TODO: Add a guard to check if the user is the owner of the api key
@UseGuards(AuthGuard)
@ApiOperation({ summary: 'Generate Api Key' })
@ApiBearerAuth()

View File

@@ -5,6 +5,7 @@ import {
HttpStatus,
Injectable,
} from '@nestjs/common'
import mongoose from 'mongoose'
import { UserRole } from 'src/users/user-roles.enum'
import { GatewayService } from '../gateway.service'
@@ -18,6 +19,14 @@ export class CanModifyDevice implements CanActivate {
const deviceId = request.params.id
const userId = request.user?.id
const isValidId = mongoose.Types.ObjectId.isValid(deviceId)
if (!isValidId) {
throw new HttpException(
{ error: 'Invalid device id' },
HttpStatus.BAD_REQUEST,
)
}
const device = await this.gatewayService.getDeviceById(deviceId)
if (
!!userId &&