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 measurement
GET {{hostname}}/configuration GET {{hostname}}/configuration
Content-Type: application/json
Identity: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ Identity: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ

View File

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

View File

@@ -1,8 +1,6 @@
package ltd.hlaeja.controller package ltd.hlaeja.controller
import ltd.hlaeja.library.deviceRegistry.Identity
import ltd.hlaeja.service.DeviceConfigurationService import ltd.hlaeja.service.DeviceConfigurationService
import ltd.hlaeja.service.DeviceRegistryService
import ltd.hlaeja.service.JwtService import ltd.hlaeja.service.JwtService
import ltd.hlaeja.util.toDeviceResponse import ltd.hlaeja.util.toDeviceResponse
import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.GetMapping
@@ -14,18 +12,12 @@ import org.springframework.web.bind.annotation.RestController
@RequestMapping("/configuration") @RequestMapping("/configuration")
class ConfigurationController( class ConfigurationController(
private val configurationService: DeviceConfigurationService, private val configurationService: DeviceConfigurationService,
private val deviceRegistry: DeviceRegistryService,
private val jwtService: JwtService, private val jwtService: JwtService,
) { ) {
@GetMapping @GetMapping
suspend fun getNodeConfiguration( suspend fun getNodeConfiguration(
@RequestHeader("Identity") identityToken: String, @RequestHeader("Identity") identityToken: String,
): Map<String, String> = extracted(identityToken) ): Map<String, String> = jwtService.getIdentity(identityToken)
.let { configurationService.getConfiguration(it.node).toDeviceResponse() } .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 package ltd.hlaeja.controller
import ltd.hlaeja.library.deviceData.MeasurementData import ltd.hlaeja.library.deviceData.MeasurementData
import ltd.hlaeja.library.deviceRegistry.Identity
import ltd.hlaeja.service.DeviceDataService import ltd.hlaeja.service.DeviceDataService
import ltd.hlaeja.service.DeviceRegistryService
import ltd.hlaeja.service.JwtService import ltd.hlaeja.service.JwtService
import org.springframework.http.HttpStatus.CREATED import org.springframework.http.HttpStatus.CREATED
import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.GetMapping
@@ -17,16 +15,15 @@ import org.springframework.web.bind.annotation.RestController
@RestController @RestController
@RequestMapping("/measurement") @RequestMapping("/measurement")
class MeasurementController( class MeasurementController(
private val deviceDataService: DeviceDataService, private val dataService: DeviceDataService,
private val deviceRegistry: DeviceRegistryService,
private val jwtService: JwtService, private val jwtService: JwtService,
) { ) {
@GetMapping @GetMapping
suspend fun getNodeMeasurement( suspend fun getNodeMeasurement(
@RequestHeader("Identity") identityToken: String, @RequestHeader("Identity") identityToken: String,
): Map<String, Number> = extracted(identityToken) ): Map<String, Number> = jwtService.getIdentity(identityToken)
.let { deviceDataService.getMeasurement(it.client, it.node).fields } .let { dataService.getMeasurement(it.client, it.node).fields }
@PostMapping @PostMapping
@ResponseStatus(CREATED) @ResponseStatus(CREATED)
@@ -34,9 +31,9 @@ class MeasurementController(
@RequestHeader("Identity") identityToken: String, @RequestHeader("Identity") identityToken: String,
@RequestBody measurement: Map<String, Number>, @RequestBody measurement: Map<String, Number>,
) { ) {
return extracted(identityToken) return jwtService.getIdentity(identityToken)
.let { .let {
deviceDataService.addMeasurement( dataService.addMeasurement(
it.client, it.client,
MeasurementData.Request( MeasurementData.Request(
mutableMapOf( 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.JwtParser
import io.jsonwebtoken.Jwts import io.jsonwebtoken.Jwts
import java.util.UUID import java.util.UUID
import ltd.hlaeja.library.deviceRegistry.Identity
import ltd.hlaeja.property.JwtProperty import ltd.hlaeja.property.JwtProperty
import ltd.hlaeja.util.PublicKeyProvider import ltd.hlaeja.util.PublicKeyProvider
import mu.KotlinLogging import mu.KotlinLogging
@@ -13,13 +14,19 @@ private val log = KotlinLogging.logger {}
@Service @Service
class JwtService( class JwtService(
jwtProperty: JwtProperty, jwtProperty: JwtProperty,
private val deviceRegistry: DeviceRegistryService,
) { ) {
private val parser: JwtParser = Jwts.parser() private val parser: JwtParser = Jwts.parser()
.verifyWith(PublicKeyProvider.load(jwtProperty.publicKey)) .verifyWith(PublicKeyProvider.load(jwtProperty.publicKey))
.build() .build()
suspend fun readIdentity( suspend fun getIdentity(
identityToken: String,
): Identity.Response = readIdentity(identityToken)
.let { deviceRegistry.getIdentityFromDevice(it) }
private suspend fun readIdentity(
identity: String, identity: String,
): UUID = parser.parseSignedClaims(identity) ): UUID = parser.parseSignedClaims(identity)
.let { UUID.fromString(it.payload["device"] as String) } .let { UUID.fromString(it.payload["device"] as String) }