Skip to content

Commit 31a11ed

Browse files
committed
Check response of nonce retrieval request
1 parent 12269c0 commit 31a11ed

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

WooCommerce/Classes/Authentication/SiteCredentialLoginUseCase.swift

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,18 +159,33 @@ private extension SiteCredentialLoginUseCase {
159159
throw SiteCredentialLoginError.invalidLoginResponse
160160
}
161161

162+
let isNonceUrl = response.url?.absoluteString.contains(Constants.wporgNoncePath) == true
163+
162164
switch response.statusCode {
163165
case 404:
164-
throw SiteCredentialLoginError.inaccessibleLoginPage
165-
case 200:
166-
guard let html = String(data: data, encoding: .utf8) else {
167-
throw SiteCredentialLoginError.invalidLoginResponse
166+
if isNonceUrl {
167+
throw SiteCredentialLoginError.inaccessibleAdminPage
168+
} else {
169+
throw SiteCredentialLoginError.inaccessibleLoginPage
168170
}
169-
if html.hasInvalidCredentialsPattern() {
170-
throw SiteCredentialLoginError.invalidCredentials
171-
}
172-
if let errorMessage = html.findLoginErrorMessage() {
173-
throw SiteCredentialLoginError.loginFailed(message: errorMessage)
171+
case 200:
172+
if isNonceUrl {
173+
// Means success
174+
// But maybe we can also validate the nonce format like Android https://github.com/woocommerce/woocommerce-android/blob/ea4a48355b5ca4d49dc27e91566aaed304ab5916/libs/fluxc/src/main/java/org/wordpress/android/fluxc/network/rest/wpapi/NonceRestClient.kt#L120
175+
return
176+
} else {
177+
// 200 for the login URL, which means a failure
178+
guard let html = String(data: data, encoding: .utf8) else {
179+
throw SiteCredentialLoginError.invalidLoginResponse
180+
}
181+
if html.hasInvalidCredentialsPattern() {
182+
throw SiteCredentialLoginError.invalidCredentials
183+
}
184+
if let errorMessage = html.findLoginErrorMessage() {
185+
throw SiteCredentialLoginError.loginFailed(message: errorMessage)
186+
} else {
187+
throw SiteCredentialLoginError.invalidLoginResponse
188+
}
174189
}
175190
default:
176191
throw SiteCredentialLoginError.unacceptableStatusCode(code: response.statusCode)

0 commit comments

Comments
 (0)