Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement recaptcha verification #2349

Open
wants to merge 12 commits into
base: development
Choose a base branch
from
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,5 @@ dependencies {
//Smart LOck authentication
implementation "com.google.android.gms:play-services-auth:${rootConfiguration.playServiceAuthVersion}"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${rootConfiguration.kotlinVersion}"
implementation "com.google.android.gms:play-services-safetynet:${rootConfiguration.safetynetVersion}"
}
1 change: 1 addition & 0 deletions app/config.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ ext {
mockitoCoreVersion = '1.10.19'
playServiceAuthVersion='16.0.1'
shimmerVersion = "0.5.0"
safetynetVersion = "16.0.0"
}
4 changes: 2 additions & 2 deletions app/src/main/java/org/fossasia/susi/ai/data/LoginModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ class LoginModel : ILoginModel {
private lateinit var authResponseCall: Call<LoginResponse>
private lateinit var userSettingResponseCall: Call<UserSetting>

override fun login(email: String, password: String, listener: ILoginModel.OnLoginFinishedListener) {
override fun login(email: String, password: String, recaptcha_response: String, listener: ILoginModel.OnLoginFinishedListener) {

authResponseCall = ClientBuilder.susiApi
.login(email, password)
.login(email, password, recaptcha_response)

authResponseCall.enqueue(object : Callback<LoginResponse> {
override fun onResponse(call: Call<LoginResponse>, response: Response<LoginResponse>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface ILoginModel {
fun onErrorSetting()
}

fun login(email: String, password: String, listener: OnLoginFinishedListener)
fun login(email: String, password: String, recaptcha_response: String, listener: OnLoginFinishedListener)

fun cancelLogin()

Expand Down
29 changes: 27 additions & 2 deletions app/src/main/java/org/fossasia/susi/ai/login/LoginActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import android.view.inputmethod.EditorInfo
import android.widget.ArrayAdapter
import android.widget.Toast
import com.google.android.gms.auth.api.credentials.Credential
import com.google.android.gms.safetynet.SafetyNet
import com.google.android.gms.tasks.OnFailureListener
import com.google.android.gms.tasks.OnSuccessListener
import kotlinx.android.synthetic.main.activity_login.*
import org.fossasia.susi.ai.R
import org.fossasia.susi.ai.chat.ChatActivity
Expand All @@ -22,6 +25,7 @@ import org.fossasia.susi.ai.helper.Utils.hideSoftKeyboard
import org.fossasia.susi.ai.login.contract.ILoginPresenter
import org.fossasia.susi.ai.login.contract.ILoginView
import org.fossasia.susi.ai.signup.SignUpActivity
import timber.log.Timber

/**
* <h1>The Login activity.</h1>
Expand All @@ -36,6 +40,7 @@ class LoginActivity : AppCompatActivity(), ILoginView {
lateinit var builder: AlertDialog.Builder
private lateinit var loginPresenter: ILoginPresenter
private lateinit var progressDialog: ProgressDialog
private var recaptcha_response = ""

@SuppressLint("InflateParams")
override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -166,10 +171,30 @@ class LoginActivity : AppCompatActivity(), ILoginView {

private fun logIn() {
logIn.setOnClickListener {
startLogin()
if (PrefManager.getBoolean(R.string.login_failed, false)) {
verifyRecaptcha()
} else {
startLogin()
recaptcha_response = ""
}
}
}

fun verifyRecaptcha() {
SafetyNet.getClient(this).verifyWithRecaptcha(RECAPTCHA_KEY)
.addOnSuccessListener(this, OnSuccessListener { response ->
val userResponseToken = response.tokenResult
if (response.tokenResult?.isNotEmpty() == true) {
Timber.d("User Response Token - " + userResponseToken)
recaptcha_response = userResponseToken
startLogin()
}
})
.addOnFailureListener(this, OnFailureListener { e ->
Timber.e("Error: " + e)
})
}

private fun startLogin() {
val stringEmail = email.editText?.text.toString()
val stringPassword = password.editText?.text.toString()
Expand All @@ -180,7 +205,7 @@ class LoginActivity : AppCompatActivity(), ILoginView {
password.error = null
inputUrl.error = null

loginPresenter.login(stringEmail, stringPassword, !customServer.isChecked, stringURL)
loginPresenter.login(stringEmail, stringPassword, recaptcha_response, !customServer.isChecked, stringURL)
}

private fun cancelLogin() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class LoginPresenter(loginActivity: LoginActivity) :
loginView?.skipLogin()
}

override fun login(email: String, password: String, isSusiServerSelected: Boolean, url: String) {
override fun login(email: String, password: String, recaptcha_response: String, isSusiServerSelected: Boolean, url: String) {
if (email.isEmpty()) {
loginView?.invalidCredentials(true, Constant.EMAIL)
return
Expand Down Expand Up @@ -114,7 +114,7 @@ class LoginPresenter(loginActivity: LoginActivity) :
this.email = email
PrefManager.putString(Constant.EMAIL, this.email)
loginView?.showProgress(true)
loginModel.login(email.trim { it <= ' ' }.toLowerCase(), password, this)
loginModel.login(email.trim { it <= ' ' }.toLowerCase(), password, recaptcha_response, this)
}

override fun cancelLogin() {
Expand Down Expand Up @@ -147,9 +147,11 @@ class LoginPresenter(loginActivity: LoginActivity) :
utilModel.saveEmail(email)
utilModel.saveAnonymity(false)
loginModel.getUserSetting(this)
PrefManager.putBoolean(R.string.login_failed, false)

message = response.body()?.message.toString()
} else if (response.code() == 422) {
PrefManager.putBoolean(R.string.login_failed, true)
loginView?.showProgress(false)
loginView?.onLoginError(utilModel.getString(R.string.invalid_credentials_title),
utilModel.getString(R.string.invalid_credentials))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface ILoginPresenter {

fun onAttach(loginView: ILoginView)

fun login(email: String, password: String, isSusiServerSelected: Boolean, url: String)
fun login(email: String, password: String, recaptcha_response: String, isSusiServerSelected: Boolean, url: String)

fun skipLogin()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ interface SusiService {
@POST("/aaa/login.json?type=access-token")
fun login(
@Query("login") email: String,
@Query("password") password: String
@Query("password") password: String,
@Query("g-recaptcha-response") recaptcha_response: String
): Call<LoginResponse>

/**
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<string name="action_wall_settings">Wallpaper</string>
<string name="used_voice" translatable="false">Used Voice before?</string>
<string name="voice_welcome">Hi! How can I help you?</string>
<string name="login_failed" translatable="false">login_failed</string>

<string name="background_no_wall">no_wall</string>

Expand Down