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: get user by id as per recipe #788

Merged
merged 1 commit into from
Sep 6, 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
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ public static AuthRecipeUserInfo getUserUsingId(AppIdentifierWithStorage appIden
return null;
}
for (LoginMethod lM : result.loginMethods) {
if (lM.getSupertokensUserId().equals(userId)) {
if (lM.getSupertokensUserId().equals(userId) && lM.recipeId == RECIPE_ID.EMAIL_PASSWORD) {
return AuthRecipeUserInfo.create(lM.getSupertokensUserId(), result.isPrimaryUser, lM);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ public static AuthRecipeUserInfo getUserById(AppIdentifierWithStorage appIdentif
return null;
}
for (LoginMethod lM : result.loginMethods) {
if (lM.getSupertokensUserId().equals(userId)) {
if (lM.getSupertokensUserId().equals(userId) && lM.recipeId == RECIPE_ID.PASSWORDLESS) {
return AuthRecipeUserInfo.create(lM.getSupertokensUserId(), result.isPrimaryUser,
lM);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/supertokens/thirdparty/ThirdParty.java
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ public static AuthRecipeUserInfo getUser(AppIdentifierWithStorage appIdentifierW
return null;
}
for (LoginMethod lM : result.loginMethods) {
if (lM.getSupertokensUserId().equals(userId)) {
if (lM.getSupertokensUserId().equals(userId) && lM.recipeId == RECIPE_ID.THIRD_PARTY) {
return AuthRecipeUserInfo.create(lM.getSupertokensUserId(), result.isPrimaryUser,
lM);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@

import com.google.gson.JsonObject;
import io.supertokens.ProcessState;
import io.supertokens.passwordless.Passwordless;
import io.supertokens.pluginInterface.STORAGE_TYPE;
import io.supertokens.pluginInterface.authRecipe.AuthRecipeUserInfo;
import io.supertokens.storageLayer.StorageLayer;
import io.supertokens.test.TestingProcessManager;
import io.supertokens.test.Utils;
import io.supertokens.test.httpRequest.HttpRequestForTesting;
import io.supertokens.thirdparty.ThirdParty;
import io.supertokens.utils.SemVer;
import org.junit.AfterClass;
import org.junit.Before;
Expand Down Expand Up @@ -194,4 +197,44 @@ public void testForAllTypesOfOutput() throws Exception {
process.kill();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
}

@Test
public void testGetUserForUsersOfOtherRecipeIds() throws Exception {
String[] args = {"../"};

TestingProcessManager.TestingProcess process = TestingProcessManager.start(args);
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

AuthRecipeUserInfo user1 = ThirdParty.signInUp(process.getProcess(), "google", "googleid", "test@example.com").user;
Passwordless.CreateCodeResponse user2code = Passwordless.createCode(process.getProcess(), "test@example.com",
null, null, null);
AuthRecipeUserInfo user2 = Passwordless.consumeCode(process.getProcess(), user2code.deviceId, user2code.deviceIdHash, user2code.userInputCode, null).user;

{
HashMap<String, String> map = new HashMap<>();
map.put("userId", user1.getSupertokensUserId());

JsonObject response = HttpRequestForTesting.sendGETRequest(process.getProcess(), "",
"http://localhost:3567/recipe/user", map, 1000, 1000, null, SemVer.v2_7.get(),
"emailpassword");
assertEquals(response.get("status").getAsString(), "UNKNOWN_USER_ID_ERROR");
}

{
HashMap<String, String> map = new HashMap<>();
map.put("userId", user2.getSupertokensUserId());

JsonObject response = HttpRequestForTesting.sendGETRequest(process.getProcess(), "",
"http://localhost:3567/recipe/user", map, 1000, 1000, null, SemVer.v2_7.get(),
"emailpassword");
assertEquals(response.get("status").getAsString(), "UNKNOWN_USER_ID_ERROR");
}

process.kill();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@

import com.google.gson.JsonObject;
import io.supertokens.ProcessState;
import io.supertokens.emailpassword.EmailPassword;
import io.supertokens.passwordless.Passwordless;
import io.supertokens.pluginInterface.STORAGE_TYPE;
import io.supertokens.pluginInterface.authRecipe.AuthRecipeUserInfo;
import io.supertokens.pluginInterface.multitenancy.TenantIdentifier;
import io.supertokens.pluginInterface.passwordless.PasswordlessStorage;
import io.supertokens.storageLayer.StorageLayer;
import io.supertokens.test.TestingProcessManager;
import io.supertokens.test.Utils;
import io.supertokens.test.httpRequest.HttpRequestForTesting;

import io.supertokens.thirdparty.ThirdParty;
import io.supertokens.utils.SemVer;
import io.supertokens.test.httpRequest.HttpResponseException;
import org.junit.AfterClass;
Expand Down Expand Up @@ -233,4 +237,42 @@ private static void checkUser(JsonObject resp, String userId, String email, Stri
assert (System.currentTimeMillis() - 10000 < user.get("timeJoined").getAsLong());
assertEquals(3, user.entrySet().size());
}

@Test
public void testGetUserForUsersOfOtherRecipeIds() throws Exception {
String[] args = {"../"};

TestingProcessManager.TestingProcess process = TestingProcessManager.start(args);
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

AuthRecipeUserInfo user1 = EmailPassword.signUp(process.getProcess(), "test@example.com", "password");
AuthRecipeUserInfo user2 = ThirdParty.signInUp(process.getProcess(), "google", "googleid", "test@example.com").user;

{
HashMap<String, String> map = new HashMap<>();
map.put("userId", user1.getSupertokensUserId());

JsonObject response = HttpRequestForTesting.sendGETRequest(process.getProcess(), "",
"http://localhost:3567/recipe/user", map, 1000, 1000, null, SemVer.v2_7.get(),
"passwordless");
assertEquals(response.get("status").getAsString(), "UNKNOWN_USER_ID_ERROR");
}

{
HashMap<String, String> map = new HashMap<>();
map.put("userId", user2.getSupertokensUserId());

JsonObject response = HttpRequestForTesting.sendGETRequest(process.getProcess(), "",
"http://localhost:3567/recipe/user", map, 1000, 1000, null, SemVer.v2_7.get(),
"passwordless");
assertEquals(response.get("status").getAsString(), "UNKNOWN_USER_ID_ERROR");
}

process.kill();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

import com.google.gson.JsonObject;
import io.supertokens.ProcessState;
import io.supertokens.emailpassword.EmailPassword;
import io.supertokens.passwordless.Passwordless;
import io.supertokens.pluginInterface.STORAGE_TYPE;
import io.supertokens.pluginInterface.authRecipe.AuthRecipeUserInfo;
import io.supertokens.storageLayer.StorageLayer;
import io.supertokens.test.TestingProcessManager;
import io.supertokens.test.Utils;
Expand Down Expand Up @@ -205,6 +208,46 @@ public void testAllTypesOfOutput() throws Exception {
}
}

@Test
public void testGetUserForUsersOfOtherRecipeIds() throws Exception {
String[] args = {"../"};

TestingProcessManager.TestingProcess process = TestingProcessManager.start(args);
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

if (StorageLayer.getStorage(process.getProcess()).getType() != STORAGE_TYPE.SQL) {
return;
}

AuthRecipeUserInfo user1 = EmailPassword.signUp(process.getProcess(), "test@example.com", "password");
Passwordless.CreateCodeResponse user2code = Passwordless.createCode(process.getProcess(), "test@example.com",
null, null, null);
AuthRecipeUserInfo user2 = Passwordless.consumeCode(process.getProcess(), user2code.deviceId, user2code.deviceIdHash, user2code.userInputCode, null).user;

{
HashMap<String, String> map = new HashMap<>();
map.put("userId", user1.getSupertokensUserId());

JsonObject response = HttpRequestForTesting.sendGETRequest(process.getProcess(), "",
"http://localhost:3567/recipe/user", map, 1000, 1000, null, SemVer.v2_7.get(),
"thirdparty");
assertEquals(response.get("status").getAsString(), "UNKNOWN_USER_ID_ERROR");
}

{
HashMap<String, String> map = new HashMap<>();
map.put("userId", user2.getSupertokensUserId());

JsonObject response = HttpRequestForTesting.sendGETRequest(process.getProcess(), "",
"http://localhost:3567/recipe/user", map, 1000, 1000, null, SemVer.v2_7.get(),
"thirdparty");
assertEquals(response.get("status").getAsString(), "UNKNOWN_USER_ID_ERROR");
}

process.kill();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
}

public static void checkUser(JsonObject user, String thirdPartyId, String thirdPartyUserId, String email) {
assertNotNull(user.get("id"));
assertNotNull(user.get("timeJoined"));
Expand Down
Loading