From a484f41d2aa393b2c5cb3999b8b4b095882bf3e4 Mon Sep 17 00:00:00 2001 From: Aram Al-Sabti Date: Tue, 15 Mar 2022 14:14:38 +0100 Subject: [PATCH] Resettable API key for generic http device --- .../admin-controller/iot-device.controller.ts | 28 +++++++++++++++++++ .../device-management/iot-device.service.ts | 12 ++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/controllers/admin-controller/iot-device.controller.ts b/src/controllers/admin-controller/iot-device.controller.ts index d479fc33..3f12e570 100644 --- a/src/controllers/admin-controller/iot-device.controller.ts +++ b/src/controllers/admin-controller/iot-device.controller.ts @@ -35,6 +35,7 @@ import { UpdateIoTDeviceDto } from "@dto/update-iot-device.dto"; import { IoTDevice } from "@entities/iot-device.entity"; import { ErrorCodes } from "@enum/error-codes.enum"; import { + checkIfUserHasAdminAccessToOrganization, checkIfUserHasReadAccessToApplication, checkIfUserHasWriteAccessToApplication, } from "@helpers/security-helper"; @@ -55,6 +56,7 @@ import { ArrayMaxSize } from "class-validator"; import { CreateIoTDeviceBatchDto } from "@dto/iot-device/create-iot-device-batch.dto"; import { UpdateIoTDeviceBatchDto } from "@dto/iot-device/update-iot-device-batch.dto"; import { buildIoTDeviceCreateUpdateAuditData, ensureUpdatePayload as ensureIoTDeviceUpdatePayload } from "@helpers/iot-device.helper"; +import { GenericHTTPDevice } from "@entities/generic-http-device.entity"; @ApiTags("IoT Device") @Controller("iot-device") @@ -342,4 +344,30 @@ export class IoTDeviceController { throw err; } } + + @Put("resetHttpDeviceApiKey/:id") + @ApiOperation({ summary: "Reset the API key of a generic HTTP device" }) + @ApiBadRequestResponse() + async resetHttpDeviceApiKey( + @Req() req: AuthenticatedRequest, + @Param("id", new ParseIntPipe()) id: number + ): Promise> { + try { + const oldIotDevice = await this.iotDeviceService.findOne(id); + checkIfUserHasWriteAccessToApplication(req, oldIotDevice?.application?.id); + + if (oldIotDevice.type !== IoTDeviceType.GenericHttp) { + throw new BadRequestException("The requested device is not a generic HTTP device"); + } + + const result = await this.iotDeviceService.resetHttpDeviceApiKey(oldIotDevice as GenericHTTPDevice); + AuditLog.success(ActionType.UPDATE, IoTDevice.name, req.user.userId, id); + return { + apiKey: result.apiKey, + }; + } catch (err) { + AuditLog.fail(ActionType.UPDATE, IoTDevice.name, req.user.userId, id); + throw err; + } + } } diff --git a/src/services/device-management/iot-device.service.ts b/src/services/device-management/iot-device.service.ts index 036b7543..46d753c3 100644 --- a/src/services/device-management/iot-device.service.ts +++ b/src/services/device-management/iot-device.service.ts @@ -50,6 +50,7 @@ import { SigFoxGroupService } from "@services/sigfox/sigfox-group.service"; import { DeleteResult, getManager, ILike, Repository, SelectQueryBuilder } from "typeorm"; import { DeviceModelService } from "./device-model.service"; import { IoTLoRaWANDeviceService } from "./iot-lorawan-device.service"; +import { v4 as uuidv4 } from "uuid"; type IoTDeviceOrSpecialized = | IoTDevice @@ -550,14 +551,14 @@ export class IoTDeviceService { deviceModelIds ); - // + // const applicationIds = iotDevicesDtoMap.reduce((ids: number[], dto) => { if (dto.iotDeviceDto.applicationId) { ids.push(dto.iotDeviceDto.applicationId); } return ids; - }, []); - + }, []); + const applications = await this.applicationService.findManyWithOrganisation( applicationIds ); @@ -597,6 +598,11 @@ export class IoTDeviceService { } } + resetHttpDeviceApiKey(httpDevice: GenericHTTPDevice): Promise { + httpDevice.apiKey = uuidv4(); + return this.iotDeviceRepository.save(httpDevice); + } + private async getApplicationsByIds(applicationIds: number[]) { return applicationIds.length ? await this.applicationService.findManyByIds(applicationIds)