Skip to content

Commit

Permalink
Added refresh button for default challenge.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanmrsulja authored and litvinovg committed Dec 11, 2023
1 parent 262b99a commit 83c553b
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package edu.cornell.mannlib.vitro.webapp.beans;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.twelvemonkeys.servlet.HttpServlet;

@WebServlet(name = "refreshCaptcha", urlPatterns = {"/refreshCaptcha"}, loadOnStartup = 5)
public class RefreshCaptchaController extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String oldChallengeId = request.getParameter("oldChallengeId");

response.setContentType("application/json");
PrintWriter out = response.getWriter();

CaptchaBundle newChallenge = CaptchaServiceBean.generateChallenge();
CaptchaServiceBean.getCaptchaChallenges().invalidate(oldChallengeId);
CaptchaServiceBean.getCaptchaChallenges().put(newChallenge.getCaptchaId(), newChallenge);

out.println("{\"challenge\": \"" + newChallenge.getB64Image() + "\", \"challengeId\": \"" +
newChallenge.getCaptchaId() + "\"}");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ else if (StringUtils.isBlank(appBean.getContactMail())) {
body.put("challengeId", captchaChallenge.getCaptchaId());
}

body.put("contextPath", vreq.getContextPath());
body.put("formAction", "submitFeedback");
body.put("captchaToUse", captchaImpl);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ private ResponseValues errorParametersNotValid(String errorMsg, String webuserna
body.put("webuseremail", webuseremail);
body.put("comments", comments);
body.put("captchaToUse", captchaImpl);
body.put("contextPath", vreq.getContextPath());

if (!captchaImpl.equals("RECAPTCHA")) {
CaptchaBundle captchaChallenge = CaptchaServiceBean.generateChallenge();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@
<!-- Your custom captcha implementation -->
<p>
<label class="realpersonLabel">${i18n().enter_in_security_field}:<span class="requiredHint"> *</span></label>
<img src="data:image/png;base64,${challenge!}" alt="Refresh page if not displayed..." style="vertical-align: middle;">
<div class="captcha-container">
<span><input id="refresh" type="button" value="" /></span>
<img id="captchaImage" src="data:image/png;base64,${challenge!}" alt="Refresh page if not displayed..." style="vertical-align: middle;">
</div>
<br />
<span><input type="text" id="userSolution" name="userSolution" style="vertical-align: middle;"></span>
<input type="text" id="challengeId" name="challengeId" value="${challengeId!}" style="display: none;">
</p>
Expand Down Expand Up @@ -71,4 +75,27 @@ ${scripts.add('<script type="text/javascript" src="${urls.base}/js/commentForm.j
document.getElementById('g-recaptcha-response').value = recaptchaResponse;
});
</script>
<#else>
<script>
$(document).ready(function () {
$('#refresh').click(function () {
var oldChallengeId = $('#challengeId').val();
$.ajax({
url: '${contextPath}/refreshCaptcha',
type: 'GET',
dataType: 'json',
data: { oldChallengeId: oldChallengeId },
success: function (data) {
$('#userSolution').val('');
$('#challengeId').val(data.challengeId);
$('#captchaImage').attr('src', 'data:image/png;base64,' + data.challenge);
},
error: function () {
console.error('Error while refreshing captcha');
}
});
});
});
</script>
</#if>
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,28 @@ form.customForm label.dateTime {
fieldset.dateTime select {
margin-top: 0;
}

.captcha-container {
position: relative;
display: inline-block;
}

.captcha-container span {
position: absolute;
top: 0;
right: 0;
background: #ffffffeb;
color: #595b5b;
border: 1px solid #595b5b;
font-size: 0.8em;
padding: 0 8px;
}

.captcha-container span input {
border: 0;
background: transparent;
}

/* ---------------------------------- */
/* ----- FOR MANAGE PUBLICATIONS ---- */
/* ---------------------------------- */
Expand Down

0 comments on commit 83c553b

Please sign in to comment.