update controller for common identification

This commit is contained in:
2024-11-30 18:52:22 +01:00
parent ff78cc7b39
commit 3e13e00914
5 changed files with 14 additions and 25 deletions

View File

@@ -1,4 +1,3 @@
### get measurement
GET {{hostname}}/configuration
Content-Type: application/json
Identity: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ

View File

@@ -1,6 +1,5 @@
### get measurement
GET {{hostname}}/measurement
Content-Type: application/json
Identity: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
### add measurement for all

View File

@@ -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<String, String> = extracted(identityToken)
): Map<String, String> = jwtService.getIdentity(identityToken)
.let { configurationService.getConfiguration(it.node).toDeviceResponse() }
private suspend fun extracted(
identityToken: String,
): Identity.Response = jwtService.readIdentity(identityToken)
.let { deviceRegistry.getIdentityFromDevice(it) }
}

View File

@@ -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<String, Number> = extracted(identityToken)
.let { deviceDataService.getMeasurement(it.client, it.node).fields }
): Map<String, Number> = 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<String, Number>,
) {
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) }
}

View File

@@ -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) }