Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ ecfbaf459af23ed5981aa3b030425f67dece7f9e

# Scala Steward: Reformat with scalafmt 3.9.7
cb2021ce0648edfbf4bb45f2962562820c7b677b

# Scala Steward: Reformat with scalafmt 3.11.0
f17d63754ce806a77e5a57c99dcc7b7cc7f1a5ca
2 changes: 1 addition & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
version="3.10.7"
version="3.11.0"
maxColumn = 120
runner.dialect = scala3
44 changes: 22 additions & 22 deletions src/main/scala/lc/captchas/CrumpledTextCaptcha.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ class CrumpledTextCaptcha extends ChallengeProvider {
def returnChallenge(level: String, size: String): Challenge = {
val r = new scala.util.Random
val n = level match {
case "easy" => 5
case "easy" => 5
case "medium" => 6
case "hard" => 7
case _ => 6
case "hard" => 7
case _ => 6
}
val characters = if (level == "hard") HelperFunctions.safeCharacters else HelperFunctions.safeAlphaNum
val secret = LazyList.continually(r.nextInt(characters.size)).map(characters).take(n).mkString
Expand Down Expand Up @@ -71,13 +71,13 @@ class CrumpledTextCaptcha extends ChallengeProvider {

var img = canvas
val numFolds = level match {
case "easy" => 7
case "medium" => 14
case "hard" => 19
case _ => 14
case "easy" => 7
case "medium" => 14
case "hard" => 19
case _ => 14
}
for (_ <- 0 until numFolds) {
img = applyFold(img, r)
img = applyFold(img, r)
}

val baos = new ByteArrayOutputStream()
Expand Down Expand Up @@ -126,27 +126,27 @@ class CrumpledTextCaptcha extends ChallengeProvider {
val rgb01 = img.getRGB(xf, yf + 1)
val rgb11 = img.getRGB(xf + 1, yf + 1)

val r00 = (rgb00 >> 16) & 0xFF
val g00 = (rgb00 >> 8) & 0xFF
val b00 = rgb00 & 0xFF
val r10 = (rgb10 >> 16) & 0xFF
val g10 = (rgb10 >> 8) & 0xFF
val b10 = rgb10 & 0xFF
val r01 = (rgb01 >> 16) & 0xFF
val g01 = (rgb01 >> 8) & 0xFF
val b01 = rgb01 & 0xFF
val r11 = (rgb11 >> 16) & 0xFF
val g11 = (rgb11 >> 8) & 0xFF
val b11 = rgb11 & 0xFF
val r00 = (rgb00 >> 16) & 0xff
val g00 = (rgb00 >> 8) & 0xff
val b00 = rgb00 & 0xff
val r10 = (rgb10 >> 16) & 0xff
val g10 = (rgb10 >> 8) & 0xff
val b10 = rgb10 & 0xff
val r01 = (rgb01 >> 16) & 0xff
val g01 = (rgb01 >> 8) & 0xff
val b01 = rgb01 & 0xff
val r11 = (rgb11 >> 16) & 0xff
val g11 = (rgb11 >> 8) & 0xff
val b11 = rgb11 & 0xff

val rInterp = (r00 * (1 - xt) * (1 - yt) + r10 * xt * (1 - yt) + r01 * (1 - xt) * yt + r11 * xt * yt).toInt
val gInterp = (g00 * (1 - xt) * (1 - yt) + g10 * xt * (1 - yt) + g01 * (1 - xt) * yt + g11 * xt * yt).toInt
val bInterp = (b00 * (1 - xt) * (1 - yt) + b10 * xt * (1 - yt) + b01 * (1 - xt) * yt + b11 * xt * yt).toInt

val rgb = (0xFF << 24) | (rInterp << 16) | (gInterp << 8) | bInterp
val rgb = (0xff << 24) | (rInterp << 16) | (gInterp << 8) | bInterp
newImg.setRGB(x, y, rgb)
} else {
newImg.setRGB(x, y, 0xFFFFFFFF)
newImg.setRGB(x, y, 0xffffffff)
}
} else {
newImg.setRGB(x, y, img.getRGB(x, y))
Expand Down
5 changes: 2 additions & 3 deletions src/main/scala/lc/core/config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Config(configFilePath: String) {
private val appConfigEither = AppConfig.codec.decode(ByteBuffer.wrap(configString.getBytes("UTF-8")))
private val appConfig = appConfigEither match {
case Right(conf) => conf
case Left(err) => throw new Exception(err.toString)
case Left(err) => throw new Exception(err.toString)
}
private val configFields: ConfigField = appConfig.toConfigField

Expand Down Expand Up @@ -121,5 +121,4 @@ class Config(configFilePath: String) {

}

object Config {
}
object Config {}
12 changes: 10 additions & 2 deletions src/main/scala/lc/core/models.scala
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,16 @@ case class AppConfig(
captchas: List[CaptchaConfig] = List.empty
) {
def toConfigField: ConfigField = ConfigField(
port, address, bufferCount, seed, captchaExpiryTimeLimit,
threadDelay, playgroundEnabled, corsHeader, maxAttemptsRatio, authRequired
port,
address,
bufferCount,
seed,
captchaExpiryTimeLimit,
threadDelay,
playgroundEnabled,
corsHeader,
maxAttemptsRatio,
authRequired
)
}
object AppConfig {
Expand Down
60 changes: 30 additions & 30 deletions src/test/scala/lc/ServerAuthSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,40 @@ class ServerAuthSpec extends AnyFunSuite {
val url = new URL("http://localhost:8889/v2/captcha")

// 1. Test without auth header
val connection1 = url.openConnection().asInstanceOf[HttpURLConnection]
connection1.setRequestMethod("POST")
connection1.setRequestProperty("Content-Type", "application/json")
connection1.setDoOutput(true)
val payload = """{"level":"debug","media":"image/png","input_type":"text","size":"350x100"}"""
val out1 = new OutputStreamWriter(connection1.getOutputStream)
out1.write(payload)
out1.close()
val connection1 = url.openConnection().asInstanceOf[HttpURLConnection]
connection1.setRequestMethod("POST")
connection1.setRequestProperty("Content-Type", "application/json")
connection1.setDoOutput(true)
val payload = """{"level":"debug","media":"image/png","input_type":"text","size":"350x100"}"""
val out1 = new OutputStreamWriter(connection1.getOutputStream)
out1.write(payload)
out1.close()

var responseCode = connection1.getResponseCode
assert(responseCode == 401, s"Expected 401 but got $responseCode")
var responseCode = connection1.getResponseCode
assert(responseCode == 401, s"Expected 401 but got $responseCode")

// 2. Test with invalid auth header
val connection2 = url.openConnection().asInstanceOf[HttpURLConnection]
connection2.setRequestMethod("POST")
connection2.setRequestProperty("Content-Type", "application/json")
connection2.setRequestProperty("Auth", "wrongsecret")
connection2.setDoOutput(true)
val out2 = new OutputStreamWriter(connection2.getOutputStream)
out2.write(payload)
out2.close()
// 2. Test with invalid auth header
val connection2 = url.openConnection().asInstanceOf[HttpURLConnection]
connection2.setRequestMethod("POST")
connection2.setRequestProperty("Content-Type", "application/json")
connection2.setRequestProperty("Auth", "wrongsecret")
connection2.setDoOutput(true)
val out2 = new OutputStreamWriter(connection2.getOutputStream)
out2.write(payload)
out2.close()

responseCode = connection2.getResponseCode
assert(responseCode == 401, s"Expected 401 but got $responseCode")
responseCode = connection2.getResponseCode
assert(responseCode == 401, s"Expected 401 but got $responseCode")

// 3. Test with valid auth header
val connection3 = url.openConnection().asInstanceOf[HttpURLConnection]
connection3.setRequestMethod("POST")
connection3.setRequestProperty("Content-Type", "application/json")
connection3.setRequestProperty("Auth", "secret123")
connection3.setDoOutput(true)
val out3 = new OutputStreamWriter(connection3.getOutputStream)
out3.write(payload)
out3.close()
// 3. Test with valid auth header
val connection3 = url.openConnection().asInstanceOf[HttpURLConnection]
connection3.setRequestMethod("POST")
connection3.setRequestProperty("Content-Type", "application/json")
connection3.setRequestProperty("Auth", "secret123")
connection3.setDoOutput(true)
val out3 = new OutputStreamWriter(connection3.getOutputStream)
out3.write(payload)
out3.close()

responseCode = connection3.getResponseCode
assert(responseCode == 200, s"Expected 200 but got $responseCode")
Expand Down
Loading