From 3e13e0091401c3e61381faad19bf94d459a644ec Mon Sep 17 00:00:00 2001 From: Swordsteel Date: Sat, 30 Nov 2024 18:52:22 +0100 Subject: [PATCH] update controller for common identification --- http/configuration.http | 1 - http/measurement.http | 1 - .../controller/ConfigurationController.kt | 10 +--------- .../hlaeja/controller/MeasurementController.kt | 18 +++++------------- .../kotlin/ltd/hlaeja/service/JwtService.kt | 9 ++++++++- 5 files changed, 14 insertions(+), 25 deletions(-) diff --git a/http/configuration.http b/http/configuration.http index b81c551..8ec74ff 100644 --- a/http/configuration.http +++ b/http/configuration.http @@ -1,4 +1,3 @@ ### get measurement GET {{hostname}}/configuration -Content-Type: application/json Identity: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ diff --git a/http/measurement.http b/http/measurement.http index ae903a5..b8bb0cc 100644 --- a/http/measurement.http +++ b/http/measurement.http @@ -1,6 +1,5 @@ ### get measurement GET {{hostname}}/measurement -Content-Type: application/json Identity: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ ### add measurement for all diff --git a/src/main/kotlin/ltd/hlaeja/controller/ConfigurationController.kt b/src/main/kotlin/ltd/hlaeja/controller/ConfigurationController.kt index 222e577..4f282d0 100644 --- a/src/main/kotlin/ltd/hlaeja/controller/ConfigurationController.kt +++ b/src/main/kotlin/ltd/hlaeja/controller/ConfigurationController.kt @@ -1,8 +1,6 @@ package ltd.hlaeja.controller -import ltd.hlaeja.library.deviceRegistry.Identity import ltd.hlaeja.service.DeviceConfigurationService -import ltd.hlaeja.service.DeviceRegistryService import ltd.hlaeja.service.JwtService import ltd.hlaeja.util.toDeviceResponse import org.springframework.web.bind.annotation.GetMapping @@ -14,18 +12,12 @@ import org.springframework.web.bind.annotation.RestController @RequestMapping("/configuration") class ConfigurationController( private val configurationService: DeviceConfigurationService, - private val deviceRegistry: DeviceRegistryService, private val jwtService: JwtService, ) { @GetMapping suspend fun getNodeConfiguration( @RequestHeader("Identity") identityToken: String, - ): Map = extracted(identityToken) + ): Map = jwtService.getIdentity(identityToken) .let { configurationService.getConfiguration(it.node).toDeviceResponse() } - - private suspend fun extracted( - identityToken: String, - ): Identity.Response = jwtService.readIdentity(identityToken) - .let { deviceRegistry.getIdentityFromDevice(it) } } diff --git a/src/main/kotlin/ltd/hlaeja/controller/MeasurementController.kt b/src/main/kotlin/ltd/hlaeja/controller/MeasurementController.kt index 26ade7a..d6e5737 100644 --- a/src/main/kotlin/ltd/hlaeja/controller/MeasurementController.kt +++ b/src/main/kotlin/ltd/hlaeja/controller/MeasurementController.kt @@ -1,9 +1,7 @@ package ltd.hlaeja.controller import ltd.hlaeja.library.deviceData.MeasurementData -import ltd.hlaeja.library.deviceRegistry.Identity import ltd.hlaeja.service.DeviceDataService -import ltd.hlaeja.service.DeviceRegistryService import ltd.hlaeja.service.JwtService import org.springframework.http.HttpStatus.CREATED import org.springframework.web.bind.annotation.GetMapping @@ -17,16 +15,15 @@ import org.springframework.web.bind.annotation.RestController @RestController @RequestMapping("/measurement") class MeasurementController( - private val deviceDataService: DeviceDataService, - private val deviceRegistry: DeviceRegistryService, + private val dataService: DeviceDataService, private val jwtService: JwtService, ) { @GetMapping suspend fun getNodeMeasurement( @RequestHeader("Identity") identityToken: String, - ): Map = extracted(identityToken) - .let { deviceDataService.getMeasurement(it.client, it.node).fields } + ): Map = jwtService.getIdentity(identityToken) + .let { dataService.getMeasurement(it.client, it.node).fields } @PostMapping @ResponseStatus(CREATED) @@ -34,9 +31,9 @@ class MeasurementController( @RequestHeader("Identity") identityToken: String, @RequestBody measurement: Map, ) { - return extracted(identityToken) + return jwtService.getIdentity(identityToken) .let { - deviceDataService.addMeasurement( + dataService.addMeasurement( it.client, MeasurementData.Request( mutableMapOf( @@ -48,9 +45,4 @@ class MeasurementController( ) } } - - private suspend fun extracted( - identityToken: String, - ): Identity.Response = jwtService.readIdentity(identityToken) - .let { deviceRegistry.getIdentityFromDevice(it) } } diff --git a/src/main/kotlin/ltd/hlaeja/service/JwtService.kt b/src/main/kotlin/ltd/hlaeja/service/JwtService.kt index c426614..4ba3c81 100644 --- a/src/main/kotlin/ltd/hlaeja/service/JwtService.kt +++ b/src/main/kotlin/ltd/hlaeja/service/JwtService.kt @@ -3,6 +3,7 @@ package ltd.hlaeja.service import io.jsonwebtoken.JwtParser import io.jsonwebtoken.Jwts import java.util.UUID +import ltd.hlaeja.library.deviceRegistry.Identity import ltd.hlaeja.property.JwtProperty import ltd.hlaeja.util.PublicKeyProvider import mu.KotlinLogging @@ -13,13 +14,19 @@ private val log = KotlinLogging.logger {} @Service class JwtService( jwtProperty: JwtProperty, + private val deviceRegistry: DeviceRegistryService, ) { private val parser: JwtParser = Jwts.parser() .verifyWith(PublicKeyProvider.load(jwtProperty.publicKey)) .build() - suspend fun readIdentity( + suspend fun getIdentity( + identityToken: String, + ): Identity.Response = readIdentity(identityToken) + .let { deviceRegistry.getIdentityFromDevice(it) } + + private suspend fun readIdentity( identity: String, ): UUID = parser.parseSignedClaims(identity) .let { UUID.fromString(it.payload["device"] as String) }