Skip to content

Commit

Permalink
Fix expired cookie removal
Browse files Browse the repository at this point in the history
  • Loading branch information
AuroraLS3 committed May 26, 2022
1 parent 0538cea commit 7aa84c2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,17 @@ public void removeCookie(String cookie) {
Optional<User> foundUser = checkCookie(cookie);
if (foundUser.isPresent()) {
USERS_BY_COOKIE.remove(cookie);
deleteCookie(foundUser.get().getUsername());
deleteCookieByUser(foundUser.get().getUsername());
deleteCookie(cookie);
}
}

private void deleteCookie(String username) {
dbSystem.getDatabase().executeTransaction(CookieChangeTransaction.removeCookie(username));
private void deleteCookie(String cookie) {
dbSystem.getDatabase().executeTransaction(CookieChangeTransaction.removeCookie(cookie));
}

private void deleteCookieByUser(String username) {
dbSystem.getDatabase().executeTransaction(CookieChangeTransaction.removeCookieByUser(username));
}

public void removeAll() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.djrapitops.plan.storage.database.sql.building.CreateTableBuilder;
import com.djrapitops.plan.storage.database.sql.building.Sql;

import static com.djrapitops.plan.storage.database.sql.building.Sql.DELETE_FROM;
import static com.djrapitops.plan.storage.database.sql.building.Sql.WHERE;

/**
Expand All @@ -40,13 +41,16 @@ public class CookieTable {
COOKIE + ',' +
EXPIRES + ") VALUES (?,?,?)";

public static final String DELETE_BY_USER_STATEMENT = "DELETE FROM " + TABLE_NAME +
public static final String DELETE_BY_COOKIE_STATEMENT = DELETE_FROM + TABLE_NAME +
WHERE + COOKIE + "=?";

public static final String DELETE_BY_USER_STATEMENT = DELETE_FROM + TABLE_NAME +
WHERE + WEB_USERNAME + "=?";

public static final String DELETE_OLDER_STATEMENT = "DELETE FROM " + TABLE_NAME +
public static final String DELETE_OLDER_STATEMENT = DELETE_FROM + TABLE_NAME +
WHERE + EXPIRES + "<=?";

public static final String DELETE_ALL_STATEMENT = "DELETE FROM " + TABLE_NAME;
public static final String DELETE_ALL_STATEMENT = DELETE_FROM + TABLE_NAME;


private CookieTable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,41 @@ public static CookieChangeTransaction storeCookie(String username, String cookie
return new CookieChangeTransaction(username, cookie, expires);
}

public static CookieChangeTransaction removeCookie(String username) {
public static CookieChangeTransaction removeCookieByUser(String username) {
return new CookieChangeTransaction(username, null, null);
}

public static CookieChangeTransaction removeCookie(String cookie) {
return new CookieChangeTransaction(null, cookie, null);
}

public static CookieChangeTransaction removeAll() {
return new CookieChangeTransaction(null, null, null);
}

@Override
protected void performOperations() {
if (username == null) {
if (username == null && cookie == null) {
execute(new ExecStatement(CookieTable.DELETE_ALL_STATEMENT) {
@Override
public void prepare(PreparedStatement statement) {
// No parameters
}
});
} else if (username == null) {
execute(new ExecStatement(CookieTable.DELETE_BY_COOKIE_STATEMENT) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, cookie);
}
});
// Perform cleanup at the same time
execute(new ExecStatement(CookieTable.DELETE_OLDER_STATEMENT) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setLong(1, System.currentTimeMillis());
}
});
} else if (cookie == null) {
execute(new ExecStatement(CookieTable.DELETE_BY_USER_STATEMENT) {
@Override
Expand Down

0 comments on commit 7aa84c2

Please sign in to comment.