mirror of
https://github.com/librecaptcha/lc-core.git
synced 2025-01-12 22:43:20 -05:00
commit
4589c3bbb0
2
.gitignore
vendored
2
.gitignore
vendored
@ -2,3 +2,5 @@
|
||||
*.log
|
||||
*.png
|
||||
/bin/
|
||||
/project/target/
|
||||
/target/
|
||||
|
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.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
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user