Merge pull request #88 from UprootStaging/rotateCaptchas

Ensure fresh Captchas are served by sorting on attempted count
This commit is contained in:
hrj 2021-04-19 11:49:42 +05:30 committed by GitHub
commit f38d6ee191
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 13 deletions

View File

@ -11,10 +11,10 @@ public class HelperFunctions {
RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
} }
public static final String safeAlphabets = "ABCDEFGHJKMNOPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; public static final String safeAlphabets = "ABCDEFGHJKMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
public static final String allAlphabets = safeAlphabets + "ILl"; public static final String allAlphabets = safeAlphabets + "ILlO";
public static final String safeNumbers = "23456789"; public static final String safeNumbers = "23456789";
public static final String allNumbers = safeNumbers + "1"; public static final String allNumbers = safeNumbers + "10";
public static final String specialCharacters = "$#%@&?"; public static final String specialCharacters = "$#%@&?";
public static final String safeCharacters = safeAlphabets + safeNumbers + specialCharacters; public static final String safeCharacters = safeAlphabets + safeNumbers + specialCharacters;

View File

@ -96,7 +96,7 @@ object Captcha {
token match { token match {
case Some(value) => { case Some(value) => {
val uuid = getUUID(value) val uuid = getUUID(value)
updateAttempted(uuid) updateAttempted(value)
Right(Id(uuid)) Right(Id(uuid))
} }
case None => { case None => {
@ -121,9 +121,9 @@ object Captcha {
} }
} }
private def updateAttempted(uuid: String): Unit = { private def updateAttempted(token: Int): Unit = {
val updateAttemptedPstmt = Statements.tlStmts.get.updateAttemptedPstmt val updateAttemptedPstmt = Statements.tlStmts.get.updateAttemptedPstmt
updateAttemptedPstmt.setString(1, uuid) updateAttemptedPstmt.setInt(1, token)
updateAttemptedPstmt.executeUpdate() updateAttemptedPstmt.executeUpdate()
} }

View File

@ -19,7 +19,10 @@ class Statements(dbConn: DBConn, maxAttempts: Int) {
"contentInput varchar, " + "contentInput varchar, " +
"image blob, " + "image blob, " +
"attempted int default 0, " + "attempted int default 0, " +
"PRIMARY KEY(token))" "PRIMARY KEY(token));" +
"""
CREATE INDEX IF NOT EXISTS attempted ON challenge(attempted);
"""
) )
stmt.execute( stmt.execute(
"CREATE TABLE IF NOT EXISTS mapId" + "CREATE TABLE IF NOT EXISTS mapId" +
@ -64,21 +67,18 @@ class Statements(dbConn: DBConn, maxAttempts: Int) {
val updateAttemptedPstmt: PreparedStatement = dbConn.con.prepareStatement( val updateAttemptedPstmt: PreparedStatement = dbConn.con.prepareStatement(
"UPDATE challenge " + "UPDATE challenge " +
"SET attempted = attempted+1 " + "SET attempted = attempted+1 " +
"WHERE token = (SELECT m.token " + "WHERE token = ?;"
"FROM mapId m, challenge c " +
"WHERE m.token=c.token AND " +
"m.uuid = ?)"
) )
val tokenPstmt: PreparedStatement = dbConn.con.prepareStatement( val tokenPstmt: PreparedStatement = dbConn.con.prepareStatement(
s""" s"""
SELECT token SELECT token, attempted
FROM challenge FROM challenge
WHERE attempted < $maxAttempts AND WHERE attempted < $maxAttempts AND
contentLevel = ? AND contentLevel = ? AND
contentType = ? AND contentType = ? AND
contentInput = ? contentInput = ?
ORDER BY RAND() LIMIT 1""" ORDER BY attempted ASC LIMIT 1"""
) )
val deleteAnswerPstmt: PreparedStatement = dbConn.con.prepareStatement( val deleteAnswerPstmt: PreparedStatement = dbConn.con.prepareStatement(