From d99b120b2c02be2a023e14394ed7cab82a9aa79a Mon Sep 17 00:00:00 2001 From: hrj Date: Tue, 5 Mar 2019 17:49:20 +0530 Subject: [PATCH] redesign the challenge provider interface to be Java compatible --- src/main/java/Challenge.java | 13 +++++++++++++ src/main/java/ChallengeProvider.java | 10 ++++++++++ src/main/scala/lc/FilterChallenge.scala | 8 ++++---- src/main/scala/lc/Main.scala | 23 ++++++++--------------- 4 files changed, 35 insertions(+), 19 deletions(-) create mode 100644 src/main/java/Challenge.java create mode 100644 src/main/java/ChallengeProvider.java diff --git a/src/main/java/Challenge.java b/src/main/java/Challenge.java new file mode 100644 index 0000000..2783e2e --- /dev/null +++ b/src/main/java/Challenge.java @@ -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; + } +} diff --git a/src/main/java/ChallengeProvider.java b/src/main/java/ChallengeProvider.java new file mode 100644 index 0000000..0ed04fc --- /dev/null +++ b/src/main/java/ChallengeProvider.java @@ -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 +} + diff --git a/src/main/scala/lc/FilterChallenge.scala b/src/main/scala/lc/FilterChallenge.scala index 0b99f39..6f8e5f0 100644 --- a/src/main/scala/lc/FilterChallenge.scala +++ b/src/main/scala/lc/FilterChallenge.scala @@ -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 diff --git a/src/main/scala/lc/Main.scala b/src/main/scala/lc/Main.scala index abdceac..faafcbb 100644 --- a/src/main/scala/lc/Main.scala +++ b/src/main/scala/lc/Main.scala @@ -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()