Skip to content

Commit

Permalink
fix: login request tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Oct 15, 2024
1 parent ec75e62 commit 618c591
Show file tree
Hide file tree
Showing 5 changed files with 562 additions and 17 deletions.
3 changes: 0 additions & 3 deletions src/main/java/io/supertokens/oauth/OAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,6 @@ public static HttpRequestForOAuthProvider.Response doOAuthProxyJsonDELETE(Main m
}

private static void checkNonSuccessResponse(HttpRequestForOAuthProvider.Response response) throws OAuthAPIException, OAuthClientNotFoundException {
if (response.statusCode == 404) {
throw new OAuthClientNotFoundException();
}
if (response.statusCode >= 400) {
String error = response.jsonResponse.getAsJsonObject().get("error").getAsString();
String errorDescription = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
package io.supertokens.webserver.api.oauth;

import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.HashMap;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

import io.supertokens.Main;
import io.supertokens.multitenancy.exception.BadPermissionException;
import io.supertokens.oauth.HttpRequestForOAuthProvider;
import io.supertokens.oauth.OAuth;
import io.supertokens.oauth.Transformations;
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.OAuthClient;
import io.supertokens.pluginInterface.oauth.exception.OAuthClientNotFoundException;
import io.supertokens.webserver.WebserverAPI;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
Expand All @@ -28,27 +43,44 @@ public String getPath() {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
try {
AppIdentifier appIdentifier = getAppIdentifier(req);
Storage storage = enforcePublicTenantAndGetPublicTenantStorage(req);
HttpRequestForOAuthProvider.Response response = OAuthProxyHelper.proxyGET(
main, req, resp,
getAppIdentifier(req),
enforcePublicTenantAndGetPublicTenantStorage(req),
null, // clientIdToCheck
"/admin/oauth2/auth/requests/login", // proxyPath
true, // proxyToAdmin
true, // camelToSnakeCaseConversion
OAuthProxyHelper.defaultGetQueryParamsFromRequest(req),
new HashMap<>() // headers
main, req, resp,
appIdentifier,
storage,
null, // clientIdToCheck
"/admin/oauth2/auth/requests/login", // proxyPath
true, // proxyToAdmin
true, // camelToSnakeCaseConversion
OAuthProxyHelper.defaultGetQueryParamsFromRequest(req),
new HashMap<>() // headers
);

if (response != null) {
Transformations.applyClientPropsWhiteList(response.jsonResponse.getAsJsonObject().get("client").getAsJsonObject());
Transformations.applyClientPropsWhiteList(
response.jsonResponse.getAsJsonObject().get("client").getAsJsonObject());

String clientId = response.jsonResponse.getAsJsonObject().get("client").getAsJsonObject()
.get("clientId").getAsString();
OAuthClient client = OAuth.getOAuthClientById(main, appIdentifier, storage, clientId);

response.jsonResponse.getAsJsonObject().get("client").getAsJsonObject()
.addProperty("enableRefreshTokenRotation", client.enableRefreshTokenRotation);
response.jsonResponse.getAsJsonObject().get("client").getAsJsonObject().addProperty("clientSecret",
client.clientSecret);

response.jsonResponse.getAsJsonObject().addProperty("status", "OK");
super.sendJsonResponse(200, response.jsonResponse, resp);
}

} catch (IOException | TenantOrAppNotFoundException | BadPermissionException e) {
} catch (IOException | TenantOrAppNotFoundException | BadPermissionException
| InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException | NoSuchPaddingException
| InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException
| StorageQueryException | InvalidConfigException e) {
throw new ServletException(e);
} catch (OAuthClientNotFoundException e) {
OAuthProxyHelper.handleOAuthClientNotFoundException(resp);
}
}
}
15 changes: 15 additions & 0 deletions src/test/java/io/supertokens/test/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.supertokens.storageLayer.StorageLayer;
import io.supertokens.test.httpRequest.HttpRequestForTesting;
import io.supertokens.test.httpRequest.HttpResponseException;
import io.supertokens.test.oauth.api.Map;
import io.supertokens.useridmapping.UserIdType;
import io.supertokens.utils.SemVer;
import io.supertokens.webserver.WebserverAPI;
Expand All @@ -35,10 +36,12 @@
import org.mockito.Mockito;

import java.io.*;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

import static org.junit.Assert.*;

Expand Down Expand Up @@ -271,4 +274,16 @@ public static <T> void assertArrayEqualsIgnoreOrder(T[] array1, T[] array2) {
array1.length == array2.length && Arrays.asList(array1).containsAll(Arrays.asList(array2))
&& Arrays.asList(array2).containsAll(Arrays.asList(array1)));
}

public static java.util.Map<String, String> splitQueryString(String query) throws UnsupportedEncodingException {
java.util.Map<String, String> queryParams = new HashMap<>();
String[] pairs = query.split("&");
for (String pair : pairs) {
int idx = pair.indexOf("=");
String key = idx > 0 ? URLDecoder.decode(pair.substring(0, idx), "UTF-8") : pair;
String value = idx > 0 && pair.length() > idx + 1 ? URLDecoder.decode(pair.substring(idx + 1), "UTF-8") : null;
queryParams.put(key, value);
}
return queryParams;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.junit.Test;
import org.junit.rules.TestRule;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
Expand Down Expand Up @@ -78,8 +77,6 @@ public void testClientList() throws Exception {
return;
}

FeatureFlag.getInstance(process.main)
.setLicenseKeyAndSyncFeatures(TotpLicenseTest.OPAQUE_KEY_WITH_MFA_FEATURE);
FeatureFlagTestContent.getInstance(process.main)
.setKeyValue(FeatureFlagTestContent.ENABLED_FEATURES, new EE_FEATURES[]{EE_FEATURES.OAUTH});

Expand Down
Loading

0 comments on commit 618c591

Please sign in to comment.