Skip to content

Commit

Permalink
fix: owner and pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Sep 18, 2024
1 parent 095a27f commit da96bc4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
import io.supertokens.oauth.exceptions.OAuthAPIException;
import io.supertokens.oauth.exceptions.OAuthClientNotFoundException;
import io.supertokens.pluginInterface.RECIPE_ID;
import io.supertokens.pluginInterface.Storage;
import io.supertokens.pluginInterface.exceptions.InvalidConfigException;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
import io.supertokens.pluginInterface.multitenancy.AppIdentifier;
import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException;
import io.supertokens.pluginInterface.oauth.exceptions.OAuth2ClientAlreadyExistsForAppException;
import io.supertokens.webserver.InputParser;
Expand Down Expand Up @@ -84,10 +86,15 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
input.addProperty("subjectType", "public");

try {
AppIdentifier appIdentifier = getAppIdentifier(req);
Storage storage = enforcePublicTenantAndGetPublicTenantStorage(req);

input.addProperty("owner", appIdentifier.getAppId());

OAuthProxyHelper.proxyJsonPOST(
main, req, resp,
getAppIdentifier(req),
enforcePublicTenantAndGetPublicTenantStorage(req),
appIdentifier,
storage,
"/admin/clients", // proxyPath
true, // proxyToAdmin
true, // camelToSnakeCaseConversion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.gson.JsonArray;
Expand All @@ -14,7 +15,9 @@
import io.supertokens.multitenancy.exception.BadPermissionException;
import io.supertokens.oauth.OAuth;
import io.supertokens.pluginInterface.RECIPE_ID;
import io.supertokens.pluginInterface.Storage;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
import io.supertokens.pluginInterface.multitenancy.AppIdentifier;
import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException;
import io.supertokens.webserver.WebserverAPI;
import jakarta.servlet.ServletException;
Expand All @@ -35,14 +38,19 @@ public String getPath() {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
try {
AppIdentifier appIdentifier = getAppIdentifier(req);
Storage storage = enforcePublicTenantAndGetPublicTenantStorage(req);
Map<String, String> queryParams = OAuthProxyHelper.defaultGetQueryParamsFromRequest(req);
queryParams.put("owner", appIdentifier.getAppId());

OAuthProxyHelper.proxyGET(
main, req, resp,
getAppIdentifier(req),
enforcePublicTenantAndGetPublicTenantStorage(req),
appIdentifier,
storage,
"/admin/clients", // proxyPath
true, // proxyToAdmin
true, // camelToSnakeCaseConversion
new HashMap<>(), // queryParams
queryParams,
new HashMap<>(), // headers
(statusCode, headers, rawBody, jsonBody) -> { // getJsonResponse
JsonObject response = new JsonObject();
Expand All @@ -67,6 +75,29 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IO
}

response.add("clients", clients);

// pagination
List<String> linkHeader = headers.get("Link");
if (linkHeader != null && !linkHeader.isEmpty()) {
for (String nextLink : linkHeader.get(0).split(",")) {
if (!nextLink.contains("rel=\"next\"")) {
continue;
}

String pageToken = null;
if (nextLink.contains("page_token=")) {
int startIndex = nextLink.indexOf("page_token=") + "page_token=".length();
int endIndex = nextLink.indexOf('>', startIndex);
if (endIndex != -1) {
pageToken = nextLink.substring(startIndex, endIndex);
}
}
if (pageToken != null) {
response.addProperty("nextPaginationToken", pageToken);
}
}
}

return response;
}
);
Expand Down

0 comments on commit da96bc4

Please sign in to comment.