Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor api user check auth0 ltr #96

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,21 @@ public static boolean isAuthDisabled() {
}

/**
* Override the current {@link #authDisabled} value.
* Override the current {@link #authDisabled} value. This is used principally for setting up test environments that
* require auth to be disabled.
*/
public static void setAuthDisabled(boolean authDisabled) {
Auth0Connection.authDisabled = authDisabled;
}

/**
* Restore default {@link #authDisabled} value. This is used principally for tearing down test environments that
* require auth to be disabled.
*/
public static void restoreDefaultAuthDisabled() {
setAuthDisabled(getDefaultAuthDisabled());
}

/**
* Confirm that the user exists in at least one of the MongoDB user collections.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.opentripplanner.middleware.models;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.mongodb.client.model.Filters;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
Expand Down Expand Up @@ -324,16 +326,6 @@ public Map<String, String> parseQueryParams() throws URISyntaxException {
).stream().collect(Collectors.toMap(NameValuePair::getName, NameValuePair::getValue));
}

/**
* Check if the trip is planned with the target time being an arriveBy or departAt query.
*
* @return true, if the trip's target time is for an arriveBy query
*/
public boolean isArriveBy() throws URISyntaxException {
// if arriveBy is not included in query params, OTP will default to false, so initialize to false
return parseQueryParams().getOrDefault("arriveBy", "false").equals("true");
}

/**
* Returns the target hour of the day that the trip is either departing at or arriving by
*/
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/org/opentripplanner/middleware/utils/JsonUtils.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package org.opentripplanner.middleware.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.type.CollectionType;
import org.opentripplanner.middleware.bugsnag.BugsnagReporter;
import org.opentripplanner.middleware.controllers.response.ResponseList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import spark.HaltException;
Expand Down Expand Up @@ -69,12 +72,20 @@ public static <T> T getPOJOFromJSON(String json, Class<T> clazz) {
return null;
}

/**
* Utility method to parse a string representing a {@link ResponseList} correctly into its parameterized type.
*/
public static <T> ResponseList<T> getResponseListFromJSON(String json, Class<T> contentClass) throws JsonProcessingException {
JavaType type = mapper.getTypeFactory().constructParametricType(ResponseList.class, contentClass);
return mapper.readValue(json, type);
}

/**
* Utility method to parse generic objects from JSON String and return as list
*/
public static <T> List<T> getPOJOFromJSONAsList(String json, Class<T> clazz) {
try {
JavaType type = mapper.getTypeFactory().constructCollectionType(List.class, clazz);
CollectionType type = mapper.getTypeFactory().constructCollectionType(List.class, clazz);
return mapper.readValue(json, type);
} catch (JsonProcessingException e) {
BugsnagReporter.reportErrorToBugsnag(
Expand Down Expand Up @@ -147,11 +158,4 @@ public static ObjectNode getObjectNode(String message, int code, Exception e) {
.put("code", code)
.put("detail", detail);
}

/**
* Get a single node value from JSON if present, else return null
*/
public static String getSingleNodeValueFromJSON(String nodeName, String json) throws JsonProcessingException {
return mapper.readTree(json).get(nodeName).textValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static org.opentripplanner.middleware.TestUtils.isEndToEnd;
import static org.opentripplanner.middleware.TestUtils.mockAuthenticatedDelete;
import static org.opentripplanner.middleware.TestUtils.mockAuthenticatedRequest;
import static org.opentripplanner.middleware.auth.Auth0Connection.getDefaultAuthDisabled;
import static org.opentripplanner.middleware.auth.Auth0Connection.isAuthDisabled;
import static org.opentripplanner.middleware.auth.Auth0Connection.restoreDefaultAuthDisabled;
import static org.opentripplanner.middleware.auth.Auth0Connection.setAuthDisabled;
import static org.opentripplanner.middleware.controllers.api.ApiUserController.DEFAULT_USAGE_PLAN_ID;

Expand All @@ -40,7 +43,6 @@ public class ApiKeyManagementTest extends OtpMiddlewareTest {
private static final Logger LOG = LoggerFactory.getLogger(ApiKeyManagementTest.class);
private static ApiUser apiUser;
private static AdminUser adminUser;
private static boolean prevAuthState;

/**
* Create an {@link ApiUser} and an {@link AdminUser} prior to unit tests
Expand All @@ -50,7 +52,6 @@ public static void setUp() throws IOException, InterruptedException {
assumeTrue(isEndToEnd);
// TODO: It might be useful to allow this to run without DISABLE_AUTH set to true (in an end-to-end environment
// using real tokens from Auth0.
prevAuthState = isAuthDisabled();
setAuthDisabled(true);
// Load config before checking if tests should run.
OtpMiddlewareTest.setUp();
Expand All @@ -68,7 +69,7 @@ public static void tearDown() {
apiUser = Persistence.apiUsers.getById(apiUser.id);
apiUser.delete();
Persistence.adminUsers.removeById(adminUser.id);
setAuthDisabled(prevAuthState);
restoreDefaultAuthDisabled();
}

/**
Expand Down Expand Up @@ -161,14 +162,14 @@ private boolean ensureApiKeyExists() {
*/
private HttpResponse<String> createApiKeyRequest(String targetUserId, AbstractUser requestingUser) {
String path = String.format("api/secure/application/%s/apikey", targetUserId);
return mockAuthenticatedRequest(path, "", requestingUser, HttpUtils.REQUEST_METHOD.POST, true);
return mockAuthenticatedRequest(path, "", requestingUser, HttpUtils.REQUEST_METHOD.POST);
}

/**
* Delete API key for target user based on authorization of requesting user
*/
private static HttpResponse<String> deleteApiKeyRequest(String targetUserId, String apiKeyId, AbstractUser requestingUser) {
String path = String.format("api/secure/application/%s/apikey/%s", targetUserId, apiKeyId);
return mockAuthenticatedRequest(path, "", requestingUser, HttpUtils.REQUEST_METHOD.DELETE, true);
return mockAuthenticatedDelete(path, requestingUser);
}
}
Loading