Skip to content

Commit ac7d739

Browse files
Support basic auth when fetching nonce with site credentials
1 parent 3cbb8c3 commit ac7d739

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

libs/fluxc/src/main/java/org/wordpress/android/fluxc/network/rest/wpapi/NonceRestClient.kt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.wordpress.android.fluxc.network.rest.wpapi
22

33
import com.android.volley.NoConnectionError
44
import com.android.volley.RequestQueue
5+
import okhttp3.Credentials
56
import org.wordpress.android.fluxc.Dispatcher
67
import org.wordpress.android.fluxc.model.SiteModel
78
import org.wordpress.android.fluxc.network.UserAgent
@@ -57,8 +58,14 @@ class NonceRestClient @Inject constructor(
5758
"pwd" to password,
5859
"redirect_to" to redirectUrl
5960
)
61+
val authHeader = Credentials.basic(username, password)
6062
val response =
61-
wpApiEncodedBodyRequestBuilder.syncPostRequest(this, wpLoginUrl, body = body)
63+
wpApiEncodedBodyRequestBuilder.syncPostRequest(
64+
this,
65+
wpLoginUrl,
66+
body = body,
67+
authHeader = authHeader
68+
)
6269
val nonce = when (response) {
6370
is Success -> {
6471
// A success means we got 200 from the wp-login.php call, which means
@@ -92,7 +99,7 @@ class NonceRestClient @Inject constructor(
9299
} else {
93100
val networkResponse = response.error.volleyError?.networkResponse
94101
if (networkResponse?.statusCode?.isRedirect() == true) {
95-
requestNonce(networkResponse.headers?.get("Location") ?: redirectUrl, username)
102+
requestNonceFromRedirect(networkResponse.headers?.get("Location") ?: redirectUrl, username, password)
96103
} else {
97104
FailedRequest(
98105
timeOfResponse = currentTimeProvider.currentDate().time,
@@ -112,9 +119,14 @@ class NonceRestClient @Inject constructor(
112119
}
113120
}
114121

115-
private suspend fun requestNonce(redirectUrl: String, username: String): Nonce {
122+
private suspend fun requestNonceFromRedirect(redirectUrl: String, username: String, password: String): Nonce {
123+
val authHeader = Credentials.basic(username, password)
116124
return when (
117-
val response = wpApiEncodedBodyRequestBuilder.syncGetRequest(this, redirectUrl)
125+
val response = wpApiEncodedBodyRequestBuilder.syncGetRequest(
126+
this,
127+
redirectUrl,
128+
authHeader = authHeader
129+
)
118130
) {
119131
is Success -> {
120132
if (response.data?.matches("[0-9a-zA-Z]{2,}".toRegex()) == true) {

libs/fluxc/src/main/java/org/wordpress/android/fluxc/network/rest/wpapi/WPAPIEncodedBodyRequestBuilder.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ class WPAPIEncodedBodyRequestBuilder @Inject constructor() {
1717
body: Map<String, String> = emptyMap(),
1818
enableCaching: Boolean = false,
1919
cacheTimeToLive: Int = BaseRequest.DEFAULT_CACHE_LIFETIME,
20-
nonce: String? = null
20+
nonce: String? = null,
21+
authHeader: String? = null
2122
) = suspendCancellableCoroutine<WPAPIResponse<String>> { cont ->
22-
callMethod(Method.GET, url, params, body, cont, enableCaching, cacheTimeToLive, nonce, restClient)
23+
callMethod(Method.GET, url, params, body, cont, enableCaching, cacheTimeToLive, nonce, authHeader, restClient)
2324
}
2425

2526
suspend fun syncPostRequest(
@@ -29,9 +30,10 @@ class WPAPIEncodedBodyRequestBuilder @Inject constructor() {
2930
body: Map<String, String> = emptyMap(),
3031
enableCaching: Boolean = false,
3132
cacheTimeToLive: Int = BaseRequest.DEFAULT_CACHE_LIFETIME,
32-
nonce: String? = null
33+
nonce: String? = null,
34+
authHeader: String? = null
3335
) = suspendCancellableCoroutine<WPAPIResponse<String>> { cont ->
34-
callMethod(Method.POST, url, params, body, cont, enableCaching, cacheTimeToLive, nonce, restClient)
36+
callMethod(Method.POST, url, params, body, cont, enableCaching, cacheTimeToLive, nonce, authHeader, restClient)
3537
}
3638

3739
@Suppress("LongParameterList")
@@ -44,6 +46,7 @@ class WPAPIEncodedBodyRequestBuilder @Inject constructor() {
4446
enableCaching: Boolean,
4547
cacheTimeToLive: Int,
4648
nonce: String?,
49+
authHeader: String?,
4750
restClient: BaseWPAPIRestClient
4851
) {
4952
val request = WPAPIEncodedBodyRequest(method, url, params, body, { response, headers ->
@@ -64,6 +67,10 @@ class WPAPIEncodedBodyRequestBuilder @Inject constructor() {
6467
request.addHeader("x-wp-nonce", nonce)
6568
}
6669

70+
if (authHeader != null) {
71+
request.addHeader("Authorization", authHeader)
72+
}
73+
6774
restClient.add(request)
6875
}
6976
}

0 commit comments

Comments
 (0)