Skip to content
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

Merged
merged 2 commits into from
Jun 14, 2018
Merged

Conversation

SanBlig
Copy link
Contributor

@SanBlig SanBlig commented Jun 6, 2018

The ChallengeProvider trait provides the methods to be implemented by the challenge providers.

The framework gets a challenge from a challenge provider, inserts the details of the challenge into the database, and gives the challenge to the user. It then gets the answer from the user, and sends it to the challenge provider for verification.

This commit includes the implementation only for FilterChallenge provider.

The ChallengeProvider trait provides the methods to be implemented by the challenge providers.

The framework gets a challenge from a challenge provider, inserts the details of the challenge into the database, and gives the challenge to the user. It then gets the answer from the user, and sends it to the challenge provider for verification.

This commit includes the implementation only for FilterChallenge provider.

class Captcha {
val con: Connection = DriverManager.getConnection("jdbc:h2:./captcha", "sa", "")
val stmt: Statement = con.createStatement();
Copy link
Contributor

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.

Copy link
Contributor Author

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.

def getCaptcha(): Boolean = {
val provider = new FilterChallenge
val (token, image) = this.getChallenge(provider)
val stmt: Statement = con.createStatement();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same with this stmt. Doesn't seem to be used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

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 (?, ?, ?)")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Convert pstmt to val by declaring and initialising in the same line.

var is mutable state, is 💣, avoid

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using prepareStatement is a good optimisation over exectuteQuery. But for it to be effective you should prepare it only once. In other words, move declaration of pstmt to the class level.


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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could convert it to a prepared statement, as an optimisation.

val secret = rs.getString("secret");
val image = rs.getString("image");
println(s"${token}\t\t${id}\t\t${secret}\t\t${image}")
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check indentation of the code in this function.

- Used prepared statements and made them immutable
- Fixed indentation
@hrj hrj merged commit f8a91db into librecaptcha:master Jun 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants