This repository was archived by the owner on Aug 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Started work on #32 Signed-off-by: Jurriaan Den Toonder <[email protected]> * Add captcha to feedback, fixes #32 Signed-off-by: Jurriaan Den Toonder <[email protected]> * Remove large if blocks * Removed application.yml from versioning and added example yml file Signed-off-by: Jurriaan Den Toonder <[email protected]> * Add google guava for ImmutableMap in EducationFeedbackController Signed-off-by: Jurriaan Den Toonder <[email protected]>
- Loading branch information
1 parent
78c7e98
commit c7f7228
Showing
11 changed files
with
207 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package ch.wisv.service; | ||
|
||
import lombok.Getter; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.web.client.RestTemplateBuilder; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.util.StringUtils; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.regex.Pattern; | ||
|
||
@Service | ||
public class CaptchaService { | ||
|
||
private static Pattern RESPONSE_PATTERN = Pattern.compile("[A-Za-z0-9_-]+"); | ||
|
||
@Getter | ||
@Value("${wisvch.recaptcha.secret}") | ||
private String secret; | ||
|
||
@Getter | ||
@Value("${wisvch.recaptcha.verifyURI}") | ||
private String verifyURI; | ||
|
||
private RestTemplateBuilder restTemplateBuilder; | ||
|
||
@Autowired | ||
public CaptchaService(RestTemplateBuilder restTemplateBuilder) { | ||
this.restTemplateBuilder = restTemplateBuilder; | ||
} | ||
|
||
public boolean validateCaptcha(String clientResponse) { | ||
if (!responseSanityCheck(clientResponse)) { | ||
return false; | ||
} | ||
Map<String, String> body = new HashMap<>(); | ||
body.put("secret", getSecret()); | ||
body.put("response", clientResponse); | ||
|
||
ResponseEntity<Map> recaptchaResponseEntity = restTemplateBuilder.build() | ||
.postForEntity(getVerifyURI() + "?secret={secret}&response={response}", body, Map.class, body); | ||
|
||
Map<String, Object> responseBody = recaptchaResponseEntity.getBody(); | ||
|
||
return (boolean) responseBody.get("success"); | ||
} | ||
|
||
private boolean responseSanityCheck(String response) { | ||
return StringUtils.hasLength(response) && RESPONSE_PATTERN.matcher(response).matches(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package ch.wisv.util; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.springframework.validation.BindingResult; | ||
import org.springframework.validation.FieldError; | ||
|
||
/** | ||
* BindingResultBuilder util class. | ||
*/ | ||
public final class BindingResultBuilder { | ||
|
||
/** | ||
* Private constructor. | ||
*/ | ||
private BindingResultBuilder() { | ||
} | ||
|
||
/** | ||
* Convert BindingResults into ErrorMap. | ||
* | ||
* @param bindingResult of type BindingResult | ||
* | ||
* @return Map | ||
*/ | ||
public static Map<String, String> createErrorMap(BindingResult bindingResult) { | ||
Map<String, String> errorMessages = new HashMap<>(); | ||
for (FieldError fieldError : bindingResult.getFieldErrors()) { | ||
errorMessages.put(fieldError.getField(), fieldError.getDefaultMessage()); | ||
} | ||
|
||
return errorMessages; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,10 @@ wisvch: | |
- chbeheer | ||
- bestuur | ||
- vc | ||
recaptcha: | ||
site: [YOUR SITE KEY] | ||
secret: [YOUR SECRET KEY] | ||
verifyURI: https://www.google.com/recaptcha/api/siteverify | ||
|
||
mailNotifications: | ||
from: [email protected] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,8 @@ | |
|
||
.awesomplete { | ||
width: 100%; | ||
} | ||
|
||
.g-recaptcha { | ||
margin-bottom: 14px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.