Skip to content

Commit

Permalink
fix: PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Mar 4, 2024
1 parent 5f00b5e commit 311b9b0
Show file tree
Hide file tree
Showing 55 changed files with 449 additions and 447 deletions.
20 changes: 3 additions & 17 deletions src/main/java/io/supertokens/ActiveUsers.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,15 @@ public static void updateLastActive(Main main, String userId) {
}
}

public static int countUsersActiveSince(AppIdentifier appIdentifier, Storage storage, long time)
public static int countUsersActiveSince(Main main, AppIdentifier appIdentifier, long time)
throws StorageQueryException, TenantOrAppNotFoundException {
Storage storage = StorageLayer.getStorage(appIdentifier.getAsPublicTenantIdentifier(), main);
return StorageUtils.getActiveUsersStorage(storage).countUsersActiveSince(appIdentifier, time);
}

@TestOnly
public static int countUsersActiveSince(Main main, long time)
throws StorageQueryException, TenantOrAppNotFoundException {
return countUsersActiveSince(new AppIdentifier(null, null),
StorageLayer.getStorage(main), time);
}

public static void removeActiveUser(AppIdentifier appIdentifier, Storage storage, String userId)
throws StorageQueryException {
try {
((AuthRecipeSQLStorage) StorageUtils.getActiveUsersStorage(storage)).startTransaction(con -> {
StorageUtils.getActiveUsersStorage(storage).deleteUserActive_Transaction(con, appIdentifier, userId);
((AuthRecipeSQLStorage) StorageUtils.getActiveUsersStorage(storage)).commitTransaction(con);
return null;
});

} catch (StorageTransactionLogicException e) {
throw new StorageQueryException(e.actualException);
}
return countUsersActiveSince(main, new AppIdentifier(null, null), time);
}
}
6 changes: 3 additions & 3 deletions src/main/java/io/supertokens/authRecipe/AuthRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ private static CanLinkAccountsResult canLinkAccountsHelper(TransactionConnection
TenantIdentifier tenantIdentifier = new TenantIdentifier(
appIdentifier.getConnectionUriDomain(), appIdentifier.getAppId(),
tenantId);
// we do not bother with getting the tenantIdentifierWithStorage here because
// we do not bother with getting the storage for each tenant here because
// we get the tenants from the user itself, and the user can only be shared across
// tenants of the same storage - therefore, the storage will be the same.

Expand Down Expand Up @@ -656,7 +656,7 @@ public static long getUsersCountForTenant(TenantIdentifier tenantIdentifier,
tenantIdentifier, includeRecipeIds);
}

public static long getUsersCountAcrossAllTenants(AppIdentifier appIdentappIdentifierfierWithStorages,
public static long getUsersCountAcrossAllTenants(AppIdentifier appIdentifier,
Storage[] storages,
RECIPE_ID[] includeRecipeIds)
throws StorageQueryException,
Expand All @@ -665,7 +665,7 @@ public static long getUsersCountAcrossAllTenants(AppIdentifier appIdentappIdenti

for (Storage storage : storages) {
count += StorageUtils.getAuthRecipeStorage(storage).getUsersCount(
appIdentappIdentifierfierWithStorages, includeRecipeIds);
appIdentifier, includeRecipeIds);
}

return count;
Expand Down
9 changes: 1 addition & 8 deletions src/main/java/io/supertokens/inmemorydb/Start.java
Original file line number Diff line number Diff line change
Expand Up @@ -1861,7 +1861,7 @@ public int deleteUserMetadata(AppIdentifier appIdentifier, String userId) throws

@Override
public void addRoleToUser(TenantIdentifier tenantIdentifier, String userId, String role)
throws StorageQueryException, UnknownRoleException, DuplicateUserRoleMappingException,
throws StorageQueryException, DuplicateUserRoleMappingException,
TenantOrAppNotFoundException {
try {
UserRolesQueries.addRoleToUser(this, tenantIdentifier, userId, role);
Expand All @@ -1870,13 +1870,6 @@ public void addRoleToUser(TenantIdentifier tenantIdentifier, String userId, Stri
SQLiteConfig config = Config.getConfig(this);
String serverErrorMessage = e.getMessage();

if (isForeignKeyConstraintError(
serverErrorMessage,
config.getRolesTable(),
new String[]{"app_id", "role"},
new Object[]{tenantIdentifier.getAppId(), role})) {
throw new UnknownRoleException();
}
if (isPrimaryKeyError(serverErrorMessage, config.getUserRolesTable(),
new String[]{"app_id", "tenant_id", "user_id", "role"})) {
throw new DuplicateUserRoleMappingException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ public static String getQueryToCreateUserRolesTable(Start start) {
+ "user_id VARCHAR(128) NOT NULL,"
+ "role VARCHAR(255) NOT NULL,"
+ "PRIMARY KEY(app_id, tenant_id, user_id, role),"
+ "FOREIGN KEY(app_id, role) REFERENCES " + Config.getConfig(start).getRolesTable()
+ " (app_id, role) ON DELETE CASCADE,"
+ "FOREIGN KEY(app_id, tenant_id) REFERENCES " + Config.getConfig(start).getTenantsTable()
+ " (app_id, tenant_id) ON DELETE CASCADE"
+ ");";
Expand Down
24 changes: 15 additions & 9 deletions src/main/java/io/supertokens/userroles/UserRoles.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ public static boolean addRoleToUser(Main main, TenantIdentifier tenantIdentifier
// We do this because it's not straight forward to replicate roles to all storages of an app
Storage appStorage = StorageLayer.getStorage(
tenantIdentifier.toAppIdentifier().getAsPublicTenantIdentifier(), main);

String[] roles = getRoles(tenantIdentifier.toAppIdentifier(), appStorage);
if (!Arrays.asList(roles).contains(role)) {
if (!doesRoleExist(tenantIdentifier.toAppIdentifier(), appStorage, role)) {
throw new UnknownRoleException();
}

Expand Down Expand Up @@ -287,15 +285,23 @@ public static String[] getRolesThatHavePermission(Main main,
}

// delete a role
public static boolean deleteRole(AppIdentifier appIdentifier, Storage storage, String role)
throws StorageQueryException {
return StorageUtils.getUserRolesStorage(storage).deleteRole(appIdentifier, role);
public static boolean deleteRole(Main main, AppIdentifier appIdentifier, String role)
throws StorageQueryException, TenantOrAppNotFoundException {

Storage[] storages = StorageLayer.getStoragesForApp(main, appIdentifier);
boolean deletedRole = false;
for (Storage storage : storages) {
UserRolesSQLStorage userRolesStorage = StorageUtils.getUserRolesStorage(storage);
deletedRole = userRolesStorage.deleteRole(appIdentifier, role) || deletedRole;
}

return deletedRole;
}

@TestOnly
public static boolean deleteRole(Main main, String role) throws StorageQueryException {
Storage storage = StorageLayer.getStorage(main);
return deleteRole(new AppIdentifier(null, null), storage, role);
public static boolean deleteRole(Main main, String role) throws StorageQueryException,
TenantOrAppNotFoundException {
return deleteRole(main, new AppIdentifier(null, null), role);
}

// retrieve all roles that have been created
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/io/supertokens/webserver/WebserverAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ private String getTenantId(HttpServletRequest req) {
apiPath = "/" + apiPath;
}
if (apiPath.equals("/")) {
if ((path.equals("") || path.equals("/"))) {
if (path.equals("") || path.equals("/")) {
return null;
}
} else {
Expand Down Expand Up @@ -316,7 +316,7 @@ protected Storage getTenantStorage(HttpServletRequest req)
protected Storage[] enforcePublicTenantAndGetAllStoragesForApp(HttpServletRequest req)
throws ServletException, BadPermissionException, TenantOrAppNotFoundException {
if (getTenantId(req) != null) {
throw new BadPermissionException("Only public tenantId can this app specific API");
throw new BadPermissionException("Only public tenantId can call this app specific API");
}

AppIdentifier appIdentifier = getAppIdentifier(req);
Expand All @@ -330,7 +330,7 @@ protected Storage enforcePublicTenantAndGetPublicTenantStorage(
this.getTenantId(req));

if (getTenantId(req) != null) {
throw new BadPermissionException("Only public tenantId can this app specific API");
throw new BadPermissionException("Only public tenantId can call this app specific API");
}

return StorageLayer.getStorage(tenantIdentifier, main);
Expand All @@ -345,7 +345,7 @@ protected StorageAndUserIdMapping getStorageAndUserIdMappingForTenantSpecificApi
userIdType);
}

protected StorageAndUserIdMapping getStorageAndUserIdMappingForAppSpecificApi(
protected StorageAndUserIdMapping enforcePublicTenantAndGetStorageAndUserIdMappingForAppSpecificApi(
HttpServletRequest req, String userId, UserIdType userIdType)
throws StorageQueryException, TenantOrAppNotFoundException, UnknownUserIdException, ServletException,
BadPermissionException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IO
try {
String userId = inputRecipeUserId;
StorageAndUserIdMapping storageAndMapping =
getStorageAndUserIdMappingForAppSpecificApi(
enforcePublicTenantAndGetStorageAndUserIdMappingForAppSpecificApi(
req, inputRecipeUserId, UserIdType.ANY);
storage = storageAndMapping.storage;
if (storageAndMapping.userIdMapping != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IO
String recipeUserId = inputRecipeUserId;
{
StorageAndUserIdMapping mappingAndStorage =
getStorageAndUserIdMappingForAppSpecificApi(
enforcePublicTenantAndGetStorageAndUserIdMappingForAppSpecificApi(
req, inputRecipeUserId, UserIdType.ANY);
if (mappingAndStorage.userIdMapping != null) {
recipeUserId = mappingAndStorage.userIdMapping.superTokensUserId;
Expand All @@ -75,7 +75,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IO
String primaryUserId = inputPrimaryUserId;
{
StorageAndUserIdMapping mappingAndStorage =
getStorageAndUserIdMappingForAppSpecificApi(
enforcePublicTenantAndGetStorageAndUserIdMappingForAppSpecificApi(
req, inputPrimaryUserId, UserIdType.ANY);
if (mappingAndStorage.userIdMapping != null) {
primaryUserId = mappingAndStorage.userIdMapping.superTokensUserId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
try {
String userId = inputRecipeUserId;
StorageAndUserIdMapping mappingAndStorage =
getStorageAndUserIdMappingForAppSpecificApi(
enforcePublicTenantAndGetStorageAndUserIdMappingForAppSpecificApi(
req, inputRecipeUserId, UserIdType.ANY);
storage = mappingAndStorage.storage;
if (mappingAndStorage.userIdMapping != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
String recipeUserId = inputRecipeUserId;
{
StorageAndUserIdMapping mappingAndStorage =
getStorageAndUserIdMappingForAppSpecificApi(
enforcePublicTenantAndGetStorageAndUserIdMappingForAppSpecificApi(
req, inputRecipeUserId, UserIdType.ANY);
if (mappingAndStorage.userIdMapping != null) {
recipeUserId = mappingAndStorage.userIdMapping.superTokensUserId;
Expand All @@ -77,7 +77,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
String primaryUserId = inputPrimaryUserId;
{
StorageAndUserIdMapping mappingAndStorage =
getStorageAndUserIdMappingForAppSpecificApi(
enforcePublicTenantAndGetStorageAndUserIdMappingForAppSpecificApi(
req, inputPrimaryUserId, UserIdType.ANY);
if (mappingAndStorage.userIdMapping != null) {
primaryUserId = mappingAndStorage.userIdMapping.superTokensUserId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
try {
String userId = inputRecipeUserId;
StorageAndUserIdMapping mappingAndStorage =
getStorageAndUserIdMappingForAppSpecificApi(
enforcePublicTenantAndGetStorageAndUserIdMappingForAppSpecificApi(
req, inputRecipeUserId, UserIdType.ANY);
if (mappingAndStorage.userIdMapping != null) {
userId = mappingAndStorage.userIdMapping.superTokensUserId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IO
}

try {
int count = ActiveUsers.countUsersActiveSince(
this.getAppIdentifier(req),
this.enforcePublicTenantAndGetPublicTenantStorage(req), sinceTimestamp);
enforcePublicTenantAndGetPublicTenantStorage(req); // to enforce this API is called from public tenant
int count = ActiveUsers.countUsersActiveSince(main, this.getAppIdentifier(req), sinceTimestamp);
JsonObject result = new JsonObject();
result.addProperty("status", "OK");
result.addProperty("count", count);
Expand Down
11 changes: 3 additions & 8 deletions src/main/java/io/supertokens/webserver/api/core/ConfigAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,9 @@ protected boolean checkAPIKey(HttpServletRequest req) {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
String pid = InputParser.getQueryParamOrThrowError(req, "pid", false);

try {
TenantIdentifier tenantIdentifier = getTenantIdentifier(req);
getTenantStorage(req); // to check if tenant exists
if (!tenantIdentifier.equals(new TenantIdentifier(null, null, null))) {
throw new ServletException(new BadPermissionException("you can call this only from the base connection uri domain, public app and tenant"));
}
} catch (TenantOrAppNotFoundException e) {
throw new ServletException(e);
TenantIdentifier tenantIdentifier = getTenantIdentifier(req);
if (!tenantIdentifier.equals(new TenantIdentifier(null, null, null))) {
throw new ServletException(new BadPermissionException("you can call this only from the base connection uri domain, public app and tenant"));
}

if ((ProcessHandle.current().pid() + "").equals(pid)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I

try {
StorageAndUserIdMapping storageAndUserIdMapping =
this.getStorageAndUserIdMappingForAppSpecificApi(req, userId, UserIdType.ANY);
this.enforcePublicTenantAndGetStorageAndUserIdMappingForAppSpecificApi(req, userId, UserIdType.ANY);

AuthRecipe.deleteUser(getAppIdentifier(req), storageAndUserIdMapping.storage, userId,
removeAllLinkedAccounts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IO
try {
AppIdentifier appIdentifier = this.getAppIdentifier(req);
StorageAndUserIdMapping storageAndUserIdMapping =
this.getStorageAndUserIdMappingForAppSpecificApi(req, userId, UserIdType.ANY);
this.enforcePublicTenantAndGetStorageAndUserIdMappingForAppSpecificApi(req, userId, UserIdType.ANY);
// if a userIdMapping exists, pass the superTokensUserId to the getUserUsingId function
if (storageAndUserIdMapping.userIdMapping != null) {
userId = storageAndUserIdMapping.userIdMapping.superTokensUserId;
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/io/supertokens/webserver/api/core/HelloAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ private void handleRequest(HttpServletRequest req, HttpServletResponse resp) thr
// API is app specific

try {
RateLimiter rateLimiter = RateLimiter.getInstance(getAppIdentifier(req), super.main, 200);
AppIdentifier appIdentifier = getAppIdentifier(req);
Storage[] storages = StorageLayer.getStoragesForApp(main, appIdentifier); // throws tenantOrAppNotFoundException

RateLimiter rateLimiter = RateLimiter.getInstance(appIdentifier, super.main, 200);
if (!rateLimiter.checkRequest()) {
if (Main.isTesting) {
super.sendTextResponse(200, "RateLimitedHello", resp);
Expand All @@ -90,9 +93,6 @@ private void handleRequest(HttpServletRequest req, HttpServletResponse resp) thr
return;
}

AppIdentifier appIdentifier = getAppIdentifier(req);
Storage[] storages = StorageLayer.getStoragesForApp(main, appIdentifier);

for (Storage storage : storages) {
// even if the public tenant does not exist, the following function will return a null
// idea here is to test that the storage is working
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IO
if (userId != null) {
// Query by userId
StorageAndUserIdMapping storageAndUserIdMapping =
this.getStorageAndUserIdMappingForAppSpecificApi(req, userId, UserIdType.ANY);
this.enforcePublicTenantAndGetStorageAndUserIdMappingForAppSpecificApi(req, userId, UserIdType.ANY);
// if a userIdMapping exists, pass the superTokensUserId to the getUserUsingId function
if (storageAndUserIdMapping.userIdMapping != null) {
userId = storageAndUserIdMapping.userIdMapping.superTokensUserId;
Expand Down Expand Up @@ -166,7 +166,7 @@ protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws IO

try {
StorageAndUserIdMapping storageAndUserIdMapping =
this.getStorageAndUserIdMappingForAppSpecificApi(req, userId, UserIdType.ANY);
this.enforcePublicTenantAndGetStorageAndUserIdMappingForAppSpecificApi(req, userId, UserIdType.ANY);
// if a userIdMapping exists, pass the superTokensUserId to the updateUsersEmailOrPassword
if (storageAndUserIdMapping.userIdMapping != null) {
userId = storageAndUserIdMapping.userIdMapping.superTokensUserId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
AppIdentifier appIdentifier = getAppIdentifier(req);
Storage storage;
try {
StorageAndUserIdMapping storageAndUidMapping = getStorageAndUserIdMappingForAppSpecificApi(
StorageAndUserIdMapping storageAndUidMapping = enforcePublicTenantAndGetStorageAndUserIdMappingForAppSpecificApi(
req, userId, UserIdType.ANY);
storage = storageAndUidMapping.storage;
} catch (UnknownUserIdException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws Servl
AppIdentifier appIdentifier = getAppIdentifier(req);
Storage storage;
try {
StorageAndUserIdMapping storageAndUserIdMapping = getStorageAndUserIdMappingForAppSpecificApi(
StorageAndUserIdMapping storageAndUserIdMapping = enforcePublicTenantAndGetStorageAndUserIdMappingForAppSpecificApi(
req, userId, UserIdType.ANY);
storage = storageAndUserIdMapping.storage;
} catch (UnknownUserIdException e) {
Expand Down
Loading

0 comments on commit 311b9b0

Please sign in to comment.