Skip to content

Commit

Permalink
disallow API token lookup via API by default #3153
Browse files Browse the repository at this point in the history
  • Loading branch information
pdurbin committed Oct 12, 2017
1 parent 449be20 commit fd743e2
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 7 deletions.
7 changes: 0 additions & 7 deletions doc/sphinx-guides/source/api/native-api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -437,13 +437,6 @@ Place this ``user-add.json`` file in your current directory and run the followin

curl -d @user-add.json -H "Content-type:application/json" "$SERVER_URL/api/builtin-users?password=$NEWUSER_PASSWORD&key=$BUILTIN_USERS_KEY"

Retrieving the API Token of a Builtin User
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

To retrieve the API token of a builtin user, given that user's password, use the curl command below::

curl "$SERVER_URL/api/builtin-users/$DV_USER_NAME/api-token?password=$DV_USER_PASSWORD"

Roles
~~~~~

Expand Down
7 changes: 7 additions & 0 deletions doc/sphinx-guides/source/installation/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1204,3 +1204,10 @@ You can replace the default dataset metadata fields that are displayed above fil
``curl http://localhost:8080/api/admin/settings/:CustomDatasetSummaryFields -X PUT -d 'producer,subtitle,alternativeTitle'``

You have to put the datasetFieldType name attribute in the :CustomDatasetSummaryFields setting for this to work.

:AllowApiTokenLookupViaApi
++++++++++++++++++++++++++

Dataverse 4.8.1 and below allowed API Token lookup via API but for better security this has been disabled by default. Set this to true if you really want the old behavior.

``curl -X PUT -d 'true' http://localhost:8080/api/admin/settings/:AllowApiTokenLookupViaApi``
9 changes: 9 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/api/BuiltinUsers.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import edu.harvard.iq.dataverse.authorization.providers.builtin.PasswordEncryption;
import edu.harvard.iq.dataverse.authorization.users.ApiToken;
import edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser;
import edu.harvard.iq.dataverse.settings.SettingsServiceBean;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.logging.Level;
Expand Down Expand Up @@ -53,6 +54,14 @@ public class BuiltinUsers extends AbstractApiBean {
@GET
@Path("{username}/api-token")
public Response getApiToken( @PathParam("username") String username, @QueryParam("password") String password ) {
boolean disabled = true;
boolean lookupAllowed = settingsSvc.isTrueForKey(SettingsServiceBean.Key.AllowApiTokenLookupViaApi, false);
if (lookupAllowed) {
disabled = false;
}
if (disabled) {
return error(Status.FORBIDDEN, "This API endpoint has been disabled.");
}
BuiltinUser u = null;
if (retrievingApiTokenViaEmailEnabled) {
u = builtinUserSvc.findByUsernameOrEmail(username);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class SettingsServiceBean {
* So there.
*/
public enum Key {
AllowApiTokenLookupViaApi,
/**
* Ordered, comma-separated list of custom fields to show above the fold
* on dataset page such as "data_type,sample,pdb"
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/api/BuiltinUsersIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import javax.json.Json;
import javax.json.JsonObjectBuilder;
import static javax.ws.rs.core.Response.Status.OK;
import static javax.ws.rs.core.Response.Status.FORBIDDEN;
import static junit.framework.Assert.assertEquals;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.Matchers.startsWith;
Expand All @@ -37,6 +38,11 @@ public class BuiltinUsersIT {
@BeforeClass
public static void setUp() {
RestAssured.baseURI = UtilIT.getRestAssuredBaseUri();

Response removeIdentifierGenerationStyle = UtilIT.deleteSetting(SettingsServiceBean.Key.AllowApiTokenLookupViaApi);
removeIdentifierGenerationStyle.then().assertThat()
.statusCode(200);

}

@Test
Expand Down Expand Up @@ -171,6 +177,15 @@ public void testLogin() {
String createdToken = createdUser.getString("data.apiToken");
logger.info(createdToken);

Response getApiTokenShouldFail = getApiTokenUsingUsername(usernameToCreate, usernameToCreate);
getApiTokenShouldFail.then().assertThat()
.body("message", equalTo("This API endpoint has been disabled."))
.statusCode(FORBIDDEN.getStatusCode());

Response setAllowApiTokenLookupViaApi = UtilIT.setSetting(SettingsServiceBean.Key.AllowApiTokenLookupViaApi, "true");
setAllowApiTokenLookupViaApi.then().assertThat()
.statusCode(OK.getStatusCode());

Response getApiTokenUsingUsername = getApiTokenUsingUsername(usernameToCreate, usernameToCreate);
getApiTokenUsingUsername.prettyPrint();
assertEquals(200, getApiTokenUsingUsername.getStatusCode());
Expand All @@ -189,6 +204,10 @@ public void testLogin() {
assertEquals(createdToken, retrievedTokenUsingEmail);
}

Response removeIdentifierGenerationStyle = UtilIT.deleteSetting(SettingsServiceBean.Key.AllowApiTokenLookupViaApi);
removeIdentifierGenerationStyle.then().assertThat()
.statusCode(200);

}

@Test
Expand Down

0 comments on commit fd743e2

Please sign in to comment.