Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
Store cnPublicKey for created trace locations
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasLechnerDev committed Apr 1, 2021
1 parent 7df0494 commit a8cce8f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package de.rki.coronawarnapp.eventregistration.checkins.qrcode

import android.os.Parcelable
import de.rki.coronawarnapp.eventregistration.events.TraceLocationUserInput
import de.rki.coronawarnapp.eventregistration.storage.entity.TraceLocationEntity
import de.rki.coronawarnapp.server.protocols.internal.pt.TraceLocationOuterClass
import kotlinx.parcelize.Parcelize
import okio.ByteString
import okio.ByteString.Companion.decodeBase64
import okio.ByteString.Companion.toByteString
import org.joda.time.Instant
import java.security.SecureRandom

const val TRACE_LOCATION_VERSION = 1

Expand All @@ -23,7 +20,7 @@ data class TraceLocation(
val endDate: Instant?,
val defaultCheckInLengthInMinutes: Int?,
val cryptographicSeed: ByteString,
val cnPublicKey: String = "hardcoded public key TODO: replace with real one",
val cnPublicKey: String,
val version: Int = TRACE_LOCATION_VERSION,
) : Parcelable {

Expand All @@ -46,14 +43,3 @@ fun TraceLocationEntity.toTraceLocation() = TraceLocation(
cnPublicKey = cnPublicKey,
version = version
)

fun TraceLocationUserInput.toTraceLocation(secureRandom: SecureRandom) = TraceLocation(
type = type,
description = description,
address = address,
startDate = startDate,
endDate = endDate,
defaultCheckInLengthInMinutes = defaultCheckInLengthInMinutes,
// cryptographic seed is a sequence of 16 random bytes
cryptographicSeed = ByteArray(16).apply { secureRandom.nextBytes(this) }.toByteString()
)
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
package de.rki.coronawarnapp.eventregistration.events

import de.rki.coronawarnapp.environment.EnvironmentSetup
import de.rki.coronawarnapp.eventregistration.checkins.qrcode.TraceLocation
import de.rki.coronawarnapp.eventregistration.checkins.qrcode.toTraceLocation
import de.rki.coronawarnapp.eventregistration.storage.repo.TraceLocationRepository
import okio.ByteString.Companion.toByteString
import java.security.SecureRandom
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class TraceLocationCreator @Inject constructor(
private val repository: TraceLocationRepository,
private val secureRandom: SecureRandom
private val secureRandom: SecureRandom,
private val environmentSetup: EnvironmentSetup
) {

suspend fun createTraceLocation(traceLocationUserInput: TraceLocationUserInput): TraceLocation {
return repository.addTraceLocation(traceLocationUserInput.toTraceLocation(secureRandom))
val cnPublicKey = environmentSetup.crowdNotifierPublicKey
val traceLocation = traceLocationUserInput.toTraceLocation(secureRandom, cnPublicKey)
return repository.addTraceLocation(traceLocation)
}
}

fun TraceLocationUserInput.toTraceLocation(secureRandom: SecureRandom, cnPublicKey: String) = TraceLocation(
type = type,
description = description,
address = address,
startDate = startDate,
endDate = endDate,
defaultCheckInLengthInMinutes = defaultCheckInLengthInMinutes,
// cryptographic seed is a sequence of 16 random bytes
cryptographicSeed = ByteArray(16).apply { secureRandom.nextBytes(this) }.toByteString(),
cnPublicKey = cnPublicKey
)

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.rki.coronawarnapp.eventregistration.checkins.qrcode

import de.rki.coronawarnapp.eventregistration.events.TraceLocationUserInput
import de.rki.coronawarnapp.eventregistration.events.toTraceLocation
import de.rki.coronawarnapp.server.protocols.internal.pt.TraceLocationOuterClass
import io.kotest.matchers.shouldBe
import io.mockk.MockKAnnotations
Expand Down Expand Up @@ -38,7 +39,7 @@ internal class TraceLocationUserInputToTraceLocationMapperTest {
startDate = Instant.parse("2020-01-01T14:00:00.000Z"),
endDate = Instant.parse("2020-01-01T18:00:00.000Z"),
defaultCheckInLengthInMinutes = 180
).toTraceLocation(secureRandom) shouldBe TraceLocation(
).toTraceLocation(secureRandom, "cnPublicKey123") shouldBe TraceLocation(
id = 0,
type = TraceLocationOuterClass.TraceLocationType.LOCATION_TYPE_TEMPORARY_PRIVATE_EVENT,
description = "Top Secret Private Event",
Expand All @@ -47,7 +48,7 @@ internal class TraceLocationUserInputToTraceLocationMapperTest {
endDate = Instant.parse("2020-01-01T18:00:00.000Z"),
defaultCheckInLengthInMinutes = 180,
cryptographicSeed = "2cc2b48c50aefe53b3974ed91e6b4ea9".decodeHex().toByteArray().toByteString(),
cnPublicKey = "hardcoded public key TODO: replace with real one"
cnPublicKey = "cnPublicKey123"
)
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package de.rki.coronawarnapp.eventregistration.events

import de.rki.coronawarnapp.eventregistration.checkins.qrcode.toTraceLocation
import de.rki.coronawarnapp.environment.EnvironmentSetup
import de.rki.coronawarnapp.eventregistration.storage.repo.TraceLocationRepository
import de.rki.coronawarnapp.server.protocols.internal.pt.TraceLocationOuterClass
import io.kotest.matchers.shouldBe
import io.mockk.MockKAnnotations
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.every
import io.mockk.impl.annotations.MockK
import io.mockk.impl.annotations.RelaxedMockK
import kotlinx.coroutines.test.runBlockingTest
Expand All @@ -20,13 +21,15 @@ internal class TraceLocationCreatorTest : BaseTest() {

@MockK lateinit var repository: TraceLocationRepository
@RelaxedMockK lateinit var secureRandom: SecureRandom
@MockK private lateinit var environmentSetup: EnvironmentSetup

@BeforeEach
fun setUp() {
MockKAnnotations.init(this)
every { environmentSetup.crowdNotifierPublicKey } returns "cnPublicKey123"
}

private fun createInstance() = TraceLocationCreator(repository, secureRandom)
private fun createInstance() = TraceLocationCreator(repository, secureRandom, environmentSetup)

@Test
fun `createTraceLocation() should return traceLocation and store it in repository when everything works fine`() =
Expand All @@ -41,7 +44,7 @@ internal class TraceLocationCreatorTest : BaseTest() {
defaultCheckInLengthInMinutes = 180
)

val expectedTraceLocation = userInput.toTraceLocation(secureRandom)
val expectedTraceLocation = userInput.toTraceLocation(secureRandom, "cnPublicKey123")

coEvery { repository.addTraceLocation(any()) } returns expectedTraceLocation

Expand Down

0 comments on commit a8cce8f

Please sign in to comment.