Skip to content

Commit

Permalink
Allow login for WPCOM suspended sites (#858)
Browse files Browse the repository at this point in the history
  • Loading branch information
selanthiraiyan authored Nov 14, 2024
2 parents c545aa5 + 1fd4b3e commit f591b6e
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 11 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ _None._

### New Features

_None._
- Add support for logging in into WPCOM suspended sites.

### Bug Fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ public struct WordPressAuthenticatorConfiguration {
///
let enableSiteCreationGuide: Bool

/// If enabled allows login into sites marked as suspended in WordPress.com
///
let enableSiteCredentialsLoginForWPCOMSuspendedSites: Bool

/// Designated Initializer
///
public init (wpcomClientId: String,
Expand Down Expand Up @@ -215,7 +219,8 @@ public struct WordPressAuthenticatorConfiguration {
enableManualErrorHandlingForSiteCredentialLogin: Bool = false,
useEnterEmailAddressAsStepValueForGetStartedVC: Bool = false,
enableSiteAddressLoginOnlyInPrologue: Bool = false,
enableSiteCreationGuide: Bool = false
enableSiteCreationGuide: Bool = false,
enableSiteCredentialsLoginForWPCOMSuspendedSites: Bool = false
) {

self.wpcomClientId = wpcomClientId
Expand Down Expand Up @@ -254,5 +259,6 @@ public struct WordPressAuthenticatorConfiguration {
self.useEnterEmailAddressAsStepValueForGetStartedVC = useEnterEmailAddressAsStepValueForGetStartedVC
self.enableSiteAddressLoginOnlyInPrologue = enableSiteAddressLoginOnlyInPrologue
self.enableSiteCreationGuide = enableSiteCreationGuide
self.enableSiteCredentialsLoginForWPCOMSuspendedSites = enableSiteCredentialsLoginForWPCOMSuspendedSites
}
}
13 changes: 13 additions & 0 deletions WordPressAuthenticator/Model/WordPressComSiteInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ public class WordPressComSiteInfo {
///
public let exists: Bool

public init(name: String, tagline: String, url: String, hasJetpack: Bool, isJetpackActive: Bool, isJetpackConnected: Bool, icon: String, isWPCom: Bool, isWP: Bool, exists: Bool) {
self.name = name
self.tagline = tagline
self.url = url
self.hasJetpack = hasJetpack
self.isJetpackActive = isJetpackActive
self.isJetpackConnected = isJetpackConnected
self.icon = icon
self.isWPCom = isWPCom
self.isWP = isWP
self.exists = exists
}

/// Initializes the current SiteInfo instance with a raw dictionary.
///
public init(remote: [AnyHashable: Any]) {
Expand Down
31 changes: 22 additions & 9 deletions WordPressAuthenticator/Services/WordPressComBlogService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ class WordPressComBlogService {

remote.fetchSiteInfo(forAddress: address, success: { response in
guard let response = response else {
failure(ServiceError.unknown)
failure(WordPressComBlogServiceError.unknown)
return
}

let site = WordPressComSiteInfo(remote: response)
success(site)

}, failure: { error in
let result = error ?? ServiceError.unknown
let result = error ?? WordPressComBlogServiceError.unknown
failure(result)
})
}
Expand All @@ -37,18 +37,28 @@ class WordPressComBlogService {
let remote = BlogServiceRemoteREST(wordPressComRestApi: anonymousAPI, siteID: 0)
remote.fetchUnauthenticatedSiteInfo(forAddress: address, success: { response in
guard let response = response else {
failure(ServiceError.unknown)
failure(WordPressComBlogServiceError.unknown)
return
}

let site = WordPressComSiteInfo(remote: response)
guard site.url != Constants.wordPressBlogURL else {
failure(ServiceError.invalidWordPressAddress)
failure(WordPressComBlogServiceError.invalidWordPressAddress)
return
}
success(site)
}, failure: { error in
let result = error ?? ServiceError.unknown
let result: Error = {
/// Check whether the site is suspended on WordPress.com and can't be connected using Jetpack
///
if let apiError = error as? WordPressAPIError<WordPressComRestApiEndpointError>,
case let .endpointError(endpointError) = apiError,
endpointError.apiErrorCode == "connection_disabled" {
return WordPressComBlogServiceError.wpcomSiteSuspended
}

return error ?? WordPressComBlogServiceError.unknown
}()
failure(result)
})
}
Expand All @@ -60,9 +70,12 @@ extension WordPressComBlogService {
enum Constants {
static let wordPressBlogURL = "https://wordpress.com/blog"
}
}

enum ServiceError: Error {
case unknown
case invalidWordPressAddress
}
public enum WordPressComBlogServiceError: Error {
case unknown
case invalidWordPressAddress
/// Whether the site is suspended on WordPress.com and can't be connected using Jetpack
///
case wpcomSiteSuspended
}
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,23 @@ private extension SiteAddressViewController {
self?.navigationController?.pushViewController(customUI, animated: true)
}
} else {
// Don't display error and allow login for suspended sites
//
if configuration.enableSiteCredentialsLoginForWPCOMSuspendedSites,
let serviceError = error as? WordPressComBlogServiceError,
serviceError == .wpcomSiteSuspended {
successBlock(WordPressComSiteInfo(name: "",
tagline: "",
url: baseSiteUrl,
hasJetpack: false,
isJetpackActive: false,
isJetpackConnected: false,
icon: "",
isWPCom: false,
isWP: true,
exists: true))
return
}
self.displayError(message: Localization.invalidURL)
}
})
Expand Down

0 comments on commit f591b6e

Please sign in to comment.