diff --git a/app/src/main/java/com/infomaniak/swisstransfer/di/ApplicationModule.kt b/app/src/main/java/com/infomaniak/swisstransfer/di/ApplicationModule.kt index c6ce30807..2490e45d8 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/di/ApplicationModule.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/di/ApplicationModule.kt @@ -20,7 +20,6 @@ package com.infomaniak.swisstransfer.di import android.app.Application import com.infomaniak.multiplatform_swisstransfer.SwissTransferInjection import com.infomaniak.swisstransfer.ui.MainApplication -import com.infomaniak.swisstransfer.ui.utils.Recaptcha import dagger.Module import dagger.Provides import dagger.hilt.InstallIn @@ -40,10 +39,6 @@ object ApplicationModule { @Singleton fun providesSwissTransferInjection() = SwissTransferInjection() - @Provides - @Singleton - fun providesRecaptchaInjection(application: Application) = Recaptcha(application) - @Provides @Singleton fun providesGlobalCoroutineScope(@DefaultDispatcher defaultDispatcher: CoroutineDispatcher): CoroutineScope { diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/MainApplication.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/MainApplication.kt index 45f3b2b53..108f71fa7 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/MainApplication.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/MainApplication.kt @@ -22,6 +22,7 @@ import com.infomaniak.multiplatform_swisstransfer.SwissTransferInjection import com.infomaniak.swisstransfer.BuildConfig import com.infomaniak.swisstransfer.ui.utils.AccountUtils import com.infomaniak.swisstransfer.ui.utils.Recaptcha +import com.infomaniak.swisstransfer.ui.utils.UploadRecaptcha import dagger.hilt.android.HiltAndroidApp import io.sentry.SentryEvent import io.sentry.SentryOptions @@ -38,7 +39,7 @@ class MainApplication : Application() { lateinit var swissTransferInjection: SwissTransferInjection @Inject - lateinit var recaptcha: Recaptcha + lateinit var uploadRecaptcha: UploadRecaptcha @Inject lateinit var globalCoroutineScope: CoroutineScope @@ -49,7 +50,7 @@ class MainApplication : Application() { globalCoroutineScope.launch { swissTransferInjection.accountManager.loadUser(userId = 0) AccountUtils.init(swissTransferInjection) - recaptcha.initializeClient() + uploadRecaptcha.initializeClient() } SentryAndroid.init(this) { options: SentryAndroidOptions -> diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/utils/Recaptcha.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/utils/UploadRecaptcha.kt similarity index 55% rename from app/src/main/java/com/infomaniak/swisstransfer/ui/utils/Recaptcha.kt rename to app/src/main/java/com/infomaniak/swisstransfer/ui/utils/UploadRecaptcha.kt index 4aed6fde9..c8ecdcc43 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/utils/Recaptcha.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/utils/UploadRecaptcha.kt @@ -23,25 +23,51 @@ import com.google.android.recaptcha.Recaptcha import com.google.android.recaptcha.RecaptchaAction import com.google.android.recaptcha.RecaptchaClient import com.infomaniak.swisstransfer.BuildConfig +import com.infomaniak.swisstransfer.ui.MainApplication import javax.inject.Inject -class Recaptcha @Inject constructor(private val application: Application) { +/** + * A class responsible for handling reCAPTCHA verification for uploads. + * + * This class uses the Google reCAPTCHA API to verify user actions and prevent abuse. + * It initializes a reCAPTCHA client and provides a method to fetch a reCAPTCHA code. + * + * @property application The application context. + */ +class UploadRecaptcha @Inject constructor(private val application: MainApplication) { private var client: RecaptchaClient? = null + /** + * Initializes the reCAPTCHA client. + * + * This method should be called before attempting to fetch a reCAPTCHA code. + * It fetches the reCAPTCHA client using the provided API site key. + * If an error occurs during initialization, it logs the error message. + * It MUST be called in the MainApplication. + */ suspend fun initializeClient() { runCatching { client = Recaptcha.fetchClient(application, BuildConfig.RECAPTCHA_API_SITE_KEY) }.onFailure { - Log.e("Recaptcha", "Getting Recaptcha client failed with an exception: $it") + Log.e("Recaptcha", "Getting Recaptcha client failed with an exception", it) } } + /** + * Fetches a reCAPTCHA code. + * + * This method executes the reCAPTCHA challenge and retrieves the code. + * The code is then passed to the provided callback function. + * + * @param callback A function that receives the reCAPTCHA code as a string. + * The code may be null if an error occurred. + */ suspend fun fetchCode(callback: (String?) -> Unit) { callback(client?.execute(RecaptchaAction.LOGIN)?.getOrNull()) } companion object { - private const val TAG = "Recaptcha" + private const val TAG = "UploadRecaptcha" } }