@@ -100,8 +100,9 @@ enum SiteCredentialLoginError: LocalizedError {
100100/// This use case handles site credential login without the need to use XMLRPC API.
101101/// Steps for login:
102102/// - Make a request to the site wp-login.php with a redirect to the nonce retrieval URL.
103- /// - Upon redirect, cancel the request and verify if the redirect URL is the nonce retrieval URL.
104- /// - If it is, make a request to retrieve nonce at that URL, the login succeeds if this is successful.
103+ /// - If the redirect succeeds with a nonce in the response, login is successful.
104+ /// - If the request does not redirect or the redirect fails, login fails.
105+ /// Ref: pe5sF9-1iQ-p2
105106///
106107final class SiteCredentialLoginUseCase : NSObject , SiteCredentialLoginProtocol {
107108 private let siteURL : String
@@ -161,32 +162,31 @@ private extension SiteCredentialLoginUseCase {
161162
162163 let isNonceUrl = response. url? . absoluteString. hasSuffix ( Constants . wporgNoncePath) == true
163164
164- switch response. statusCode {
165- case 404 :
166- if isNonceUrl {
167- throw SiteCredentialLoginError . inaccessibleAdminPage
168- } else {
169- throw SiteCredentialLoginError . inaccessibleLoginPage
170- }
171- case 200 :
172- if isNonceUrl,
173- let nonceString = String ( data: data, encoding: . utf8) ,
165+ switch ( isNonceUrl, response. statusCode) {
166+ case ( true , 200 ) :
167+ if let nonceString = String ( data: data, encoding: . utf8) ,
174168 nonceString. isValidNonce ( ) {
175169 // success!
176170 return
177171 } else {
178- // 200 for the login URL, which means a failure
179- guard let html = String ( data: data, encoding: . utf8) else {
180- throw SiteCredentialLoginError . invalidLoginResponse
181- }
182- if html. hasInvalidCredentialsPattern ( ) {
183- throw SiteCredentialLoginError . invalidCredentials
184- }
185- if let errorMessage = html. findLoginErrorMessage ( ) {
186- throw SiteCredentialLoginError . loginFailed ( message: errorMessage)
187- } else {
188- throw SiteCredentialLoginError . invalidLoginResponse
189- }
172+ throw SiteCredentialLoginError . invalidLoginResponse
173+ }
174+ case ( true , 404 ) :
175+ throw SiteCredentialLoginError . inaccessibleAdminPage
176+ case ( false , 404 ) :
177+ throw SiteCredentialLoginError . inaccessibleLoginPage
178+ case ( false , 200 ) :
179+ // 200 for the login URL, which means a failure
180+ guard let html = String ( data: data, encoding: . utf8) else {
181+ throw SiteCredentialLoginError . invalidLoginResponse
182+ }
183+ if html. hasInvalidCredentialsPattern ( ) {
184+ throw SiteCredentialLoginError . invalidCredentials
185+ }
186+ if let errorMessage = html. findLoginErrorMessage ( ) {
187+ throw SiteCredentialLoginError . loginFailed ( message: errorMessage)
188+ } else {
189+ throw SiteCredentialLoginError . invalidLoginResponse
190190 }
191191 default :
192192 throw SiteCredentialLoginError . unacceptableStatusCode ( code: response. statusCode)
0 commit comments