Skip to content

Commit

Permalink
Code review
Browse files Browse the repository at this point in the history
  • Loading branch information
tevincent committed Oct 17, 2024
1 parent 57d1b06 commit 42747d2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package com.infomaniak.swisstransfer.ui
import android.app.Application
import com.infomaniak.multiplatform_swisstransfer.SwissTransferInjection
import com.infomaniak.swisstransfer.BuildConfig
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
Expand All @@ -37,7 +37,7 @@ class MainApplication : Application() {
lateinit var swissTransferInjection: SwissTransferInjection

@Inject
lateinit var recaptcha: Recaptcha
lateinit var uploadRecaptcha: UploadRecaptcha

@Inject
lateinit var globalCoroutineScope: CoroutineScope
Expand All @@ -46,7 +46,7 @@ class MainApplication : Application() {
super.onCreate()
globalCoroutineScope.launch {
swissTransferInjection.accountManager.loadUser(userId = 0)
recaptcha.initializeClient()
uploadRecaptcha.initializeClient()
}

SentryAndroid.init(this) { options: SentryAndroidOptions ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

0 comments on commit 42747d2

Please sign in to comment.