Fix expired cookie removal

This commit is contained in:
Aurora Lahtela 2022-05-26 09:57:29 +03:00
parent 0538cea8be
commit 7aa84c27e4
3 changed files with 35 additions and 8 deletions

View File

@ -128,12 +128,17 @@ public class ActiveCookieStore implements SubSystem {
Optional<User> foundUser = checkCookie(cookie); Optional<User> foundUser = checkCookie(cookie);
if (foundUser.isPresent()) { if (foundUser.isPresent()) {
USERS_BY_COOKIE.remove(cookie); USERS_BY_COOKIE.remove(cookie);
deleteCookie(foundUser.get().getUsername()); deleteCookieByUser(foundUser.get().getUsername());
deleteCookie(cookie);
} }
} }
private void deleteCookie(String username) { private void deleteCookie(String cookie) {
dbSystem.getDatabase().executeTransaction(CookieChangeTransaction.removeCookie(username)); dbSystem.getDatabase().executeTransaction(CookieChangeTransaction.removeCookie(cookie));
}
private void deleteCookieByUser(String username) {
dbSystem.getDatabase().executeTransaction(CookieChangeTransaction.removeCookieByUser(username));
} }
public void removeAll() { public void removeAll() {

View File

@ -20,6 +20,7 @@ import com.djrapitops.plan.storage.database.DBType;
import com.djrapitops.plan.storage.database.sql.building.CreateTableBuilder; import com.djrapitops.plan.storage.database.sql.building.CreateTableBuilder;
import com.djrapitops.plan.storage.database.sql.building.Sql; 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; import static com.djrapitops.plan.storage.database.sql.building.Sql.WHERE;
/** /**
@ -40,13 +41,16 @@ public class CookieTable {
COOKIE + ',' + COOKIE + ',' +
EXPIRES + ") VALUES (?,?,?)"; 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 + "=?"; 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 + "<=?"; 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() { private CookieTable() {

View File

@ -39,23 +39,41 @@ public class CookieChangeTransaction extends Transaction {
return new CookieChangeTransaction(username, cookie, expires); return new CookieChangeTransaction(username, cookie, expires);
} }
public static CookieChangeTransaction removeCookie(String username) { public static CookieChangeTransaction removeCookieByUser(String username) {
return new CookieChangeTransaction(username, null, null); return new CookieChangeTransaction(username, null, null);
} }
public static CookieChangeTransaction removeCookie(String cookie) {
return new CookieChangeTransaction(null, cookie, null);
}
public static CookieChangeTransaction removeAll() { public static CookieChangeTransaction removeAll() {
return new CookieChangeTransaction(null, null, null); return new CookieChangeTransaction(null, null, null);
} }
@Override @Override
protected void performOperations() { protected void performOperations() {
if (username == null) { if (username == null && cookie == null) {
execute(new ExecStatement(CookieTable.DELETE_ALL_STATEMENT) { execute(new ExecStatement(CookieTable.DELETE_ALL_STATEMENT) {
@Override @Override
public void prepare(PreparedStatement statement) { public void prepare(PreparedStatement statement) {
// No parameters // 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) { } else if (cookie == null) {
execute(new ExecStatement(CookieTable.DELETE_BY_USER_STATEMENT) { execute(new ExecStatement(CookieTable.DELETE_BY_USER_STATEMENT) {
@Override @Override