Skip to content

Commit

Permalink
Add recaptcha
Browse files Browse the repository at this point in the history
  • Loading branch information
tevincent committed Oct 16, 2024
1 parent 81da48c commit 91fee53
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 2 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ android {
vectorDrawables {
useSupportLibrary = true
}

buildConfigField("String", "RECAPTCHA_API_SITE_KEY", "\"6LfaxOgpAAAAAI3Sj4rtB2oAFjkRJILiGEt-LUsc\"")
}

buildTypes {
Expand Down Expand Up @@ -119,6 +121,7 @@ dependencies {

// Others
implementation(libs.kotlinx.serialization)
implementation(libs.recaptcha)

// Test
testImplementation(libs.junit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
*/
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 @@ -30,10 +33,17 @@ import javax.inject.Singleton
@InstallIn(SingletonComponent::class)
object ApplicationModule {

@Provides
fun providesMainApplication(application: Application) = application as MainApplication

@Provides
@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
@@ -1,6 +1,6 @@
/*
* Infomaniak SwissTransfer - Android
* Copyright (C) 2023-2024 Infomaniak Network SA
* Copyright (C) 2024 Infomaniak Network SA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +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 dagger.hilt.android.HiltAndroidApp
import io.sentry.SentryEvent
import io.sentry.SentryOptions
Expand All @@ -35,12 +36,18 @@ class MainApplication : Application() {
@Inject
lateinit var swissTransferInjection: SwissTransferInjection

@Inject
lateinit var recaptcha: Recaptcha

@Inject
lateinit var globalCoroutineScope: CoroutineScope

override fun onCreate() {
super.onCreate()
globalCoroutineScope.launch { swissTransferInjection.loadDefaultAccount() }
globalCoroutineScope.launch {
swissTransferInjection.loadDefaultAccount()
recaptcha.initializeClient()
}

SentryAndroid.init(this) { options: SentryAndroidOptions ->
// Register the callback as an option
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Infomaniak SwissTransfer - Android
* Copyright (C) 2024 Infomaniak Network SA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.swisstransfer.ui.utils

import android.app.Application
import android.util.Log
import com.google.android.recaptcha.Recaptcha
import com.google.android.recaptcha.RecaptchaAction
import com.google.android.recaptcha.RecaptchaClient
import com.infomaniak.swisstransfer.BuildConfig
import javax.inject.Inject

class Recaptcha @Inject constructor(private val application: Application) {

private var client: RecaptchaClient? = null

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")
}
}

suspend fun fetchCode(callback: (String?) -> Unit) {
callback(client?.execute(RecaptchaAction.LOGIN)?.getOrNull())
}

companion object {
private const val TAG = "Recaptcha"
}
}
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ serialization = "1.7.1"
swisstransfer = "0.1.1"
sentry = "4.12.0"
sentry-android = "7.15.0"
recaptcha = "18.6.1"

[libraries]
androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" }
Expand All @@ -43,6 +44,7 @@ kotlinx-serialization = { module = "org.jetbrains.kotlinx:kotlinx-serialization-
navigation-compose = { group = "androidx.navigation", name = "navigation-compose", version.ref = "navigation" }
swisstransfer-core = { module = "com.github.Infomaniak.multiplatform-SwissTransfer:STCore", version.ref = "swisstransfer" }
sentry-android = { module = "io.sentry:sentry-android", version.ref = "sentry-android" }
recaptcha = { module = "com.google.android.recaptcha:recaptcha", version.ref = "recaptcha" }
# Tests
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
Expand Down

0 comments on commit 91fee53

Please sign in to comment.