|
34 | 34 | import io.supertokens.pluginInterface.multitenancy.TenantIdentifier; |
35 | 35 | import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException; |
36 | 36 | import io.supertokens.pluginInterface.usermetadata.sqlStorage.UserMetadataSQLStorage; |
| 37 | +import io.supertokens.session.Session; |
| 38 | +import io.supertokens.session.info.SessionInformationHolder; |
37 | 39 | import io.supertokens.storageLayer.StorageLayer; |
38 | 40 | import io.supertokens.test.TestingProcessManager; |
39 | 41 | import io.supertokens.test.Utils; |
| 42 | +import io.supertokens.test.httpRequest.HttpRequestForTesting; |
40 | 43 | import io.supertokens.test.httpRequest.HttpResponseException; |
41 | 44 | import io.supertokens.thirdparty.InvalidProviderConfigException; |
42 | 45 | import io.supertokens.useridmapping.UserIdMapping; |
| 46 | +import io.supertokens.utils.SemVer; |
43 | 47 | import org.junit.After; |
44 | 48 | import org.junit.AfterClass; |
45 | 49 | import org.junit.Before; |
46 | 50 | import org.junit.Test; |
47 | 51 |
|
48 | 52 | import java.io.IOException; |
| 53 | +import java.util.HashMap; |
| 54 | +import java.util.Map; |
49 | 55 |
|
50 | 56 | import static org.junit.Assert.*; |
51 | 57 |
|
@@ -375,4 +381,91 @@ public void testEmailVerificationWithUsersOnDifferentTenantStorages() throws Exc |
375 | 381 | assertFalse( t1EvStorage. isEmailVerified( t0. toAppIdentifier(), user2. getSupertokensUserId(), "[email protected]")); // ensure t1 storage does not have user2's ev |
376 | 382 | assertFalse( t0EvStorage. isEmailVerified( t0. toAppIdentifier(), user1. getSupertokensUserId(), "[email protected]")); // ensure t0 storage does not have user1's ev |
377 | 383 | } |
| 384 | + |
| 385 | + @Test |
| 386 | + public void testSessionCannotGetAcrossAllStorageOrRevokedAcrossAllTenantsFromNonPublicTenant() throws Exception { |
| 387 | + if (StorageLayer.getBaseStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) { |
| 388 | + return; |
| 389 | + } |
| 390 | + |
| 391 | + if (StorageLayer.isInMemDb(process.getProcess())) { |
| 392 | + return; |
| 393 | + } |
| 394 | + |
| 395 | + TenantIdentifier t0 = new TenantIdentifier(null, null, null); |
| 396 | + Storage t0Storage = (StorageLayer.getStorage(t0, process.getProcess())); |
| 397 | + |
| 398 | + TenantIdentifier t1 = new TenantIdentifier(null, null, "t1"); |
| 399 | + Storage t1Storage = (StorageLayer.getStorage(t1, process.getProcess())); |
| 400 | + |
| 401 | + // Create users |
| 402 | + AuthRecipeUserInfo user1 = EmailPassword. signUp( t0, t0Storage, process. getProcess(), "[email protected]", "password123"); |
| 403 | + AuthRecipeUserInfo user2 = EmailPassword. signUp( t1, t1Storage, process. getProcess(), "[email protected]", "password123"); |
| 404 | + |
| 405 | + UserIdMapping.populateExternalUserIdForUsers(t0.toAppIdentifier(), t0Storage, new AuthRecipeUserInfo[]{user1}); |
| 406 | + UserIdMapping.populateExternalUserIdForUsers(t1.toAppIdentifier(), t1Storage, new AuthRecipeUserInfo[]{user2}); |
| 407 | + |
| 408 | + SessionInformationHolder sess1 = Session.createNewSession(t0, t0Storage, |
| 409 | + process.getProcess(), user1.getSupertokensUserId(), new JsonObject(), new JsonObject()); |
| 410 | + SessionInformationHolder sess2 = Session.createNewSession(t1, t1Storage, |
| 411 | + process.getProcess(), user2.getSupertokensUserId(), new JsonObject(), new JsonObject()); |
| 412 | + |
| 413 | + { |
| 414 | + Map<String, String> params = new HashMap<>(); |
| 415 | + params.put("fetchAcrossAllTenants", "true"); |
| 416 | + params.put("userId", user1.getSupertokensUserId()); |
| 417 | + |
| 418 | + JsonObject sessionResponse = HttpRequestForTesting.sendGETRequest(process.getProcess(), "", |
| 419 | + HttpRequestForTesting.getMultitenantUrl(t1, "/recipe/session/user"), |
| 420 | + params, 1000, 1000, null, SemVer.v4_0.get(), |
| 421 | + "session"); |
| 422 | + assertEquals("OK", sessionResponse.get("status").getAsString()); |
| 423 | + assertEquals(1, sessionResponse.get("sessionHandles").getAsJsonArray().size()); |
| 424 | + assertEquals(sess1.session.handle, sessionResponse.get("sessionHandles").getAsJsonArray().get(0).getAsString()); |
| 425 | + } |
| 426 | + |
| 427 | + { |
| 428 | + try { |
| 429 | + Map<String, String> params = new HashMap<>(); |
| 430 | + params.put("fetchAcrossAllTenants", "true"); |
| 431 | + params.put("userId", user1.getSupertokensUserId()); |
| 432 | + |
| 433 | + JsonObject sessionResponse = HttpRequestForTesting.sendGETRequest(process.getProcess(), "", |
| 434 | + HttpRequestForTesting.getMultitenantUrl(t1, "/recipe/session/user"), |
| 435 | + params, 1000, 1000, null, SemVer.v5_0.get(), |
| 436 | + "session"); |
| 437 | + fail(); |
| 438 | + } catch (HttpResponseException e) { |
| 439 | + assertEquals(403, e.statusCode); |
| 440 | + } |
| 441 | + } |
| 442 | + |
| 443 | + { |
| 444 | + try { |
| 445 | + JsonObject requestBody = new JsonObject(); |
| 446 | + requestBody.addProperty("userId", user1.getSupertokensUserId()); |
| 447 | + requestBody.addProperty("revokeAcrossAllTenants", true); |
| 448 | + |
| 449 | + JsonObject sessionResponse = HttpRequestForTesting.sendJsonPOSTRequest(process.getProcess(), "", |
| 450 | + HttpRequestForTesting.getMultitenantUrl(t1, "/recipe/session/remove"), requestBody, |
| 451 | + 1000, 1000, null, SemVer.v5_0.get(), |
| 452 | + "session"); |
| 453 | + fail(); |
| 454 | + } catch (HttpResponseException e) { |
| 455 | + assertEquals(403, e.statusCode); |
| 456 | + } |
| 457 | + } |
| 458 | + |
| 459 | + { |
| 460 | + JsonObject requestBody = new JsonObject(); |
| 461 | + requestBody.addProperty("userId", user1.getSupertokensUserId()); |
| 462 | + requestBody.addProperty("revokeAcrossAllTenants", true); |
| 463 | + |
| 464 | + JsonObject sessionResponse = HttpRequestForTesting.sendJsonPOSTRequest(process.getProcess(), "", |
| 465 | + HttpRequestForTesting.getMultitenantUrl(t1, "/recipe/session/remove"), requestBody, |
| 466 | + 1000, 1000, null, SemVer.v4_0.get(), |
| 467 | + "session"); |
| 468 | + assertEquals("OK", sessionResponse.get("status").getAsString()); |
| 469 | + } |
| 470 | + } |
378 | 471 | } |
0 commit comments