Skip to content

Commit

Permalink
fix: PR changes
Browse files Browse the repository at this point in the history
  • Loading branch information
anku255 committed Feb 28, 2024
1 parent 7d25645 commit 3bb6adf
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 63 deletions.
7 changes: 3 additions & 4 deletions src/main/java/io/supertokens/bulkimport/BulkImport.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import io.supertokens.pluginInterface.bulkimport.BulkImportStorage.BulkImportUserStatus;
import io.supertokens.pluginInterface.bulkimport.BulkImportUser;
import io.supertokens.pluginInterface.bulkimport.BulkImportUserInfo;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
import io.supertokens.pluginInterface.multitenancy.AppIdentifierWithStorage;
import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException;
Expand Down Expand Up @@ -54,7 +53,7 @@ public static void addUsers(AppIdentifierWithStorage appIdentifierWithStorage, L
public static BulkImportUserPaginationContainer getUsers(AppIdentifierWithStorage appIdentifierWithStorage,
@Nonnull Integer limit, @Nullable BulkImportUserStatus status, @Nullable String paginationToken)
throws StorageQueryException, BulkImportUserPaginationToken.InvalidTokenException {
List<BulkImportUserInfo> users;
List<BulkImportUser> users;

if (paginationToken == null) {
users = appIdentifierWithStorage.getBulkImportStorage()
Expand All @@ -69,11 +68,11 @@ public static BulkImportUserPaginationContainer getUsers(AppIdentifierWithStorag
int maxLoop = users.size();
if (users.size() == limit + 1) {
maxLoop = limit;
BulkImportUserInfo user = users.get(limit);
BulkImportUser user = users.get(limit);
nextPaginationToken = new BulkImportUserPaginationToken(user.id, user.createdAt).generateToken();
}

List<BulkImportUserInfo> resultUsers = users.subList(0, maxLoop);
List<BulkImportUser> resultUsers = users.subList(0, maxLoop);
return new BulkImportUserPaginationContainer(resultUsers, nextPaginationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import io.supertokens.pluginInterface.bulkimport.BulkImportUserInfo;
import io.supertokens.pluginInterface.bulkimport.BulkImportUser;

public class BulkImportUserPaginationContainer {
public final List<BulkImportUserInfo> users;
public final List<BulkImportUser> users;
public final String nextPaginationToken;

public BulkImportUserPaginationContainer(@Nonnull List<BulkImportUserInfo> users, @Nullable String nextPaginationToken) {
public BulkImportUserPaginationContainer(@Nonnull List<BulkImportUser> users, @Nullable String nextPaginationToken) {
this.users = users;
this.nextPaginationToken = nextPaginationToken;
}
Expand Down
35 changes: 18 additions & 17 deletions src/main/java/io/supertokens/bulkimport/BulkImportUserUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@
import io.supertokens.multitenancy.Multitenancy;
import io.supertokens.pluginInterface.bulkimport.BulkImportUser;
import io.supertokens.pluginInterface.bulkimport.BulkImportUser.LoginMethod;
import io.supertokens.pluginInterface.bulkimport.BulkImportUser.LoginMethod.EmailPasswordLoginMethod;
import io.supertokens.pluginInterface.bulkimport.BulkImportUser.LoginMethod.ThirdPartyLoginMethod;
import io.supertokens.pluginInterface.bulkimport.BulkImportUser.LoginMethod.PasswordlessLoginMethod;
import io.supertokens.pluginInterface.bulkimport.BulkImportUser.TotpDevice;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
import io.supertokens.pluginInterface.multitenancy.AppIdentifier;
Expand All @@ -52,15 +49,15 @@
import static io.supertokens.utils.JsonValidatorUtils.validateJsonFieldType;

public class BulkImportUserUtils {
public static BulkImportUser createBulkImportUserFromJSON(Main main, AppIdentifier appIdentifier, JsonObject userData, String id)
public static BulkImportUser createBulkImportUserFromJSON(Main main, AppIdentifier appIdentifier, JsonObject userData, String id, String[] allUserRoles)
throws InvalidBulkImportDataException, StorageQueryException, TenantOrAppNotFoundException {
List<String> errors = new ArrayList<>();

String externalUserId = parseAndValidateFieldType(userData, "externalUserId", ValueType.STRING, false, String.class,
errors, ".");
JsonObject userMetadata = parseAndValidateFieldType(userData, "userMetadata", ValueType.OBJECT, false,
JsonObject.class, errors, ".");
List<String> userRoles = getParsedUserRoles(userData, errors);
List<String> userRoles = getParsedUserRoles(userData, allUserRoles, errors);
List<TotpDevice> totpDevices = getParsedTotpDevices(userData, errors);
List<LoginMethod> loginMethods = getParsedLoginMethods(main, appIdentifier, userData, errors);

Expand All @@ -72,7 +69,7 @@ public static BulkImportUser createBulkImportUserFromJSON(Main main, AppIdentifi
return new BulkImportUser(id, externalUserId, userMetadata, userRoles, totpDevices, loginMethods);
}

private static List<String> getParsedUserRoles(JsonObject userData, List<String> errors) {
private static List<String> getParsedUserRoles(JsonObject userData, String[] allUserRoles, List<String> errors) {
JsonArray jsonUserRoles = parseAndValidateFieldType(userData, "roles", ValueType.ARRAY_OF_STRING,
false,
JsonArray.class, errors, ".");
Expand All @@ -83,7 +80,14 @@ private static List<String> getParsedUserRoles(JsonObject userData, List<String>

// We already know that the jsonUserRoles is an array of non-empty strings, we will normalise each role now
List<String> userRoles = new ArrayList<>();
jsonUserRoles.forEach(role -> userRoles.add(validateAndNormaliseUserRole(role.getAsString(), errors)));
jsonUserRoles.forEach(role -> {
String normalisedRole = validateAndNormaliseUserRole(role.getAsString(), errors);
if (Arrays.asList(allUserRoles).contains(normalisedRole)) {
userRoles.add(normalisedRole);
} else {
errors.add("Role " + normalisedRole + " does not exist.");
}
});
return userRoles;
}

Expand Down Expand Up @@ -138,7 +142,7 @@ private static List<LoginMethod> getParsedLoginMethods(Main main, AppIdentifier
String tenantId = parseAndValidateFieldType(jsonLoginMethodObj, "tenantId", ValueType.STRING, false, String.class, errors, " for a loginMethod.");
Boolean isVerified = parseAndValidateFieldType(jsonLoginMethodObj, "isVerified", ValueType.BOOLEAN, false, Boolean.class, errors, " for a loginMethod.");
Boolean isPrimary = parseAndValidateFieldType(jsonLoginMethodObj, "isPrimary", ValueType.BOOLEAN, false, Boolean.class, errors, " for a loginMethod.");
Integer timeJoined = parseAndValidateFieldType(jsonLoginMethodObj, "timeJoinedInMSSinceEpoch", ValueType.INTEGER, false, Integer.class, errors, " for a loginMethod");
Long timeJoined = parseAndValidateFieldType(jsonLoginMethodObj, "timeJoinedInMSSinceEpoch", ValueType.LONG, false, Long.class, errors, " for a loginMethod");

recipeId = validateAndNormaliseRecipeId(recipeId, errors);
tenantId= validateAndNormaliseTenantId(main, appIdentifier, tenantId, recipeId, errors);
Expand All @@ -157,8 +161,7 @@ private static List<LoginMethod> getParsedLoginMethods(Main main, AppIdentifier
hashingAlgorithm = normalisedHashingAlgorithm != null ? normalisedHashingAlgorithm.toString() : hashingAlgorithm;
passwordHash = validateAndNormalisePasswordHash(main, appIdentifier, normalisedHashingAlgorithm, passwordHash, errors);

EmailPasswordLoginMethod emailPasswordLoginMethod = new EmailPasswordLoginMethod(email, passwordHash, hashingAlgorithm);
loginMethods.add(new LoginMethod(tenantId, recipeId, isVerified, isPrimary, timeJoinedInMSSinceEpoch, emailPasswordLoginMethod, null, null));
loginMethods.add(new LoginMethod(tenantId, recipeId, isVerified, isPrimary, timeJoinedInMSSinceEpoch, email, passwordHash, hashingAlgorithm, null, null, null));
} else if ("thirdparty".equals(recipeId)) {
String email = parseAndValidateFieldType(jsonLoginMethodObj, "email", ValueType.STRING, true, String.class, errors, " for a thirdparty recipe.");
String thirdPartyId = parseAndValidateFieldType(jsonLoginMethodObj, "thirdPartyId", ValueType.STRING, true, String.class, errors, " for a thirdparty recipe.");
Expand All @@ -168,17 +171,15 @@ private static List<LoginMethod> getParsedLoginMethods(Main main, AppIdentifier
thirdPartyId = validateAndNormaliseThirdPartyId(thirdPartyId, errors);
thirdPartyUserId = validateAndNormaliseThirdPartyUserId(thirdPartyUserId, errors);

ThirdPartyLoginMethod thirdPartyLoginMethod = new ThirdPartyLoginMethod(email, thirdPartyId, thirdPartyUserId);
loginMethods.add(new LoginMethod(tenantId, recipeId, isVerified, isPrimary, timeJoinedInMSSinceEpoch, null, thirdPartyLoginMethod, null));
loginMethods.add(new LoginMethod(tenantId, recipeId, isVerified, isPrimary, timeJoinedInMSSinceEpoch, email, null, null, thirdPartyId, thirdPartyUserId, null));
} else if ("passwordless".equals(recipeId)) {
String email = parseAndValidateFieldType(jsonLoginMethodObj, "email", ValueType.STRING, false, String.class, errors, " for a passwordless recipe.");
String phoneNumber = parseAndValidateFieldType(jsonLoginMethodObj, "phoneNumber", ValueType.STRING, false, String.class, errors, " for a passwordless recipe.");

email = validateAndNormaliseEmail(email, errors);
phoneNumber = validateAndNormalisePhoneNumber(phoneNumber, errors);

PasswordlessLoginMethod passwordlessLoginMethod = new PasswordlessLoginMethod(email, phoneNumber);
loginMethods.add(new LoginMethod(tenantId, recipeId, isVerified, isPrimary, timeJoinedInMSSinceEpoch, null, null, passwordlessLoginMethod));
loginMethods.add(new LoginMethod(tenantId, recipeId, isVerified, isPrimary, timeJoinedInMSSinceEpoch, email, null, null, null, null, phoneNumber));
}
}
return loginMethods;
Expand Down Expand Up @@ -314,10 +315,10 @@ private static Boolean validateAndNormaliseIsVerified(Boolean isVerified) {
return isVerified == null ? false : isVerified;
}

private static long validateAndNormaliseTimeJoined(Integer timeJoined, List<String> errors) {
// We default timeJoined to 0 if it is null
private static long validateAndNormaliseTimeJoined(Long timeJoined, List<String> errors) {
// We default timeJoined to currentTime if it is null
if (timeJoined == null) {
return 0;
return System.currentTimeMillis();
}

if (timeJoined > System.currentTimeMillis()) {
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/io/supertokens/utils/JsonValidatorUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public static <T> T parseAndValidateFieldType(JsonObject jsonObject, String key,
Integer intValue = jsonObject.get(key).getAsNumber().intValue();
value = (T) intValue;
break;
case LONG:
Long longValue = jsonObject.get(key).getAsNumber().longValue();
value = (T) longValue;
break;
case BOOLEAN:
Boolean boolValue = jsonObject.get(key).getAsBoolean();
value = (T) boolValue;
Expand Down Expand Up @@ -69,6 +73,7 @@ public static <T> T parseAndValidateFieldType(JsonObject jsonObject, String key,
public enum ValueType {
STRING,
INTEGER,
LONG,
BOOLEAN,
OBJECT,
ARRAY_OF_STRING,
Expand All @@ -79,6 +84,7 @@ private static String getTypeForErrorMessage(ValueType type) {
return switch (type) {
case STRING -> "string";
case INTEGER -> "integer";
case LONG -> "integer"; // choosing integer over long because it is user facing
case BOOLEAN -> "boolean";
case OBJECT -> "object";
case ARRAY_OF_STRING -> "array of string";
Expand All @@ -91,7 +97,7 @@ public static boolean validateJsonFieldType(JsonObject jsonObject, String key, V
return switch (expectedType) {
case STRING -> jsonObject.get(key).isJsonPrimitive() && jsonObject.getAsJsonPrimitive(key).isString()
&& !jsonObject.get(key).getAsString().isBlank();
case INTEGER -> jsonObject.get(key).isJsonPrimitive() && jsonObject.getAsJsonPrimitive(key).isNumber();
case INTEGER, LONG -> jsonObject.get(key).isJsonPrimitive() && jsonObject.getAsJsonPrimitive(key).isNumber();
case BOOLEAN -> jsonObject.get(key).isJsonPrimitive() && jsonObject.getAsJsonPrimitive(key).isBoolean();
case OBJECT -> jsonObject.get(key).isJsonObject();
case ARRAY_OF_OBJECT, ARRAY_OF_STRING -> jsonObject.get(key).isJsonArray()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import io.supertokens.output.Logging;
import io.supertokens.pluginInterface.bulkimport.BulkImportStorage.BulkImportUserStatus;
import io.supertokens.pluginInterface.bulkimport.BulkImportUser;
import io.supertokens.pluginInterface.bulkimport.BulkImportUserInfo;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
import io.supertokens.pluginInterface.multitenancy.AppIdentifierWithStorage;
import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException;
Expand Down Expand Up @@ -93,7 +92,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
result.addProperty("status", "OK");

JsonArray usersJson = new JsonArray();
for (BulkImportUserInfo user : users.users) {
for (BulkImportUser user : users.users) {
usersJson.add(user.toJsonObject());
}
result.add("users", usersJson);
Expand Down Expand Up @@ -130,12 +129,20 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S
throw new ServletException(e);
}

String[] allUserRoles = null;

try {
allUserRoles = appIdentifierWithStorage.getUserRolesStorage().getRoles(appIdentifierWithStorage);
} catch (StorageQueryException e) {
throw new ServletException(e);
}

JsonArray errorsJson = new JsonArray();
ArrayList<BulkImportUser> usersToAdd = new ArrayList<>();

for (int i = 0; i < users.size(); i++) {
try {
BulkImportUser user = BulkImportUserUtils.createBulkImportUserFromJSON(main, appIdentifierWithStorage, users.get(i).getAsJsonObject(), Utils.getUUID());
BulkImportUser user = BulkImportUserUtils.createBulkImportUserFromJSON(main, appIdentifierWithStorage, users.get(i).getAsJsonObject(), Utils.getUUID(), allUserRoles);
usersToAdd.add(user);
} catch (io.supertokens.bulkimport.exceptions.InvalidBulkImportDataException e) {
JsonObject errorObj = new JsonObject();
Expand All @@ -147,9 +154,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S
errorObj.addProperty("index", i);
errorObj.add("errors", errors);
errorsJson.add(errorObj);
} catch (Exception e) {
System.out.println("error: " + e.getMessage());
e.printStackTrace();
} catch (StorageQueryException | TenantOrAppNotFoundException e) {
throw new ServletException(e);
}
}
Expand Down
Loading

0 comments on commit 3bb6adf

Please sign in to comment.