CVE-2020-26230: Exposure of Sensitive Information to an Unauthorized Actor (CWE-200)
value: Radar COVID is the official COVID-19 exposure notification app for Spain. In affected versions of Radar COVID, identification and de-anonymization of COVID-19 positive users that upload Radar COVID TEKs to the Radar COVID server is possible. This vulnerability enables the identification and de-anonymization of COVID-19 positive users when using Radar COVID. The vulnerability is caused by the fact that Radar COVID connections to the server (uploading of TEKs to the backend) are only made by COVID-19 positives. Therefore, any on-path observer with the ability to monitor traffic between the app and the server can identify which users had a positive test. Such an adversary can be the mobile network operator (MNO) if the connection is done through a mobile network, the Internet Service Provider (ISP) if the connection is done through the Internet (e.g., a home network), a VPN provider used by the user, the local network operator in the case of enterprise networks, or any eavesdropper with access to the same network (WiFi or Ethernet) as the user as could be the case of public WiFi hotspots deployed at shopping centers, airports, hotels, and coffee shops. The attacker may also de-anonymize the user. For this additional stage to succeed, the adversary needs to correlate Radar COVID traffic to other identifiable information from the victim. This could be achieved by associating the connection to a contract with the name of the victim or by associating Radar COVID traffic to other user-generated flows containing identifiers in the clear (e.g., HTTP cookies or other mobile flows sending unique identifiers like the IMEI or the AAID without encryption). The former can be executed, for instance, by the Internet Service Provider or the MNO. The latter can be executed by any on-path adversary, such as the network provider or even the cloud provider that hosts more than one service accessed by the victim. The farther the adversary is either from the victim (the client) or the end-point (the server), the less likely it may be that the adversary has access to re-identification information. The vulnerability has been mitigated with the injection of dummy traffic from the application to the backend. Dummy traffic is generated by all users independently of whether they are COVID-19 positive or not. The issue was fixed in iOS in version 1.0.8 (uniform distribution), 1.1.0 (exponential distribution), Android in version 1.0.7 (uniform distribution), 1.1.0 (exponential distribution), Backend in version 1.1.2-RELEASE. For more information see the referenced GitHub Security Advisory.
CWE description: The product exposes sensitive information to an actor that is not explicitly authorized to have access to that information.
CVSS v3 base score: 5.3
Published: 2020-11-13T19:15Z
Repository: https://github.com/RadarCOVID/radar-covid-android
Language: Kotlin
Files changed: app/src/main/java/es/gob/radarcovid/RadarCovidApplication.kt, app/src/main/java/es/gob/radarcovid/common/di/module/RepositoryModule.kt, app/src/main/java/es/gob/radarcovid/common/di/module/ServicesModule.kt, app/src/main/java/es/gob/radarcovid/datamanager/repository/FakeInfectionReportRepository.kt, app/src/main/java/es/gob/radarcovid/datamanager/repository/FakeInfectionReportRepositoryImpl.kt, app/src/main/java/es/gob/radarcovid/datamanager/usecase/ReportFakeInfectionUseCase.kt, app/src/main/java/es/gob/radarcovid/datamanager/usecase/ReportInfectedUseCase.kt, app/src/main/java/es/gob/radarcovid/features/worker/FakeInfectionReportWorker.kt
Commit date: 2020-09-25 09:30:31 +0200
Commit message: *[FakeReport] Implemented random fake infection reports

Diff: diff --git a/app/src/main/java/es/gob/radarcovid/RadarCovidApplication.kt b/app/src/main/java/es/gob/radarcovid/RadarCovidApplication.kt
index f11f92c..5c92dad 100644
--- a/app/src/main/java/es/gob/radarcovid/RadarCovidApplication.kt
+++ b/app/src/main/java/es/gob/radarcovid/RadarCovidApplication.kt
@@ -12,10 +12,11 @@ package es.gob.radarcovid
 
 import dagger.android.AndroidInjector
 import dagger.android.support.DaggerApplication
 import es.gob.radarcovid.common.base.broadcast.ExposureStatusChangeBroadcastReceiver
 import es.gob.radarcovid.common.di.component.DaggerApplicationComponent
+import es.gob.radarcovid.features.worker.FakeInfectionReportWorker
 import io.reactivex.rxjava3.plugins.RxJavaPlugins
 import okhttp3.CertificatePinner
 import org.dpppt.android.sdk.DP3T
 import org.dpppt.android.sdk.models.ApplicationInfo
 import org.dpppt.android.sdk.util.SignatureUtil
@@ -44,10 +45,12 @@ class RadarCovidApplication : DaggerApplication() {
             BuildConfig.DEBUG
         )
         DP3T.setCertificatePinner(certificatePinner)
         DP3T.setUserAgent(userAgent)
 
+        FakeInfectionReportWorker.start(this)
+
         registerReceiver(ExposureStatusChangeBroadcastReceiver(), DP3T.getUpdateIntentFilter())
 
     }
 
     override fun applicationInjector(): AndroidInjector<out DaggerApplication> =
diff --git a/app/src/main/java/es/gob/radarcovid/common/di/module/RepositoryModule.kt b/app/src/main/java/es/gob/radarcovid/common/di/module/RepositoryModule.kt
index 97fd727..81523de 100644
--- a/app/src/main/java/es/gob/radarcovid/common/di/module/RepositoryModule.kt
+++ b/app/src/main/java/es/gob/radarcovid/common/di/module/RepositoryModule.kt
@@ -29,26 +29,23 @@ class RepositoryModule {
     fun providesBuildInfoRepository(repository: BuildInfoRepositoryImpl): BuildInfoRepository =
         repository
 
     @Provides
     @PerApplication
-    fun providesExposureStatusRepository(repository: ExposureStatusRepositoryImpl): ExposureStatusRepository =
+    fun providesFakeInfectionReportRepository(repository: FakeInfectionReportRepositoryImpl): FakeInfectionReportRepository =
         repository
 
     @Provides
     @PerApplication
-    fun providesDomainRepository(repository: DomainRepositoryImpl): DomainRepository = repository
+    fun providesExposureStatusRepository(repository: ExposureStatusRepositoryImpl): ExposureStatusRepository =
+        repository
 
     @Provides
     @PerApplication
     fun providesPreferencesRepository(repository: PreferencesRepositoryImpl): PreferencesRepository =
         repository
 
-    @Provides
-    @PerApplication
-    fun providesExampleRepository(repository: ExampleRepositoryImpl): ExampleRepository = repository
-
     @Provides
     @PerApplication
     fun providesApiRepository(repository: ApiRepositoryImpl): ApiRepository = repository
 
 }
\ No newline at end of file
diff --git a/app/src/main/java/es/gob/radarcovid/common/di/module/ServicesModule.kt b/app/src/main/java/es/gob/radarcovid/common/di/module/ServicesModule.kt
index 64c8d55..9a391c6 100644
--- a/app/src/main/java/es/gob/radarcovid/common/di/module/ServicesModule.kt
+++ b/app/src/main/java/es/gob/radarcovid/common/di/module/ServicesModule.kt
@@ -12,15 +12,20 @@ package es.gob.radarcovid.common.di.module
 
 import dagger.Module
 import dagger.android.ContributesAndroidInjector
 import es.gob.radarcovid.common.base.broadcast.ExposureStatusChangeBroadcastReceiver
 import es.gob.radarcovid.common.di.scope.PerService
+import es.gob.radarcovid.features.worker.FakeInfectionReportWorker
 import es.gob.radarcovid.features.worker.HealerWorker
 
 @Module
 abstract class ServicesModule {
 
+    @PerService
+    @ContributesAndroidInjector
+    abstract fun bindsFakeInfectedReportWorker(): FakeInfectionReportWorker
+
     @PerService
     @ContributesAndroidInjector
     abstract fun bindsHealerWorker(): HealerWorker
 
     @PerService
diff --git a/app/src/main/java/es/gob/radarcovid/datamanager/repository/DomainRepositoryImpl.kt b/app/src/main/java/es/gob/radarcovid/datamanager/repository/DomainRepositoryImpl.kt
deleted file mode 100644
index e5dde20..0000000
--- a/app/src/main/java/es/gob/radarcovid/datamanager/repository/DomainRepositoryImpl.kt
+++ /dev/null
@@ -1,20 +0,0 @@
-/*
- * Copyright (c) 2020 Gobierno de España
- *
- * This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at https://mozilla.org/MPL/2.0/.
- *
- * SPDX-License-Identifier: MPL-2.0
- */
-
-package es.gob.radarcovid.datamanager.repository
-
-import es.gob.radarcovid.models.domain.ExposureInfo
-import javax.inject.Inject
-
-class DomainRepositoryImpl @Inject constructor() : DomainRepository {
-
-    override var exposureInfo: ExposureInfo? = null
-
-}
\ No newline at end of file
diff --git a/app/src/main/java/es/gob/radarcovid/datamanager/repository/ExampleRepository.kt b/app/src/main/java/es/gob/radarcovid/datamanager/repository/ExampleRepository.kt
deleted file mode 100644
index 9d24a6a..0000000
--- a/app/src/main/java/es/gob/radarcovid/datamanager/repository/ExampleRepository.kt
+++ /dev/null
@@ -1,19 +0,0 @@
-/*
- * Copyright (c) 2020 Gobierno de España
- *
- * This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at https://mozilla.org/MPL/2.0/.
- *
- * SPDX-License-Identifier: MPL-2.0
- */
-
-package es.gob.radarcovid.datamanager.repository
-
-import org.funktionale.either.Either
-
-interface ExampleRepository {
-
-    fun exampleRequest(): Either<Throwable, String>
-
-}
\ No newline at end of file
diff --git a/app/src/main/java/es/gob/radarcovid/datamanager/repository/ExampleRepositoryImpl.kt b/app/src/main/java/es/gob/radarcovid/datamanager/repository/ExampleRepositoryImpl.kt
deleted file mode 100644
index 37de69a..0000000
--- a/app/src/main/java/es/gob/radarcovid/datamanager/repository/ExampleRepositoryImpl.kt
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * Copyright (c) 2020 Gobierno de España
- *
- * This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at https://mozilla.org/MPL/2.0/.
- *
- * SPDX-License-Identifier: MPL-2.0
- */
-
-package es.gob.radarcovid.datamanager.repository
-
-import es.gob.radarcovid.common.base.BaseRepository
-import es.gob.radarcovid.datamanager.api.ApiInterface
-import org.funktionale.either.Either
-import javax.inject.Inject
-
-class ExampleRepositoryImpl @Inject constructor(private val apiInterface: ApiInterface) :
-    BaseRepository(), ExampleRepository {
-
-    override fun exampleRequest(): Either<Throwable, String> = callService {
-        apiInterface.test()
-    }
-
-}
\ No newline at end of file
diff --git a/app/src/main/java/es/gob/radarcovid/datamanager/repository/DomainRepository.kt b/app/src/main/java/es/gob/radarcovid/datamanager/repository/FakeInfectionReportRepository.kt
similarity index 70%
rename from app/src/main/java/es/gob/radarcovid/datamanager/repository/DomainRepository.kt
rename to app/src/main/java/es/gob/radarcovid/datamanager/repository/FakeInfectionReportRepository.kt
index ad3913b..401b3d9 100644
--- a/app/src/main/java/es/gob/radarcovid/datamanager/repository/DomainRepository.kt
+++ b/app/src/main/java/es/gob/radarcovid/datamanager/repository/FakeInfectionReportRepository.kt
@@ -8,12 +8,12 @@
  * SPDX-License-Identifier: MPL-2.0
  */
 
 package es.gob.radarcovid.datamanager.repository
 
-import es.gob.radarcovid.models.domain.ExposureInfo
+import io.reactivex.rxjava3.core.Completable
 
-interface DomainRepository {
+interface FakeInfectionReportRepository {
 
-    var exposureInfo: ExposureInfo?
+    fun notifyFakeInfected(authCode: String): Completable
 
 }
\ No newline at end of file
diff --git a/app/src/main/java/es/gob/radarcovid/datamanager/repository/FakeInfectionReportRepositoryImpl.kt b/app/src/main/java/es/gob/radarcovid/datamanager/repository/FakeInfectionReportRepositoryImpl.kt
new file mode 100644
index 0000000..e789995
--- /dev/null
+++ b/app/src/main/java/es/gob/radarcovid/datamanager/repository/FakeInfectionReportRepositoryImpl.kt
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2020 Gobierno de España
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ *
+ * SPDX-License-Identifier: MPL-2.0
+ */
+
+package es.gob.radarcovid.datamanager.repository
+
+import android.content.Context
+import es.gob.radarcovid.BuildConfig
+import io.reactivex.rxjava3.core.Completable
+import org.dpppt.android.sdk.DP3T
+import org.dpppt.android.sdk.models.ExposeeAuthMethodAuthorization
+import javax.inject.Inject
+import javax.inject.Named
+
+class FakeInfectionReportRepositoryImpl @Inject constructor(
+    @Named("applicationContext") private val context: Context
+) : FakeInfectionReportRepository {
+
+    override fun notifyFakeInfected(authCode: String): Completable =
+        Completable.create {
+            if (BuildConfig.isMock) {
+                it.onComplete()
+            } else {
+                DP3T.sendFakeInfectedRequest(context,
+                    ExposeeAuthMethodAuthorization("Bearer $authCode"),
+                    { it.onComplete() },
+                    { it.onError(Exception("Error notifying fake infection")) }
+                )
+            }
+        }
+
+}
\ No newline at end of file
diff --git a/app/src/main/java/es/gob/radarcovid/datamanager/usecase/ReportFakeInfectionUseCase.kt b/app/src/main/java/es/gob/radarcovid/datamanager/usecase/ReportFakeInfectionUseCase.kt
new file mode 100644
index 0000000..d89e615
--- /dev/null
+++ b/app/src/main/java/es/gob/radarcovid/datamanager/usecase/ReportFakeInfectionUseCase.kt
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2020 Gobierno de España
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ *
+ * SPDX-License-Identifier: MPL-2.0
+ */
+
+package es.gob.radarcovid.datamanager.usecase
+
+import es.gob.radarcovid.datamanager.repository.ApiRepository
+import es.gob.radarcovid.datamanager.repository.FakeInfectionReportRepository
+import es.gob.radarcovid.models.request.RequestVerifyCode
+import es.gob.radarcovid.models.response.ResponseToken
+import io.reactivex.rxjava3.core.Completable
+import io.reactivex.rxjava3.core.Observable
+import javax.inject.Inject
+
+class ReportFakeInfectionUseCase @Inject constructor(
+    private val fakeInfectionReportRepository: FakeInfectionReportRepository,
+    private val apiRepository: ApiRepository
+) {
+
+    companion object {
+
+        const val FAKE_REPORT_CODE: String = "112358132134"
+        
+    }
+
+
+    fun reportFakeInfection(): Completable =
+        getFakeVerifyToken().flatMapCompletable {
+            fakeInfectionReportRepository.notifyFakeInfected(it.token)
+        }.concatWith {
+            it.onComplete()
+        }
+
+    private fun getFakeVerifyToken(): Observable<ResponseToken> {
+        return Observable.create { emitter ->
+            val result = apiRepository.verifyCode(RequestVerifyCode(null, FAKE_REPORT_CODE))
+
+            if (result.isLeft()) {
+                emitter.onError(result.left().get())
+            } else {
+                emitter.onNext(result.right().get())
+                emitter.onComplete()
+            }
+
+        }
+    }
+
+}
\ No newline at end of file
diff --git a/app/src/main/java/es/gob/radarcovid/datamanager/usecase/ReportInfectedUseCase.kt b/app/src/main/java/es/gob/radarcovid/datamanager/usecase/ReportInfectedUseCase.kt
index e6347fb..b846f8d 100644
--- a/app/src/main/java/es/gob/radarcovid/datamanager/usecase/ReportInfectedUseCase.kt
+++ b/app/src/main/java/es/gob/radarcovid/datamanager/usecase/ReportInfectedUseCase.kt
@@ -12,10 +12,11 @@ package es.gob.radarcovid.datamanager.usecase
 
 import es.gob.radarcovid.common.base.utils.JwtTokenUtils
 import es.gob.radarcovid.datamanager.repository.ApiRepository
 import es.gob.radarcovid.datamanager.repository.ContactTracingRepository
 import es.gob.radarcovid.datamanager.repository.PreferencesRepository
+import es.gob.radarcovid.models.exception.GenericRequestException
 import es.gob.radarcovid.models.request.RequestVerifyCode
 import es.gob.radarcovid.models.response.ResponseToken
 import io.reactivex.rxjava3.core.Completable
 import io.reactivex.rxjava3.core.Observable
 import java.util.*
@@ -26,21 +27,22 @@ class ReportInfectedUseCase @Inject constructor(
     private val preferencesRepository: PreferencesRepository,
     private val apiRepository: ApiRepository,
     private val jwtTokenUtils: JwtTokenUtils
 ) {
 
-    fun reportInfected(reportCode: String): Completable {
-
-        return getVerifyToken(reportCode).flatMapCompletable {
-            contactTracingRepository.notifyInfected(it.token, jwtTokenUtils.getOnset(it.token))
-        }.concatWith {
-            preferencesRepository.setInfectionReportDate(Date())
-            it.onComplete()
+    fun reportInfected(reportCode: String): Completable =
+        if (reportCode == ReportFakeInfectionUseCase.FAKE_REPORT_CODE) {
+            Completable.create { it.onError(GenericRequestException()) }
+        } else {
+            getVerifyToken(reportCode).flatMapCompletable {
+                contactTracingRepository.notifyInfected(it.token, jwtTokenUtils.getOnset(it.token))
+            }.concatWith {
+                preferencesRepository.setInfectionReportDate(Date())
+                it.onComplete()
+            }
         }
 
-    }
-
     private fun getVerifyToken(reportCode: String): Observable<ResponseToken> {
         return Observable.create { emitter ->
             val result = apiRepository.verifyCode(RequestVerifyCode(null, reportCode))
 
             if (result.isLeft()) {
diff --git a/app/src/main/java/es/gob/radarcovid/features/worker/FakeInfectionReportWorker.kt b/app/src/main/java/es/gob/radarcovid/features/worker/FakeInfectionReportWorker.kt
new file mode 100644
index 0000000..321cc1d
--- /dev/null
+++ b/app/src/main/java/es/gob/radarcovid/features/worker/FakeInfectionReportWorker.kt
@@ -0,0 +1,73 @@
+/*
+ * Copyright (c) 2020 Gobierno de España
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ *
+ * SPDX-License-Identifier: MPL-2.0
+ */
+
+package es.gob.radarcovid.features.worker
+
+import android.content.Context
+import androidx.work.*
+import dagger.android.HasAndroidInjector
+import es.gob.radarcovid.datamanager.usecase.ReportFakeInfectionUseCase
+import org.dpppt.android.sdk.BuildConfig
+import org.dpppt.android.sdk.DP3T
+import java.util.concurrent.TimeUnit
+import javax.inject.Inject
+import kotlin.random.Random
+
+class FakeInfectionReportWorker(context: Context, workerParams: WorkerParameters) :
+    Worker(context, workerParams) {
+
+    init {
+        (context.applicationContext as? HasAndroidInjector)
+            ?.androidInjector()
+            ?.inject(this)
+    }
+
+    companion object {
+
+        private const val TAG = "FakeInfectionReportWorker"
+
+        fun start(context: Context) {
+            start(context, getRandomDelay(), ExistingWorkPolicy.KEEP)
+        }
+
+        private fun start(
+            context: Context,
+            delayInMinutes: Long,
+            existingWorkPolicy: ExistingWorkPolicy
+        ) {
+            val constraints = Constraints.Builder()
+                .setRequiredNetworkType(NetworkType.CONNECTED)
+                .build()
+            val work =
+                OneTimeWorkRequest
+                    .Builder(FakeInfectionReportWorker::class.java)
+                    .setInitialDelay(delayInMinutes, TimeUnit.MINUTES)
+                    .setConstraints(constraints)
+                    .build()
+            WorkManager.getInstance(context)
+                .enqueueUniqueWork(TAG, existingWorkPolicy, work)
+        }
+
+        private fun getRandomDelay(): Long = Random.nextLong(180, 360)
+
+    }
+
+    @Inject
+    lateinit var reportFakeInfectionUseCase: ReportFakeInfectionUseCase
+
+    override fun doWork(): Result {
+        if (BuildConfig.DEBUG)
+            DP3T.addWorkerStartedToHistory(applicationContext, TAG)
+        reportFakeInfectionUseCase.reportFakeInfection().subscribe()
+        start(applicationContext, getRandomDelay(), ExistingWorkPolicy.APPEND)
+        return Result.success()
+    }
+    
+}
\ No newline at end of file

Vulnerable code:     fun providesExposureStatusRepository(repository: ExposureStatusRepositoryImpl): ExposureStatusRepository =
    fun providesDomainRepository(repository: DomainRepositoryImpl): DomainRepository = repository
    @Provides
    @PerApplication
    fun providesExampleRepository(repository: ExampleRepositoryImpl): ExampleRepository = repository

/*
 * Copyright (c) 2020 Gobierno de España
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 *
 * SPDX-License-Identifier: MPL-2.0
 */

package es.gob.radarcovid.datamanager.repository

import es.gob.radarcovid.models.domain.ExposureInfo
import javax.inject.Inject

class DomainRepositoryImpl @Inject constructor() : DomainRepository {

    override var exposureInfo: ExposureInfo? = null

}
/*
 * Copyright (c) 2020 Gobierno de España
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 *
 * SPDX-License-Identifier: MPL-2.0
 */

package es.gob.radarcovid.datamanager.repository

import org.funktionale.either.Either

interface ExampleRepository {

    fun exampleRequest(): Either<Throwable, String>

}
/*
 * Copyright (c) 2020 Gobierno de España
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 *
 * SPDX-License-Identifier: MPL-2.0
 */

package es.gob.radarcovid.datamanager.repository

import es.gob.radarcovid.common.base.BaseRepository
import es.gob.radarcovid.datamanager.api.ApiInterface
import org.funktionale.either.Either
import javax.inject.Inject

class ExampleRepositoryImpl @Inject constructor(private val apiInterface: ApiInterface) :
    BaseRepository(), ExampleRepository {

    override fun exampleRequest(): Either<Throwable, String> = callService {
        apiInterface.test()
    }

}
import es.gob.radarcovid.models.domain.ExposureInfo
interface DomainRepository {
    var exposureInfo: ExposureInfo?
    fun reportInfected(reportCode: String): Completable {

        return getVerifyToken(reportCode).flatMapCompletable {
            contactTracingRepository.notifyInfected(it.token, jwtTokenUtils.getOnset(it.token))
        }.concatWith {
            preferencesRepository.setInfectionReportDate(Date())
            it.onComplete()
    }

Fixed code: import es.gob.radarcovid.features.worker.FakeInfectionReportWorker
        FakeInfectionReportWorker.start(this)

    fun providesFakeInfectionReportRepository(repository: FakeInfectionReportRepositoryImpl): FakeInfectionReportRepository =
    fun providesExposureStatusRepository(repository: ExposureStatusRepositoryImpl): ExposureStatusRepository =
        repository
import es.gob.radarcovid.features.worker.FakeInfectionReportWorker
    @PerService
    @ContributesAndroidInjector
    abstract fun bindsFakeInfectedReportWorker(): FakeInfectionReportWorker

import io.reactivex.rxjava3.core.Completable
interface FakeInfectionReportRepository {
    fun notifyFakeInfected(authCode: String): Completable
/*
 * Copyright (c) 2020 Gobierno de España
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 *
 * SPDX-License-Identifier: MPL-2.0
 */

package es.gob.radarcovid.datamanager.repository

import android.content.Context
import es.gob.radarcovid.BuildConfig
import io.reactivex.rxjava3.core.Completable
import org.dpppt.android.sdk.DP3T
import org.dpppt.android.sdk.models.ExposeeAuthMethodAuthorization
import javax.inject.Inject
import javax.inject.Named

class FakeInfectionReportRepositoryImpl @Inject constructor(
    @Named("applicationContext") private val context: Context
) : FakeInfectionReportRepository {

    override fun notifyFakeInfected(authCode: String): Completable =
        Completable.create {
            if (BuildConfig.isMock) {
                it.onComplete()
            } else {
                DP3T.sendFakeInfectedRequest(context,
                    ExposeeAuthMethodAuthorization("Bearer $authCode"),
                    { it.onComplete() },
                    { it.onError(Exception("Error notifying fake infection")) }
                )
            }
        }

}
/*
 * Copyright (c) 2020 Gobierno de España
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 *
 * SPDX-License-Identifier: MPL-2.0
 */

package es.gob.radarcovid.datamanager.usecase

import es.gob.radarcovid.datamanager.repository.ApiRepository
import es.gob.radarcovid.datamanager.repository.FakeInfectionReportRepository
import es.gob.radarcovid.models.request.RequestVerifyCode
import es.gob.radarcovid.models.response.ResponseToken
import io.reactivex.rxjava3.core.Completable
import io.reactivex.rxjava3.core.Observable
import javax.inject.Inject

class ReportFakeInfectionUseCase @Inject constructor(
    private val fakeInfectionReportRepository: FakeInfectionReportRepository,
    private val apiRepository: ApiRepository
) {

    companion object {

        const val FAKE_REPORT_CODE: String = "112358132134"
        
    }


    fun reportFakeInfection(): Completable =
        getFakeVerifyToken().flatMapCompletable {
            fakeInfectionReportRepository.notifyFakeInfected(it.token)
        }.concatWith {
            it.onComplete()
        }

    private fun getFakeVerifyToken(): Observable<ResponseToken> {
        return Observable.create { emitter ->
            val result = apiRepository.verifyCode(RequestVerifyCode(null, FAKE_REPORT_CODE))

            if (result.isLeft()) {
                emitter.onError(result.left().get())
            } else {
                emitter.onNext(result.right().get())
                emitter.onComplete()
            }

        }
    }

}
import es.gob.radarcovid.models.exception.GenericRequestException
    fun reportInfected(reportCode: String): Completable =
        if (reportCode == ReportFakeInfectionUseCase.FAKE_REPORT_CODE) {
            Completable.create { it.onError(GenericRequestException()) }
        } else {
            getVerifyToken(reportCode).flatMapCompletable {
                contactTracingRepository.notifyInfected(it.token, jwtTokenUtils.getOnset(it.token))
            }.concatWith {
                preferencesRepository.setInfectionReportDate(Date())
                it.onComplete()
            }
/*
 * Copyright (c) 2020 Gobierno de España
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 *
 * SPDX-License-Identifier: MPL-2.0
 */

package es.gob.radarcovid.features.worker

import android.content.Context
import androidx.work.*
import dagger.android.HasAndroidInjector
import es.gob.radarcovid.datamanager.usecase.ReportFakeInfectionUseCase
import org.dpppt.android.sdk.BuildConfig
import org.dpppt.android.sdk.DP3T
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import kotlin.random.Random

class FakeInfectionReportWorker(context: Context, workerParams: WorkerParameters) :
    Worker(context, workerParams) {

    init {
        (context.applicationContext as? HasAndroidInjector)
            ?.androidInjector()
            ?.inject(this)
    }

    companion object {

        private const val TAG = "FakeInfectionReportWorker"

        fun start(context: Context) {
            start(context, getRandomDelay(), ExistingWorkPolicy.KEEP)
        }

        private fun start(
            context: Context,
            delayInMinutes: Long,
            existingWorkPolicy: ExistingWorkPolicy
        ) {
            val constraints = Constraints.Builder()
                .setRequiredNetworkType(NetworkType.CONNECTED)
                .build()
            val work =
                OneTimeWorkRequest
                    .Builder(FakeInfectionReportWorker::class.java)
                    .setInitialDelay(delayInMinutes, TimeUnit.MINUTES)
                    .setConstraints(constraints)
                    .build()
            WorkManager.getInstance(context)
                .enqueueUniqueWork(TAG, existingWorkPolicy, work)
        }

        private fun getRandomDelay(): Long = Random.nextLong(180, 360)

    }

    @Inject
    lateinit var reportFakeInfectionUseCase: ReportFakeInfectionUseCase

    override fun doWork(): Result {
        if (BuildConfig.DEBUG)
            DP3T.addWorkerStartedToHistory(applicationContext, TAG)
        reportFakeInfectionUseCase.reportFakeInfection().subscribe()
        start(applicationContext, getRandomDelay(), ExistingWorkPolicy.APPEND)
        return Result.success()
    }
    
}