Skip to content

Commit

Permalink
fix: pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Sep 18, 2024
1 parent f3c022f commit 095a27f
Show file tree
Hide file tree
Showing 15 changed files with 40 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@
import java.util.Map;
import java.util.stream.Collectors;

public class HttpRequest {
public class HttpRequestForOry {
// This is a helper class to make HTTP requests to the hydra server specifically.
// Although this is similar to HttpRequest, this is slightly modified to be able to work with
// form data, headers in request and responses, query params in non-get requests, reading responses in
// case of errors, etc.
// Left the original HttpRequest as is to avoid any issues with existing code.

private static final int CONNECTION_TIMEOUT = 5000;
private static final int READ_TIMEOUT = 5000;

Expand Down
22 changes: 11 additions & 11 deletions src/main/java/io/supertokens/oauth/OAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private static void checkForOauthFeature(AppIdentifier appIdentifier, Main main)
"feature.");
}

public static HttpRequest.Response doOAuthProxyGET(Main main, AppIdentifier appIdentifier, Storage storage, String path, boolean proxyToAdmin, boolean camelToSnakeCaseConversion, Map<String, String> queryParams, Map<String, String> headers) throws StorageQueryException, OAuthClientNotFoundException, TenantOrAppNotFoundException, FeatureNotEnabledException, InvalidConfigException, IOException, OAuthAPIException {
public static HttpRequestForOry.Response doOAuthProxyGET(Main main, AppIdentifier appIdentifier, Storage storage, String path, boolean proxyToAdmin, boolean camelToSnakeCaseConversion, Map<String, String> queryParams, Map<String, String> headers) throws StorageQueryException, OAuthClientNotFoundException, TenantOrAppNotFoundException, FeatureNotEnabledException, InvalidConfigException, IOException, OAuthAPIException {
checkForOauthFeature(appIdentifier, main);
OAuthStorage oauthStorage = StorageUtils.getOAuthStorage(storage);

Expand All @@ -90,7 +90,7 @@ public static HttpRequest.Response doOAuthProxyGET(Main main, AppIdentifier appI
}
String fullUrl = baseURL + path;

HttpRequest.Response response = HttpRequest.doGet(fullUrl, headers, queryParams);
HttpRequestForOry.Response response = HttpRequestForOry.doGet(fullUrl, headers, queryParams);

// Response transformations
response.jsonResponse = Transformations.transformJsonResponseFromHydra(main, appIdentifier, response.jsonResponse);
Expand All @@ -106,7 +106,7 @@ public static HttpRequest.Response doOAuthProxyGET(Main main, AppIdentifier appI
return response;
}

public static HttpRequest.Response doOAuthProxyFormPOST(Main main, AppIdentifier appIdentifier, Storage storage, String path, boolean proxyToAdmin, boolean camelToSnakeCaseConversion, Map<String, String> formFields, Map<String, String> headers) throws StorageQueryException, OAuthClientNotFoundException, TenantOrAppNotFoundException, FeatureNotEnabledException, InvalidConfigException, IOException, OAuthAPIException {
public static HttpRequestForOry.Response doOAuthProxyFormPOST(Main main, AppIdentifier appIdentifier, Storage storage, String path, boolean proxyToAdmin, boolean camelToSnakeCaseConversion, Map<String, String> formFields, Map<String, String> headers) throws StorageQueryException, OAuthClientNotFoundException, TenantOrAppNotFoundException, FeatureNotEnabledException, InvalidConfigException, IOException, OAuthAPIException {
checkForOauthFeature(appIdentifier, main);
OAuthStorage oauthStorage = StorageUtils.getOAuthStorage(storage);

Expand All @@ -133,7 +133,7 @@ public static HttpRequest.Response doOAuthProxyFormPOST(Main main, AppIdentifier
}
String fullUrl = baseURL + path;

HttpRequest.Response response = HttpRequest.doFormPost(fullUrl, headers, formFields);
HttpRequestForOry.Response response = HttpRequestForOry.doFormPost(fullUrl, headers, formFields);

// Response transformations
response.jsonResponse = Transformations.transformJsonResponseFromHydra(main, appIdentifier, response.jsonResponse);
Expand All @@ -148,7 +148,7 @@ public static HttpRequest.Response doOAuthProxyFormPOST(Main main, AppIdentifier
return response;
}

public static HttpRequest.Response doOAuthProxyJsonPOST(Main main, AppIdentifier appIdentifier, Storage storage, String path, boolean proxyToAdmin, boolean camelToSnakeCaseConversion, JsonObject jsonInput, Map<String, String> headers) throws StorageQueryException, OAuthClientNotFoundException, TenantOrAppNotFoundException, FeatureNotEnabledException, InvalidConfigException, IOException, OAuthAPIException {
public static HttpRequestForOry.Response doOAuthProxyJsonPOST(Main main, AppIdentifier appIdentifier, Storage storage, String path, boolean proxyToAdmin, boolean camelToSnakeCaseConversion, JsonObject jsonInput, Map<String, String> headers) throws StorageQueryException, OAuthClientNotFoundException, TenantOrAppNotFoundException, FeatureNotEnabledException, InvalidConfigException, IOException, OAuthAPIException {
checkForOauthFeature(appIdentifier, main);
OAuthStorage oauthStorage = StorageUtils.getOAuthStorage(storage);

Expand All @@ -175,7 +175,7 @@ public static HttpRequest.Response doOAuthProxyJsonPOST(Main main, AppIdentifier
}
String fullUrl = baseURL + path;

HttpRequest.Response response = HttpRequest.doJsonPost(fullUrl, headers, jsonInput);
HttpRequestForOry.Response response = HttpRequestForOry.doJsonPost(fullUrl, headers, jsonInput);

// Response transformations
response.jsonResponse = Transformations.transformJsonResponseFromHydra(main, appIdentifier, response.jsonResponse);
Expand All @@ -190,7 +190,7 @@ public static HttpRequest.Response doOAuthProxyJsonPOST(Main main, AppIdentifier
return response;
}

public static HttpRequest.Response doOAuthProxyJsonPUT(Main main, AppIdentifier appIdentifier, Storage storage, String path, boolean proxyToAdmin, boolean camelToSnakeCaseConversion, Map<String, String> queryParams, JsonObject jsonInput, Map<String, String> headers) throws StorageQueryException, OAuthClientNotFoundException, TenantOrAppNotFoundException, FeatureNotEnabledException, InvalidConfigException, IOException, OAuthAPIException {
public static HttpRequestForOry.Response doOAuthProxyJsonPUT(Main main, AppIdentifier appIdentifier, Storage storage, String path, boolean proxyToAdmin, boolean camelToSnakeCaseConversion, Map<String, String> queryParams, JsonObject jsonInput, Map<String, String> headers) throws StorageQueryException, OAuthClientNotFoundException, TenantOrAppNotFoundException, FeatureNotEnabledException, InvalidConfigException, IOException, OAuthAPIException {
checkForOauthFeature(appIdentifier, main);
OAuthStorage oauthStorage = StorageUtils.getOAuthStorage(storage);

Expand Down Expand Up @@ -218,7 +218,7 @@ public static HttpRequest.Response doOAuthProxyJsonPUT(Main main, AppIdentifier
}
String fullUrl = baseURL + path;

HttpRequest.Response response = HttpRequest.doJsonPut(fullUrl, queryParams, headers, jsonInput);
HttpRequestForOry.Response response = HttpRequestForOry.doJsonPut(fullUrl, queryParams, headers, jsonInput);

// Response transformations
response.jsonResponse = Transformations.transformJsonResponseFromHydra(main, appIdentifier, response.jsonResponse);
Expand All @@ -233,7 +233,7 @@ public static HttpRequest.Response doOAuthProxyJsonPUT(Main main, AppIdentifier
return response;
}

public static HttpRequest.Response doOAuthProxyJsonDELETE(Main main, AppIdentifier appIdentifier, Storage storage, String path, boolean proxyToAdmin, boolean camelToSnakeCaseConversion, JsonObject jsonInput, Map<String, String> headers) throws StorageQueryException, OAuthClientNotFoundException, TenantOrAppNotFoundException, FeatureNotEnabledException, InvalidConfigException, IOException, OAuthAPIException {
public static HttpRequestForOry.Response doOAuthProxyJsonDELETE(Main main, AppIdentifier appIdentifier, Storage storage, String path, boolean proxyToAdmin, boolean camelToSnakeCaseConversion, JsonObject jsonInput, Map<String, String> headers) throws StorageQueryException, OAuthClientNotFoundException, TenantOrAppNotFoundException, FeatureNotEnabledException, InvalidConfigException, IOException, OAuthAPIException {
checkForOauthFeature(appIdentifier, main);
OAuthStorage oauthStorage = StorageUtils.getOAuthStorage(storage);

Expand All @@ -260,7 +260,7 @@ public static HttpRequest.Response doOAuthProxyJsonDELETE(Main main, AppIdentifi
}
String fullUrl = baseURL + path;

HttpRequest.Response response = HttpRequest.doJsonDelete(fullUrl, headers, jsonInput);
HttpRequestForOry.Response response = HttpRequestForOry.doJsonDelete(fullUrl, headers, jsonInput);

// Response transformations
response.jsonResponse = Transformations.transformJsonResponseFromHydra(main, appIdentifier, response.jsonResponse);
Expand All @@ -275,7 +275,7 @@ public static HttpRequest.Response doOAuthProxyJsonDELETE(Main main, AppIdentifi
return response;
}

private static void checkNonSuccessResponse(HttpRequest.Response response) throws OAuthAPIException, OAuthClientNotFoundException {
private static void checkNonSuccessResponse(HttpRequestForOry.Response response) throws OAuthAPIException, OAuthClientNotFoundException {
if (response.statusCode == 404) {
throw new OAuthClientNotFoundException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import io.supertokens.Main;
import io.supertokens.featureflag.exceptions.FeatureNotEnabledException;
import io.supertokens.multitenancy.exception.BadPermissionException;
import io.supertokens.oauth.HttpRequest;
import io.supertokens.oauth.HttpRequestForOry;
import io.supertokens.oauth.OAuth;
import io.supertokens.oauth.exceptions.OAuthAPIException;
import io.supertokens.oauth.exceptions.OAuthClientNotFoundException;
Expand Down Expand Up @@ -65,7 +65,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IO
true, // camelToSnakeCaseConversion
OAuthProxyHelper.defaultGetQueryParamsFromRequest(req),
new HashMap<>(), // getHeadersForProxy
(statusCode, headers, rawBody, jsonBody) -> { // handleResponse
(statusCode, headers, rawBody, jsonBody) -> { // getJsonResponse
return jsonBody.getAsJsonObject();
}
);
Expand Down Expand Up @@ -93,7 +93,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
true, // camelToSnakeCaseConversion
input, // jsonBody
new HashMap<>(), // headers
(statusCode, headers, rawBody, jsonBody) -> { // handleResponse
(statusCode, headers, rawBody, jsonBody) -> { // getJsonResponse
String clientId = jsonBody.getAsJsonObject().get("clientId").getAsString();

try {
Expand All @@ -120,7 +120,7 @@ protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws IO
try {
Map<String, String> queryParams = new HashMap<>();
queryParams.put("clientId", clientId);
HttpRequest.Response response = OAuth.doOAuthProxyGET(
HttpRequestForOry.Response response = OAuth.doOAuthProxyGET(
main,
getAppIdentifier(req),
enforcePublicTenantAndGetPublicTenantStorage(req),
Expand Down Expand Up @@ -151,7 +151,7 @@ protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws IO
new HashMap<>(), // queryParams
input, // jsonBody
new HashMap<>(), // headers
(statusCode, headers, rawBody, jsonBody) -> { // handleResponse
(statusCode, headers, rawBody, jsonBody) -> { // getJsonResponse
return jsonBody.getAsJsonObject();
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws IO
OAuthProxyHelper.defaultGetQueryParamsFromRequest(req),
input, // jsonBody
new HashMap<>(), // headers
(statusCode, headers, rawBody, jsonBody) -> { // handleResponse
(statusCode, headers, rawBody, jsonBody) -> { // getJsonResponse
JsonObject response = jsonBody.getAsJsonObject();
response.addProperty("status", "OK");
return response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
false, // camelToSnakeCaseConversion
queryParams,
headers,
(statusCode, responseHeaders, rawBody, jsonBody) -> { // handleResponse
(statusCode, responseHeaders, rawBody, jsonBody) -> { // getJsonResponse
if (headers == null || !responseHeaders.containsKey("Location")) {
throw new IllegalStateException("Invalid response from hydra");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IO
true, // camelToSnakeCaseConversion
new HashMap<>(), // queryParams
new HashMap<>(), // headers
(statusCode, headers, rawBody, jsonBody) -> { // handleResponse
(statusCode, headers, rawBody, jsonBody) -> { // getJsonResponse
JsonObject response = new JsonObject();
response.addProperty("status", "OK");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IO
true, // camelToSnakeCaseConversion
OAuthProxyHelper.defaultGetQueryParamsFromRequest(req),
new HashMap<>(), // headers
(statusCode, headers, rawBody, jsonBody) -> { // handleResponse
(statusCode, headers, rawBody, jsonBody) -> { // getJsonResponse
JsonObject response = jsonBody.getAsJsonObject();
response.addProperty("status", "OK");
return response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IO
true, // camelToSnakeCaseConversion
OAuthProxyHelper.defaultGetQueryParamsFromRequest(req),
new HashMap<>(), // headers
(statusCode, headers, rawBody, jsonBody) -> { // handleResponse
(statusCode, headers, rawBody, jsonBody) -> { // getJsonResponse
JsonObject response = jsonBody.getAsJsonObject();
response.addProperty("status", "OK");
return response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IO
true, // camelToSnakeCaseConversion
OAuthProxyHelper.defaultGetQueryParamsFromRequest(req),
new HashMap<>(), // headers
(statusCode, headers, rawBody, jsonBody) -> { // handleResponse
(statusCode, headers, rawBody, jsonBody) -> { // getJsonResponse
JsonObject response = jsonBody.getAsJsonObject();
response.addProperty("status", "OK");
return response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import io.supertokens.Main;
import io.supertokens.featureflag.exceptions.FeatureNotEnabledException;
import io.supertokens.oauth.HttpRequest;
import io.supertokens.oauth.HttpRequestForOry;
import io.supertokens.oauth.OAuth;
import io.supertokens.oauth.exceptions.OAuthAPIException;
import io.supertokens.oauth.exceptions.OAuthClientNotFoundException;
Expand All @@ -35,7 +35,7 @@ public static void proxyGET(Main main, HttpServletRequest req, HttpServletRespon
Map<String, String> queryParams, Map<String, String> headers,
GetJsonResponse getJsonResponse) throws IOException, ServletException {
try {
HttpRequest.Response response = OAuth.doOAuthProxyGET(main, appIdentifier, storage, path, proxyToAdmin, camelToSnakeCaseConversion, queryParams, headers);
HttpRequestForOry.Response response = OAuth.doOAuthProxyGET(main, appIdentifier, storage, path, proxyToAdmin, camelToSnakeCaseConversion, queryParams, headers);

JsonObject jsonResponse = getJsonResponse.apply(
response.statusCode,
Expand All @@ -61,7 +61,7 @@ public static void proxyFormPOST(Main main, HttpServletRequest req, HttpServletR
Map<String, String> formFields, Map<String, String> headers,
GetJsonResponse getJsonResponse) throws IOException, ServletException {
try {
HttpRequest.Response response = OAuth.doOAuthProxyFormPOST(main, appIdentifier, storage, path, proxyToAdmin, camelToSnakeCaseConversion, formFields, headers);
HttpRequestForOry.Response response = OAuth.doOAuthProxyFormPOST(main, appIdentifier, storage, path, proxyToAdmin, camelToSnakeCaseConversion, formFields, headers);

JsonObject jsonResponse = getJsonResponse.apply(
response.statusCode,
Expand All @@ -87,7 +87,7 @@ public static void proxyJsonPOST(Main main, HttpServletRequest req, HttpServletR
JsonObject jsonInput, Map<String, String> headers,
GetJsonResponse getJsonResponse) throws IOException, ServletException {
try {
HttpRequest.Response response = OAuth.doOAuthProxyJsonPOST(main, appIdentifier, storage, path, proxyToAdmin, camelToSnakeCaseConversion, jsonInput, headers);
HttpRequestForOry.Response response = OAuth.doOAuthProxyJsonPOST(main, appIdentifier, storage, path, proxyToAdmin, camelToSnakeCaseConversion, jsonInput, headers);

JsonObject jsonResponse = getJsonResponse.apply(
response.statusCode,
Expand All @@ -114,7 +114,7 @@ public static void proxyJsonPUT(Main main, HttpServletRequest req, HttpServletRe
Map<String, String> headers, GetJsonResponse getJsonResponse) throws IOException, ServletException {

try {
HttpRequest.Response response = OAuth.doOAuthProxyJsonPUT(main, appIdentifier, storage, path, proxyToAdmin, camelToSnakeCaseConversion, queryParams, jsonInput, headers);
HttpRequestForOry.Response response = OAuth.doOAuthProxyJsonPUT(main, appIdentifier, storage, path, proxyToAdmin, camelToSnakeCaseConversion, queryParams, jsonInput, headers);

JsonObject jsonResponse = getJsonResponse.apply(
response.statusCode,
Expand All @@ -140,7 +140,7 @@ public static void proxyJsonDELETE(Main main, HttpServletRequest req, HttpServle
JsonObject jsonInput, Map<String, String> headers,
GetJsonResponse getJsonResponse) throws IOException, ServletException {
try {
HttpRequest.Response response = OAuth.doOAuthProxyJsonDELETE(main, appIdentifier, storage, path, proxyToAdmin, camelToSnakeCaseConversion, jsonInput, headers);
HttpRequestForOry.Response response = OAuth.doOAuthProxyJsonDELETE(main, appIdentifier, storage, path, proxyToAdmin, camelToSnakeCaseConversion, jsonInput, headers);

JsonObject jsonResponse = getJsonResponse.apply(
response.statusCode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws IO
OAuthProxyHelper.defaultGetQueryParamsFromRequest(req),
input, // getJsonBody
new HashMap<>(), // getHeadersForProxy
(statusCode, headers, rawBody, jsonBody) -> { // handleResponse
(statusCode, headers, rawBody, jsonBody) -> { // getJsonResponse
JsonObject response = jsonBody.getAsJsonObject();
response.addProperty("status", "OK");
return response;
Expand Down
Loading

0 comments on commit 095a27f

Please sign in to comment.