From 7c7285b9f7f2a63f0b338f9b375b8c299fb88a02 Mon Sep 17 00:00:00 2001 From: Rahul Rudragoudar Date: Sun, 9 Jun 2019 14:04:25 +0530 Subject: [PATCH] Conflict resolution Signed-off-by: Rahul Rudragoudar --- client/index.html | 23 +++++++++++++++++++++++ client/script.js | 9 +++++++++ client/style.css | 24 ++++++++++++++++++++++++ src/main/scala/lc/Main.scala | 26 +++++++++++++++++++++++++- 4 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 client/index.html create mode 100644 client/script.js create mode 100644 client/style.css diff --git a/client/index.html b/client/index.html new file mode 100644 index 0000000..2c9bb31 --- /dev/null +++ b/client/index.html @@ -0,0 +1,23 @@ + + + + Libre Captcha + + + +
+

Libre Captcha


+

Open Source solution to Captchas

+

v0.2 (Beta)

+
+
+ + + +
+
+

+
+ + + diff --git a/client/script.js b/client/script.js new file mode 100644 index 0000000..068467d --- /dev/null +++ b/client/script.js @@ -0,0 +1,9 @@ +document.getElementById("reg-btn").addEventListener("click", function(){ + var email = document.getElementById("email").value; + var url = window.location.origin+"/v1/token?email="+email + fetch(url) + .then(res => res.json()) + .then((data) => { + document.getElementById("token").innerHTML = "SECRET "+data.token; + }) +}) diff --git a/client/style.css b/client/style.css new file mode 100644 index 0000000..cc18455 --- /dev/null +++ b/client/style.css @@ -0,0 +1,24 @@ +body { + font-family: sans-serif; +} + +.header { + text-align: center; +} + +.form { + width: 200px; + margin: 0 auto; +} + +.form input { + width: 100%; + margin: 2px; + padding: 2px; +} + +#token { + margin: 10px; + padding: 3px; + text-align: center; +} diff --git a/src/main/scala/lc/Main.scala b/src/main/scala/lc/Main.scala index d011e96..412e27b 100644 --- a/src/main/scala/lc/Main.scala +++ b/src/main/scala/lc/Main.scala @@ -24,11 +24,13 @@ class Captcha(throttle: Int) { val stmt: Statement = con.createStatement() stmt.execute("CREATE TABLE IF NOT EXISTS challenge(token varchar, id varchar, secret varchar, provider varchar, contentType varchar, image blob, solved boolean default False, PRIMARY KEY(token))") stmt.execute("CREATE TABLE IF NOT EXISTS mapId(uuid varchar, token varchar, PRIMARY KEY(uuid), FOREIGN KEY(token) REFERENCES challenge(token))") + stmt.execute("CREATE TABLE IF NOT EXISTS users(email varchar, hash int)") val insertPstmt: PreparedStatement = con.prepareStatement("INSERT INTO challenge(token, id, secret, provider, contentType, image) VALUES (?, ?, ?, ?, ?, ?)") val mapPstmt: PreparedStatement = con.prepareStatement("INSERT INTO mapId(uuid, token) VALUES (?, ?)") val selectPstmt: PreparedStatement = con.prepareStatement("SELECT secret, provider FROM challenge WHERE token = ?") val imagePstmt: PreparedStatement = con.prepareStatement("SELECT image FROM challenge c, mapId m WHERE c.token=m.token AND m.uuid = ?") - val updatePstmt: PreparedStatement = con.prepareStatement("UPDATE challenge SET solved = True WHERE token = ?") + val updatePstmt: PreparedStatement = con.prepareStatement("UPDATE challenge c, mapId m SET c.solved = True WHERE c.token = m.token AND m.uuid = ?") + val userPstmt: PreparedStatement = con.prepareStatement("INSERT INTO users(email, hash) VALUES (?,?)") val providers = Map("FilterChallenge" -> new FilterChallenge, "FontFunCaptcha" -> new FontFunCaptcha, @@ -128,6 +130,16 @@ class Captcha(throttle: Int) { providers(provider).checkAnswer(secret, answer.answer) } + def getHash(email: String): Int = { + val secret = "" + val str = email+secret + val hash = str.hashCode() + userPstmt.setString(1, email) + userPstmt.setInt(2, hash) + userPstmt.executeUpdate() + hash + } + def display(): Unit = { val rs: ResultSet = stmt.executeQuery("SELECT * FROM challenge") println("token\t\tid\t\tsecret\t\tsolved") @@ -149,6 +161,7 @@ case class Size(height: Int, width: Int) case class Parameters(level: String, media: String, input_type: String, size: Option[Size]) case class Id(id: String) case class Answer(answer: String, id: String) +case class Secret(token: Int) class Server(port: Int){ val captcha = new Captcha(0) @@ -194,6 +207,17 @@ class Server(port: Int){ 0 },"POST") + host.addContext("/v1/register", new FileContextHandler(new File("client/"))) + + host.addContext("/v1/token", (req,resp) => { + val params = req.getParams() + val hash = captcha.getHash(params.get("email")) + val token = Secret(hash) + resp.getHeaders().add("Content-Type", "application/json") + resp.send(200, write(token)) + 0 + }) + def start(): Unit = { server.start() }