added captcha services

This commit is contained in:
Mykhaylo
2022-09-06 16:35:03 +02:00
parent eb7b78d59c
commit 6f8b3e1381
4 changed files with 190 additions and 0 deletions

View File

@@ -29,6 +29,9 @@ import org.dspace.services.ConfigurationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
/**
* @author Mykhaylo Boychuk (mykhaylo.boychuk@4science.com)
*/
public class CaptchaServiceImpl implements CaptchaService {
private static final Logger log = org.apache.logging.log4j.LogManager.getLogger(CaptchaServiceImpl.class);
@@ -119,4 +122,5 @@ public class CaptchaServiceImpl implements CaptchaService {
}
}
}
}

View File

@@ -0,0 +1,134 @@
package org.dspace.eperson;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
/**
* This model class represent the response for validation of reCaptcha token
*
* @author Mohamed Eskander (mohamed.eskander at 4science dot it)
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonPropertyOrder({
"success",
"score",
"action",
"challenge_ts",
"hostname",
"error-codes"
})
public class GoogleResponse {
@JsonProperty("success")
private boolean success;
@JsonProperty("score")
private float score;
@JsonProperty("action")
private String action;
@JsonProperty("challenge_ts")
private String challengeTs;
@JsonProperty("hostname")
private String hostname;
@JsonProperty("error-codes")
private ErrorCode[] errorCodes;
public boolean isSuccess() {
return success;
}
public float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getChallengeTs() {
return challengeTs;
}
public void setChallengeTs(String challengeTs) {
this.challengeTs = challengeTs;
}
public String getHostname() {
return hostname;
}
public void setHostname(String hostname) {
this.hostname = hostname;
}
public ErrorCode[] getErrorCodes() {
return errorCodes;
}
public void setErrorCodes(ErrorCode[] errorCodes) {
this.errorCodes = errorCodes;
}
@JsonIgnore
public boolean hasClientError() {
ErrorCode[] errors = getErrorCodes();
if (errors == null) {
return false;
}
for (ErrorCode error : errors) {
switch (error) {
case InvalidResponse:
case MissingResponse:
return true;
default: break;
}
}
return false;
}
static enum ErrorCode {
MissingSecret,
InvalidSecret,
MissingResponse,
InvalidResponse;
private static Map<String, ErrorCode> errorsMap = new HashMap<>(4);
static {
errorsMap.put("missing-input-secret", MissingSecret);
errorsMap.put("invalid-input-secret", InvalidSecret);
errorsMap.put("missing-input-response", MissingResponse);
errorsMap.put("invalid-input-response", InvalidResponse);
}
@JsonCreator
public static ErrorCode forValue(String value) {
return errorsMap.get(value.toLowerCase());
}
}
}

View File

@@ -0,0 +1,22 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.eperson;
public class InvalidReCaptchaException extends RuntimeException {
private static final long serialVersionUID = -5328794674744121744L;
public InvalidReCaptchaException(String message) {
super(message);
}
public InvalidReCaptchaException(String message, Exception cause) {
super(message, cause);
}
}

View File

@@ -0,0 +1,30 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.eperson.service;
import org.dspace.eperson.InvalidReCaptchaException;
/**
* This service for validate the reCaptcha token
*
* @author Mohamed Eskander (mohamed.eskander at 4science dot it)
*/
public interface CaptchaService {
public String REGISTER_ACTION = "register_email";
/**
* validate the entered reCaptcha token
*
* @param response reCaptcha token to be validated
* @param action action of reCaptcha
* @throws InvalidReCaptchaException if reCaptcha was not successfully validated
*/
public void processResponse(String response, String action) throws InvalidReCaptchaException;
}