Add method to choose a random captcha instance each time (#28)

* git ignore .db

* Add method to choose random captcha
This commit is contained in:
Rahul Rudragoudar 2019-03-26 18:57:56 +05:30 committed by hrj
parent f2c588ef23
commit e67d19c857
2 changed files with 12 additions and 4 deletions

1
.gitignore vendored
View File

@ -1,6 +1,7 @@
*.class *.class
*.log *.log
*.png *.png
*.db
/bin/ /bin/
/project/target/ /project/target/
/target/ /target/

View File

@ -26,11 +26,18 @@ class Captcha {
val selectPstmt: PreparedStatement = con.prepareStatement("SELECT secret, provider FROM challenge WHERE token = ?") val selectPstmt: PreparedStatement = con.prepareStatement("SELECT secret, provider FROM challenge WHERE token = ?")
val imagePstmt: PreparedStatement = con.prepareStatement("SELECT image FROM challenge WHERE token = ?") val imagePstmt: PreparedStatement = con.prepareStatement("SELECT image FROM challenge WHERE token = ?")
val filters = Map("FilterChallenge" -> new FilterChallenge, val filters = Map("FilterChallenge" -> new FilterChallenge,
"FontFunCaptcha" -> new FontFunCaptcha, "FontFunCaptcha" -> new FontFunCaptcha,
"GifCaptcha" -> new GifCaptcha, "GifCaptcha" -> new GifCaptcha,
"ShadowTextCaptcha" -> new ShadowTextCaptcha "ShadowTextCaptcha" -> new ShadowTextCaptcha)
)
def getProvider(): String = {
val random = new scala.util.Random
val keys = filters.keys
val providerMap = keys.toVector(random.nextInt(keys.size))
providerMap
}
def getCaptcha(id: Id): Array[Byte] = { def getCaptcha(id: Id): Array[Byte] = {
imagePstmt.setString(1, id.id) imagePstmt.setString(1, id.id)
@ -45,7 +52,7 @@ class Captcha {
def getChallenge(param: Parameters): Id = { def getChallenge(param: Parameters): Id = {
//TODO: eval params to choose a provider //TODO: eval params to choose a provider
val providerMap = "FontFunCaptcha" val providerMap = getProvider()
val provider = filters(providerMap) val provider = filters(providerMap)
val challenge = provider.returnChallenge() val challenge = provider.returnChallenge()
val blob = new ByteArrayInputStream(challenge.content) val blob = new ByteArrayInputStream(challenge.content)
@ -63,7 +70,7 @@ class Captcha {
} }
val task = new Runnable { val task = new Runnable {
val providerMap = "FilterChallenge" val providerMap = getProvider()
val provider = filters(providerMap) val provider = filters(providerMap)
def run(): Unit = { def run(): Unit = {
val challenge = provider.returnChallenge() val challenge = provider.returnChallenge()