Add support for configuration.

main
Paulo Gustavo Veiga 2024-02-18 22:55:19 -08:00
parent af97d51cba
commit fef33dad03
4 changed files with 18 additions and 20 deletions

View File

@ -60,8 +60,11 @@ public class UserController extends BaseController {
@Autowired @Autowired
private AuthenticationManager authManager; private AuthenticationManager authManager;
@Value("${google.recaptcha2.enabled:false}") @Value("${app.registration.enabled:true}")
private Boolean recatchaEnabled; private Boolean registrationEnabled;
@Value("${app.registration.captcha.enabled:true}")
private Boolean registrationCaptchaEnabled;
@Value("${app.accounts.exclusion.domain:''}") @Value("${app.accounts.exclusion.domain:''}")
private String domainBanExclusion; private String domainBanExclusion;
@ -74,6 +77,9 @@ public class UserController extends BaseController {
public void registerUser(@RequestBody RestUserRegistration registration, @NotNull HttpServletRequest request, public void registerUser(@RequestBody RestUserRegistration registration, @NotNull HttpServletRequest request,
@NotNull HttpServletResponse response) throws WiseMappingException, BindException { @NotNull HttpServletResponse response) throws WiseMappingException, BindException {
logger.debug("Register new user:" + registration.getEmail()); logger.debug("Register new user:" + registration.getEmail());
if (!registrationEnabled) {
throw new WiseMappingException("Registration is disabled. You can enable it using app.registration.enabled");
}
if (registration.getPassword().length() > Account.MAX_PASSWORD_LENGTH_SIZE) { if (registration.getPassword().length() > Account.MAX_PASSWORD_LENGTH_SIZE) {
throw new PasswordTooLongException(); throw new PasswordTooLongException();
@ -85,7 +91,6 @@ public class UserController extends BaseController {
remoteIp = request.getRemoteAddr(); remoteIp = request.getRemoteAddr();
} }
logger.debug("Remote address" + remoteIp); logger.debug("Remote address" + remoteIp);
verify(registration, remoteIp); verify(registration, remoteIp);
final Account user = new Account(); final Account user = new Account();
@ -120,7 +125,7 @@ public class UserController extends BaseController {
validator.validate(registration, errors); validator.validate(registration, errors);
// If captcha is enabled, generate it ... // If captcha is enabled, generate it ...
if (recatchaEnabled) { if (registrationCaptchaEnabled) {
final String recaptcha = registration.getRecaptcha(); final String recaptcha = registration.getRecaptcha();
if (recaptcha != null) { if (recaptcha != null) {
final String reCaptchaResponse = captchaService.verifyRecaptcha(remoteAddress, recaptcha); final String reCaptchaResponse = captchaService.verifyRecaptcha(remoteAddress, recaptcha);

View File

@ -47,14 +47,13 @@ public class RecaptchaService {
private final static ObjectMapper objectMapper = new ObjectMapper(); private final static ObjectMapper objectMapper = new ObjectMapper();
public static final String CATCH_ERROR_CODE_TIMEOUT_OR_DUPLICATE = "timeout-or-duplicate"; public static final String CATCH_ERROR_CODE_TIMEOUT_OR_DUPLICATE = "timeout-or-duplicate";
public static final String CATCHA_ERROR_CODE_INPUT_RESPONSE = "invalid-input-response";
@Value("${google.recaptcha2.secretKey}") @Value("${app.registration.captcha.secretKey:''}")
private String recaptchaSecret; private String recaptchaSecret;
@Nullable @Nullable
public String verifyRecaptcha(@NotNull String ip, @NotNull String recaptcha) { public String verifyRecaptcha(@NotNull String ip, @NotNull String recaptcha) {
String result = StringUtils.EMPTY;
final List<NameValuePair> build = Form.form() final List<NameValuePair> build = Form.form()
.add("secret", recaptchaSecret) .add("secret", recaptchaSecret)
.add("response", recaptcha) .add("response", recaptcha)
@ -66,7 +65,6 @@ public class RecaptchaService {
logger.debug("Response from recaptchaSecret: " + recaptchaSecret); logger.debug("Response from recaptchaSecret: " + recaptchaSecret);
logger.debug("Response from recaptcha: " + recaptcha); logger.debug("Response from recaptcha: " + recaptcha);
String result = StringUtils.EMPTY;
try { try {
final byte[] body = Request final byte[] body = Request
.Post(GOOGLE_RECAPTCHA_VERIFY_URL) .Post(GOOGLE_RECAPTCHA_VERIFY_URL)
@ -101,8 +99,4 @@ public class RecaptchaService {
return result; return result;
} }
public void setRecaptchaSecret(String recaptchaSecret) {
this.recaptchaSecret = recaptchaSecret;
}
} }

View File

@ -86,10 +86,9 @@ app:
# accounts: # accounts:
# exclusion: # exclusion:
# domain: # domain:
registration:
google: enabled: false
recaptcha2: captcha:
enabled: true enabled: false
secretKey: 6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe secretKey: 6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe
siteKey: 6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI

View File

@ -3,7 +3,7 @@
<div style="background-color: #fbeed5; max-width: 650px; font-family: Arial, sans-serif; color: #000; padding: 5px;"> <div style="background-color: #fbeed5; max-width: 650px; font-family: Arial, sans-serif; color: #000; padding: 5px;">
<div style="height: 60px; font-size: 14px; font-weight: bold; padding-bottom: 4px;"> <div style="height: 60px; font-size: 14px; font-weight: bold; padding-bottom: 4px;">
<table style="display: inline;width: 100%;"> <table style="display: inline;width: 100%;">
<tbody> <body>
<tr> <tr>
<td height="50px" width="2px" <td height="50px" width="2px"
style="padding: 0; padding-right: 5px; text-align:center;font-weight: normal;font-family: Arial, sans-serif"> style="padding: 0; padding-right: 5px; text-align:center;font-weight: normal;font-family: Arial, sans-serif">
@ -18,7 +18,7 @@
${messageTitle} ${messageTitle}
</td> </td>
</tr> </tr>
</tbody> </body>
</table> </table>
</div> </div>
<div style="font-size: 13px; background-color: #FFF; padding: 10px 7px 7px 7px; min-height: 100px"> <div style="font-size: 13px; background-color: #FFF; padding: 10px 7px 7px 7px; min-height: 100px">