Commit 07cfa2073595265d7d7329fc8c58a88c769d5d15

Authored by Tiago Guimarães
1 parent 76fb0c258d
Exists in DSpace52++

first edition verify

dspace-api/pom.xml
... ... @@ -676,6 +676,11 @@
676 676 <version>1</version>
677 677 <type>jar</type>
678 678 </dependency>
  679 + <dependency>
  680 + <groupId>org.glassfish</groupId>
  681 + <artifactId>javax.json</artifactId>
  682 + <version>1.0.2</version>
  683 + </dependency>
679 684 </dependencies>
680 685  
681 686 </project>
... ...
dspace-api/src/main/java/pt/uminho/sdum/utils/VerifyRecaptcha.java
... ... @@ -0,0 +1,75 @@
  1 +package pt.uminho.sdum.utils;
  2 +
  3 +
  4 +import java.io.BufferedReader;
  5 +import java.io.DataOutputStream;
  6 +import java.io.IOException;
  7 +import java.io.InputStreamReader;
  8 +import java.io.StringReader;
  9 +import java.net.URL;
  10 +
  11 +import javax.json.Json;
  12 +import javax.json.JsonObject;
  13 +import javax.json.JsonReader;
  14 +import javax.net.ssl.HttpsURLConnection;
  15 +
  16 +public class VerifyRecaptcha {
  17 +
  18 + public static final String url = "https://www.google.com/recaptcha/api/siteverify";
  19 + public static final String secret = "6LfOliQTAAAAAO3gKRe5J-rNfG7DSHbdua71R57h";
  20 + private final static String USER_AGENT = "Mozilla/5.0";
  21 +
  22 + public static boolean verify(String gRecaptchaResponse) throws IOException {
  23 + if (gRecaptchaResponse == null || "".equals(gRecaptchaResponse)) {
  24 + return false;
  25 + }
  26 +
  27 + try {
  28 + URL obj = new URL(url);
  29 + HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
  30 +
  31 + // add reuqest header
  32 + con.setRequestMethod("POST");
  33 + con.setRequestProperty("User-Agent", USER_AGENT);
  34 + con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
  35 +
  36 + String postParams = "secret=" + secret + "&response="
  37 + + gRecaptchaResponse;
  38 +
  39 + // Send post request
  40 + con.setDoOutput(true);
  41 + DataOutputStream wr = new DataOutputStream(con.getOutputStream());
  42 + wr.writeBytes(postParams);
  43 + wr.flush();
  44 + wr.close();
  45 +
  46 + int responseCode = con.getResponseCode();
  47 + System.out.println("\nSending 'POST' request to URL : " + url);
  48 + System.out.println("Post parameters : " + postParams);
  49 + System.out.println("Response Code : " + responseCode);
  50 +
  51 + BufferedReader in = new BufferedReader(new InputStreamReader(
  52 + con.getInputStream()));
  53 + String inputLine;
  54 + StringBuilder response = new StringBuilder();
  55 +
  56 + while ((inputLine = in.readLine()) != null) {
  57 + response.append(inputLine);
  58 + }
  59 + in.close();
  60 +
  61 + // print result
  62 + System.out.println(response.toString());
  63 +
  64 + //parse JSON response and return 'success' value
  65 + JsonReader jsonReader = Json.createReader(new StringReader(response.toString()));
  66 + JsonObject jsonObject = jsonReader.readObject();
  67 + jsonReader.close();
  68 +
  69 + return jsonObject.getBoolean("success");
  70 + } catch (Exception e) {
  71 + e.printStackTrace();
  72 + return false;
  73 + }
  74 + }
  75 +}
... ...
dspace-jspui/src/main/java/org/dspace/app/webui/servlet/RequestItemServlet.java
... ... @@ -26,6 +26,7 @@ import org.dspace.storage.bitstore.BitstreamStorageManager;
26 26 import org.dspace.storage.rdbms.DatabaseManager;
27 27 import org.dspace.storage.rdbms.TableRow;
28 28 import org.dspace.utils.DSpace;
  29 +import pt.uminho.sdum.utils.VerifyRecaptcha;
29 30  
30 31 import javax.mail.MessagingException;
31 32 import javax.servlet.ServletException;
... ... @@ -175,6 +176,10 @@ public class RequestItemServlet extends DSpaceServlet
175 176  
176 177 if (request.getParameter("submit") != null)
177 178 {
  179 + String gRecaptchaResponse = request
  180 + .getParameter("g-recaptcha-response");
  181 + boolean verify = VerifyRecaptcha.verify(gRecaptchaResponse);
  182 +
178 183 String reqname = request.getParameter("reqname");
179 184 String coment = request.getParameter("coment");
180 185 if (coment == null || coment.equals(""))
... ... @@ -183,7 +188,8 @@ public class RequestItemServlet extends DSpaceServlet
183 188  
184 189 // Check all data is there
185 190 if (requesterEmail == null || requesterEmail.equals("") ||
186   - reqname == null || reqname.equals(""))
  191 + reqname == null || reqname.equals("") ||
  192 + !verify)
187 193 {
188 194 request.setAttribute("handle",handle);
189 195 request.setAttribute("bitstream-id", bitstream_id);
... ... @@ -255,6 +261,7 @@ public class RequestItemServlet extends DSpaceServlet
255 261 ""), me);
256 262 JSPManager.showInternalError(request, response);
257 263 }
  264 +
258 265 }
259 266 else
260 267 {
... ...