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

fix: added version check for multitenant apis #669

Merged
merged 2 commits into from
May 8, 2023
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
3 changes: 2 additions & 1 deletion coreDriverInterfaceSupported.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"2.18",
"2.19",
"2.20",
"2.21"
"2.21",
"2.22"
]
}
1 change: 1 addition & 0 deletions src/main/java/io/supertokens/utils/SemVer.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class SemVer implements Comparable<SemVer> {
public static final SemVer v2_19 = new SemVer("2.19");
public static final SemVer v2_20 = new SemVer("2.20");
public static final SemVer v2_21 = new SemVer("2.21");
public static final SemVer v2_22 = new SemVer("2.22");

final private String version;

Expand Down
13 changes: 11 additions & 2 deletions src/main/java/io/supertokens/webserver/WebserverAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ public abstract class WebserverAPI extends HttpServlet {
supportedVersions.add(SemVer.v2_19);
supportedVersions.add(SemVer.v2_20);
supportedVersions.add(SemVer.v2_21);
supportedVersions.add(SemVer.v2_22);
}

public static SemVer getLatestCDIVersion() {
return SemVer.v2_21;
return SemVer.v2_22;
}

public WebserverAPI(Main main, String rid) {
Expand Down Expand Up @@ -396,9 +397,17 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) throws
if (this.checkAPIKey(req)) {
assertThatAPIKeyCheckPasses(req);
}

tenantIdentifier = getTenantIdentifierWithStorageFromRequest(req);
SemVer version = getVersionFromRequest(req);

// Check for CDI version for multitenancy
if (version.lesserThan(SemVer.v2_22) && !tenantIdentifier.getTenantId().equals(TenantIdentifier.DEFAULT_TENANT_ID)) {
sendTextResponse(404, "Not found", resp);
return;
}

if (this.versionNeeded(req)) {
SemVer version = getVersionFromRequest(req);
assertThatVersionIsCompatible(version);
Logging.info(main, tenantIdentifier,
"API called: " + req.getRequestURI() + ". Method: " + req.getMethod() + ". Version: " + version,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import io.supertokens.test.httpRequest.HttpResponseException;
import io.supertokens.thirdparty.InvalidProviderConfigException;
import io.supertokens.thirdparty.ThirdParty;
import io.supertokens.utils.SemVer;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
Expand Down Expand Up @@ -309,7 +310,7 @@ private long getUserCount(TenantIdentifier tenantIdentifier, String []recipeIds,
JsonObject response = HttpRequestForTesting.sendGETRequest(process.getProcess(), "",
HttpRequestForTesting.getMultitenantUrl(tenantIdentifier, "/users/count"),
params, 1000, 1000, null,
Utils.getCdiVersionStringLatestForTests(), null);
SemVer.v2_22.get(), null);

assertEquals("OK", response.get("status").getAsString());

Expand All @@ -326,7 +327,7 @@ private String[] getUsers(TenantIdentifier tenantIdentifier, String []recipeIds)
JsonObject response = HttpRequestForTesting.sendGETRequest(process.getProcess(), "",
HttpRequestForTesting.getMultitenantUrl(tenantIdentifier, "/users"),
params, 1000, 1000, null,
Utils.getCdiVersionStringLatestForTests(), null);
SemVer.v2_22.get(), null);

assertEquals("OK", response.get("status").getAsString());

Expand Down Expand Up @@ -356,7 +357,7 @@ private String[] getUsers(TenantIdentifier tenantIdentifier, String[] emails, St
JsonObject response = HttpRequestForTesting.sendGETRequest(process.getProcess(), "",
HttpRequestForTesting.getMultitenantUrl(tenantIdentifier, "/users"),
params, 1000, 1000, null,
Utils.getCdiVersionStringLatestForTests(), null);
SemVer.v2_22.get(), null);

assertEquals("OK", response.get("status").getAsString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@
import java.util.HashMap;
import java.util.Random;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.*;

public class MultitenantAPITest {
TestingProcessManager.TestingProcess process;
Expand Down Expand Up @@ -191,7 +190,7 @@ private JsonObject signUp(TenantIdentifier tenantIdentifier, String email, Strin
JsonObject signUpResponse = HttpRequestForTesting.sendJsonPOSTRequest(process.getProcess(), "",
HttpRequestForTesting.getMultitenantUrl(tenantIdentifier, "/recipe/signup"),
requestBody, 1000, 1000, null,
Utils.getCdiVersionStringLatestForTests(), "emailpassword");
SemVer.v2_22.get(), "emailpassword");
assertEquals("OK", signUpResponse.getAsJsonPrimitive("status").getAsString());
return signUpResponse.getAsJsonObject("user");
}
Expand All @@ -204,7 +203,7 @@ private JsonObject successfulSignIn(TenantIdentifier tenantIdentifier, String em
JsonObject signInResponse = HttpRequestForTesting.sendJsonPOSTRequest(process.getProcess(), "",
HttpRequestForTesting.getMultitenantUrl(tenantIdentifier, "/recipe/signin"),
requestBody, 1000, 1000, null,
Utils.getCdiVersionStringLatestForTests(), "emailpassword");
SemVer.v2_22.get(), "emailpassword");
assertEquals("OK", signInResponse.getAsJsonPrimitive("status").getAsString());
return signInResponse.getAsJsonObject("user");
}
Expand All @@ -217,7 +216,7 @@ private void wrongCredentialsSignIn(TenantIdentifier tenantIdentifier, String em
JsonObject signInResponse = HttpRequestForTesting.sendJsonPOSTRequest(process.getProcess(), "",
HttpRequestForTesting.getMultitenantUrl(tenantIdentifier, "/recipe/signin"),
requestBody, 1000, 1000, null,
Utils.getCdiVersionStringLatestForTests(), "emailpassword");
SemVer.v2_22.get(), "emailpassword");
assertEquals("WRONG_CREDENTIALS_ERROR", signInResponse.getAsJsonPrimitive("status").getAsString());
}

Expand All @@ -227,7 +226,7 @@ private JsonObject getUserUsingId(TenantIdentifier tenantIdentifier, String user
map.put("userId", userId);
JsonObject userResponse = HttpRequestForTesting.sendGETRequest(process.getProcess(), "",
HttpRequestForTesting.getMultitenantUrl(tenantIdentifier, "/recipe/user"),
map, 1000, 1000, null, Utils.getCdiVersionStringLatestForTests(),
map, 1000, 1000, null, SemVer.v2_22.get(),
"emailpassword");
assertEquals("OK", userResponse.getAsJsonPrimitive("status").getAsString());
return userResponse.getAsJsonObject("user");
Expand All @@ -241,7 +240,7 @@ private void updatePassword(TenantIdentifier tenantIdentifier, String userId, St

JsonObject response = HttpRequestForTesting.sendJsonPUTRequest(process.getProcess(), "",
HttpRequestForTesting.getMultitenantUrl(tenantIdentifier, "/recipe/user"), body,
1000, 1000, null, SemVer.v2_8.get(),
1000, 1000, null, SemVer.v2_22.get(),
RECIPE_ID.EMAIL_PASSWORD.toString());
assertEquals("OK", response.getAsJsonPrimitive("status").getAsString());
}
Expand All @@ -254,7 +253,7 @@ private void updateEmail(TenantIdentifier tenantIdentifier, String userId, Strin

JsonObject response = HttpRequestForTesting.sendJsonPUTRequest(process.getProcess(), "",
HttpRequestForTesting.getMultitenantUrl(tenantIdentifier, "/recipe/user"), body,
1000, 1000, null, SemVer.v2_8.get(),
1000, 1000, null, SemVer.v2_22.get(),
RECIPE_ID.EMAIL_PASSWORD.toString());
assertEquals("OK", response.getAsJsonPrimitive("status").getAsString());
}
Expand All @@ -268,7 +267,7 @@ private void updateEmailAndPassword(TenantIdentifier tenantIdentifier, String us

JsonObject response = HttpRequestForTesting.sendJsonPUTRequest(process.getProcess(), "",
HttpRequestForTesting.getMultitenantUrl(tenantIdentifier, "/recipe/user"), body,
1000, 1000, null, SemVer.v2_8.get(),
1000, 1000, null, SemVer.v2_22.get(),
RECIPE_ID.EMAIL_PASSWORD.toString());
assertEquals("OK", response.getAsJsonPrimitive("status").getAsString());
}
Expand All @@ -279,7 +278,7 @@ private JsonObject getUserUsingEmail(TenantIdentifier tenantIdentifier, String e
map.put("email", email);
JsonObject userResponse = HttpRequestForTesting.sendGETRequest(process.getProcess(), "",
HttpRequestForTesting.getMultitenantUrl(tenantIdentifier, "/recipe/user"),
map, 1000, 1000, null, Utils.getCdiVersionStringLatestForTests(),
map, 1000, 1000, null, SemVer.v2_22.get(),
"emailpassword");
assertEquals("OK", userResponse.getAsJsonPrimitive("status").getAsString());
return userResponse.getAsJsonObject("user");
Expand All @@ -293,7 +292,7 @@ private String generatePasswordResetToken(TenantIdentifier tenantIdentifier, Str
JsonObject response = HttpRequestForTesting.sendJsonPOSTRequest(process.getProcess(), "",
HttpRequestForTesting.getMultitenantUrl(tenantIdentifier, "/recipe/user/password/reset/token"),
requestBody, 1000, 1000, null,
Utils.getCdiVersionStringLatestForTests(), "emailpassword");
SemVer.v2_22.get(), "emailpassword");

assertEquals(response.get("status").getAsString(), "OK");
assertEquals(response.entrySet().size(), 2);
Expand All @@ -312,7 +311,7 @@ private void successfulResetPasswordUsingToken(TenantIdentifier tenantIdentifier
JsonObject passwordResetResponse = HttpRequestForTesting.sendJsonPOSTRequest(process.getProcess(), "",
HttpRequestForTesting.getMultitenantUrl(tenantIdentifier, "/recipe/user/password/reset"),
resetPasswordBody, 1000, 1000, null,
Utils.getCdiVersionStringLatestForTests(), "emailpassword");
SemVer.v2_22.get(), "emailpassword");
assertEquals(passwordResetResponse.get("status").getAsString(), "OK");
assertEquals(passwordResetResponse.get("userId").getAsString(), userId);
assertEquals(passwordResetResponse.entrySet().size(), 2);
Expand All @@ -328,7 +327,7 @@ private void invalidResetPasswordUsingToken(TenantIdentifier tenantIdentifier, S
JsonObject passwordResetResponse = HttpRequestForTesting.sendJsonPOSTRequest(process.getProcess(), "",
HttpRequestForTesting.getMultitenantUrl(tenantIdentifier, "/recipe/user/password/reset"),
resetPasswordBody, 1000, 1000, null,
Utils.getCdiVersionStringLatestForTests(), "emailpassword");
SemVer.v2_22.get(), "emailpassword");
assertEquals(passwordResetResponse.get("status").getAsString(), "RESET_PASSWORD_INVALID_TOKEN_ERROR");
}

Expand Down Expand Up @@ -639,4 +638,23 @@ public void testImportUsersWorksCorrectlyAcrossTenants() throws Exception {
wrongCredentialsSignIn(t1, email, password);
wrongCredentialsSignIn(t3, email, password);
}

@Test
public void testThatTenantIdIsNotAllowedForOlderCDIVersion() throws Exception {
createTenants(false);

JsonObject requestBody = new JsonObject();
requestBody.addProperty("email", "test@example.com");
requestBody.addProperty("password", "password");

try {
HttpRequestForTesting.sendJsonPOSTRequest(process.getProcess(), "",
HttpRequestForTesting.getMultitenantUrl(t2, "/recipe/signup"),
requestBody, 1000, 1000, null,
SemVer.v2_21.get(), "emailpassword");
fail();
} catch (HttpResponseException e) {
assertEquals(404, e.statusCode);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private void verifyEmail(TenantIdentifier tenantIdentifier, String userId, Strin
JsonObject response = HttpRequestForTesting.sendJsonPOSTRequest(process.getProcess(), "",
HttpRequestForTesting.getMultitenantUrl(tenantIdentifier, "/recipe/user/email/verify/token"),
requestBody, 1000, 1000, null,
Utils.getCdiVersionStringLatestForTests(), "emailverification");
SemVer.v2_22.get(), "emailverification");

assertEquals(response.entrySet().size(), 2);
assertEquals(response.get("status").getAsString(), "OK");
Expand All @@ -173,7 +173,7 @@ private void verifyEmail(TenantIdentifier tenantIdentifier, String userId, Strin
JsonObject response2 = HttpRequestForTesting.sendJsonPOSTRequest(process.getProcess(), "",
HttpRequestForTesting.getMultitenantUrl(tenantIdentifier, "/recipe/user/email/verify"),
verifyResponseBody, 1000, 1000, null,
Utils.getCdiVersionStringLatestForTests(), "emailverification");
SemVer.v2_22.get(), "emailverification");

assertEquals(response2.entrySet().size(), 3);
assertEquals(response2.get("status").getAsString(), "OK");
Expand All @@ -191,7 +191,7 @@ private boolean isEmailVerified(TenantIdentifier tenantIdentifier, String userId
JsonObject verifyResponse = HttpRequestForTesting.sendGETRequest(process.getProcess(), "",
HttpRequestForTesting.getMultitenantUrl(tenantIdentifier, "/recipe/user/email/verify"), map, 1000, 1000,
null,
SemVer.v2_7.get(), "emailverification");
SemVer.v2_22.get(), "emailverification");
assertEquals(verifyResponse.entrySet().size(), 2);
assertEquals(verifyResponse.get("status").getAsString(), "OK");
return verifyResponse.get("isVerified").getAsBoolean();
Expand Down
Loading