BearND has uploaded a new change for review. https://gerrit.wikimedia.org/r/325327
Change subject: Revert "Add CentralAuthTokenClient" ...................................................................... Revert "Add CentralAuthTokenClient" This reverts commit 8218d146ceb9205cfdf7a2f6fc74d5a6f1ec2c22. Not needed anymore since we're passing the central auth tokens through the ShareCookieManager. See patches starting with I91599b410dfbc6136838e9301290866ee733aaa8, in particular Ic3e05370d9bb028bac80446acf8b184c49b479a7 "Copy CentralAuth tokens for www.wikidata.org requests" Change-Id: Ic4f2c180f3b85599795c9bce2934c85068c99d2f --- D app/src/main/java/org/wikipedia/descriptions/centralauth/CentralAuthToken.java D app/src/main/java/org/wikipedia/descriptions/centralauth/CentralAuthTokenClient.java D app/src/main/java/org/wikipedia/descriptions/centralauth/CentralAuthTokenRetrievalFailedException.java D app/src/test/java/org/wikipedia/descriptions/centralauth/CentralAuthClientTest.java D app/src/test/res/raw/centralauth.json D app/src/test/res/raw/centralauth_notloggedin.json 6 files changed, 0 insertions(+), 206 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/apps/android/wikipedia refs/changes/27/325327/1 diff --git a/app/src/main/java/org/wikipedia/descriptions/centralauth/CentralAuthToken.java b/app/src/main/java/org/wikipedia/descriptions/centralauth/CentralAuthToken.java deleted file mode 100644 index 4df0068..0000000 --- a/app/src/main/java/org/wikipedia/descriptions/centralauth/CentralAuthToken.java +++ /dev/null @@ -1,36 +0,0 @@ -package org.wikipedia.descriptions.centralauth; - -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; - -import com.google.gson.annotations.SerializedName; - -import org.wikipedia.dataclient.mwapi.MwResponse; -import org.wikipedia.json.annotations.Required; - -/** - * Represents the Gson response of a CentralAuth token request. - * - * Note: a CentralAuthToken is only valid for a single request, and will become invalid after 10 - * seconds.[1] - * - * [1] https://www.mediawiki.org/wiki/Extension:CentralAuth/API - */ -public class CentralAuthToken extends MwResponse { - @SuppressWarnings("unused") @SerializedName("centralauthtoken") @Nullable - private Token child; - - public boolean success() { - return child != null && child.centralAuthToken != null; - } - - /** Only call if #success returns true */ - @NonNull String getToken() { - return child.centralAuthToken; - } - - private static class Token { - @SuppressWarnings("unused") @SerializedName("centralauthtoken") @Required - private String centralAuthToken; - } -} diff --git a/app/src/main/java/org/wikipedia/descriptions/centralauth/CentralAuthTokenClient.java b/app/src/main/java/org/wikipedia/descriptions/centralauth/CentralAuthTokenClient.java deleted file mode 100644 index 51ab11a..0000000 --- a/app/src/main/java/org/wikipedia/descriptions/centralauth/CentralAuthTokenClient.java +++ /dev/null @@ -1,67 +0,0 @@ -package org.wikipedia.descriptions.centralauth; - -import android.support.annotation.NonNull; -import android.support.annotation.VisibleForTesting; - -import org.wikipedia.dataclient.WikiSite; -import org.wikipedia.dataclient.retrofit.MwCachedService; -import org.wikipedia.dataclient.retrofit.RetrofitException; - -import retrofit2.Call; -import retrofit2.Response; -import retrofit2.http.GET; - -/** - * When accessing the API using a cross-domain AJAX request (CORS), use this to authenticate as the - * current SUL user. Use action=centralauthtoken on this wiki to retrieve the token, before making - * the CORS request. Each token may only be used once, and expires after 10 seconds. - */ -public class CentralAuthTokenClient { - @NonNull - private final MwCachedService<Service> cachedService = new MwCachedService<>(Service.class); - - public interface Callback { - void success(@NonNull Call<CentralAuthToken> call, @NonNull String token); - void failure(@NonNull Call<CentralAuthToken> call, @NonNull Throwable caught); - } - - @NonNull public Call<CentralAuthToken> request(@NonNull final WikiSite wiki, - @NonNull final Callback cb) { - Service service = cachedService.service(wiki); - return request(service, cb); - } - - @VisibleForTesting @NonNull Call<CentralAuthToken> request(@NonNull final Service service, - @NonNull final Callback cb) { - Call<CentralAuthToken> call = service.get(); - call.enqueue(new retrofit2.Callback<CentralAuthToken>() { - @Override - public void onResponse(Call<CentralAuthToken> call, Response<CentralAuthToken> response) { - if (response.isSuccessful()) { - final CentralAuthToken body = response.body(); - if (body.success()) { - cb.success(call, body.getToken()); - } else if (body.hasError()) { - cb.failure(call, new CentralAuthTokenRetrievalFailedException(body.getError())); - } else { - // no error and no token. Whaaat? - cb.failure(call, new RuntimeException("unexpected response from server")); - } - } else { - cb.failure(call, RetrofitException.httpError(response, cachedService.retrofit())); - } - } - - @Override - public void onFailure(Call<CentralAuthToken> call, Throwable t) { - cb.failure(call, t); - } - }); - return call; - } - - @VisibleForTesting interface Service { - @GET("w/api.php?action=centralauthtoken&format=json") - Call<CentralAuthToken> get(); - } -} diff --git a/app/src/main/java/org/wikipedia/descriptions/centralauth/CentralAuthTokenRetrievalFailedException.java b/app/src/main/java/org/wikipedia/descriptions/centralauth/CentralAuthTokenRetrievalFailedException.java deleted file mode 100644 index 439a6d2..0000000 --- a/app/src/main/java/org/wikipedia/descriptions/centralauth/CentralAuthTokenRetrievalFailedException.java +++ /dev/null @@ -1,16 +0,0 @@ -package org.wikipedia.descriptions.centralauth; - -import org.wikipedia.server.mwapi.MwServiceError; - -public class CentralAuthTokenRetrievalFailedException extends Exception { - private final MwServiceError error; - - public CentralAuthTokenRetrievalFailedException(MwServiceError error) { - super(error.getTitle()); - this.error = error; - } - - public MwServiceError getError() { - return error; - } -} diff --git a/app/src/test/java/org/wikipedia/descriptions/centralauth/CentralAuthClientTest.java b/app/src/test/java/org/wikipedia/descriptions/centralauth/CentralAuthClientTest.java deleted file mode 100644 index 333a57c..0000000 --- a/app/src/test/java/org/wikipedia/descriptions/centralauth/CentralAuthClientTest.java +++ /dev/null @@ -1,74 +0,0 @@ -package org.wikipedia.descriptions.centralauth; - -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; - -import com.google.gson.stream.MalformedJsonException; - -import org.junit.Test; -import org.wikipedia.descriptions.centralauth.CentralAuthTokenClient.Callback; -import org.wikipedia.descriptions.centralauth.CentralAuthTokenClient.Service; -import org.wikipedia.test.MockWebServerTest; - -import retrofit2.Call; - -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.eq; -import static org.mockito.Matchers.isA; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; - -public class CentralAuthClientTest extends MockWebServerTest { - @NonNull private final CentralAuthTokenClient subject = new CentralAuthTokenClient(); - - @Test public void testRequestSuccess() throws Throwable { - enqueueFromFile("centralauth.json"); - - Callback cb = mock(Callback.class); - Call<CentralAuthToken> call = request(cb); - - server().takeRequest(); - assertCallbackSuccess(call, cb, "86bd5e1b225ec3e31ec98ac0526867031d8cd4b"); - } - - @Test public void testRequestResponseFailure() throws Throwable { - enqueueFromFile("centralauth_notloggedin.json"); - - Callback cb = mock(Callback.class); - Call<CentralAuthToken> call = request(cb); - - server().takeRequest(); - assertCallbackFailure(call, cb, CentralAuthTokenRetrievalFailedException.class); - } - - @Test public void testRequestResponseMalformed() throws Throwable { - server().enqueue("'"); - - Callback cb = mock(Callback.class); - Call<CentralAuthToken> call = request(cb); - - server().takeRequest(); - assertCallbackFailure(call, cb, MalformedJsonException.class); - } - - private void assertCallbackSuccess(@NonNull Call<CentralAuthToken> call, - @NonNull Callback cb, - @Nullable String expectedToken) { - verify(cb).success(eq(call), eq(expectedToken)); - //noinspection unchecked - verify(cb, never()).failure(any(Call.class), any(Throwable.class)); - } - - private void assertCallbackFailure(@NonNull Call<CentralAuthToken> call, - @NonNull Callback cb, - @NonNull Class<? extends Throwable> throwable) { - //noinspection unchecked - verify(cb, never()).success(any(Call.class), any(String.class)); - verify(cb).failure(eq(call), isA(throwable)); - } - - private Call<CentralAuthToken> request(@NonNull Callback cb) { - return subject.request(service(Service.class), cb); - } -} \ No newline at end of file diff --git a/app/src/test/res/raw/centralauth.json b/app/src/test/res/raw/centralauth.json deleted file mode 100644 index 3096148..0000000 --- a/app/src/test/res/raw/centralauth.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "centralauthtoken": { - "centralauthtoken": "86bd5e1b225ec3e31ec98ac0526867031d8cd4b" - } -} \ No newline at end of file diff --git a/app/src/test/res/raw/centralauth_notloggedin.json b/app/src/test/res/raw/centralauth_notloggedin.json deleted file mode 100644 index 47a1ef0..0000000 --- a/app/src/test/res/raw/centralauth_notloggedin.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "servedby": "mw1280", - "error": { - "code": "notloggedin", - "info": "Anonymous users cannot obtain a centralauthtoken", - "*": "See https://www.wikidata.org/w/api.php for API usage" - } -} \ No newline at end of file -- To view, visit https://gerrit.wikimedia.org/r/325327 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ic4f2c180f3b85599795c9bce2934c85068c99d2f Gerrit-PatchSet: 1 Gerrit-Project: apps/android/wikipedia Gerrit-Branch: master Gerrit-Owner: BearND <bsitzm...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits