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

Add perPage param to get users endpoint #353

Merged
merged 2 commits into from
Nov 12, 2020
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
27 changes: 18 additions & 9 deletions src/main/java/com/conveyal/datatools/manager/auth/Auth0Users.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class Auth0Users {
private static final String SEARCH_API_VERSION = "v3";
public static final String API_PATH = "/api/" + MANAGEMENT_API_VERSION;
public static final String USERS_API_PATH = API_PATH + "/users";
public static final int DEFAULT_ITEMS_PER_PAGE = 10;
// Cached API token so that we do not have to request a new one each time a Management API request is made.
private static Auth0AccessToken cachedToken = null;
private static final ObjectMapper mapper = new ObjectMapper();
Expand All @@ -48,20 +49,21 @@ public class Auth0Users {
/**
* Overload of user search query URL to restrict search to current users only.
*/
private static URI getUrl(String searchQuery, int page, int perPage, boolean includeTotals) {
return getUrl(searchQuery, page, perPage, includeTotals, true);
private static URI getSearchUrl(String searchQuery, int page, int perPage, boolean includeTotals) {
return getSearchUrl(searchQuery, page, perPage, includeTotals, true);
}

/**
* Constructs a user search query URL.
* @param searchQuery search query to perform (null value implies default query)
* @param page which page of users to return
* @param perPage number of users to return per page
* @param perPage number of users to return per page. Max value is 1000 per Auth0 docs:
* https://auth0.com/docs/users/user-search/view-search-results-by-page#limitation
* @param includeTotals whether to include the total number of users in search results
* @param limitToCurrentUsers whether to restrict the search to current users only
* @return URI to perform the search query
*/
private static URI getUrl(String searchQuery, int page, int perPage, boolean includeTotals, boolean limitToCurrentUsers) {
private static URI getSearchUrl(String searchQuery, int page, int perPage, boolean includeTotals, boolean limitToCurrentUsers) {
// Filter users by datatools client_id, unless excluded (by limitToCurrentUsers) and a search query is provided.
// This allows for a less restricted, wider search to be carried out on tenant users.
String searchCurrentUsersOnly = "app_metadata.datatools.client_id:" + clientId;
Expand Down Expand Up @@ -223,8 +225,15 @@ public static Auth0AccessToken getCachedApiToken() {
* @return JSON string of users matching search query
*/
public static String getAuth0Users(String searchQuery, int page) {
return getAuth0Users(searchQuery, page, DEFAULT_ITEMS_PER_PAGE);
}

URI uri = getUrl(searchQuery, page, 10, false);
/**
* Wrapper method for performing user search.
* @return JSON string of users matching search query
*/
public static String getAuth0Users(String searchQuery, int page, int perPage) {
URI uri = getSearchUrl(searchQuery, page, perPage, false);
return doRequest(uri);
}

Expand All @@ -235,7 +244,7 @@ public static String getAuth0Users(String searchQuery, int page) {
* @return JSON string of users matching search query.
*/
public static String getUnrestrictedAuth0Users(String searchQuery) {
URI uri = getUrl(searchQuery, 0, 10, false, false);
URI uri = getSearchUrl(searchQuery, 0, DEFAULT_ITEMS_PER_PAGE, false, false);
return doRequest(uri);
}

Expand Down Expand Up @@ -280,7 +289,7 @@ public static Auth0UserProfile getUserById(String id) {
*/
private static URIBuilder getURIBuilder() {
URIBuilder builder = new URIBuilder();
if (AUTH0_DOMAIN.equals("your-auth0-domain")) {
if ("your-auth0-domain".equals(AUTH0_DOMAIN)) {
// set items for testing purposes assuming use of a Wiremock server
builder.setScheme("http");
builder.setPort(8089);
Expand Down Expand Up @@ -315,7 +324,7 @@ public static Set<String> getVerifiedEmailsBySubscription(String subscriptionTyp
continue;
}
String email = user.get("email").asText();
Boolean emailVerified = user.get("email_verified").asBoolean();
boolean emailVerified = user.get("email_verified").asBoolean();
// only send email if address has been verified
if (!emailVerified) {
LOG.warn("Skipping user {}. User's email address has not been verified.", email);
Expand All @@ -331,7 +340,7 @@ public static Set<String> getVerifiedEmailsBySubscription(String subscriptionTyp
* Get number of users for the application.
*/
public static int getAuth0UserCount(String searchQuery) throws IOException {
URI uri = getUrl(searchQuery, 0, 1, true);
URI uri = getSearchUrl(searchQuery, 0, 1, true);
String result = doRequest(uri);
JsonNode jsonNode = new ObjectMapper().readTree(result);
return jsonNode.get("total").asInt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import static com.conveyal.datatools.common.utils.SparkUtils.logMessageAndHalt;
import static com.conveyal.datatools.manager.auth.Auth0Users.API_PATH;
import static com.conveyal.datatools.manager.auth.Auth0Connection.authDisabled;
import static com.conveyal.datatools.manager.auth.Auth0Users.DEFAULT_ITEMS_PER_PAGE;
import static com.conveyal.datatools.manager.auth.Auth0Users.USERS_API_PATH;
import static com.conveyal.datatools.manager.auth.Auth0Users.getAuth0Users;
import static com.conveyal.datatools.manager.auth.Auth0Users.getUserById;
Expand All @@ -61,7 +62,7 @@ public class UserController {
private static final String AUTH0_CLIENT_ID = DataManager.getConfigPropertyAsText("AUTH0_CLIENT_ID");
public static final int TEST_AUTH0_PORT = 8089;
public static final String TEST_AUTH0_DOMAIN = String.format("localhost:%d", TEST_AUTH0_PORT);
private static Logger LOG = LoggerFactory.getLogger(UserController.class);
private static final Logger LOG = LoggerFactory.getLogger(UserController.class);
private static final String UTF_8 = "UTF-8";
public static final String DEFAULT_BASE_USERS_URL = "https://" + AUTH0_DOMAIN + USERS_API_PATH;
/** Users URL uses Auth0 domain by default, but can be overridden with {@link #setBaseUsersUrl(String)} for testing. */
Expand Down Expand Up @@ -92,8 +93,9 @@ public static boolean inTestingEnvironment() {
private static String getAllUsers(Request req, Response res) {
res.type("application/json");
int page = Integer.parseInt(req.queryParams("page"));
int perPage = Integer.parseInt(req.queryParamOrDefault("perPage", Integer.toString(DEFAULT_ITEMS_PER_PAGE)));
String queryString = filterUserSearchQuery(req);
String users = getAuth0Users(queryString, page);
String users = getAuth0Users(queryString, page, perPage);
return users;
}

Expand Down