-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add reCAPTCHA and emain domain validation
- Loading branch information
Showing
17 changed files
with
577 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package ubc.pavlab.rdp; | ||
|
||
import org.springframework.beans.factory.FactoryBean; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.web.client.RestTemplate; | ||
import ubc.pavlab.rdp.validation.EmailValidator; | ||
import ubc.pavlab.rdp.validation.RecaptchaValidator; | ||
import ubc.pavlab.rdp.validation.ResourceBasedEmailValidatorFactory; | ||
|
||
import java.time.Duration; | ||
|
||
/** | ||
* This configuration provides a few {@link org.springframework.validation.Validator} beans. | ||
*/ | ||
@Configuration | ||
public class ValidationConfig { | ||
|
||
@Bean | ||
public FactoryBean<EmailValidator> emailValidator( | ||
@Value("${rdp.settings.allowed-email-domains-file}") Resource allowedEmailDomainsFile, | ||
@Value("${rdp.settings.allowed-email-domains-refresh-delay}") Duration refreshDelay, | ||
@Value("${rdp.settings.allow-internationalized-domain-names}") boolean allowIdn ) { | ||
return new ResourceBasedEmailValidatorFactory( allowedEmailDomainsFile, refreshDelay, allowIdn ); | ||
} | ||
|
||
@Bean | ||
public RecaptchaValidator recaptchaValidator( @Value("${rdp.settings.recaptcha.secret}") String secret ) { | ||
return new RecaptchaValidator( new RestTemplate(), secret ); | ||
} | ||
} |
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
81 changes: 81 additions & 0 deletions
81
src/main/java/ubc/pavlab/rdp/validation/EmailValidator.java
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,81 @@ | ||
package ubc.pavlab.rdp.validation; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.validation.Errors; | ||
import org.springframework.validation.Validator; | ||
|
||
import java.net.IDN; | ||
import java.util.Set; | ||
import java.util.TreeSet; | ||
|
||
/** | ||
* This validator only supports ASCII-only domains. | ||
* | ||
* @author poirigui | ||
*/ | ||
public class EmailValidator implements Validator { | ||
|
||
/** | ||
* List of allowed domains. | ||
*/ | ||
private final Set<String> allowedDomains; | ||
|
||
private final boolean allowIdn; | ||
|
||
public EmailValidator() { | ||
this.allowedDomains = null; | ||
this.allowIdn = false; | ||
} | ||
|
||
public EmailValidator( Set<String> allowedDomains, boolean allowIdn ) { | ||
if ( allowedDomains != null ) { | ||
// ascii-only domains, case-insensitive | ||
if ( allowedDomains.stream().anyMatch( d -> !StringUtils.isAsciiPrintable( d ) ) ) { | ||
throw new IllegalArgumentException( "Allowed domains must only contain ASCII-printable characters." ); | ||
} | ||
this.allowedDomains = new TreeSet<>( String.CASE_INSENSITIVE_ORDER ); | ||
this.allowedDomains.addAll( allowedDomains ); | ||
} else { | ||
this.allowedDomains = null; | ||
} | ||
this.allowIdn = allowIdn; | ||
} | ||
|
||
@Override | ||
public boolean supports( Class<?> clazz ) { | ||
return String.class.isAssignableFrom( clazz ); | ||
} | ||
|
||
@Override | ||
public void validate( Object target, Errors errors ) { | ||
String email = (String) target; | ||
String[] parts = email.split( "@", 2 ); | ||
if ( parts.length != 2 ) { | ||
errors.reject( "EmailValidator.invalidAddress" ); | ||
return; | ||
} | ||
String address = parts[0]; | ||
if ( address.isEmpty() ) { | ||
errors.reject( "EmailValidator.emptyUser" ); | ||
} | ||
String domain = parts[1]; | ||
if ( domain.isEmpty() ) { | ||
errors.reject( "EmailValidator.emptyDomain" ); | ||
return; | ||
} | ||
if ( allowIdn ) { | ||
try { | ||
domain = IDN.toASCII( domain ); | ||
} catch ( IllegalArgumentException e ) { | ||
errors.reject( "EmailValidator.domainNotConformToRfc3490", new String[]{ e.getMessage() }, "" ); | ||
return; | ||
} | ||
} else if ( !StringUtils.isAsciiPrintable( domain ) ) { | ||
errors.reject( "EmailValidator.domainContainsUnsupportedCharacters" ); | ||
return; | ||
} | ||
if ( allowedDomains != null && !allowedDomains.contains( domain ) ) { | ||
errors.reject( "EmailValidator.domainNotAllowed" ); | ||
} | ||
} | ||
} |
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,10 @@ | ||
package ubc.pavlab.rdp.validation; | ||
|
||
import lombok.Data; | ||
import lombok.Value; | ||
|
||
@Value | ||
public class Recaptcha { | ||
String response; | ||
String remoteIp; | ||
} |
Oops, something went wrong.