-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial commit for the framework #3
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package lc | ||
|
||
import com.sksamuel.scrimage._ | ||
import java.sql._ | ||
import java.io._ | ||
|
||
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(); | ||
stmt.execute("CREATE TABLE IF NOT EXISTS challenge(token varchar, id varchar, secret varchar, image blob)"); | ||
|
||
def getCaptcha(): Boolean = { | ||
val provider = new FilterChallenge | ||
val (token, image) = this.getChallenge(provider) | ||
val stmt: Statement = con.createStatement(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same with this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this wasn't being used. I've removed it in the most recent commit. |
||
image.output(new File("Captcha.png")) | ||
println(s"Token: ${token}") | ||
println("Enter your answer: ") | ||
val answer = scala.io.StdIn.readLine() | ||
this.getAnswer(token, answer, provider) | ||
} | ||
|
||
def getChallenge(provider: ChallengeProvider): (String, Image) = { | ||
val (image, secret) = provider.returnChallenge() | ||
val token = scala.util.Random.nextInt(10000).toString | ||
var pstmt: PreparedStatement = null | ||
pstmt = con.prepareStatement("INSERT INTO challenge(token, id, secret) VALUES (?, ?, ?)") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Convert var is mutable state, is 💣, avoid There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. using |
||
pstmt.setString(1, token) | ||
pstmt.setString(2, provider.id) | ||
pstmt.setString(3, secret) | ||
//TODO: insert image into database | ||
pstmt.executeUpdate() | ||
(token, image) | ||
} | ||
|
||
def getAnswer(token: String, answer: String, provider: ChallengeProvider): Boolean = { | ||
val stmt: Statement = con.createStatement(); | ||
val rs: ResultSet = stmt.executeQuery("SELECT secret FROM challenge WHERE token = "+token); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could convert it to a prepared statement, as an optimisation. |
||
rs.next() | ||
val secret = rs.getString("secret") | ||
provider.checkAnswer(secret, answer) | ||
} | ||
|
||
def display(): Unit = { | ||
val stmt: Statement = con.createStatement(); | ||
val rs: ResultSet = stmt.executeQuery("SELECT * FROM challenge"); | ||
|
||
println("token\t\tid\t\tsecret\t\timage"); | ||
while(rs.next()) { | ||
val token = rs.getString("token"); | ||
val id = rs.getString("id"); | ||
val secret = rs.getString("secret"); | ||
val image = rs.getString("image"); | ||
println(s"${token}\t\t${id}\t\t${secret}\t\t${image}") | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check indentation of the code in this function. |
||
} | ||
|
||
def closeConnection(): Unit = { | ||
con.close() | ||
} | ||
} | ||
|
||
object LCFramework{ | ||
def main(args: scala.Array[String]) { | ||
val captcha = new Captcha | ||
val result = captcha.getCaptcha() | ||
println(result) | ||
captcha.display() | ||
captcha.closeConnection() | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this
stmt
for? Doesn't seem to be used.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm using it to execute the
CREATE TABLE
statement.