mirror of
https://github.com/librecaptcha/lc-core.git
synced 2025-01-13 06:53:19 -05:00
redesign the challenge provider interface to be Java compatible
This commit is contained in:
parent
618823fa7d
commit
d99b120b2c
13
src/main/java/Challenge.java
Normal file
13
src/main/java/Challenge.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
10
src/main/java/ChallengeProvider.java
Normal file
10
src/main/java/ChallengeProvider.java
Normal 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
|
||||||
|
}
|
||||||
|
|
@ -6,9 +6,9 @@ import java.awt.image.BufferedImage
|
|||||||
import java.awt.Font
|
import java.awt.Font
|
||||||
import java.awt.Color
|
import java.awt.Color
|
||||||
|
|
||||||
class FilterChallenge extends ChallengeProvider{
|
class FilterChallenge extends ChallengeProvider {
|
||||||
val id = "filter"
|
def getId = "filter"
|
||||||
def returnChallenge(): (Image, String) = {
|
def returnChallenge(): Challenge = {
|
||||||
val filterTypes = List(new FilterType1, new FilterType2)
|
val filterTypes = List(new FilterType1, new FilterType2)
|
||||||
val r = new scala.util.Random
|
val r = new scala.util.Random
|
||||||
val alphabet = "abcdefghijklmnopqrstuvwxyz"
|
val alphabet = "abcdefghijklmnopqrstuvwxyz"
|
||||||
@ -25,7 +25,7 @@ class FilterChallenge extends ChallengeProvider{
|
|||||||
var image = new Image(canvas, ImageMetadata.empty)
|
var image = new Image(canvas, ImageMetadata.empty)
|
||||||
val s = scala.util.Random.nextInt(2)
|
val s = scala.util.Random.nextInt(2)
|
||||||
image = filterTypes(s).applyFilter(image)
|
image = filterTypes(s).applyFilter(image)
|
||||||
(image, secret)
|
new Challenge(image.bytes, "image/png", secret)
|
||||||
}
|
}
|
||||||
def checkAnswer(secret: String, answer: String): Boolean = {
|
def checkAnswer(secret: String, answer: String): Boolean = {
|
||||||
secret == answer
|
secret == answer
|
||||||
|
@ -15,13 +15,6 @@ import org.json4s.jackson.Serialization.{read, write}
|
|||||||
import java.util.concurrent._
|
import java.util.concurrent._
|
||||||
import scala.Array
|
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 {
|
class Captcha {
|
||||||
val con: Connection = DriverManager.getConnection("jdbc:h2:./captcha", "sa", "")
|
val con: Connection = DriverManager.getConnection("jdbc:h2:./captcha", "sa", "")
|
||||||
val stmt: Statement = con.createStatement()
|
val stmt: Statement = con.createStatement()
|
||||||
@ -47,13 +40,13 @@ class Captcha {
|
|||||||
//TODO: eval params to choose a provider
|
//TODO: eval params to choose a provider
|
||||||
val providerMap = "FilterChallenge"
|
val providerMap = "FilterChallenge"
|
||||||
val provider = filters(providerMap)
|
val provider = filters(providerMap)
|
||||||
val (image, secret) = provider.returnChallenge()
|
val challenge = provider.returnChallenge()
|
||||||
val blob = new ByteArrayInputStream(image.bytes)
|
val blob = new ByteArrayInputStream(challenge.content)
|
||||||
val token = scala.util.Random.nextInt(10000).toString
|
val token = scala.util.Random.nextInt(10000).toString
|
||||||
val id = Id(token)
|
val id = Id(token)
|
||||||
insertPstmt.setString(1, token)
|
insertPstmt.setString(1, token)
|
||||||
insertPstmt.setString(2, provider.id)
|
insertPstmt.setString(2, provider.getId)
|
||||||
insertPstmt.setString(3, secret)
|
insertPstmt.setString(3, challenge.secret)
|
||||||
insertPstmt.setString(4, providerMap)
|
insertPstmt.setString(4, providerMap)
|
||||||
insertPstmt.setBlob(5, blob)
|
insertPstmt.setBlob(5, blob)
|
||||||
insertPstmt.executeUpdate()
|
insertPstmt.executeUpdate()
|
||||||
@ -64,13 +57,13 @@ class Captcha {
|
|||||||
val providerMap = "FilterChallenge"
|
val providerMap = "FilterChallenge"
|
||||||
val provider = filters(providerMap)
|
val provider = filters(providerMap)
|
||||||
def run(): Unit = {
|
def run(): Unit = {
|
||||||
val (image, secret) = provider.returnChallenge()
|
val challenge = provider.returnChallenge()
|
||||||
val blob = new ByteArrayInputStream(image.bytes)
|
val blob = new ByteArrayInputStream(challenge.content)
|
||||||
val token = scala.util.Random.nextInt(10000).toString
|
val token = scala.util.Random.nextInt(10000).toString
|
||||||
val id = Id(token)
|
val id = Id(token)
|
||||||
insertPstmt.setString(1, token)
|
insertPstmt.setString(1, token)
|
||||||
insertPstmt.setString(2, provider.id)
|
insertPstmt.setString(2, provider.getId)
|
||||||
insertPstmt.setString(3, secret)
|
insertPstmt.setString(3, challenge.secret)
|
||||||
insertPstmt.setString(4, providerMap)
|
insertPstmt.setString(4, providerMap)
|
||||||
insertPstmt.setBlob(5, blob)
|
insertPstmt.setBlob(5, blob)
|
||||||
insertPstmt.executeUpdate()
|
insertPstmt.executeUpdate()
|
||||||
|
Loading…
Reference in New Issue
Block a user