Skip to content

Commit

Permalink
Merge f7f2082 into 20de6f7
Browse files Browse the repository at this point in the history
  • Loading branch information
wetneb committed Mar 5, 2019
2 parents 20de6f7 + f7f2082 commit 3fcf445
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 1 deletion.
Expand Up @@ -160,6 +160,11 @@ public class ApiConnection {
*/
final static String LOGIN_WRONG_TOKEN = "WrongToken";

/**
* MediaWiki assert= parameter to ensure we are editting while logged in.
*/
private static final String ASSERT_PARAMETER = "assert";

/**
* URL to access the Wikibase API.
*/
Expand All @@ -179,7 +184,7 @@ public class ApiConnection {
*/
@JsonIgnore
String password = "";

/**
* Map of cookies that are currently set.
*/
Expand Down Expand Up @@ -394,6 +399,9 @@ String fetchToken(String tokenType) throws IOException, MediaWikiApiErrorExcepti
*/
public JsonNode sendJsonRequest(String requestMethod, Map<String,String> parameters) throws IOException, MediaWikiApiErrorException {
parameters.put(ApiConnection.PARAM_FORMAT, "json");
if (loggedIn) {
parameters.put(ApiConnection.ASSERT_PARAMETER, "user");
}
try (InputStream response = sendRequest(requestMethod, parameters)) {
JsonNode root = this.mapper.readTree(response);
this.checkErrors(root);
Expand Down Expand Up @@ -475,6 +483,24 @@ public void logWarnings(JsonNode root) {
logger.warn("API warning " + warning);
}
}

/**
* Checks that the credentials are still valid for the
* user currently logged in. This can fail if (for instance)
* the cookies expired, or were invalidated by a logout from
* a different client.
*
* This method queries the API and throws {@class AssertUserFailedException}
* if the check failed. This does not update the state of the connection
* object.
* @throws MediaWikiApiErrorException
* @throws IOException
*/
public void checkCredentials() throws IOException, MediaWikiApiErrorException {
Map<String,String> parameters = new HashMap<>();
parameters.put("action", "query");
sendJsonRequest("POST", parameters);
}

/**
* Extracts warnings that are returned in an API response.
Expand Down
@@ -0,0 +1,37 @@
package org.wikidata.wdtk.wikibaseapi.apierrors;

/*
* #%L
* Wikidata Toolkit Wikibase API
* %%
* Copyright (C) 2014 - 2015 Wikidata Toolkit Developers
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

/**
* Exception to indicate that we tried to perform an action while our login
* credentials have expired. See
* <a href="https://www.mediawiki.org/wiki/API:Assert">MediaWiki documentation</a>.
*
* @author Antonin Delpeuch
*
*/
public class AssertUserFailedException extends MediaWikiApiErrorException {

public AssertUserFailedException(String errorMessage) {
super(MediaWikiApiErrorHandler.ERROR_NO_SUCH_ENTITY, errorMessage);
}

}
Expand Up @@ -33,6 +33,7 @@ public class MediaWikiApiErrorHandler {
public final static String ERROR_INVALID_TOKEN = "badtoken";
public final static String ERROR_NO_SUCH_ENTITY = "no-such-entity";
public final static String ERROR_MAXLAG = "maxlag";
private static final String ERROR_ASSERT_USER_FAILED = "assertuserfailed";

/**
* Creates and throws a suitable {@link MediaWikiApiErrorException} for the
Expand All @@ -58,6 +59,8 @@ public static void throwMediaWikiApiErrorException(String errorCode,
throw new NoSuchEntityErrorException(errorMessage);
case ERROR_MAXLAG:
throw new MaxlagErrorException(errorMessage);
case ERROR_ASSERT_USER_FAILED:
throw new AssertUserFailedException(errorMessage);
default:
throw new MediaWikiApiErrorException(errorCode, errorMessage);
}
Expand Down
Expand Up @@ -41,6 +41,7 @@
import org.junit.Before;
import org.junit.Test;
import org.wikidata.wdtk.util.CompressionType;
import org.wikidata.wdtk.wikibaseapi.apierrors.AssertUserFailedException;
import org.wikidata.wdtk.wikibaseapi.apierrors.MediaWikiApiErrorException;

import com.fasterxml.jackson.core.JsonProcessingException;
Expand Down Expand Up @@ -114,8 +115,16 @@ public void setUp() throws Exception {

params.clear();
params.put("action", "logout");
params.put("assert", "user");
params.put("format", "json");
this.con.setWebResource(params, "{}");

params.put("action", "query");
params.put("assert", "user");
params.put("format", "json");
this.con.setWebResourceFromPath(params, this.getClass(),
"/assert-user-failed.json", CompressionType.NONE);
params.clear();
}

@Test
Expand Down Expand Up @@ -319,6 +328,15 @@ public void testErrorMessages() {
i++;
}
}

@Test(expected = AssertUserFailedException.class)
public void testCheckCredentials() throws IOException, MediaWikiApiErrorException, LoginFailedException {
// we first login successfully
this.con.login("username", "password");
assertTrue(this.con.isLoggedIn());
// after a while, the credentials expire
this.con.checkCredentials();
}

private List<String> testCookieList() {
List<String> cookieList = new ArrayList<String>();
Expand Down
@@ -0,0 +1 @@
{"error":{"code":"assertuserfailed","info":"Assertion that the user is logged in failed.","*":"See https://www.wikidata.org/w/api.php for API usage. Subscribe to the mediawiki-api-announce mailing list at &lt;https://lists.wikimedia.org/mailman/listinfo/mediawiki-api-announce&gt; for notice of API deprecations and breaking changes."},"servedby":"mw1347"}

0 comments on commit 3fcf445

Please sign in to comment.