redesign the challenge provider interface to be Java compatible

This commit is contained in:
hrj 2019-03-05 17:49:20 +05:30
parent 618823fa7d
commit d99b120b2c
4 changed files with 35 additions and 19 deletions

View File

@ -0,0 +1,13 @@
package lc;
public class Challenge {
public final byte[] content;
public final String contentType;
public final String secret;
public Challenge(final byte[] content, final String contentType, final String secret) {
this.content = content;
this.contentType = contentType;
this.secret = secret;
}
}

View File

@ -0,0 +1,10 @@
package lc;
interface ChallengeProvider {
public String getId();
public Challenge returnChallenge();
public boolean checkAnswer(String secret, String answer);
//TODO: def configure(): Unit
}

View File

@ -6,9 +6,9 @@ import java.awt.image.BufferedImage
import java.awt.Font
import java.awt.Color
class FilterChallenge extends ChallengeProvider{
val id = "filter"
def returnChallenge(): (Image, String) = {
class FilterChallenge extends ChallengeProvider {
def getId = "filter"
def returnChallenge(): Challenge = {
val filterTypes = List(new FilterType1, new FilterType2)
val r = new scala.util.Random
val alphabet = "abcdefghijklmnopqrstuvwxyz"
@ -25,7 +25,7 @@ class FilterChallenge extends ChallengeProvider{
var image = new Image(canvas, ImageMetadata.empty)
val s = scala.util.Random.nextInt(2)
image = filterTypes(s).applyFilter(image)
(image, secret)
new Challenge(image.bytes, "image/png", secret)
}
def checkAnswer(secret: String, answer: String): Boolean = {
secret == answer

View File

@ -15,13 +15,6 @@ import org.json4s.jackson.Serialization.{read, write}
import java.util.concurrent._
import scala.Array
trait ChallengeProvider {
val id: String
def returnChallenge(): (Image, String)
def checkAnswer(secret: String, answer: String): Boolean
//TODO: def configure(): Unit
}
class Captcha {
val con: Connection = DriverManager.getConnection("jdbc:h2:./captcha", "sa", "")
val stmt: Statement = con.createStatement()
@ -47,13 +40,13 @@ class Captcha {
//TODO: eval params to choose a provider
val providerMap = "FilterChallenge"
val provider = filters(providerMap)
val (image, secret) = provider.returnChallenge()
val blob = new ByteArrayInputStream(image.bytes)
val challenge = provider.returnChallenge()
val blob = new ByteArrayInputStream(challenge.content)
val token = scala.util.Random.nextInt(10000).toString
val id = Id(token)
insertPstmt.setString(1, token)
insertPstmt.setString(2, provider.id)
insertPstmt.setString(3, secret)
insertPstmt.setString(2, provider.getId)
insertPstmt.setString(3, challenge.secret)
insertPstmt.setString(4, providerMap)
insertPstmt.setBlob(5, blob)
insertPstmt.executeUpdate()
@ -64,13 +57,13 @@ class Captcha {
val providerMap = "FilterChallenge"
val provider = filters(providerMap)
def run(): Unit = {
val (image, secret) = provider.returnChallenge()
val blob = new ByteArrayInputStream(image.bytes)
val challenge = provider.returnChallenge()
val blob = new ByteArrayInputStream(challenge.content)
val token = scala.util.Random.nextInt(10000).toString
val id = Id(token)
insertPstmt.setString(1, token)
insertPstmt.setString(2, provider.id)
insertPstmt.setString(3, secret)
insertPstmt.setString(2, provider.getId)
insertPstmt.setString(3, challenge.secret)
insertPstmt.setString(4, providerMap)
insertPstmt.setBlob(5, blob)
insertPstmt.executeUpdate()