Skip to content

Commit 5d49005

Browse files
authored
Login: Support login for sites with captcha plugins (#16372)
2 parents 1ff86b1 + 705c0c7 commit 5d49005

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

RELEASE-NOTES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
23.8
55
-----
6+
- [*] Better support site credential login for sites with captcha plugins [https://github.com/woocommerce/woocommerce-ios/pull/16372]
67
- [*] Crash fix attempt to resolve the race condition in request authenticator swapping [https://github.com/woocommerce/woocommerce-ios/pull/16370]
78
- [Internal] Fix broken navigation to a variable product selector [https://github.com/woocommerce/woocommerce-ios/pull/16363]
89

WooCommerce/Classes/Authentication/AuthenticationManager.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ extension AuthenticationManager: WordPressAuthenticatorDelegate {
372372
}
373373
}()
374374

375-
// Only show the tutorial if the error can be solved by the app password flow.
375+
// Show the tutorial immediately if it's obvious that the error can be solved by the app password flow.
376376
if isAppPasswordAuthError {
377377
presentAppPasswordTutorial(error: error, for: siteURL, in: viewController)
378378
} else {
@@ -798,10 +798,22 @@ private extension AuthenticationManager {
798798
/// Presents login error alert before redirecting user to the site login using a web view.
799799
///
800800
private func presentAppPasswordAlert(error: Error, for siteURL: String, in viewController: UIViewController) {
801-
801+
let shouldEnableWebFlow: Bool = {
802+
/// Since our detection of invalid credentials error might be inaccurate,
803+
/// adding the fallback to web flow would unblock users from login.
804+
if let siteCredentialError = error as? SiteCredentialLoginError,
805+
case .invalidCredentials = siteCredentialError {
806+
return true
807+
}
808+
return false
809+
}()
810+
let defaultAction = shouldEnableWebFlow ? { [weak self] in
811+
guard let self else { return }
812+
presentApplicationPasswordWebView(for: siteURL, in: viewController)
813+
} : nil
802814
let alertController = FancyAlertViewController.makeSiteCredentialLoginErrorAlert(
803815
message: (error as NSError).localizedDescription,
804-
defaultAction: nil
816+
defaultAction: defaultAction
805817
)
806818

807819
viewController.present(alertController, animated: true)

WooCommerce/Classes/Authentication/SiteCredentialLoginUseCase.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,14 @@ private extension SiteCredentialLoginUseCase {
183183
guard let html = String(data: data, encoding: .utf8) else {
184184
throw SiteCredentialLoginError.invalidLoginResponse
185185
}
186-
if html.hasInvalidCredentialsPattern() {
186+
187+
// Extracts error message from the HTML to determine whether there's an authentication issue
188+
// otherwise we'll assume it's an invalid response
189+
let errorMessage = html.findLoginErrorMessage() ?? ""
190+
191+
if html.hasInvalidCredentialsPattern(),
192+
!errorMessage.lowercased().contains(Constants.captchaText) {
187193
throw SiteCredentialLoginError.invalidCredentials
188-
}
189-
if let errorMessage = html.findLoginErrorMessage() {
190-
throw SiteCredentialLoginError.loginFailed(message: errorMessage)
191194
} else {
192195
throw SiteCredentialLoginError.invalidLoginResponse
193196
}
@@ -227,6 +230,7 @@ extension SiteCredentialLoginUseCase {
227230
static let loginPath = "/wp-login.php"
228231
static let adminPath = "/wp-admin"
229232
static let wporgNoncePath = "/admin-ajax.php?action=rest-nonce"
233+
static let captchaText = "captcha"
230234
}
231235
}
232236

0 commit comments

Comments
 (0)