-
-
Notifications
You must be signed in to change notification settings - Fork 733
HCaptcha support. #5953
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
Open
dbrant
wants to merge
22
commits into
main
Choose a base branch
from
hcaptcha
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
HCaptcha support. #5953
Changes from 6 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
8c2306e
Preliminary HCaptcha support.
dbrant 20ac312
Merge branch 'main' into hcaptcha
dbrant b3d33da
Further.
dbrant ad71a6d
Yep.
dbrant 37272e6
Close the loop.
dbrant fd9208a
Merge branch 'main' into hcaptcha
dbrant 8ac738f
Merge branch 'main' into hcaptcha
dbrant 8f5c267
Refactor into helper class.
dbrant 3089247
Underpinnings for hCaptcha.
dbrant 9301381
Preliminary HCaptcha support.
dbrant dc648de
Further.
dbrant c30ec25
Yep.
dbrant c4f594a
Close the loop.
dbrant 2a1e4f8
Refactor into helper class.
dbrant e39ff4e
Merge branch 'hcaptcha' of github.com:wikimedia/apps-android-wikipedi…
dbrant a7fe9c2
Clean up.
dbrant 7331a7b
Put vals into constructor.
dbrant c68eba4
Merge branch 'hCaptchaPre' into hcaptcha
dbrant 4db3bec
Wire up to remote config.
dbrant 89a30c4
Merge branch 'main' into hcaptcha
dbrant 07f19e5
Merge branch 'main' into hcaptcha
dbrant aa290b7
Clean up.
dbrant File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,11 @@ import androidx.lifecycle.lifecycleScope | |
import androidx.lifecycle.repeatOnLifecycle | ||
import com.google.android.material.dialog.MaterialAlertDialogBuilder | ||
import com.google.android.material.textfield.TextInputLayout | ||
import com.hcaptcha.sdk.HCaptcha | ||
import com.hcaptcha.sdk.HCaptchaConfig | ||
import com.hcaptcha.sdk.HCaptchaSize | ||
import com.hcaptcha.sdk.HCaptchaTheme | ||
import com.hcaptcha.sdk.HCaptchaTokenResponse | ||
import kotlinx.coroutines.launch | ||
import org.wikipedia.R | ||
import org.wikipedia.WikipediaApp | ||
|
@@ -49,6 +54,9 @@ class CreateAccountActivity : BaseActivity() { | |
private var userNameTextWatcher: TextWatcher? = null | ||
private val viewModel: CreateAccountActivityViewModel by viewModels() | ||
|
||
private var hCaptcha: HCaptcha? = null | ||
private var tokenResponse: HCaptchaTokenResponse? = null | ||
|
||
public override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
binding = ActivityCreateAccountBinding.inflate(layoutInflater) | ||
|
@@ -92,6 +100,9 @@ class CreateAccountActivity : BaseActivity() { | |
is CreateAccountActivityViewModel.AccountInfoState.DoCreateAccount -> { | ||
doCreateAccount(it.token) | ||
} | ||
is CreateAccountActivityViewModel.AccountInfoState.HandleHCaptcha -> { | ||
showHCaptcha() | ||
} | ||
is CreateAccountActivityViewModel.AccountInfoState.HandleCaptcha -> { | ||
captchaHandler.handleCaptcha(it.token, CaptchaResult(it.captchaId)) | ||
} | ||
|
@@ -163,7 +174,14 @@ class CreateAccountActivity : BaseActivity() { | |
finish() | ||
} | ||
binding.footerContainer.privacyPolicyLink.setOnClickListener { | ||
FeedbackUtil.showPrivacyPolicy(this) | ||
|
||
|
||
|
||
showHCaptcha() | ||
|
||
|
||
|
||
//FeedbackUtil.showPrivacyPolicy(this) | ||
} | ||
binding.footerContainer.forgotPasswordLink.setOnClickListener { | ||
visitInExternalBrowser(this, PageTitle("Special:PasswordReset", wiki).uri.toUri()) | ||
|
@@ -181,6 +199,54 @@ class CreateAccountActivity : BaseActivity() { | |
} | ||
} | ||
|
||
private fun showHCaptcha() { | ||
if (hCaptcha == null) { | ||
|
||
hCaptcha = HCaptcha.getClient(this) | ||
hCaptcha?.setup( | ||
HCaptchaConfig.builder() | ||
.siteKey("f1f21d64-6384-4114-b7d0-d9d23e203b4a") | ||
.theme(if (WikipediaApp.instance.currentTheme.isDark) HCaptchaTheme.DARK else HCaptchaTheme.LIGHT) | ||
.host("meta.wikimedia.org") | ||
|
||
.jsSrc("https://assets-hcaptcha.wikimedia.org/1/api.js") | ||
.endpoint("https://hcaptcha.wikimedia.org") | ||
.assethost("https://assets-hcaptcha.wikimedia.org") | ||
.imghost("https://imgs-hcaptcha.wikimedia.org") | ||
.reportapi("https://report-hcaptcha.wikimedia.org") | ||
|
||
.sentry(false) | ||
|
||
//.loading(true) | ||
//.locale("en") | ||
//.size(HCaptchaSize.INVISIBLE) | ||
//.hideDialog(false) | ||
//.tokenExpiration(10) | ||
//.diagnosticLog(true) | ||
//.retryPredicate { config, exception -> | ||
// exception.hCaptchaError == HCaptchaError.SESSION_TIMEOUT | ||
//} | ||
|
||
.build()) | ||
|
||
hCaptcha?.addOnSuccessListener { response -> | ||
tokenResponse = response | ||
doCreateAccount(viewModel.token.orEmpty(), hCaptchaToken = response.tokenResult) | ||
}?.addOnFailureListener { e -> | ||
L.e("hCaptcha failed: ${e.message} (${e.statusCode})") | ||
tokenResponse = null | ||
FeedbackUtil.showMessage(this, "hCaptcha failed: ${e.message} (${e.statusCode})") | ||
}?.addOnOpenListener { | ||
FeedbackUtil.showMessage(this, "hCaptcha shown") | ||
} | ||
} | ||
hCaptcha?.verifyWithHCaptcha() | ||
} | ||
|
||
private fun resetHCaptcha() { | ||
hCaptcha?.reset() | ||
hCaptcha = null | ||
} | ||
|
||
private fun handleAccountCreationError(message: String) { | ||
if (message.contains("blocked")) { | ||
FeedbackUtil.makeSnackbar(this, getString(R.string.create_account_ip_block_message)) | ||
|
@@ -195,13 +261,15 @@ class CreateAccountActivity : BaseActivity() { | |
L.w("Account creation failed with result $message") | ||
} | ||
|
||
private fun doCreateAccount(token: String) { | ||
private fun doCreateAccount(token: String, hCaptchaToken: String? = null) { | ||
showProgressBar(true) | ||
val email = getText(binding.createAccountEmail).ifEmpty { null } | ||
val password = getText(binding.createAccountPasswordInput) | ||
val repeat = getText(binding.createAccountPasswordRepeat) | ||
val userName = getText(binding.createAccountUsername) | ||
viewModel.doCreateAccount(token, captchaHandler.captchaId().toString(), captchaHandler.captchaWord().toString(), userName, password, repeat, email) | ||
viewModel.doCreateAccount(token, captchaHandler.captchaId(), | ||
if (hCaptchaToken.isNullOrEmpty()) captchaHandler.captchaWord() else hCaptchaToken, | ||
userName, password, repeat, email) | ||
} | ||
|
||
public override fun onStop() { | ||
|
@@ -210,6 +278,7 @@ class CreateAccountActivity : BaseActivity() { | |
} | ||
|
||
public override fun onDestroy() { | ||
resetHCaptcha() | ||
captchaHandler.dispose() | ||
userNameTextWatcher?.let { binding.createAccountUsername.editText?.removeTextChangedListener(it) } | ||
super.onDestroy() | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can leave this line unset
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The WebView popped up by the hCaptcha SDK requires a base URL that matches our
content-security-policy
, otherwise theiframe
inside the WebView will refuse to load the hcaptcha content.